diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2012-04-10 16:11:15 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2012-04-10 16:11:15 -0700 |
commit | 38abf85defad8a15899687e35c7037bb2ddf42b0 (patch) | |
tree | 7ddde67b2bab7bb6b589249e6cb079f375c1a760 | |
parent | 407377d0d945ccab189546ce0ce9d5f7d4a7f076 (diff) | |
download | txr-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.
-rw-r--r-- | ChangeLog | 16 | ||||
-rw-r--r-- | arith.c | 9 | ||||
-rw-r--r-- | arith.h | 1 | ||||
-rw-r--r-- | lib.c | 13 | ||||
-rw-r--r-- | rand.c | 5 |
5 files changed, 38 insertions, 6 deletions
@@ -1,5 +1,21 @@ 2012-04-10 Kaz Kylheku <kaz@kylheku.com> + * 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. + +2012-04-10 Kaz Kylheku <kaz@kylheku.com> + * configure: Switch from _POSIX_C_SOURCE=2 to _XOPEN_SOURCE. We will be relying on the gettimeofday function which only came into POSIX in 2001, but was in Unix long before then. @@ -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); } @@ -28,4 +28,5 @@ val make_bignum(void); val bignum(cnum cn); int highest_bit(int_ptr_t n); val normalize(val bignum); +val in_int_ptr_range(val bignum); void arith_init(void); @@ -1151,11 +1151,18 @@ val num(cnum n) cnum c_num(val num) { - switch (tag(num)) { - case TAG_CHR: case TAG_NUM: + switch (type(num)) { + case CHR: case NUM: return ((cnum) num) >> TAG_SHIFT; + case BGNUM: + if (in_int_ptr_range(num)) { + int_ptr_t out; + mp_get_intptr(mp(num), &out); + return out; + } + uw_throwf(error_s, lit("c_num: ~s is out of cnum range"), num, nao); default: - type_mismatch(lit("~s is not a fixnum"), num, nao); + type_mismatch(lit("c_num: ~s is not an integer"), num, nao); } } @@ -136,8 +136,9 @@ val make_random_state(val seed) r->state[3] = (s >> 96) & 0xFFFFFFFFul; #endif } else if (nullp(seed)) { - r->state[0] = (rand32_t) time(0); - r->state[1] = (rand32_t) clock(); + val time = time_sec_usec(); + r->state[0] = (rand32_t) c_num(car(time)); + r->state[1] = (rand32_t) c_num(cdr(time)); memset(r->state + 2, 0xAA, sizeof r->state - 2 * sizeof r->state[0]); } else if (random_state_p(seed)) { struct random_state *rseed = (struct random_state *) |