[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Guile-commits] 21/69: Implement lcm with new integer lib
From: |
Andy Wingo |
Subject: |
[Guile-commits] 21/69: Implement lcm with new integer lib |
Date: |
Fri, 7 Jan 2022 08:27:09 -0500 (EST) |
wingo pushed a commit to branch wip-inline-digits
in repository guile.
commit 3373662ea043b9d8be277a2696c609a2ab8fdb62
Author: Andy Wingo <wingo@pobox.com>
AuthorDate: Sun Dec 19 09:52:39 2021 +0100
Implement lcm with new integer lib
* libguile/integers.c (scm_integer_lcm_ii)
(scm_integer_lcm_zi, scm_integer_lcm_zz): New internal functions.
* libguile/integers.h: Declare internal functions.
* libguile/numbers.c (scm_lcm): Use the new functions.
---
libguile/integers.c | 37 +++++++++++++++++++++++++++++++++++++
libguile/integers.h | 4 ++++
libguile/numbers.c | 48 +++++++++---------------------------------------
3 files changed, 50 insertions(+), 39 deletions(-)
diff --git a/libguile/integers.c b/libguile/integers.c
index cca40bc8b..3c7a86709 100644
--- a/libguile/integers.c
+++ b/libguile/integers.c
@@ -1817,3 +1817,40 @@ scm_integer_gcd_zz (SCM x, SCM y)
scm_remember_upto_here_2 (x, y);
return take_mpz (result);
}
+
+SCM
+scm_integer_lcm_ii (scm_t_inum x, scm_t_inum y)
+{
+ SCM d = scm_integer_gcd_ii (x, y);
+ if (scm_is_eq (d, SCM_INUM0))
+ return d;
+ else
+ return scm_abs (scm_product (SCM_I_MAKINUM (x),
+ scm_quotient (SCM_I_MAKINUM (y), d)));
+}
+
+SCM
+scm_integer_lcm_zi (SCM x, scm_t_inum y)
+{
+ if (y == 0) return SCM_INUM0;
+ if (y < 0) y = - y;
+ mpz_t result, zx;
+ mpz_init (result);
+ alias_bignum_to_mpz (scm_bignum (x), zx);
+ mpz_lcm_ui (result, zx, y);
+ scm_remember_upto_here_1 (x);
+ return take_mpz (result);
+}
+
+SCM
+scm_integer_lcm_zz (SCM x, SCM y)
+{
+ mpz_t result, zx, zy;
+ mpz_init (result);
+ alias_bignum_to_mpz (scm_bignum (x), zx);
+ alias_bignum_to_mpz (scm_bignum (y), zy);
+ mpz_lcm (result, zx, zy);
+ scm_remember_upto_here_2 (x, y);
+ /* shouldn't need to normalize b/c lcm of 2 bigs should be big */
+ return take_mpz (result);
+}
diff --git a/libguile/integers.h b/libguile/integers.h
index 699525b3f..31407b79e 100644
--- a/libguile/integers.h
+++ b/libguile/integers.h
@@ -128,6 +128,10 @@ SCM_INTERNAL SCM scm_integer_gcd_ii (scm_t_inum x,
scm_t_inum y);
SCM_INTERNAL SCM scm_integer_gcd_zi (SCM x, scm_t_inum y);
SCM_INTERNAL SCM scm_integer_gcd_zz (SCM x, SCM y);
+SCM_INTERNAL SCM scm_integer_lcm_ii (scm_t_inum x, scm_t_inum y);
+SCM_INTERNAL SCM scm_integer_lcm_zi (SCM x, scm_t_inum y);
+SCM_INTERNAL SCM scm_integer_lcm_zz (SCM x, SCM y);
+
#endif /* SCM_INTEGERS_H */
diff --git a/libguile/numbers.c b/libguile/numbers.c
index f0ddc9379..7aea6661a 100644
--- a/libguile/numbers.c
+++ b/libguile/numbers.c
@@ -2844,33 +2844,15 @@ SCM_PRIMITIVE_GENERIC (scm_i_lcm, "lcm", 0, 2, 1,
SCM
scm_lcm (SCM n1, SCM n2)
{
- if (SCM_UNLIKELY (SCM_UNBNDP (n2)))
+ if (SCM_UNBNDP (n2))
return SCM_UNBNDP (n1) ? SCM_INUM1 : scm_abs (n1);
- if (SCM_LIKELY (SCM_I_INUMP (n1)))
+ if (SCM_I_INUMP (n1))
{
- if (SCM_LIKELY (SCM_I_INUMP (n2)))
- {
- SCM d = scm_gcd (n1, n2);
- if (scm_is_eq (d, SCM_INUM0))
- return d;
- else
- return scm_abs (scm_product (n1, scm_quotient (n2, d)));
- }
- else if (SCM_LIKELY (SCM_BIGP (n2)))
- {
- /* inum n1, big n2 */
- inumbig:
- {
- SCM result = scm_i_mkbig ();
- scm_t_inum nn1 = SCM_I_INUM (n1);
- if (nn1 == 0) return SCM_INUM0;
- if (nn1 < 0) nn1 = - nn1;
- mpz_lcm_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (n2), nn1);
- scm_remember_upto_here_1 (n2);
- return result;
- }
- }
+ if (SCM_I_INUMP (n2))
+ return scm_integer_lcm_ii (SCM_I_INUM (n1), SCM_I_INUM (n2));
+ else if (SCM_BIGP (n2))
+ return scm_integer_lcm_zi (n2, SCM_I_INUM (n1));
else if (SCM_REALP (n2) && scm_is_integer (n2))
goto handle_inexacts;
else
@@ -2878,22 +2860,10 @@ scm_lcm (SCM n1, SCM n2)
}
else if (SCM_LIKELY (SCM_BIGP (n1)))
{
- /* big n1 */
if (SCM_I_INUMP (n2))
- {
- SCM_SWAP (n1, n2);
- goto inumbig;
- }
- else if (SCM_LIKELY (SCM_BIGP (n2)))
- {
- SCM result = scm_i_mkbig ();
- mpz_lcm(SCM_I_BIG_MPZ (result),
- SCM_I_BIG_MPZ (n1),
- SCM_I_BIG_MPZ (n2));
- scm_remember_upto_here_2(n1, n2);
- /* shouldn't need to normalize b/c lcm of 2 bigs should be big */
- return result;
- }
+ return scm_integer_lcm_zi (n1, SCM_I_INUM (n2));
+ else if (SCM_BIGP (n2))
+ return scm_integer_lcm_zz (n1, n2);
else if (SCM_REALP (n2) && scm_is_integer (n2))
goto handle_inexacts;
else
- [Guile-commits] 37/69: Build scm_integer_p on scm_is_integer, not vice versa, (continued)
- [Guile-commits] 37/69: Build scm_integer_p on scm_is_integer, not vice versa, Andy Wingo, 2022/01/07
- [Guile-commits] 36/69: Simplify scm_bigprint, Andy Wingo, 2022/01/07
- [Guile-commits] 38/69: Reimplement = on integer lib, clean up scm_num_eq_p, Andy Wingo, 2022/01/07
- [Guile-commits] 41/69: Simplify implementation of min, max, Andy Wingo, 2022/01/07
- [Guile-commits] 46/69: Clean up scm_divide, Andy Wingo, 2022/01/07
- [Guile-commits] 48/69: Fix scm_integer_to_double_z to always round; clean ups, Andy Wingo, 2022/01/07
- [Guile-commits] 54/69: Remove unused conv-{u,}integer.i.c, Andy Wingo, 2022/01/07
- [Guile-commits] 58/69: Expose frexp from integers lib, Andy Wingo, 2022/01/07
- [Guile-commits] 18/69: Implement round-remainder with new integer lib, Andy Wingo, 2022/01/07
- [Guile-commits] 28/69: Implement scm_modulo_expt with new integer library, Andy Wingo, 2022/01/07
- [Guile-commits] 21/69: Implement lcm with new integer lib,
Andy Wingo <=
- [Guile-commits] 30/69: Implement scm_ash with new integer library, Andy Wingo, 2022/01/07
- [Guile-commits] 32/69: Implement scm_logcount with new integer library, Andy Wingo, 2022/01/07
- [Guile-commits] 35/69: Implement integer-to-string with new integer library, Andy Wingo, 2022/01/07
- [Guile-commits] 05/69: Implement floor-quotient with new integer lib, Andy Wingo, 2022/01/07
- [Guile-commits] 06/69: Implement floor-remainder with new integer lib, Andy Wingo, 2022/01/07
- [Guile-commits] 14/69: Implement centered-quotient with new integer lib, Andy Wingo, 2022/01/07
- [Guile-commits] 11/69: Implement truncate-quotient with new integer lib, Andy Wingo, 2022/01/07
- [Guile-commits] 13/69: Implement truncate-divide with new integer lib, Andy Wingo, 2022/01/07
- [Guile-commits] 31/69: Implement scm_bit_extract with new integer library, Andy Wingo, 2022/01/07
- [Guile-commits] 34/69: Implement scm_integer_length with new integer library, Andy Wingo, 2022/01/07