From bdddef3cfdd2e59b5dce783675e136b480bdd3b8 Mon Sep 17 00:00:00 2001 From: Andy Wingo Date: Fri, 7 Jan 2022 11:45:38 +0100 Subject: [PATCH] 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