[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Guile-commits] 27/69: Implement scm_lognot with new integer library
From: |
Andy Wingo |
Subject: |
[Guile-commits] 27/69: Implement scm_lognot with new integer library |
Date: |
Fri, 7 Jan 2022 08:27:10 -0500 (EST) |
wingo pushed a commit to branch wip-inline-digits
in repository guile.
commit 81ad8b6484b7b9911fda182aae38ba03c06fdece
Author: Andy Wingo <wingo@pobox.com>
AuthorDate: Sun Dec 19 14:43:05 2021 +0100
Implement scm_lognot with new integer library
* libguile/integers.c (scm_integer_lognot_i, scm_integer_lognot_z):
* libguile/integers.h: Declare the new internal functions.
* libguile/numbers.c (scm_lognot): Use new internal functions.
---
libguile/integers.c | 17 +++++++++++++++++
libguile/integers.h | 3 +++
libguile/numbers.c | 20 +++++---------------
3 files changed, 25 insertions(+), 15 deletions(-)
diff --git a/libguile/integers.c b/libguile/integers.c
index fc71c7acc..2ae2c30d5 100644
--- a/libguile/integers.c
+++ b/libguile/integers.c
@@ -2028,3 +2028,20 @@ scm_integer_logbit_uz (unsigned long index, SCM n)
scm_remember_upto_here_1 (n);
return val;
}
+
+SCM
+scm_integer_lognot_i (scm_t_inum n)
+{
+ return SCM_I_MAKINUM (~n);
+}
+
+SCM
+scm_integer_lognot_z (SCM n)
+{
+ mpz_t result, zn;
+ mpz_init (result);
+ alias_bignum_to_mpz (scm_bignum (n), zn);
+ mpz_com (result, zn);
+ scm_remember_upto_here_1 (n);
+ return take_mpz (result);
+}
diff --git a/libguile/integers.h b/libguile/integers.h
index feba5963f..105b86b63 100644
--- a/libguile/integers.h
+++ b/libguile/integers.h
@@ -151,6 +151,9 @@ 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);
+SCM_INTERNAL SCM scm_integer_lognot_i (scm_t_inum n);
+SCM_INTERNAL SCM scm_integer_lognot_z (SCM n);
+
#endif /* SCM_INTEGERS_H */
diff --git a/libguile/numbers.c b/libguile/numbers.c
index 548618e74..3e8431757 100644
--- a/libguile/numbers.c
+++ b/libguile/numbers.c
@@ -3177,22 +3177,12 @@ SCM_DEFINE (scm_lognot, "lognot", 1, 0, 0,
"@end lisp")
#define FUNC_NAME s_scm_lognot
{
- if (SCM_I_INUMP (n)) {
- /* No overflow here, just need to toggle all the bits making up the inum.
- Enhancement: No need to strip the tag and add it back, could just xor
- a block of 1 bits, if that worked with the various debug versions of
- the SCM typedef. */
- return SCM_I_MAKINUM (~ SCM_I_INUM (n));
-
- } else if (SCM_BIGP (n)) {
- SCM result = scm_i_mkbig ();
- mpz_com (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (n));
- scm_remember_upto_here_1 (n);
- return result;
-
- } else {
+ if (SCM_I_INUMP (n))
+ return scm_integer_lognot_i (SCM_I_INUM (n));
+ else if (SCM_BIGP (n))
+ return scm_integer_lognot_z (n);
+ else
SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
- }
}
#undef FUNC_NAME
- [Guile-commits] 03/69: Implement odd? and even? with new integer lib, (continued)
- [Guile-commits] 03/69: Implement odd? and even? with new integer lib, Andy Wingo, 2022/01/07
- [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 <=
- [Guile-commits] 26/69: Implement scm_logbit_p with new integer library, Andy Wingo, 2022/01/07
- [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