diff --git a/lib/srfi/27/rand.c b/lib/srfi/27/rand.c index 4ae30f50..ece6eb53 100644 --- a/lib/srfi/27/rand.c +++ b/lib/srfi/27/rand.c @@ -10,16 +10,26 @@ #define sexp_random_source_p(x) sexp_check_tag(x, rs_type_id) -#define sexp_random_state(x) (sexp_slot_ref((x), 0)) -#define sexp_random_data(x) ((struct random_data*)(&sexp_slot_ref((x), 1))) - #define sexp_random_init(x, seed) \ initstate_r(seed, \ sexp_string_data(sexp_random_state(x)), \ SEXP_RANDOM_STATE_SIZE, \ sexp_random_data(x)) -#define sexp_sizeof_random (sexp_sizeof_header + sizeof(struct random_data) + sizeof(sexp)) +#if SEXP_BSD +typedef unsigned int sexp_random_t; +#define sexp_call_random(rs, dst) ((dst) = rand_r(sexp_random_data(rs))) +#define sexp_seed_random(n, rs) *sexp_random_data(rs) = (n) +#else +typedef struct random_data sexp_random_t; +#define sexp_call_random(rs, dst) random_r(sexp_random_data(rs), &dst) +#define sexp_seed_random(n, rs) srandom_r(n, sexp_random_data(rs)) +#endif + +#define sexp_random_state(x) (sexp_slot_ref((x), 0)) +#define sexp_random_data(x) ((sexp_random_t*)(&sexp_slot_ref((x), 1))) + +#define sexp_sizeof_random (sexp_sizeof_header + sizeof(sexp_random_t) + sizeof(sexp)) static sexp_uint_t rs_type_id; static sexp default_random_source; @@ -30,7 +40,7 @@ static sexp sexp_rs_random_integer (sexp ctx, sexp rs, sexp bound) { if (! sexp_random_source_p(rs)) res = sexp_type_exception(ctx, "not a random-source", rs); if (sexp_fixnump(bound)) { - random_r(sexp_random_data(rs), &n); + sexp_call_random(rs, n); res = sexp_make_fixnum(n % sexp_unbox_fixnum(bound)); #if USE_BIGNUMS } else if (sexp_bignump(bound)) { @@ -39,10 +49,10 @@ static sexp sexp_rs_random_integer (sexp ctx, sexp rs, sexp bound) { res = sexp_make_bignum(ctx, hi); data = (int32_t*) sexp_bignum_data(res); for (i=0; i