[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Guile-commits] 04/69: Implement abs with new integer lib
From: |
Andy Wingo |
Subject: |
[Guile-commits] 04/69: Implement abs with new integer lib |
Date: |
Fri, 7 Jan 2022 08:27:06 -0500 (EST) |
wingo pushed a commit to branch wip-inline-digits
in repository guile.
commit 452052f3be3c6c71d5ca879ce80f0ab36e507c53
Author: Andy Wingo <wingo@pobox.com>
AuthorDate: Fri Dec 3 14:23:51 2021 +0100
Implement abs with new integer lib
* libguile/integers.c (scm_integer_abs_i, scm_integer_abs_z): New
internal functions.
* libguile/integers.h: Declare internal functions.
* libguile/numbers.c (scm_abs): Use the new functions.
---
libguile/integers.c | 22 ++++++++++++++++++++++
libguile/integers.h | 3 +++
libguile/numbers.c | 18 ++----------------
3 files changed, 27 insertions(+), 16 deletions(-)
diff --git a/libguile/integers.c b/libguile/integers.c
index e449ff635..34a1d4af8 100644
--- a/libguile/integers.c
+++ b/libguile/integers.c
@@ -244,3 +244,25 @@ scm_is_integer_odd_z (SCM z)
{
return bignum_limbs (scm_bignum (z))[0] & 1;
}
+
+SCM
+scm_integer_abs_i (scm_t_inum i)
+{
+ if (i >= 0)
+ return SCM_I_MAKINUM (i);
+
+ unsigned long abs = long_magnitude (i);
+ if (SCM_LIKELY (SCM_POSFIXABLE (abs)))
+ return SCM_I_MAKINUM (abs);
+
+ return ulong_to_bignum (abs);
+}
+
+SCM
+scm_integer_abs_z (SCM z)
+{
+ if (!bignum_is_negative (scm_bignum (z)))
+ return z;
+
+ return SCM_PACK (negate_bignum (clone_bignum (scm_bignum (z))));
+}
diff --git a/libguile/integers.h b/libguile/integers.h
index 2bd937669..753ff1f74 100644
--- a/libguile/integers.h
+++ b/libguile/integers.h
@@ -26,6 +26,9 @@
SCM_INTERNAL int scm_is_integer_odd_i (scm_t_inum i);
SCM_INTERNAL int scm_is_integer_odd_z (SCM z);
+SCM_INTERNAL SCM scm_integer_abs_i (scm_t_inum i);
+SCM_INTERNAL SCM scm_integer_abs_z (SCM z);
+
#endif /* SCM_INTEGERS_H */
diff --git a/libguile/numbers.c b/libguile/numbers.c
index a91d5963d..ea0cf67bb 100644
--- a/libguile/numbers.c
+++ b/libguile/numbers.c
@@ -919,15 +919,7 @@ SCM_PRIMITIVE_GENERIC (scm_abs, "abs", 1, 0, 0,
#define FUNC_NAME s_scm_abs
{
if (SCM_I_INUMP (x))
- {
- scm_t_inum xx = SCM_I_INUM (x);
- if (xx >= 0)
- return x;
- else if (SCM_POSFIXABLE (-xx))
- return SCM_I_MAKINUM (-xx);
- else
- return scm_i_inum2big (-xx);
- }
+ return scm_integer_abs_i (SCM_I_INUM (x));
else if (SCM_LIKELY (SCM_REALP (x)))
{
double xx = SCM_REAL_VALUE (x);
@@ -941,13 +933,7 @@ SCM_PRIMITIVE_GENERIC (scm_abs, "abs", 1, 0, 0,
return x;
}
else if (SCM_BIGP (x))
- {
- const int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
- if (sgn < 0)
- return scm_i_clonebig (x, 0);
- else
- return x;
- }
+ return scm_integer_abs_z (x);
else if (SCM_FRACTIONP (x))
{
if (scm_is_false (scm_negative_p (SCM_FRACTION_NUMERATOR (x))))
- [Guile-commits] 23/69: Implement scm_logior with new integer library, (continued)
- [Guile-commits] 23/69: Implement scm_logior with new integer library, Andy Wingo, 2022/01/07
- [Guile-commits] 24/69: Implement scm_logxor with new integer library, Andy Wingo, 2022/01/07
- [Guile-commits] 01/69: Fix type confusion in heap-numbers-equal? calls from VM, Andy Wingo, 2022/01/07
- [Guile-commits] 07/69: Implement floor-divide with new integer lib, Andy Wingo, 2022/01/07
- [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 <=
- [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, 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