[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Guile-commits] 79/85: Optimize scm_integer_mul_ii
From: |
Andy Wingo |
Subject: |
[Guile-commits] 79/85: Optimize scm_integer_mul_ii |
Date: |
Thu, 13 Jan 2022 03:40:26 -0500 (EST) |
wingo pushed a commit to branch main
in repository guile.
commit 2cf80ca2383f2c0e0a4171296e136cfcc579eeb2
Author: Andy Wingo <wingo@pobox.com>
AuthorDate: Sun Jan 9 23:10:18 2022 +0100
Optimize scm_integer_mul_ii
* libguile/integers.c (make_bignum_2): Always include.
(scm_integer_mul_ii): Avoid making a temporary allocation.
---
libguile/integers.c | 28 +++++++++++++++++-----------
1 file changed, 17 insertions(+), 11 deletions(-)
diff --git a/libguile/integers.c b/libguile/integers.c
index 3e7df7c12..de1d2b3d5 100644
--- a/libguile/integers.c
+++ b/libguile/integers.c
@@ -211,7 +211,6 @@ make_bignum_1 (int is_negative, mp_limb_t limb)
return is_negative ? negate_bignum(z) : z;
}
-#if SCM_SIZEOF_LONG == 4
static struct scm_bignum *
make_bignum_2 (int is_negative, mp_limb_t lo, mp_limb_t hi)
{
@@ -220,7 +219,6 @@ make_bignum_2 (int is_negative, mp_limb_t lo, mp_limb_t hi)
z->limbs[1] = hi;
return is_negative ? negate_bignum(z) : z;
}
-#endif
static struct scm_bignum *
make_bignum_from_uint64 (uint64_t val)
@@ -3024,17 +3022,25 @@ scm_integer_mul_ii (scm_t_inum x, scm_t_inum y)
int64_t k = x * (int64_t) y;
if (SCM_FIXABLE (k))
return SCM_I_MAKINUM (k);
-#else
- if (x == 0)
- return SCM_INUM0;
- scm_t_inum ax = (x > 0) ? x : -x;
- scm_t_inum ay = (y > 0) ? y : -y;
- if (SCM_MOST_POSITIVE_FIXNUM / ax >= ay)
- return SCM_I_MAKINUM (x * y);
#endif
- // FIXME: Use mpn_mul with two-limb result to avoid allocating.
- return scm_integer_mul_zi (long_to_bignum (x), y);
+ mp_limb_t xd[1] = { long_magnitude (x) };
+ mp_limb_t lo;
+ int negative = (x < 0) != (y < 0);
+ mp_limb_t hi = mpn_mul_1 (&lo, xd, 1, long_magnitude (y));
+ if (!hi)
+ {
+ if (negative)
+ {
+ if (lo <= long_magnitude (SCM_MOST_NEGATIVE_FIXNUM))
+ return SCM_I_MAKINUM (negative_long (lo));
+ }
+ else if (lo <= SCM_MOST_POSITIVE_FIXNUM)
+ return SCM_I_MAKINUM (lo);
+ return scm_from_bignum (make_bignum_1 (negative, lo));
+ }
+
+ return scm_from_bignum (make_bignum_2 (negative, lo, hi));
}
SCM
- [Guile-commits] 48/85: Reimplement scm_is_{un, }signed_integer for bignums, (continued)
- [Guile-commits] 48/85: Reimplement scm_is_{un, }signed_integer for bignums, Andy Wingo, 2022/01/13
- [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 <=
- [Guile-commits] 80/85: Optimize integer-expt for fixnums, Andy Wingo, 2022/01/13
- [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