summaryrefslogtreecommitdiffstats
path: root/arith.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2019-01-25 06:33:29 -0800
committerKaz Kylheku <kaz@kylheku.com>2019-01-25 06:33:29 -0800
commit4c71a77f2c39a8158c80b40af1a5cc2358251a00 (patch)
treef3c49877aed2a6898b89662f06a17cbc06c080ca /arith.c
parent8195fbed4a8067362656d38e56fec3c4eb3deef1 (diff)
downloadtxr-4c71a77f2c39a8158c80b40af1a5cc2358251a00.tar.gz
txr-4c71a77f2c39a8158c80b40af1a5cc2358251a00.tar.bz2
txr-4c71a77f2c39a8158c80b40af1a5cc2358251a00.zip
Provide faster bignum-in-fixed-integer range tests in MPI.
* mpi/mpi.c (mp_in_range, mp_in_intptr_range, mp_in_uintptr_range): New functions. * mpi/mpi.h (mp_in_range, mp_in_intptr_range, mp_in_uintptr_range): Declared. * arith.c (NUM_MAX_MP, INT_PTR_MAX_MP, UINT_PTR_MAX_MP, INT_PTR_MAX_SUCC_MP): Static variables removed. Note that INT_PTR_MAX_MP was not used at all! (normalize): Use mp_in_range instead magnitude comparison to NUM_MAX_MP. (in_int_ptr_range, in_uint_ptr_range): Static functions removed. (c_unum): Use mp_in_uintptr_range instead of in_uint_ptr_range. (arith_init): Remove initializations of removed variables. (arith_free_all): Remove cleanup of removed variables, leaving function empty. * lib.c (c_num): Use mp_in_intptr_range instead of in_int_ptr_range.
Diffstat (limited to 'arith.c')
-rw-r--r--arith.c41
1 files changed, 2 insertions, 39 deletions
diff --git a/arith.c b/arith.c
index 8bc459ca..f349b57a 100644
--- a/arith.c
+++ b/arith.c
@@ -54,9 +54,6 @@
#define CNUM_BIT ((int) sizeof (cnum) * CHAR_BIT)
#define ABS(A) ((A) < 0 ? -(A) : (A))
-static mp_int NUM_MAX_MP, INT_PTR_MAX_MP, UINT_PTR_MAX_MP;
-static mp_int INT_PTR_MAX_SUCC_MP;
-
val make_bignum(void)
{
val n = make_obj();
@@ -165,7 +162,7 @@ val bignum_dbl_ipt(double_intptr_t di)
val normalize(val bignum)
{
- if (mp_cmp_mag(mp(bignum), &NUM_MAX_MP) == MP_GT) {
+ if (!mp_in_range(mp(bignum), NUM_MAX, 0)) {
return bignum;
} else {
cnum fixnum;
@@ -174,27 +171,6 @@ val normalize(val bignum)
}
}
-val in_int_ptr_range(val bignum)
-{
- switch (mp_cmp_mag(mp(bignum), &INT_PTR_MAX_SUCC_MP)) {
- default:
- case MP_GT:
- return nil;
- case MP_EQ:
- if (mp_cmp_z(mp(bignum)) == MP_GT)
- return nil;
- /* fallthrough */
- case MP_LT:
- return t;
- }
-}
-
-static val in_uint_ptr_range(val bignum)
-{
- return (mp_cmp_z(mp(bignum)) == MP_LT ||
- mp_cmp_mag(mp(bignum), &UINT_PTR_MAX_MP) == MP_GT) ? nil : t;
-}
-
ucnum c_unum(val num)
{
switch (type(num)) {
@@ -206,7 +182,7 @@ ucnum c_unum(val num)
}
goto range;
case BGNUM:
- if (in_uint_ptr_range(num)) {
+ if (mp_in_uintptr_range(mp(num))) {
uint_ptr_t out;
mp_get_uintptr(mp(num), &out);
return out;
@@ -3348,15 +3324,6 @@ static val flo_set_round_mode(val mode)
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);
- mp_init(&UINT_PTR_MAX_MP);
- mp_set_uintptr(&UINT_PTR_MAX_MP, -1);
- mp_init(&INT_PTR_MAX_SUCC_MP);
- mp_set_intptr(&INT_PTR_MAX_SUCC_MP, INT_PTR_MIN - 1);
- mp_neg(&INT_PTR_MAX_SUCC_MP, &INT_PTR_MAX_SUCC_MP);
log2_init();
if (opt_compat && opt_compat <= 199) {
@@ -3423,8 +3390,4 @@ void arith_init(void)
void arith_free_all(void)
{
- mp_clear(&NUM_MAX_MP);
- mp_clear(&INT_PTR_MAX_MP);
- mp_clear(&UINT_PTR_MAX_MP);
- mp_clear(&INT_PTR_MAX_SUCC_MP);
}