summaryrefslogtreecommitdiffstats
path: root/rand.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2017-06-18 08:34:08 -0700
committerKaz Kylheku <kaz@kylheku.com>2017-06-18 10:14:34 -0700
commitf9964fe5c922535d5284ad22d62fddbdca315e97 (patch)
tree48aa53103215b6434c90eeec1606f16f04503f94 /rand.c
parent1e35fa11db662e6237e76f800a9294809bcb1660 (diff)
downloadtxr-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.c20
1 files changed, 14 insertions, 6 deletions
diff --git a/rand.c b/rand.c
index 6797dcdc..acbc1497 100644
--- a/rand.c
+++ b/rand.c
@@ -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);