diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2017-06-18 08:34:08 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2017-06-18 10:14:34 -0700 |
commit | f9964fe5c922535d5284ad22d62fddbdca315e97 (patch) | |
tree | 48aa53103215b6434c90eeec1606f16f04503f94 /rand.c | |
parent | 1e35fa11db662e6237e76f800a9294809bcb1660 (diff) | |
download | txr-f9964fe5c922535d5284ad22d62fddbdca315e97.tar.gz txr-f9964fe5c922535d5284ad22d62fddbdca315e97.tar.bz2 txr-f9964fe5c922535d5284ad22d62fddbdca315e97.zip |
Handle returns of MPI functions that return MP_TOOBIG.
* arith.c (do_mp_error): New function.
(num_from_buffer, plus, minus, mul, floordiv, expt, exptmod,
logtrunc, sign_extend, ash, bit): Handle errors from select
MPI functions: those that have the mp_ign attribute.
* ffi.c (unum_carray, num_carray): Likewise.
* rand.c (random): Likewise.
Diffstat (limited to 'rand.c')
-rw-r--r-- | rand.c | 20 |
1 files changed, 14 insertions, 6 deletions
@@ -217,6 +217,7 @@ val random_fixnum(val state) val random(val state, val modulus) { + val self = lit("random"); struct rand_state *r = coerce(struct rand_state *, cobj_handle(state, random_state_s)); @@ -233,21 +234,28 @@ val random(val state, val modulus) ucnum i; for (i = 0; i < rands_needed; i++) { rand32_t rnd = rand32(r); + mp_err mpe = MP_OKAY; #if MP_DIGIT_SIZE >= 4 if (i > 0) - mp_mul_2d(om, 32, om); + mpe = mp_mul_2d(om, 32, om); else rnd &= msb_rand_mask; - mp_add_d(om, rnd, om); + if (mpe == MP_OKAY) + mpe = mp_add_d(om, rnd, om); #else if (i > 0) - mp_mul_2d(om, 16, om); + mpe = mp_mul_2d(om, 16, om); else rnd &= msb_rand_mask; - mp_add_d(om, rnd & 0xFFFF, om); - mp_mul_2d(om, 16, om); - mp_add_d(om, rnd >> 16, om); + if (mpe == MP_OKAY) + mpe = mp_add_d(om, rnd & 0xFFFF, om); + if (mpe == MP_OKAY) + mpe = mp_mul_2d(om, 16, om); + if (mpe == MP_OKAY) + mp_add_d(om, rnd >> 16, om); #endif + if (mpe != MP_OKAY) + do_mp_error(self, mpe); } if (mp_cmp(om, m) != MP_LT) { mp_zero(om); |