diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2016-01-18 06:42:10 -0800 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2016-01-18 06:42:10 -0800 |
commit | 166927780f449e7b0c77345cad3150684d42d150 (patch) | |
tree | 917e60ffcc24e1970f1768369734dd2542a03e22 /mpi | |
parent | 4a9d763799c3f0b3c17182b44e8497e174547120 (diff) | |
download | txr-166927780f449e7b0c77345cad3150684d42d150.tar.gz txr-166927780f449e7b0c77345cad3150684d42d150.tar.bz2 txr-166927780f449e7b0c77345cad3150684d42d150.zip |
New random-state-get-vec function.
* arith.c (UINT_PTR_MAX_MP): New static variable.
(biggnum_from_uintptr): New function.
(in_uint_ptr_range): New static function.
(c_uint_ptr_num): New function.
(arith_init): Initialize UINT_PTR_MAX_MP.
* arith.h (bignum_from_uintptr, c_uint_ptr_num): Declared.
* eval.c (eval_init): Register random-state-get-vec.
* mpi/mpi.c (mp_set_uintptr, mp_get_uintptr): New functions.
(mp_set_intptr, mp_get_intptr): Expressed in terms of
mp_set_uintptr and mp_get_uintptr.
* mpi/mpi.h (mp_set_uintptr, mp_get_uintptr): Declared.
* rand.c (make_random_state): Handle vector seed.
(random_state_get_vec): New function.
* rand.h (random_state_get_vec): Declared.
* txr.1: Documented new feature in make-random-state
and new random-state-get-vec function.
Diffstat (limited to 'mpi')
-rw-r--r-- | mpi/mpi.c | 38 | ||||
-rw-r--r-- | mpi/mpi.h | 2 |
2 files changed, 29 insertions, 11 deletions
@@ -526,19 +526,17 @@ mp_err mp_set_int(mp_int *mp, long z) /* }}} */ -mp_err mp_set_intptr(mp_int *mp, int_ptr_t z) +mp_err mp_set_uintptr(mp_int *mp, uint_ptr_t z) { - int_ptr_t v = z > 0 ? z : -z; - if (sizeof z > sizeof (mp_digit)) { int ix, shift; - const int nd = (sizeof v + sizeof (mp_digit) - 1) / sizeof (mp_digit); + const int nd = (sizeof z + sizeof (mp_digit) - 1) / sizeof (mp_digit); ARGCHK(mp != NULL, MP_BADARG); mp_zero(mp); - if(z == 0) + if (z == 0) return MP_OKAY; /* shortcut for zero */ s_mp_grow(mp, nd); @@ -547,24 +545,32 @@ mp_err mp_set_intptr(mp_int *mp, int_ptr_t z) for (ix = 0, shift = 0; ix < nd; ix++, shift += MP_DIGIT_BIT) { - DIGIT(mp, ix) = (v >> shift) & MP_DIGIT_MAX; + DIGIT(mp, ix) = (z >> shift) & MP_DIGIT_MAX; } + } else { - mp_set(mp, v); + mp_set(mp, z); } + return MP_OKAY; +} - if(z < 0) +mp_err mp_set_intptr(mp_int *mp, int_ptr_t z) +{ + int_ptr_t v = z > 0 ? z : -z; + mp_err err = mp_set_uintptr(mp, v); + + if (err == MP_OKAY && z < 0) SIGN(mp) = MP_NEG; - return MP_OKAY; + return err; } /* * No checks here: assumes that the mp is in range! */ -mp_err mp_get_intptr(mp_int *mp, int_ptr_t *z) +mp_err mp_get_uintptr(mp_int *mp, uint_ptr_t *z) { - int_ptr_t out = 0; + uint_ptr_t out = 0; #if MP_DIGIT_SIZE < SIZEOF_PTR int ix; @@ -579,6 +585,16 @@ mp_err mp_get_intptr(mp_int *mp, int_ptr_t *z) return MP_OKAY; } +mp_err mp_get_intptr(mp_int *mp, int_ptr_t *z) +{ + uint_ptr_t tmp = 0; + int_ptr_t out; + mp_get_uintptr(mp, &tmp); + out = tmp; + *z = (SIGN(mp) == MP_NEG) ? -out : out; + return MP_OKAY; +} + #ifdef HAVE_DOUBLE_INTPTR_T mp_err mp_set_double_intptr(mp_int *mp, double_intptr_t z) { @@ -101,7 +101,9 @@ void mp_clear_array(mp_int mp[], int count); void mp_zero(mp_int *mp); void mp_set(mp_int *mp, mp_digit d); mp_err mp_set_int(mp_int *mp, long z); +mp_err mp_set_uintptr(mp_int *mp, uint_ptr_t z); mp_err mp_set_intptr(mp_int *mp, int_ptr_t z); +mp_err mp_get_uintptr(mp_int *mp, uint_ptr_t *z); mp_err mp_get_intptr(mp_int *mp, int_ptr_t *z); #ifdef HAVE_DOUBLE_INTPTR_T mp_err mp_set_double_intptr(mp_int *mp, double_intptr_t z); |