aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPaul Eggert2024-07-11 12:27:36 +0200
committerPaul Eggert2024-07-11 16:01:41 +0200
commit1c8e64a9536ed092af27279fa3f044cf031a4324 (patch)
treeea4df87d22f36b9eb5456c5782865003a99aee2f /src
parent2fb7bb41bee5e39391c9abc8013bcef39782e88d (diff)
downloademacs-1c8e64a9536ed092af27279fa3f044cf031a4324.tar.gz
emacs-1c8e64a9536ed092af27279fa3f044cf031a4324.zip
New FASTER_BIGNUM macro to test slow-path code
* src/bignum.h (FASTER_BIGNUM): New macro. (mpz_set_intmax, mpz_set_uintmax): Optimize only if FASTER_BIGNUM. Also, use ckd_add to test for overflow instead of doing it by hand.
Diffstat (limited to 'src')
-rw-r--r--src/bignum.h16
1 files changed, 12 insertions, 4 deletions
diff --git a/src/bignum.h b/src/bignum.h
index 2749f8370d0..54ba0cde410 100644
--- a/src/bignum.h
+++ b/src/bignum.h
@@ -25,6 +25,12 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
25#include <gmp.h> 25#include <gmp.h>
26#include "lisp.h" 26#include "lisp.h"
27 27
28/* Compile with -DFASTER_BIGNUM=0 to disable common optimizations and
29 allow easier testing of some slow-path code. */
30#ifndef FASTER_BIGNUM
31# define FASTER_BIGNUM 1
32#endif
33
28/* Number of data bits in a limb. */ 34/* Number of data bits in a limb. */
29#ifndef GMP_NUMB_BITS 35#ifndef GMP_NUMB_BITS
30enum { GMP_NUMB_BITS = TYPE_WIDTH (mp_limb_t) }; 36enum { GMP_NUMB_BITS = TYPE_WIDTH (mp_limb_t) };
@@ -68,16 +74,18 @@ mpz_set_intmax (mpz_t result, intmax_t v)
68 /* mpz_set_si works in terms of long, but Emacs may use a wider 74 /* mpz_set_si works in terms of long, but Emacs may use a wider
69 integer type, and so sometimes will have to construct the mpz_t 75 integer type, and so sometimes will have to construct the mpz_t
70 by hand. */ 76 by hand. */
71 if (LONG_MIN <= v && v <= LONG_MAX) 77 long int i;
72 mpz_set_si (result, v); 78 if (FASTER_BIGNUM && !ckd_add (&i, v, 0))
79 mpz_set_si (result, i);
73 else 80 else
74 mpz_set_intmax_slow (result, v); 81 mpz_set_intmax_slow (result, v);
75} 82}
76INLINE void ARG_NONNULL ((1)) 83INLINE void ARG_NONNULL ((1))
77mpz_set_uintmax (mpz_t result, uintmax_t v) 84mpz_set_uintmax (mpz_t result, uintmax_t v)
78{ 85{
79 if (v <= ULONG_MAX) 86 unsigned long int i;
80 mpz_set_ui (result, v); 87 if (FASTER_BIGNUM && !ckd_add (&i, v, 0))
88 mpz_set_ui (result, i);
81 else 89 else
82 mpz_set_uintmax_slow (result, v); 90 mpz_set_uintmax_slow (result, v);
83} 91}