[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Guile-commits] 26/69: Implement scm_logbit_p with new integer library
From: |
Andy Wingo |
Subject: |
[Guile-commits] 26/69: Implement scm_logbit_p with new integer library |
Date: |
Fri, 7 Jan 2022 08:27:09 -0500 (EST) |
wingo pushed a commit to branch wip-inline-digits
in repository guile.
commit 5f697a7a56b047389dc69594de4e81416bc3bb02
Author: Andy Wingo <wingo@pobox.com>
AuthorDate: Sun Dec 19 11:06:36 2021 +0100
Implement scm_logbit_p with new integer library
* libguile/integers.c (scm_integer_logbit_ui, scm_integer_logbit_uz):
* libguile/integers.h: Declare the new internal functions.
* libguile/numbers.c (scm_logbit_p): Use new internal functions.
---
libguile/integers.c | 20 ++++++++++++++++++++
libguile/integers.h | 3 +++
libguile/numbers.c | 18 ++----------------
3 files changed, 25 insertions(+), 16 deletions(-)
diff --git a/libguile/integers.c b/libguile/integers.c
index ff5499af9..fc71c7acc 100644
--- a/libguile/integers.c
+++ b/libguile/integers.c
@@ -2008,3 +2008,23 @@ scm_integer_logtest_zz (SCM x, SCM y)
{
return scm_is_eq (scm_integer_logand_zz (x, y), SCM_INUM0);
}
+
+int
+scm_integer_logbit_ui (unsigned long index, scm_t_inum n)
+{
+ if (index < SCM_LONG_BIT)
+ /* Assume two's complement representation. */
+ return (n >> index) & 1;
+ else
+ return n < 0;
+}
+
+int
+scm_integer_logbit_uz (unsigned long index, SCM n)
+{
+ mpz_t zn;
+ alias_bignum_to_mpz (scm_bignum (n), zn);
+ int val = mpz_tstbit (zn, index);
+ scm_remember_upto_here_1 (n);
+ return val;
+}
diff --git a/libguile/integers.h b/libguile/integers.h
index 56334f0a8..feba5963f 100644
--- a/libguile/integers.h
+++ b/libguile/integers.h
@@ -148,6 +148,9 @@ SCM_INTERNAL int scm_integer_logtest_ii (scm_t_inum x,
scm_t_inum y);
SCM_INTERNAL int scm_integer_logtest_zi (SCM x, scm_t_inum y);
SCM_INTERNAL int scm_integer_logtest_zz (SCM x, SCM y);
+SCM_INTERNAL int scm_integer_logbit_ui (unsigned long bit, scm_t_inum n);
+SCM_INTERNAL int scm_integer_logbit_uz (unsigned long bit, SCM n);
+
#endif /* SCM_INTEGERS_H */
diff --git a/libguile/numbers.c b/libguile/numbers.c
index 26d1c061f..548618e74 100644
--- a/libguile/numbers.c
+++ b/libguile/numbers.c
@@ -3155,23 +3155,9 @@ SCM_DEFINE (scm_logbit_p, "logbit?", 2, 0, 0,
iindex = scm_to_ulong (index);
if (SCM_I_INUMP (j))
- {
- if (iindex < SCM_LONG_BIT - 1)
- /* Arrange for the number to be converted to unsigned before
- checking the bit, to ensure that we're testing the bit in a
- two's complement representation (regardless of the native
- representation. */
- return scm_from_bool ((1UL << iindex) & SCM_I_INUM (j));
- else
- /* Portably check the sign. */
- return scm_from_bool (SCM_I_INUM (j) < 0);
- }
+ return scm_from_bool (scm_integer_logbit_ui (iindex, SCM_I_INUM (j)));
else if (SCM_BIGP (j))
- {
- int val = mpz_tstbit (SCM_I_BIG_MPZ (j), iindex);
- scm_remember_upto_here_1 (j);
- return scm_from_bool (val);
- }
+ return scm_from_bool (scm_integer_logbit_uz (iindex, j));
else
SCM_WRONG_TYPE_ARG (SCM_ARG2, j);
}
- [Guile-commits] 10/69: Implement ceiling-divide with new integer lib, (continued)
- [Guile-commits] 10/69: Implement ceiling-divide with new integer lib, Andy Wingo, 2022/01/07
- [Guile-commits] 16/69: Implement centered-divide with new integer lib, Andy Wingo, 2022/01/07
- [Guile-commits] 20/69: Implement gcd with new integer lib, Andy Wingo, 2022/01/07
- [Guile-commits] 39/69: Clean up <, reimplement in terms of integer lib, Andy Wingo, 2022/01/07
- [Guile-commits] 40/69: positive?, negative? use integer lib, Andy Wingo, 2022/01/07
- [Guile-commits] 04/69: Implement abs with new integer lib, Andy Wingo, 2022/01/07
- [Guile-commits] 09/69: Implement ceiling-remainder with new integer lib, Andy Wingo, 2022/01/07
- [Guile-commits] 22/69: Implement scm_logand with new integer library, Andy Wingo, 2022/01/07
- [Guile-commits] 29/69: Reimplement integer-expt in Scheme, Andy Wingo, 2022/01/07
- [Guile-commits] 27/69: Implement scm_lognot with new integer library, Andy Wingo, 2022/01/07
- [Guile-commits] 26/69: Implement scm_logbit_p with new integer library,
Andy Wingo <=
- [Guile-commits] 25/69: Implement scm_logtest with new integer library, Andy Wingo, 2022/01/07
- [Guile-commits] 33/69: Integer library takes bignums via opaque struct pointer, Andy Wingo, 2022/01/07
- [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