[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Guile-commits] 22/85: Implement scm_logior with new integer library
From: |
Andy Wingo |
Subject: |
[Guile-commits] 22/85: Implement scm_logior with new integer library |
Date: |
Thu, 13 Jan 2022 03:40:16 -0500 (EST) |
wingo pushed a commit to branch main
in repository guile.
commit 7e85ffa8221ccfba50aec93da284f61265454d2e
Author: Andy Wingo <wingo@pobox.com>
AuthorDate: Sun Dec 19 10:13:42 2021 +0100
Implement scm_logior with new integer library
* libguile/integers.c (scm_integer_logior_ii, scm_integer_logior_zi)
(scm_integer_logior_zz): New internal functions.
* libguile/integers.h: Declare the new internal functions.
* libguile/numbers.c (scm_logior): Use new internal functions.
---
libguile/integers.c | 34 ++++++++++++++++++++++++++++++++++
libguile/integers.h | 4 ++++
libguile/numbers.c | 40 ++++------------------------------------
3 files changed, 42 insertions(+), 36 deletions(-)
diff --git a/libguile/integers.c b/libguile/integers.c
index eb65ed221..816a49d93 100644
--- a/libguile/integers.c
+++ b/libguile/integers.c
@@ -1924,3 +1924,37 @@ scm_integer_logand_zz (SCM x, SCM y)
scm_remember_upto_here_2 (x, y);
return take_mpz (result);
}
+
+SCM
+scm_integer_logior_ii (scm_t_inum x, scm_t_inum y)
+{
+ return SCM_I_MAKINUM (x | y);
+}
+
+SCM
+scm_integer_logior_zi (SCM x, scm_t_inum y)
+{
+ if (y == 0)
+ return x;
+
+ mpz_t result, zx, zy;
+ mpz_init (result);
+ alias_bignum_to_mpz (scm_bignum (x), zx);
+ mpz_init_set_si (zy, y);
+ mpz_ior (result, zy, zx);
+ scm_remember_upto_here_1 (x);
+ mpz_clear (zy);
+ return take_mpz (result);
+}
+
+SCM
+scm_integer_logior_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_ior (result, zy, zx);
+ scm_remember_upto_here_2 (x, y);
+ return take_mpz (result);
+}
diff --git a/libguile/integers.h b/libguile/integers.h
index d2ab465d7..d4f977c4e 100644
--- a/libguile/integers.h
+++ b/libguile/integers.h
@@ -136,6 +136,10 @@ SCM_INTERNAL SCM scm_integer_logand_ii (scm_t_inum x,
scm_t_inum y);
SCM_INTERNAL SCM scm_integer_logand_zi (SCM x, scm_t_inum y);
SCM_INTERNAL SCM scm_integer_logand_zz (SCM x, SCM y);
+SCM_INTERNAL SCM scm_integer_logior_ii (scm_t_inum x, scm_t_inum y);
+SCM_INTERNAL SCM scm_integer_logior_zi (SCM x, scm_t_inum y);
+SCM_INTERNAL SCM scm_integer_logior_zz (SCM x, SCM y);
+
#endif /* SCM_INTEGERS_H */
diff --git a/libguile/numbers.c b/libguile/numbers.c
index c4955b641..ae904647c 100644
--- a/libguile/numbers.c
+++ b/libguile/numbers.c
@@ -2945,8 +2945,6 @@ SCM_DEFINE (scm_i_logand, "logand", 0, 2, 1,
SCM scm_logand (SCM n1, SCM n2)
#define FUNC_NAME s_scm_logand
{
- scm_t_inum nn1;
-
if (SCM_UNBNDP (n2))
{
if (SCM_UNBNDP (n1))
@@ -3007,8 +3005,6 @@ SCM_DEFINE (scm_i_logior, "logior", 0, 2, 1,
SCM scm_logior (SCM n1, SCM n2)
#define FUNC_NAME s_scm_logior
{
- scm_t_inum nn1;
-
if (SCM_UNBNDP (n2))
{
if (SCM_UNBNDP (n1))
@@ -3021,47 +3017,19 @@ SCM scm_logior (SCM n1, SCM n2)
if (SCM_I_INUMP (n1))
{
- nn1 = SCM_I_INUM (n1);
if (SCM_I_INUMP (n2))
- {
- long nn2 = SCM_I_INUM (n2);
- return SCM_I_MAKINUM (nn1 | nn2);
- }
+ return scm_integer_logior_ii (SCM_I_INUM (n1), SCM_I_INUM (n2));
else if (SCM_BIGP (n2))
- {
- intbig:
- if (nn1 == 0)
- return n2;
- {
- SCM result_z = scm_i_mkbig ();
- mpz_t nn1_z;
- mpz_init_set_si (nn1_z, nn1);
- mpz_ior (SCM_I_BIG_MPZ (result_z), nn1_z, SCM_I_BIG_MPZ (n2));
- scm_remember_upto_here_1 (n2);
- mpz_clear (nn1_z);
- return scm_i_normbig (result_z);
- }
- }
+ return scm_integer_logior_zi (n2, SCM_I_INUM (n1));
else
SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
}
else if (SCM_BIGP (n1))
{
if (SCM_I_INUMP (n2))
- {
- SCM_SWAP (n1, n2);
- nn1 = SCM_I_INUM (n1);
- goto intbig;
- }
+ return scm_integer_logior_zi (n1, SCM_I_INUM (n2));
else if (SCM_BIGP (n2))
- {
- SCM result_z = scm_i_mkbig ();
- mpz_ior (SCM_I_BIG_MPZ (result_z),
- SCM_I_BIG_MPZ (n1),
- SCM_I_BIG_MPZ (n2));
- scm_remember_upto_here_2 (n1, n2);
- return scm_i_normbig (result_z);
- }
+ return scm_integer_logior_zz (n1, n2);
else
SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
}
- [Guile-commits] 19/85: Implement gcd with new integer lib, (continued)
- [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
- [Guile-commits] 64/85: Avoid scm_i_mkbig outside numbers.c., Andy Wingo, 2022/01/13
- [Guile-commits] 78/85: Optimize bignum subtraction, Andy Wingo, 2022/01/13
- [Guile-commits] 70/85: Fix bug when making mpz from 0, Andy Wingo, 2022/01/13
- [Guile-commits] 83/85: Don't use HAVE_COPYSIGN in libguile/numbers.c, Andy Wingo, 2022/01/13
- [Guile-commits] 20/85: Implement lcm with new integer lib, Andy Wingo, 2022/01/13
- [Guile-commits] 22/85: Implement scm_logior with new integer library,
Andy Wingo <=
- [Guile-commits] 30/85: Implement scm_bit_extract with new integer library, Andy Wingo, 2022/01/13
- [Guile-commits] 28/85: Reimplement integer-expt in Scheme, Andy Wingo, 2022/01/13
- [Guile-commits] 15/85: Implement centered-divide with new integer lib, Andy Wingo, 2022/01/13
- [Guile-commits] 32/85: Integer library takes bignums via opaque struct pointer, Andy Wingo, 2022/01/13
- [Guile-commits] 38/85: Clean up <, reimplement in terms of integer lib, Andy Wingo, 2022/01/13
- [Guile-commits] 39/85: positive?, negative? use integer lib, Andy Wingo, 2022/01/13
- [Guile-commits] 41/85: Clean up scm_sum, Andy Wingo, 2022/01/13
- [Guile-commits] 43/85: Simplify scm_product, use integer lib, Andy Wingo, 2022/01/13
- [Guile-commits] 44/85: Remove support for allowing exact numbers to be divided by zero, Andy Wingo, 2022/01/13
- [Guile-commits] 45/85: Clean up scm_divide, Andy Wingo, 2022/01/13