aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/stdlib/bigint.c32
-rw-r--r--src/stdlib/bigint.h7
-rw-r--r--src/stdlib/datatypes.h2
-rw-r--r--src/stdlib/intX.h10
-rw-r--r--src/stdlib/numX.h4
5 files changed, 28 insertions, 27 deletions
diff --git a/src/stdlib/bigint.c b/src/stdlib/bigint.c
index 3ed3f3cf..be01a001 100644
--- a/src/stdlib/bigint.c
+++ b/src/stdlib/bigint.c
@@ -23,7 +23,7 @@ int Int$print(FILE *f, Int_t i) {
if (likely(i.small & 1L)) {
return _print_int(f, (int64_t)((i.small) >> 2L));
} else {
- return gmp_fprintf(f, "%Zd", *i.big);
+ return gmp_fprintf(f, "%Zd", i.big);
}
}
@@ -50,7 +50,7 @@ Text_t Int$value_as_text(Int_t i) {
if (likely(i.small & 1L)) {
return _int64_to_text(i.small >> 2L);
} else {
- char *str = mpz_get_str(NULL, 10, *i.big);
+ char *str = mpz_get_str(NULL, 10, i.big);
return Text$from_str(str);
}
}
@@ -72,9 +72,9 @@ static bool Int$is_none(const void *i, const TypeInfo_t *info) {
public
PUREFUNC int32_t Int$compare_value(const Int_t x, const Int_t y) {
if (likely(x.small & y.small & 1L)) return (x.small > y.small) - (x.small < y.small);
- else if (x.small & 1) return -mpz_cmp_si(*y.big, x.small);
- else if (y.small & 1) return mpz_cmp_si(*x.big, y.small);
- else return x.big == y.big ? 0 : mpz_cmp(*x.big, *y.big);
+ else if (x.small & 1) return -mpz_cmp_si(y.big, x.small);
+ else if (y.small & 1) return mpz_cmp_si(x.big, y.small);
+ else return x.big == y.big ? 0 : mpz_cmp(x.big, y.big);
}
public
@@ -86,7 +86,7 @@ PUREFUNC int32_t Int$compare(const void *x, const void *y, const TypeInfo_t *inf
public
PUREFUNC bool Int$equal_value(const Int_t x, const Int_t y) {
if (likely((x.small | y.small) & 1L)) return x.small == y.small;
- else return x.big == y.big ? 0 : (mpz_cmp(*x.big, *y.big) == 0);
+ else return x.big == y.big ? 0 : (mpz_cmp(x.big, y.big) == 0);
}
public
@@ -112,7 +112,7 @@ PUREFUNC uint64_t Int$hash(const void *vx, const TypeInfo_t *info) {
if (likely(x->small & 1L)) {
return siphash24((void *)x, sizeof(Int_t));
} else {
- char *str = mpz_get_str(NULL, 16, *x->big);
+ char *str = mpz_get_str(NULL, 16, x->big);
return siphash24((void *)str, strlen(str));
}
}
@@ -126,7 +126,7 @@ Text_t Int$hex(Int_t i, Int_t digits_int, bool uppercase, bool prefix) {
return Text$from_str(String(
hex(u64, .no_prefix = !prefix, .digits = Int32$from_int(digits_int, false), .uppercase = uppercase)));
} else {
- char *str = mpz_get_str(NULL, 16, *i.big);
+ char *str = mpz_get_str(NULL, 16, i.big);
if (uppercase) {
for (char *c = str; *c; c++)
*c = (char)toupper(*c);
@@ -151,7 +151,7 @@ Text_t Int$octal(Int_t i, Int_t digits_int, bool prefix) {
return Text$from_str(String(oct(u64, .no_prefix = !prefix, .digits = Int32$from_int(digits_int, false))));
} else {
int64_t digits = Int64$from_int(digits_int, false);
- char *str = mpz_get_str(NULL, 8, *i.big);
+ char *str = mpz_get_str(NULL, 8, i.big);
int64_t needed_zeroes = digits - (int64_t)strlen(str);
if (needed_zeroes <= 0) return prefix ? Text$concat(Text("0o"), Text$from_str(str)) : Text$from_str(str);
@@ -170,7 +170,7 @@ Int_t Int$slow_plus(Int_t x, Int_t y) {
if (y.small < 0L) mpz_sub_ui(result, result, (uint64_t)(-(y.small >> 2L)));
else mpz_add_ui(result, result, (uint64_t)(y.small >> 2L));
} else {
- mpz_add(result, result, *y.big);
+ mpz_add(result, result, y.big);
}
return Int$from_mpz(result);
}
@@ -183,7 +183,7 @@ Int_t Int$slow_minus(Int_t x, Int_t y) {
if (y.small < 0L) mpz_add_ui(result, result, (uint64_t)(-(y.small >> 2L)));
else mpz_sub_ui(result, result, (uint64_t)(y.small >> 2L));
} else {
- mpz_sub(result, result, *y.big);
+ mpz_sub(result, result, y.big);
}
return Int$from_mpz(result);
}
@@ -193,7 +193,7 @@ Int_t Int$slow_times(Int_t x, Int_t y) {
mpz_t result;
mpz_init_set_int(result, x);
if (y.small & 1L) mpz_mul_si(result, result, y.small >> 2L);
- else mpz_mul(result, result, *y.big);
+ else mpz_mul(result, result, y.big);
return Int$from_mpz(result);
}
@@ -206,7 +206,7 @@ Int_t Int$slow_divided_by(Int_t dividend, Int_t divisor) {
mpz_init_set_int(remainder, divisor);
mpz_tdiv_qr(quotient, remainder, quotient, remainder);
if (mpz_sgn(remainder) < 0) {
- bool d_positive = likely(divisor.small & 1L) ? divisor.small > 0x1L : mpz_sgn(*divisor.big) > 0;
+ bool d_positive = likely(divisor.small & 1L) ? divisor.small > 0x1L : mpz_sgn(divisor.big) > 0;
if (d_positive) mpz_sub_ui(quotient, quotient, 1);
else mpz_add_ui(quotient, quotient, 1);
}
@@ -328,9 +328,9 @@ Int_t Int$gcd(Int_t x, Int_t y) {
mpz_t result;
mpz_init(result);
- if (x.small & 0x1L) mpz_gcd_ui(result, *y.big, (uint64_t)labs(x.small >> 2L));
- else if (y.small & 0x1L) mpz_gcd_ui(result, *x.big, (uint64_t)labs(y.small >> 2L));
- else mpz_gcd(result, *x.big, *y.big);
+ if (x.small & 0x1L) mpz_gcd_ui(result, y.big, (uint64_t)labs(x.small >> 2L));
+ else if (y.small & 0x1L) mpz_gcd_ui(result, x.big, (uint64_t)labs(y.small >> 2L));
+ else mpz_gcd(result, x.big, y.big);
return Int$from_mpz(result);
}
diff --git a/src/stdlib/bigint.h b/src/stdlib/bigint.h
index 558bc099..614e1501 100644
--- a/src/stdlib/bigint.h
+++ b/src/stdlib/bigint.h
@@ -34,13 +34,14 @@ bool Int$get_bit(Int_t x, Int_t bit_index);
#define SMALLEST_SMALL_INT -0x40000000
#define Int$from_mpz(mpz) \
- (mpz_cmpabs_ui(mpz, BIGGEST_SMALL_INT) <= 0 ? ((Int_t){.small = (mpz_get_si(mpz) << 2L) | 1L}) \
- : ((Int_t){.big = memcpy(new (mpz_t), &mpz, sizeof(mpz_t))}))
+ (mpz_cmpabs_ui(mpz, BIGGEST_SMALL_INT) <= 0 \
+ ? ((Int_t){.small = (mpz_get_si(mpz) << 2L) | 1L}) \
+ : ((Int_t){.big = memcpy(new (__mpz_struct), mpz, sizeof(__mpz_struct))}))
#define mpz_init_set_int(mpz, i) \
do { \
if likely ((i).small & 1L) mpz_init_set_si(mpz, (i).small >> 2L); \
- else mpz_init_set(mpz, *(i).big); \
+ else mpz_init_set(mpz, (i).big); \
} while (0)
#define I_small(i) ((Int_t){.small = (int64_t)((uint64_t)(i) << 2L) | 1L})
diff --git a/src/stdlib/datatypes.h b/src/stdlib/datatypes.h
index 60b5dd55..caabdacd 100644
--- a/src/stdlib/datatypes.h
+++ b/src/stdlib/datatypes.h
@@ -30,7 +30,7 @@
typedef union {
int64_t small;
- mpz_t *big;
+ __mpz_struct *big;
} Int_t;
#define OptionalInt_t Int_t
diff --git a/src/stdlib/intX.h b/src/stdlib/intX.h
index a4a782ab..765543fd 100644
--- a/src/stdlib/intX.h
+++ b/src/stdlib/intX.h
@@ -40,10 +40,10 @@ Text_t NAMESPACED(as_text)(const void *i, bool colorize, const TypeInfo_t *type)
Text_t NAMESPACED(value_as_text)(INTX_T i);
PUREFUNC int32_t NAMESPACED(compare)(const void *x, const void *y, const TypeInfo_t *type);
PUREFUNC bool NAMESPACED(equal)(const void *x, const void *y, const TypeInfo_t *type);
-Text_t NAMESPACED(hex)(INTX_T i, INTX_T digits, bool uppercase, bool prefix);
-Text_t NAMESPACED(octal)(INTX_T i, INTX_T digits, bool prefix);
+Text_t NAMESPACED(hex)(INTX_T i, Int_t digits, bool uppercase, bool prefix);
+Text_t NAMESPACED(octal)(INTX_T i, Int_t digits, bool prefix);
List_t NAMESPACED(bits)(INTX_T x);
-bool NAMESPACED(get_bit)(INTX_T x, INTX_T bit_index);
+bool NAMESPACED(get_bit)(INTX_T x, Int_t bit_index);
Closure_t NAMESPACED(to)(INTX_T first, INTX_T last, OPT_T step);
Closure_t NAMESPACED(onward)(INTX_T first, INTX_T step);
PUREFUNC OPT_T NAMESPACED(parse)(Text_t text, Text_t *remainder);
@@ -110,8 +110,8 @@ MACROLIKE PUREFUNC INTX_T NAMESPACED(from_int)(Int_t i, bool truncate) {
#endif
return ret;
}
- if (!truncate && unlikely(!mpz_fits_slong_p(*i.big))) fail("Integer is too big to fit in an " NAME_STR ": ", i);
- return mpz_get_si(*i.big);
+ if (!truncate && unlikely(!mpz_fits_slong_p(i.big))) fail("Integer is too big to fit in an " NAME_STR ": ", i);
+ return mpz_get_si(i.big);
}
#if INTX_H__INT_BITS < 64
diff --git a/src/stdlib/numX.h b/src/stdlib/numX.h
index fa8ae6d3..3d65cb59 100644
--- a/src/stdlib/numX.h
+++ b/src/stdlib/numX.h
@@ -69,11 +69,11 @@ MACROLIKE CONSTFUNC NUM_T NAMESPACED(from_int)(Int_t i, bool truncate) {
fail("Could not convert integer to " TYPE_STR " without losing precision: ", i.small >> 2);
return ret;
} else {
- NUM_T ret = mpz_get_d(*i.big);
+ NUM_T ret = mpz_get_d(i.big);
if (!truncate) {
mpz_t roundtrip;
mpz_init_set_d(roundtrip, (double)ret);
- if unlikely (mpz_cmp(*i.big, roundtrip) != 0)
+ if unlikely (mpz_cmp(i.big, roundtrip) != 0)
fail("Could not convert integer to " TYPE_STR " without losing precision: ", i);
}
return ret;