aboutsummaryrefslogtreecommitdiff
path: root/builtins/integers.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-08-13 13:50:19 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-08-13 13:50:19 -0400
commit148782e61ba4efbf2981e7d9e18279e92054e149 (patch)
tree8ef1b1cf37957926ee944100265365cf4ebcce6f /builtins/integers.c
parent1ce826b9c3e9e57fe3acbd2abb5252bdeb630963 (diff)
Create inlined fast versions of integer ops
Diffstat (limited to 'builtins/integers.c')
-rw-r--r--builtins/integers.c72
1 files changed, 13 insertions, 59 deletions
diff --git a/builtins/integers.c b/builtins/integers.c
index 6a976d42..776fdc3d 100644
--- a/builtins/integers.c
+++ b/builtins/integers.c
@@ -137,12 +137,7 @@ public CORD Int$octal(Int_t i, Int_t digits_int, bool prefix) {
}
}
-public Int_t Int$plus(Int_t x, Int_t y) {
-
- const int64_t z = (int64_t)((uint64_t)x.small + (uint64_t)y.small);
- if (__builtin_expect(((z|2) == (int32_t)z), 1))
- return (Int_t){.small=(z-1)};
-
+public Int_t Int$slow_plus(Int_t x, Int_t y) {
mpz_t result;
mpz_init_set_int(result, x);
if (y.small & 1) {
@@ -155,11 +150,7 @@ public Int_t Int$plus(Int_t x, Int_t y) {
return Int$from_mpz(result);
}
-public Int_t Int$minus(Int_t x, Int_t y) {
- const int64_t z = (int64_t)(((uint64_t)x.small ^ 3) - (uint64_t)y.small);
- if (__builtin_expect(((z & ~2) == (int32_t)z), 1))
- return (Int_t){.small=z};
-
+public Int_t Int$slow_minus(Int_t x, Int_t y) {
mpz_t result;
mpz_init_set_int(result, x);
if (y.small & 1) {
@@ -172,13 +163,7 @@ public Int_t Int$minus(Int_t x, Int_t y) {
return Int$from_mpz(result);
}
-public Int_t Int$times(Int_t x, Int_t y) {
- if (__builtin_expect(((x.small & y.small) & 1) != 0, 1)) {
- const int64_t z = (x.small>>1) * (y.small>>1);
- if (__builtin_expect(z == (int32_t)z, 1))
- return (Int_t){.small=z+1};
- }
-
+public Int_t Int$slow_times(Int_t x, Int_t y) {
mpz_t result;
mpz_init_set_int(result, x);
if (y.small & 1) {
@@ -191,13 +176,7 @@ public Int_t Int$times(Int_t x, Int_t y) {
return Int$from_mpz(result);
}
-public Int_t Int$divided_by(Int_t x, Int_t y) {
- if (__builtin_expect(((x.small & y.small) & 1) != 0, 1)) {
- const int64_t z = 4*(x.small>>1) / (y.small>>1);
- if (__builtin_expect(z == (int32_t)z, 1))
- return (Int_t){.small=z+1};
- }
-
+public Int_t Int$slow_divided_by(Int_t x, Int_t y) {
mpz_t result;
mpz_init_set_int(result, x);
if (y.small & 1) {
@@ -210,7 +189,7 @@ public Int_t Int$divided_by(Int_t x, Int_t y) {
return Int$from_mpz(result);
}
-public Int_t Int$modulo(Int_t x, Int_t modulus)
+public Int_t Int$slow_modulo(Int_t x, Int_t modulus)
{
mpz_t result;
mpz_init_set_int(result, x);
@@ -220,7 +199,7 @@ public Int_t Int$modulo(Int_t x, Int_t modulus)
return Int$from_mpz(result);
}
-public Int_t Int$modulo1(Int_t x, Int_t modulus)
+public Int_t Int$slow_modulo1(Int_t x, Int_t modulus)
{
mpz_t result;
mpz_init_set_int(result, x);
@@ -232,14 +211,8 @@ public Int_t Int$modulo1(Int_t x, Int_t modulus)
return Int$from_mpz(result);
}
-public Int_t Int$left_shifted(Int_t x, Int_t y)
+public Int_t Int$slow_left_shifted(Int_t x, Int_t y)
{
- if (__builtin_expect(((x.small & y.small) & 1) != 0, 1)) {
- const int64_t z = 4*((x.small>>2) << (y.small>>2));
- if (__builtin_expect(z == (int32_t)z, 1))
- return (Int_t){.small=z+1};
- }
-
mp_bitcnt_t bits = (mp_bitcnt_t)Int$as_i64(y);
mpz_t result;
mpz_init_set_int(result, x);
@@ -247,14 +220,8 @@ public Int_t Int$left_shifted(Int_t x, Int_t y)
return Int$from_mpz(result);
}
-public Int_t Int$right_shifted(Int_t x, Int_t y)
+public Int_t Int$slow_right_shifted(Int_t x, Int_t y)
{
- if (__builtin_expect(((x.small & y.small) & 1) != 0, 1)) {
- const int64_t z = 4*((x.small>>2) >> (y.small>>2));
- if (__builtin_expect(z == (int32_t)z, 1))
- return (Int_t){.small=z+1};
- }
-
mp_bitcnt_t bits = (mp_bitcnt_t)Int$as_i64(y);
mpz_t result;
mpz_init_set_int(result, x);
@@ -262,12 +229,8 @@ public Int_t Int$right_shifted(Int_t x, Int_t y)
return Int$from_mpz(result);
}
-public Int_t Int$bit_and(Int_t x, Int_t y)
+public Int_t Int$slow_bit_and(Int_t x, Int_t y)
{
- const int64_t z = x.small & y.small;
- if (__builtin_expect((z & 1) == 1, 1))
- return (Int_t){.small=z};
-
mpz_t result;
mpz_init_set_int(result, x);
mpz_t y_mpz;
@@ -276,11 +239,8 @@ public Int_t Int$bit_and(Int_t x, Int_t y)
return Int$from_mpz(result);
}
-public Int_t Int$bit_or(Int_t x, Int_t y)
+public Int_t Int$slow_bit_or(Int_t x, Int_t y)
{
- if (__builtin_expect(((x.small & y.small) & 1) == 1, 1))
- return (Int_t){.small=(x.small | y.small)};
-
mpz_t result;
mpz_init_set_int(result, x);
mpz_t y_mpz;
@@ -289,11 +249,8 @@ public Int_t Int$bit_or(Int_t x, Int_t y)
return Int$from_mpz(result);
}
-public Int_t Int$bit_xor(Int_t x, Int_t y)
+public Int_t Int$slow_bit_xor(Int_t x, Int_t y)
{
- if (__builtin_expect(((x.small & y.small) & 1) == 1, 1))
- return (Int_t){.small=(x.small ^ y.small) | 1};
-
mpz_t result;
mpz_init_set_int(result, x);
mpz_t y_mpz;
@@ -302,11 +259,8 @@ public Int_t Int$bit_xor(Int_t x, Int_t y)
return Int$from_mpz(result);
}
-public Int_t Int$negated(Int_t x)
+public Int_t Int$slow_negated(Int_t x)
{
- if (__builtin_expect((x.small & 1), 1))
- return (Int_t){.small=(~x.small) ^ 3};
-
mpz_t result;
mpz_init_set_int(result, x);
mpz_neg(result, result);
@@ -314,7 +268,7 @@ public Int_t Int$negated(Int_t x)
return Int$from_mpz(result);
}
-public Int_t Int$negative(Int_t x)
+public Int_t Int$slow_negative(Int_t x)
{
if (__builtin_expect((x.small & 1), 1))
return (Int_t){.small=4*-((x.small)>>2) + 1};