[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
master 0c850df888e 13/17: Optimize smallish mpz to native int conversion
From: |
Paul Eggert |
Subject: |
master 0c850df888e 13/17: Optimize smallish mpz to native int conversion |
Date: |
Thu, 11 Jul 2024 10:01:59 -0400 (EDT) |
branch: master
commit 0c850df888ebb68096a82ab32089e809de591620
Author: Paul Eggert <eggert@cs.ucla.edu>
Commit: Paul Eggert <eggert@cs.ucla.edu>
Optimize smallish mpz to native int conversion
* src/bignum.c (make_integer_mpz, mpz_to_intmax):
If FASTER_BIGNUM, optimize the common case where the value fits in
long int. In this case we can use mpz_fits_slong_p and mpz_get_si
instead of looping with mpz_getlimbn.
(mpz_to_uintmax): Likewise for unsigned long int and mpz_get_ui.
---
src/bignum.c | 34 +++++++++++++++++++++++++++++++++-
1 file changed, 33 insertions(+), 1 deletion(-)
diff --git a/src/bignum.c b/src/bignum.c
index 1fe195d78ea..7589691dd0c 100644
--- a/src/bignum.c
+++ b/src/bignum.c
@@ -145,9 +145,19 @@ make_neg_biguint (uintmax_t n)
Lisp_Object
make_integer_mpz (void)
{
+ if (FASTER_BIGNUM && mpz_fits_slong_p (mpz[0]))
+ {
+ long int v = mpz_get_si (mpz[0]);
+ if (!FIXNUM_OVERFLOW_P (v))
+ return make_fixnum (v);
+ }
+
size_t bits = mpz_sizeinbase (mpz[0], 2);
- if (bits <= FIXNUM_BITS)
+ if (! (FASTER_BIGNUM
+ && FIXNUM_OVERFLOW_P (LONG_MIN)
+ && FIXNUM_OVERFLOW_P (LONG_MAX))
+ && bits <= FIXNUM_BITS)
{
EMACS_INT v = 0;
int i = 0, shift = 0;
@@ -216,6 +226,17 @@ mpz_set_uintmax_slow (mpz_t result, uintmax_t v)
bool
mpz_to_intmax (mpz_t const z, intmax_t *pi)
{
+ if (FASTER_BIGNUM)
+ {
+ if (mpz_fits_slong_p (z))
+ {
+ *pi = mpz_get_si (z);
+ return true;
+ }
+ if (LONG_MIN <= INTMAX_MIN && INTMAX_MAX <= LONG_MAX)
+ return false;
+ }
+
ptrdiff_t bits = mpz_sizeinbase (z, 2);
bool negative = mpz_sgn (z) < 0;
@@ -246,6 +267,17 @@ mpz_to_intmax (mpz_t const z, intmax_t *pi)
bool
mpz_to_uintmax (mpz_t const z, uintmax_t *pi)
{
+ if (FASTER_BIGNUM)
+ {
+ if (mpz_fits_ulong_p (z))
+ {
+ *pi = mpz_get_ui (z);
+ return true;
+ }
+ if (UINTMAX_MAX <= ULONG_MAX)
+ return false;
+ }
+
if (mpz_sgn (z) < 0)
return false;
ptrdiff_t bits = mpz_sizeinbase (z, 2);
- master updated (166685a7d95 -> c3e6923b004), Paul Eggert, 2024/07/11
- master c45ae286b54 03/17: Refactor decode_ticks_hz via switch, Paul Eggert, 2024/07/11
- master 0e221d3789a 02/17: Refactor timefns order, Paul Eggert, 2024/07/11
- master 22a3a90f763 04/17: Split lisp_to_timespec in two, Paul Eggert, 2024/07/11
- master 34bde2f790d 05/17: Push some time conversions down, Paul Eggert, 2024/07/11
- master 2fb7bb41bee 11/17: In timefns, call natnump only for non-fixnums, Paul Eggert, 2024/07/11
- master 1c8e64a9536 12/17: New FASTER_BIGNUM macro to test slow-path code, Paul Eggert, 2024/07/11
- master 0c850df888e 13/17: Optimize smallish mpz to native int conversion,
Paul Eggert <=
- master 5e8a38ecb2a 07/17: Rename timefns internals, Paul Eggert, 2024/07/11
- master a6a3f322453 06/17: Speed up decode-time when not doing subseconds, Paul Eggert, 2024/07/11
- master 6ef052d9b23 08/17: Reduce size of integer product in timefns, Paul Eggert, 2024/07/11
- master abafc6ca014 09/17: In timefns, prefer ui mul and div, Paul Eggert, 2024/07/11
- master 75f53d7c2c1 14/17: In timefns.c avoid by-hand overflow checking, Paul Eggert, 2024/07/11
- master c3e6923b004 17/17: Rename timefns static function lisp_time_struct, Paul Eggert, 2024/07/11
- master e8b3c4cb58c 15/17: Decode current time directly to timespec, Paul Eggert, 2024/07/11
- master b6cbf0cbb66 10/17: In timefns, do gcd reduction more often, Paul Eggert, 2024/07/11
- master e30706fd12b 16/17: Avoid mpz for some common timestamp cases, Paul Eggert, 2024/07/11
- master 35365620e4c 01/17: Refactor timefns more functionally, Paul Eggert, 2024/07/11