From 9b9087bfef72417b4930512d33103d9a5a748cd8 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 13 Aug 2024 02:42:02 -0400 Subject: Get rid of slow_* int functions --- builtins/integers.c | 80 ++++++++++++++++++++++++++++++++++++++++++--------- builtins/integers.h | 82 +++++++++-------------------------------------------- environment.c | 1 - 3 files changed, 80 insertions(+), 83 deletions(-) diff --git a/builtins/integers.c b/builtins/integers.c index 1faae4c2..eaaae660 100644 --- a/builtins/integers.c +++ b/builtins/integers.c @@ -129,7 +129,12 @@ public CORD Int$octal(Int_t i, Int_t digits_int, bool prefix) { } } -public Int_t Int$slow_plus(Int_t x, Int_t y) { +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)}; + mpz_t result; mpz_init_set_int(result, x); if (y.small & 1) { @@ -142,7 +147,11 @@ public Int_t Int$slow_plus(Int_t x, Int_t y) { return Int$from_mpz(result); } -public Int_t Int$slow_minus(Int_t x, Int_t y) { +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}; + mpz_t result; mpz_init_set_int(result, x); if (y.small & 1) { @@ -155,7 +164,13 @@ public Int_t Int$slow_minus(Int_t x, Int_t y) { return Int$from_mpz(result); } -public Int_t Int$slow_times(Int_t x, Int_t y) { +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}; + } + mpz_t result; mpz_init_set_int(result, x); if (y.small & 1) { @@ -168,7 +183,13 @@ public Int_t Int$slow_times(Int_t x, Int_t y) { return Int$from_mpz(result); } -public Int_t Int$slow_divided_by(Int_t x, Int_t y) { +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}; + } + mpz_t result; mpz_init_set_int(result, x); if (y.small & 1) { @@ -181,7 +202,7 @@ public Int_t Int$slow_divided_by(Int_t x, Int_t y) { return Int$from_mpz(result); } -public Int_t Int$slow_modulo(Int_t x, Int_t modulus) +public Int_t Int$modulo(Int_t x, Int_t modulus) { mpz_t result; mpz_init_set_int(result, x); @@ -191,7 +212,7 @@ public Int_t Int$slow_modulo(Int_t x, Int_t modulus) return Int$from_mpz(result); } -public Int_t Int$slow_modulo1(Int_t x, Int_t modulus) +public Int_t Int$modulo1(Int_t x, Int_t modulus) { mpz_t result; mpz_init_set_int(result, x); @@ -203,8 +224,14 @@ public Int_t Int$slow_modulo1(Int_t x, Int_t modulus) return Int$from_mpz(result); } -public Int_t Int$slow_left_shifted(Int_t x, Int_t y) +public Int_t Int$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); @@ -212,8 +239,14 @@ public Int_t Int$slow_left_shifted(Int_t x, Int_t y) return Int$from_mpz(result); } -public Int_t Int$slow_right_shifted(Int_t x, Int_t y) +public Int_t Int$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); @@ -221,8 +254,12 @@ public Int_t Int$slow_right_shifted(Int_t x, Int_t y) return Int$from_mpz(result); } -public Int_t Int$slow_bit_and(Int_t x, Int_t y) +public Int_t Int$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; @@ -231,8 +268,11 @@ public Int_t Int$slow_bit_and(Int_t x, Int_t y) return Int$from_mpz(result); } -public Int_t Int$slow_bit_or(Int_t x, Int_t y) +public Int_t Int$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; @@ -241,8 +281,11 @@ public Int_t Int$slow_bit_or(Int_t x, Int_t y) return Int$from_mpz(result); } -public Int_t Int$slow_bit_xor(Int_t x, Int_t y) +public Int_t Int$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; @@ -251,8 +294,11 @@ public Int_t Int$slow_bit_xor(Int_t x, Int_t y) return Int$from_mpz(result); } -public Int_t Int$slow_negated(Int_t x) +public Int_t Int$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); @@ -260,16 +306,22 @@ public Int_t Int$slow_negated(Int_t x) return Int$from_mpz(result); } -public Int_t Int$slow_negative(Int_t x) +public Int_t Int$negative(Int_t x) { + if (__builtin_expect((x.small & 1), 1)) + return (Int_t){.small=4*-((x.small)>>2) + 1}; + mpz_t result; mpz_init_set_int(result, x); mpz_neg(result, result); return Int$from_mpz(result); } -public Int_t Int$slow_abs(Int_t x) +public Int_t Int$abs(Int_t x) { + if (__builtin_expect((x.small & 1), 1)) + return (Int_t){.small=4*labs((x.small)>>2) + 1}; + mpz_t result; mpz_init_set_int(result, x); mpz_abs(result, result); diff --git a/builtins/integers.h b/builtins/integers.h index 2fc2688e..33df2f89 100644 --- a/builtins/integers.h +++ b/builtins/integers.h @@ -81,74 +81,20 @@ Int_t Int$from_i64(int64_t i); Int_t Int$from_num(double n); #define I(i) ((int64_t)(i) == (int32_t)(i) ? ((Int_t){.small=((uint64_t)(i)<<2)|1}) : Int$from_i64(i)) -Int_t Int$slow_plus(Int_t x, Int_t y); -Int_t Int$slow_minus(Int_t x, Int_t y); -Int_t Int$slow_times(Int_t x, Int_t y); -Int_t Int$slow_divided_by(Int_t x, Int_t y); -Int_t Int$slow_modulo(Int_t x, Int_t y); -Int_t Int$slow_modulo1(Int_t x, Int_t y); -Int_t Int$slow_left_shifted(Int_t x, Int_t y); -Int_t Int$slow_right_shifted(Int_t x, Int_t y); -Int_t Int$slow_bit_and(Int_t x, Int_t y); -Int_t Int$slow_bit_or(Int_t x, Int_t y); -Int_t Int$slow_bit_xor(Int_t x, Int_t y); -Int_t Int$slow_negative(Int_t x); -Int_t Int$slow_negated(Int_t x); -Int_t Int$slow_abs(Int_t x); -#define Int$plus(_x, _y) ({ \ - Int_t x = _x, y = _y; \ - const int64_t z = (int64_t)((uint64_t)x.small + (uint64_t)y.small); \ - __builtin_expect(((z|2) != (int32_t)z), 0) ? Int$slow_plus(x, y) : (Int_t){.small=(z-1)}; }) - -#define Int$minus(_x, _y) ({ \ - Int_t x = _x, y = _y; \ - const int64_t z = (int64_t)(((uint64_t)x.small ^ 3) - (uint64_t)y.small); \ - __builtin_expect(((z & ~2) != (int32_t)z), 0) ? Int$slow_minus(x, y) : (Int_t){.small=z}; }) - -#define Int$times(_x, _y) ({ \ - Int_t x = _x, y = _y; \ - __builtin_expect(((x.small & y.small) & 1) == 0, 0) ? Int$slow_times(x, y) : ({ \ - const int64_t z = (x.small>>1) * (y.small>>1); \ - __builtin_expect(z != (int32_t)z, 0) ? Int$slow_times(x, y) : (Int_t){.small=z+1}; }); }) - -#define Int$divided_by(_x, _y) ({ \ - Int_t x = _x, y = _y; \ - __builtin_expect(((x.small & y.small) & 1) == 0, 0) ? Int$slow_divided_by(x, y) : ({ \ - const int64_t z = 4*(x.small>>1) / (y.small>>1); \ - __builtin_expect(z != (int32_t)z, 0) ? Int$slow_divided_by(x, y) : (Int_t){.small=z+1}; }); }) -#define Int$left_shifted(_x, _y) ({ \ - Int_t x = _x, y = _y; \ - __builtin_expect(((x.small & y.small) & 1) == 0, 0) ? Int$slow_left_shifted(x, y) : ({ \ - const int64_t z = 4*((x.small>>2) << (y.small>>2)); \ - __builtin_expect(z != (int32_t)z, 0) ? Int$slow_left_shifted(x, y) : (Int_t){.small=z+1}; }); }) -#define Int$right_shifted(_x, _y) ({ \ - Int_t x = _x, y = _y; \ - __builtin_expect(((x.small & y.small) & 1) == 0, 0) ? Int$slow_right_shifted(x, y) : ({ \ - const int64_t z = 4*((x.small>>2) >> (y.small>>2)); \ - __builtin_expect(z != (int32_t)z, 0) ? Int$slow_right_shifted(x, y) : (Int_t){.small=z+1}; }); }) -#define Int$bit_and(_x, _y) ({ \ - Int_t x = _x, y = _y; \ - const int64_t z = x.small & y.small; \ - __builtin_expect((z & 1) == 1, 1) ? (Int_t){.small=z} : Int$slow_bit_and(x, y); }) -#define Int$bit_or(_x, _y) ({ \ - Int_t x = _x, y = _y; \ - __builtin_expect(((x.small & y.small) & 1) == 1, 1) ? (Int_t){.small=(x.small | y.small)} : Int$slow_bit_or(x, y); }) -#define Int$bit_xor(_x, _y) ({ \ - Int_t x = _x, y = _y; \ - __builtin_expect(((x.small & y.small) & 1) == 1, 1) ? (Int_t){.small=(x.small ^ y.small) | 1} : Int$slow_bit_xor(x, y); }) - -#define Int$modulo(x, y) Int$slow_modulo(x, y) -#define Int$modulo1(x, y) Int$slow_modulo1(x, y) - -#define Int$negative(_x) ({ \ - Int_t x = _x; \ - __builtin_expect((x.small & 1), 1) ? (Int_t){.small=4*-((x.small)/4) + 1} : Int$slow_negative(x); }) -#define Int$negated(_x) ({ \ - Int_t x = _x; \ - __builtin_expect((x.small & 1), 1) ? (Int_t){.small=(~x.small) ^ 3} : Int$slow_negated(x); }) -#define Int$abs(_x) ({ \ - Int_t x = _x; \ - __builtin_expect((x.small & 1), 1) ? (Int_t){.small=4*labs((x.small)/4) + 1} : Int$slow_abs(x); }) +Int_t Int$plus(Int_t x, Int_t y); +Int_t Int$minus(Int_t x, Int_t y); +Int_t Int$times(Int_t x, Int_t y); +Int_t Int$divided_by(Int_t x, Int_t y); +Int_t Int$modulo(Int_t x, Int_t y); +Int_t Int$modulo1(Int_t x, Int_t y); +Int_t Int$left_shifted(Int_t x, Int_t y); +Int_t Int$right_shifted(Int_t x, Int_t y); +Int_t Int$bit_and(Int_t x, Int_t y); +Int_t Int$bit_or(Int_t x, Int_t y); +Int_t Int$bit_xor(Int_t x, Int_t y); +Int_t Int$negative(Int_t x); +Int_t Int$negated(Int_t x); +Int_t Int$abs(Int_t x); extern const TypeInfo $Int; diff --git a/environment.c b/environment.c index fc10cc5b..cbf35af4 100644 --- a/environment.c +++ b/environment.c @@ -92,7 +92,6 @@ env_t *new_compilation_unit(CORD *libname) {"octal", "Int$octal", "func(i:Int, digits=0, prefix=yes)->Text"}, {"random", "Int$random", "func(min:Int, max:Int)->Int"}, {"from_text", "Int$from_text", "func(text:Text, the_rest=!&Text)->Int"}, - {"abs", "labs", "func(i:Int)->Int"}, {"to", "Int$to", "func(from:Int,to:Int)->Range"}, {"plus", "Int$plus", "func(x:Int,y:Int)->Int"}, {"minus", "Int$minus", "func(x:Int,y:Int)->Int"}, -- cgit v1.2.3