[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Guile-commits] 65/69: Avoid scm_i_mkbig outside numbers.c.
From: |
Andy Wingo |
Subject: |
[Guile-commits] 65/69: Avoid scm_i_mkbig outside numbers.c. |
Date: |
Fri, 7 Jan 2022 08:27:28 -0500 (EST) |
wingo pushed a commit to branch wip-inline-digits
in repository guile.
commit 7beac6427c17132866df4f1bc6141ca809d20c95
Author: Andy Wingo <wingo@pobox.com>
AuthorDate: Fri Jan 7 11:45:38 2022 +0100
Avoid scm_i_mkbig outside numbers.c.
* libguile/random.c (scm_c_random_bignum):
* libguile/socket.c (scm_from_ipv6): Avoid scm_i_mkbig.
* libguile/numbers.h:
* libguile/numbers.c (scm_i_mkbig): Remove.
---
libguile/numbers.c | 9 ---------
libguile/numbers.h | 1 -
libguile/random.c | 22 ++++++++++++++--------
libguile/socket.c | 11 +++++++----
4 files changed, 21 insertions(+), 22 deletions(-)
diff --git a/libguile/numbers.c b/libguile/numbers.c
index 0b09d0d8e..e62646ea2 100644
--- a/libguile/numbers.c
+++ b/libguile/numbers.c
@@ -291,15 +291,6 @@ make_bignum (void)
}
-SCM
-scm_i_mkbig ()
-{
- /* Return a newly created bignum. */
- SCM z = make_bignum ();
- mpz_init (SCM_I_BIG_MPZ (z));
- return z;
-}
-
SCM
scm_i_long2big (long x)
{
diff --git a/libguile/numbers.h b/libguile/numbers.h
index f5cfe221e..80bc11720 100644
--- a/libguile/numbers.h
+++ b/libguile/numbers.h
@@ -347,7 +347,6 @@ SCM_INTERNAL SCM scm_i_divide (SCM x, SCM y, SCM rest);
SCM_INTERNAL SCM scm_i_exact_integer_sqrt (SCM k);
/* bignum internal functions */
-SCM_INTERNAL SCM scm_i_mkbig (void);
SCM_API /* FIXME: not internal */ SCM scm_i_normbig (SCM x);
SCM_API /* FIXME: not internal */ double scm_i_big2dbl (SCM b);
SCM_API /* FIXME: not internal */ SCM scm_i_long2big (long n);
diff --git a/libguile/random.c b/libguile/random.c
index c6755e677..04b92b9cf 100644
--- a/libguile/random.c
+++ b/libguile/random.c
@@ -1,4 +1,4 @@
-/* Copyright 1999-2001,2003,2005-2006,2009-2010,2012-2014,2017-2019
+/* Copyright 1999-2001,2003,2005-2006,2009-2010,2012-2014,2017-2019,2022
Free Software Foundation, Inc.
This file is part of Guile.
@@ -311,8 +311,11 @@ scm_c_random64 (scm_t_rstate *state, uint64_t m)
SCM
scm_c_random_bignum (scm_t_rstate *state, SCM m)
{
- SCM result = scm_i_mkbig ();
- const size_t m_bits = mpz_sizeinbase (SCM_I_BIG_MPZ (m), 2);
+ mpz_t result, zm;
+ mpz_init (result);
+ mpz_init (zm);
+ scm_to_mpz (m, zm);
+ const size_t m_bits = mpz_sizeinbase (zm, 2);
/* how many bits would only partially fill the last uint32_t? */
const size_t end_bits = m_bits % (sizeof (uint32_t) * SCM_CHAR_BIT);
uint32_t *random_chunks = NULL;
@@ -321,7 +324,7 @@ scm_c_random_bignum (scm_t_rstate *state, SCM m)
const uint32_t num_chunks = num_full_chunks + ((end_bits) ? 1 : 0);
/* we know the result will be this big */
- mpz_realloc2 (SCM_I_BIG_MPZ (result), m_bits);
+ mpz_realloc2 (result, m_bits);
random_chunks =
(uint32_t *) scm_gc_calloc (num_chunks * sizeof (uint32_t),
@@ -332,7 +335,7 @@ scm_c_random_bignum (scm_t_rstate *state, SCM m)
uint32_t *current_chunk = random_chunks + (num_chunks - 1);
uint32_t chunks_left = num_chunks;
- mpz_set_ui (SCM_I_BIG_MPZ (result), 0);
+ mpz_set_ui (result, 0);
if (end_bits)
{
@@ -352,7 +355,7 @@ scm_c_random_bignum (scm_t_rstate *state, SCM m)
*current_chunk-- = state->rng->random_bits (state);
chunks_left--;
}
- mpz_import (SCM_I_BIG_MPZ (result),
+ mpz_import (result,
num_chunks,
-1,
sizeof (uint32_t),
@@ -361,11 +364,14 @@ scm_c_random_bignum (scm_t_rstate *state, SCM m)
random_chunks);
/* if result >= m, regenerate it (it is important to regenerate
all bits in order not to get a distorted distribution) */
- } while (mpz_cmp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (m)) >= 0);
+ } while (mpz_cmp (result, zm) >= 0);
scm_gc_free (random_chunks,
num_chunks * sizeof (uint32_t),
"random bignum chunks");
- return scm_i_normbig (result);
+ mpz_clear (zm);
+ SCM ret = scm_from_mpz (result);
+ mpz_clear (result);
+ return ret;
}
/*
diff --git a/libguile/socket.c b/libguile/socket.c
index 68fb016b2..e9c658ed7 100644
--- a/libguile/socket.c
+++ b/libguile/socket.c
@@ -1,4 +1,4 @@
-/* Copyright 1996-1998,2000-2007,2009,2011-2015,2018,2021
+/* Copyright 1996-1998,2000-2007,2009,2011-2015,2018,2021,2022
Free Software Foundation, Inc.
This file is part of Guile.
@@ -213,15 +213,18 @@ SCM_DEFINE (scm_inet_makeaddr, "inet-makeaddr", 2, 0, 0,
static SCM
scm_from_ipv6 (const uint8_t *src)
{
- SCM result = scm_i_mkbig ();
- mpz_import (SCM_I_BIG_MPZ (result),
+ mpz_t z;
+ mpz_init (z);
+ mpz_import (z,
1, /* chunk */
1, /* big-endian chunk ordering */
16, /* chunks are 16 bytes long */
1, /* big-endian byte ordering */
0, /* "nails" -- leading unused bits per chunk */
src);
- return scm_i_normbig (result);
+ SCM ret = scm_from_mpz (z);
+ mpz_clear (z);
+ return ret;
}
/* convert a host ordered SCM integer to a 128 bit IPv6 address in
- [Guile-commits] 11/69: Implement truncate-quotient with new integer lib, (continued)
- [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
- [Guile-commits] 42/69: Clean up scm_sum, Andy Wingo, 2022/01/07
- [Guile-commits] 43/69: Simplify scm_difference, use integer lib, Andy Wingo, 2022/01/07
- [Guile-commits] 44/69: Simplify scm_product, use integer lib, Andy Wingo, 2022/01/07
- [Guile-commits] 52/69: Reimplement scm_{to,from}_{int64,uint64}, Andy Wingo, 2022/01/07
- [Guile-commits] 53/69: Implement scm_{to,from}_wchar inline, Andy Wingo, 2022/01/07
- [Guile-commits] 60/69: divide2double refactor, Andy Wingo, 2022/01/07
- [Guile-commits] 65/69: Avoid scm_i_mkbig outside numbers.c.,
Andy Wingo <=
- [Guile-commits] 57/69: Refactor scm_sqrt in terms of integers.[ch], Andy Wingo, 2022/01/07
- [Guile-commits] 59/69: Remove dead bignum frexp code from numbers.c, Andy Wingo, 2022/01/07
- [Guile-commits] 47/69: Fix deprecated bit-count* when counting 0 bits, Andy Wingo, 2022/01/07
- [Guile-commits] 49/69: Reimplement scm_is_{un, }signed_integer for bignums, Andy Wingo, 2022/01/07
- [Guile-commits] 51/69: Reimplement scm_{to,from}_{int32,uint32}, Andy Wingo, 2022/01/07
- [Guile-commits] 45/69: Remove support for allowing exact numbers to be divided by zero, Andy Wingo, 2022/01/07
- [Guile-commits] 50/69: Reimplement scm_from_int8 etc, Andy Wingo, 2022/01/07
- [Guile-commits] 56/69: Reimplement exact-integer-sqrt with integers.[ch], Andy Wingo, 2022/01/07
- [Guile-commits] 55/69: scm_to_mpz uses integer lib, Andy Wingo, 2022/01/07
- [Guile-commits] 62/69: Remove last non-admin SCM_I_BIG_MPZ uses in numbers.c, Andy Wingo, 2022/01/07