From ceae17c1bb42b04c4055cf88b3e51b26328ebe22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Kochma=C5=84ski?= Date: Sat, 14 Dec 2024 22:31:04 +0100 Subject: [PATCH] bignum: replace references to gmp limbs with ecl macros GMP_LIMB_BITS -> ECL_BIGNUM_LIMB_BITS mp_limb_t -> ecl_limb_t --- src/c/hash.d | 4 ++-- src/c/num_rand.d | 31 +++++++++++++++---------------- src/h/number.h | 9 +++++---- src/h/object.h | 2 ++ 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/c/hash.d b/src/c/hash.d index 5a1968b40..a36d3f839 100644 --- a/src/c/hash.d +++ b/src/c/hash.d @@ -37,7 +37,7 @@ _hash_eql(cl_hashkey h, cl_object x) case t_bignum: return hash_string(h, (unsigned char*)ECL_BIGNUM_LIMBS(x), labs(ECL_BIGNUM_SIZE(x)) * - sizeof(mp_limb_t)); + sizeof(ecl_limb_t)); case t_ratio: h = _hash_eql(h, x->ratio.num); return _hash_eql(h, x->ratio.den); @@ -244,7 +244,7 @@ _hash_equalp(int depth, cl_hashkey h, cl_object x) /* FIXME! We should be more precise here! */ return hash_string(h, (unsigned char*)ecl_bignum(x)->_mp_d, abs(ecl_bignum(x)->_mp_size) * - sizeof(mp_limb_t)); + sizeof(ecl_limb_t)); case t_ratio: h = _hash_equalp(0, h, x->ratio.num); return _hash_equalp(0, h, x->ratio.den); diff --git a/src/c/num_rand.d b/src/c/num_rand.d index afb2b3ee6..cbb3d1251 100644 --- a/src/c/num_rand.d +++ b/src/c/num_rand.d @@ -100,17 +100,17 @@ generate_double(cl_object state) return (generate_int64(state) >> 11) * (1.0 / 9007199254740991.0); } -static mp_limb_t +static ecl_limb_t generate_limb(cl_object state) { -#if GMP_LIMB_BITS <= 32 +#if ECL_BIGNUM_LIMB_BITS <= 32 return generate_int64(state); #else -# if GMP_LIMB_BITS <= 64 +# if ECL_BIGNUM_LIMB_BITS <= 64 return generate_int64(state); # else -# if GMP_LIMB_BITS <= 128 - mp_limb_t high = generate_int64(state); +# if ECL_BIGNUM_LIMB_BITS <= 128 + ecl_limb_t high = generate_int64(state); return (high << 64) | generate_int64(state); # endif # endif @@ -180,21 +180,21 @@ generate_double(cl_object state) return generate_int32(state) * (1.0 / 4294967296.0); } -static mp_limb_t +static ecl_limb_t generate_limb(cl_object state) { -#if GMP_LIMB_BITS <= 32 +#if ECL_BIGNUM_LIMB_BITS <= 32 return generate_int32(state); #else -# if GMP_LIMB_BITS <= 64 - mp_limb_t high = generate_int32(state); +# if ECL_BIGNUM_LIMB_BITS <= 64 + ecl_limb_t high = generate_int32(state); return (high << 32) | generate_int32(state); # else -# if GMP_LIMB_BITS <= 128 - mp_limb_t word0 = generate_int32(state); - mp_limb_t word1 = generate_int32(state); - mp_limb_t word2 = generate_int32(state); - mp_limb_t word3 = generate_int32(state); +# if ECL_BIGNUM_LIMB_BITS <= 128 + ecl_limb_t word0 = generate_int32(state); + ecl_limb_t word1 = generate_int32(state); + ecl_limb_t word2 = generate_int32(state); + ecl_limb_t word3 = generate_int32(state); return (word3 << 96) | (word3 << 64) | (word1 << 32) || word0; # endif # endif @@ -233,8 +233,7 @@ random_integer(cl_object limit, cl_object state) bit_length = ECL_FIXNUM_BITS; buffer = ecl_ash(ecl_make_fixnum(1), bit_length); for (bit_length = mpz_size(ecl_bignum(buffer)); bit_length; ) { - ECL_BIGNUM_LIMBS(buffer)[--bit_length] = - generate_limb(state); + ECL_BIGNUM_LIMBS(buffer)[--bit_length] = generate_limb(state); } return cl_mod(buffer, limit); } diff --git a/src/h/number.h b/src/h/number.h index 150a0da78..506c25c48 100644 --- a/src/h/number.h +++ b/src/h/number.h @@ -8,7 +8,7 @@ * */ -/* number.h -- GMP interface. */ +/* number.h -- INTEGER interface. */ #ifndef ECL_NUMBER_H #define ECL_NUMBER_H @@ -27,9 +27,10 @@ extern "C" { name##aux.value->_mp_d = name##data, \ (cl_object)(&name##aux)) -#define ECL_BIGNUM_DIM(x) (ecl_bignum(x)->_mp_alloc) /* number of allocated limbs */ -#define ECL_BIGNUM_SIZE(x) (ecl_bignum(x)->_mp_size) /* number of limbs in use times sign of the bignum */ -#define ECL_BIGNUM_LIMBS(x) (ecl_bignum(x)->_mp_d) /* pointer to array of allocated limbs */ +#define ECL_BIGNUM_LIMB_BITS GMP_LIMB_BITS +#define ECL_BIGNUM_DIM(x) (ecl_bignum(x)->_mp_alloc) /* number of allocated limbs */ +#define ECL_BIGNUM_SIZE(x) (ecl_bignum(x)->_mp_size) /* number of limbs in use times sign of the bignum */ +#define ECL_BIGNUM_LIMBS(x) (ecl_bignum(x)->_mp_d) /* pointer to array of allocated limbs */ typedef void (*_ecl_big_binary_op)(cl_object out, cl_object o1, cl_object o2); extern ECL_API _ecl_big_binary_op _ecl_big_boole_operator(int op); diff --git a/src/h/object.h b/src/h/object.h index ee35bb6be..996f3c5e0 100644 --- a/src/h/object.h +++ b/src/h/object.h @@ -212,6 +212,8 @@ struct ecl_long_float { #define ecl_long_float(o) ((o)->longfloat.value) typedef mpz_t big_num_t; +typedef mp_limb_t ecl_limb_t; + struct ecl_bignum { _ECL_HDR; big_num_t value;