[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Guile-commits] 73/85: Optimize scm_integer_mul_zz.
From: |
Andy Wingo |
Subject: |
[Guile-commits] 73/85: Optimize scm_integer_mul_zz. |
Date: |
Thu, 13 Jan 2022 03:40:25 -0500 (EST) |
wingo pushed a commit to branch main
in repository guile.
commit ad6811a12b8a3aab68d0610c799c51515ec7c427
Author: Andy Wingo <wingo@pobox.com>
AuthorDate: Sun Jan 9 16:46:27 2022 +0100
Optimize scm_integer_mul_zz.
* libguile/integers.c (scm_integer_mul_zz): Optimize to avoid temporary
allocations.
---
libguile/integers.c | 24 ++++++++++++++++++------
1 file changed, 18 insertions(+), 6 deletions(-)
diff --git a/libguile/integers.c b/libguile/integers.c
index ee2907f30..60c0fd34a 100644
--- a/libguile/integers.c
+++ b/libguile/integers.c
@@ -2939,13 +2939,25 @@ scm_integer_mul_zi (struct scm_bignum *x, scm_t_inum y)
SCM
scm_integer_mul_zz (struct scm_bignum *x, struct scm_bignum *y)
{
- mpz_t result, zx, zy;
- mpz_init (result);
- alias_bignum_to_mpz (x, zx);
- alias_bignum_to_mpz (y, zy);
- mpz_mul (result, zx, zy);
+ size_t xn = bignum_limb_count (x);
+ size_t yn = bignum_limb_count (y);
+ if (xn == 0 || yn == 0)
+ return SCM_INUM0;
+
+ struct scm_bignum *result = allocate_bignum (xn + yn);
+ mp_limb_t *rd = bignum_limbs (result);
+ const mp_limb_t *xd = bignum_limbs (x);
+ const mp_limb_t *yd = bignum_limbs (y);
+ int negate = bignum_is_negative (x) != bignum_is_negative (y);
+ if (xd == yd)
+ mpn_sqr (rd, xd, xn);
+ else if (xn <= yn)
+ mpn_mul (rd, yd, yn, xd, xn);
+ else
+ mpn_mul (rd, xd, xn, yd, yn);
scm_remember_upto_here_2 (x, y);
- return take_mpz (result);
+ return normalize_bignum
+ (bignum_negate_if (negate, (bignum_trim1 (result))));
}
int
- [Guile-commits] 36/85: Build scm_integer_p on scm_is_integer, not vice versa, (continued)
- [Guile-commits] 36/85: Build scm_integer_p on scm_is_integer, not vice versa, Andy Wingo, 2022/01/13
- [Guile-commits] 37/85: Reimplement = on integer lib, clean up scm_num_eq_p, Andy Wingo, 2022/01/13
- [Guile-commits] 40/85: Simplify implementation of min, max, Andy Wingo, 2022/01/13
- [Guile-commits] 42/85: Simplify scm_difference, use integer lib, Andy Wingo, 2022/01/13
- [Guile-commits] 47/85: Fix scm_integer_to_double_z to always round; clean ups, Andy Wingo, 2022/01/13
- [Guile-commits] 50/85: Reimplement scm_{to,from}_{int32,uint32}, Andy Wingo, 2022/01/13
- [Guile-commits] 67/85: scm_to_ipv6 uses scm_to_mpz, Andy Wingo, 2022/01/13
- [Guile-commits] 68/85: Bignums avoid both custom GMP allocator and finalizers, Andy Wingo, 2022/01/13
- [Guile-commits] 69/85: take_mpz optimization, Andy Wingo, 2022/01/13
- [Guile-commits] 71/85: Re-rewrite integer-expt in C, Andy Wingo, 2022/01/13
- [Guile-commits] 73/85: Optimize scm_integer_mul_zz.,
Andy Wingo <=
- [Guile-commits] 84/85: Have log and log10(real nan) return real nan regardless of sign, Andy Wingo, 2022/01/13
- [Guile-commits] 85/85: Remove dead code in scm_integer_inexact_sqrt_z, Andy Wingo, 2022/01/13
- [Guile-commits] 24/85: Implement scm_logtest with new integer library, Andy Wingo, 2022/01/13
- [Guile-commits] 13/85: Implement centered-quotient with new integer lib, Andy Wingo, 2022/01/13
- [Guile-commits] 23/85: Implement scm_logxor with new integer library, Andy Wingo, 2022/01/13
- [Guile-commits] 19/85: Implement gcd with new integer lib, Andy Wingo, 2022/01/13
- [Guile-commits] 57/85: Expose frexp from integers lib, Andy Wingo, 2022/01/13
- [Guile-commits] 59/85: divide2double refactor, Andy Wingo, 2022/01/13
- [Guile-commits] 60/85: Simplify scm_exact_integer_quotient, Andy Wingo, 2022/01/13
- [Guile-commits] 61/85: Remove last non-admin SCM_I_BIG_MPZ uses in numbers.c, Andy Wingo, 2022/01/13