[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Guile-commits] 80/85: Optimize integer-expt for fixnums
From: |
Andy Wingo |
Subject: |
[Guile-commits] 80/85: Optimize integer-expt for fixnums |
Date: |
Thu, 13 Jan 2022 03:40:26 -0500 (EST) |
wingo pushed a commit to branch main
in repository guile.
commit 6fe43301aa66f898f3b27d604c45c93da5286e12
Author: Andy Wingo <wingo@pobox.com>
AuthorDate: Mon Jan 10 11:12:36 2022 +0100
Optimize integer-expt for fixnums
* libguile/integers.c (scm_integer_expt_ii): Add some optimizations for
cases in which we can avoid allocating an mpz.
---
libguile/integers.c | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git a/libguile/integers.c b/libguile/integers.c
index de1d2b3d5..a173d01f2 100644
--- a/libguile/integers.c
+++ b/libguile/integers.c
@@ -2265,6 +2265,27 @@ SCM
scm_integer_expt_ii (scm_t_inum n, scm_t_inum k)
{
ASSERT (k >= 0);
+ if (k == 0)
+ return SCM_INUM1;
+ if (k == 1)
+ return SCM_I_MAKINUM (n);
+ if (n == -1)
+ return scm_is_integer_odd_i (k) ? SCM_I_MAKINUM (-1) : SCM_INUM1;
+ if (n == 2)
+ {
+ if (k < SCM_I_FIXNUM_BIT - 1)
+ return SCM_I_MAKINUM (1L << k);
+ if (k < 64)
+ return scm_integer_from_uint64 (((uint64_t) 1) << k);
+ size_t nlimbs = k / (sizeof (mp_limb_t)*8) + 1;
+ size_t high_shift = k & (sizeof (mp_limb_t)*8 - 1);
+ struct scm_bignum *result = allocate_bignum (nlimbs);
+ mp_limb_t *rd = bignum_limbs (result);
+ mpn_zero(rd, nlimbs - 1);
+ rd[nlimbs - 1] = ((mp_limb_t) 1) << high_shift;
+ return scm_from_bignum (result);
+ }
+
mpz_t res;
mpz_init (res);
mpz_ui_pow_ui (res, inum_magnitude (n), k);
- [Guile-commits] 54/85: scm_to_mpz uses integer lib, (continued)
- [Guile-commits] 54/85: scm_to_mpz uses integer lib, Andy Wingo, 2022/01/13
- [Guile-commits] 51/85: Reimplement scm_{to,from}_{int64,uint64}, Andy Wingo, 2022/01/13
- [Guile-commits] 52/85: Implement scm_{to,from}_wchar inline, Andy Wingo, 2022/01/13
- [Guile-commits] 58/85: Remove dead bignum frexp code from numbers.c, Andy Wingo, 2022/01/13
- [Guile-commits] 53/85: Remove unused conv-{u,}integer.i.c, Andy Wingo, 2022/01/13
- [Guile-commits] 63/85: Use scm_integer_to_double_z in numbers.c instead of big2dbl, Andy Wingo, 2022/01/13
- [Guile-commits] 66/85: Finish srfi-60 port off old scm mpz API, Andy Wingo, 2022/01/13
- [Guile-commits] 74/85: Less pessimal scm_integer_sub_zi, Andy Wingo, 2022/01/13
- [Guile-commits] 76/85: Avoid bignum clone in scm_integer_sub_zz, Andy Wingo, 2022/01/13
- [Guile-commits] 79/85: Optimize scm_integer_mul_ii, Andy Wingo, 2022/01/13
- [Guile-commits] 80/85: Optimize integer-expt for fixnums,
Andy Wingo <=
- [Guile-commits] 81/85: Optimize logand against a positive inum, Andy Wingo, 2022/01/13
- [Guile-commits] 82/85: Simplify scm_abs for the real case, Andy Wingo, 2022/01/13
- [Guile-commits] 09/85: Implement ceiling-divide with new integer lib, Andy Wingo, 2022/01/13
- [Guile-commits] 08/85: Implement ceiling-remainder with new integer lib, Andy Wingo, 2022/01/13
- [Guile-commits] 11/85: Implement truncate-remainder with new integer lib, Andy Wingo, 2022/01/13
- [Guile-commits] 49/85: Reimplement scm_from_int8 etc, Andy Wingo, 2022/01/13
- [Guile-commits] 55/85: Reimplement exact-integer-sqrt with integers.[ch], Andy Wingo, 2022/01/13
- [Guile-commits] 56/85: Refactor scm_sqrt in terms of integers.[ch], Andy Wingo, 2022/01/13
- [Guile-commits] 62/85: Simplify magnitude, angle, Andy Wingo, 2022/01/13
- [Guile-commits] 65/85: Start porting srfi-60 off the bad bignum interfaces, Andy Wingo, 2022/01/13