summaryrefslogtreecommitdiffstats
path: root/arith.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2012-04-10 16:11:15 -0700
committerKaz Kylheku <kaz@kylheku.com>2012-04-10 16:11:15 -0700
commit38abf85defad8a15899687e35c7037bb2ddf42b0 (patch)
tree7ddde67b2bab7bb6b589249e6cb079f375c1a760 /arith.c
parent407377d0d945ccab189546ce0ce9d5f7d4a7f076 (diff)
downloadtxr-38abf85defad8a15899687e35c7037bb2ddf42b0.tar.gz
txr-38abf85defad8a15899687e35c7037bb2ddf42b0.tar.bz2
txr-38abf85defad8a15899687e35c7037bb2ddf42b0.zip
* arith.c (INT_PTR_MAX_MP): New static variable.
(in_int_ptr_range): New function. (arith_init): Initialize INT_PTR_MAX_MP. * arith.h (in_int_ptr_range): Declared. * lib.c (c_num): Allow bignums to be converted to a cnum, if they are in range, rather than allowing only fixnums. * rand.c (make_random_state): Now that we have such a function, initialize random seed using time value from time_sec_usec rather than from time and clock. clock is bad for random seeding because it measures virtual time since the start of the process.
Diffstat (limited to 'arith.c')
-rw-r--r--arith.c9
1 files changed, 8 insertions, 1 deletions
diff --git a/arith.c b/arith.c
index de8d782e..1d784a6e 100644
--- a/arith.c
+++ b/arith.c
@@ -51,7 +51,7 @@
#define CNUM_BIT ((int) sizeof (cnum) * CHAR_BIT)
#define ABS(A) ((A) < 0 ? -(A) : (A))
-static mp_int NUM_MAX_MP;
+static mp_int NUM_MAX_MP, INT_PTR_MAX_MP;
val make_bignum(void)
{
@@ -90,6 +90,11 @@ val normalize(val bignum)
}
}
+val in_int_ptr_range(val bignum)
+{
+ return (mp_cmp_mag(mp(bignum), &INT_PTR_MAX_MP) == MP_GT) ? nil : t;
+}
+
int highest_bit(int_ptr_t n)
{
#if SIZEOF_PTR == 8
@@ -1487,4 +1492,6 @@ void arith_init(void)
{
mp_init(&NUM_MAX_MP);
mp_set_intptr(&NUM_MAX_MP, NUM_MAX);
+ mp_init(&INT_PTR_MAX_MP);
+ mp_set_intptr(&INT_PTR_MAX_MP, INT_PTR_MAX);
}