mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-04-30 03:40:34 +02:00
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.
This commit is contained in:
parent
0c502a4d3c
commit
bdddef3cfd
4 changed files with 21 additions and 22 deletions
|
@ -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
|
||||||
scm_i_long2big (long x)
|
scm_i_long2big (long x)
|
||||||
{
|
{
|
||||||
|
|
|
@ -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);
|
SCM_INTERNAL SCM scm_i_exact_integer_sqrt (SCM k);
|
||||||
|
|
||||||
/* bignum internal functions */
|
/* 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 */ SCM scm_i_normbig (SCM x);
|
||||||
SCM_API /* FIXME: not internal */ double scm_i_big2dbl (SCM b);
|
SCM_API /* FIXME: not internal */ double scm_i_big2dbl (SCM b);
|
||||||
SCM_API /* FIXME: not internal */ SCM scm_i_long2big (long n);
|
SCM_API /* FIXME: not internal */ SCM scm_i_long2big (long n);
|
||||||
|
|
|
@ -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.
|
Free Software Foundation, Inc.
|
||||||
|
|
||||||
This file is part of Guile.
|
This file is part of Guile.
|
||||||
|
@ -311,8 +311,11 @@ scm_c_random64 (scm_t_rstate *state, uint64_t m)
|
||||||
SCM
|
SCM
|
||||||
scm_c_random_bignum (scm_t_rstate *state, SCM m)
|
scm_c_random_bignum (scm_t_rstate *state, SCM m)
|
||||||
{
|
{
|
||||||
SCM result = scm_i_mkbig ();
|
mpz_t result, zm;
|
||||||
const size_t m_bits = mpz_sizeinbase (SCM_I_BIG_MPZ (m), 2);
|
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? */
|
/* how many bits would only partially fill the last uint32_t? */
|
||||||
const size_t end_bits = m_bits % (sizeof (uint32_t) * SCM_CHAR_BIT);
|
const size_t end_bits = m_bits % (sizeof (uint32_t) * SCM_CHAR_BIT);
|
||||||
uint32_t *random_chunks = NULL;
|
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);
|
const uint32_t num_chunks = num_full_chunks + ((end_bits) ? 1 : 0);
|
||||||
|
|
||||||
/* we know the result will be this big */
|
/* we know the result will be this big */
|
||||||
mpz_realloc2 (SCM_I_BIG_MPZ (result), m_bits);
|
mpz_realloc2 (result, m_bits);
|
||||||
|
|
||||||
random_chunks =
|
random_chunks =
|
||||||
(uint32_t *) scm_gc_calloc (num_chunks * sizeof (uint32_t),
|
(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 *current_chunk = random_chunks + (num_chunks - 1);
|
||||||
uint32_t chunks_left = num_chunks;
|
uint32_t chunks_left = num_chunks;
|
||||||
|
|
||||||
mpz_set_ui (SCM_I_BIG_MPZ (result), 0);
|
mpz_set_ui (result, 0);
|
||||||
|
|
||||||
if (end_bits)
|
if (end_bits)
|
||||||
{
|
{
|
||||||
|
@ -352,7 +355,7 @@ scm_c_random_bignum (scm_t_rstate *state, SCM m)
|
||||||
*current_chunk-- = state->rng->random_bits (state);
|
*current_chunk-- = state->rng->random_bits (state);
|
||||||
chunks_left--;
|
chunks_left--;
|
||||||
}
|
}
|
||||||
mpz_import (SCM_I_BIG_MPZ (result),
|
mpz_import (result,
|
||||||
num_chunks,
|
num_chunks,
|
||||||
-1,
|
-1,
|
||||||
sizeof (uint32_t),
|
sizeof (uint32_t),
|
||||||
|
@ -361,11 +364,14 @@ scm_c_random_bignum (scm_t_rstate *state, SCM m)
|
||||||
random_chunks);
|
random_chunks);
|
||||||
/* if result >= m, regenerate it (it is important to regenerate
|
/* if result >= m, regenerate it (it is important to regenerate
|
||||||
all bits in order not to get a distorted distribution) */
|
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,
|
scm_gc_free (random_chunks,
|
||||||
num_chunks * sizeof (uint32_t),
|
num_chunks * sizeof (uint32_t),
|
||||||
"random bignum chunks");
|
"random bignum chunks");
|
||||||
return scm_i_normbig (result);
|
mpz_clear (zm);
|
||||||
|
SCM ret = scm_from_mpz (result);
|
||||||
|
mpz_clear (result);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -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.
|
Free Software Foundation, Inc.
|
||||||
|
|
||||||
This file is part of Guile.
|
This file is part of Guile.
|
||||||
|
@ -213,15 +213,18 @@ SCM_DEFINE (scm_inet_makeaddr, "inet-makeaddr", 2, 0, 0,
|
||||||
static SCM
|
static SCM
|
||||||
scm_from_ipv6 (const uint8_t *src)
|
scm_from_ipv6 (const uint8_t *src)
|
||||||
{
|
{
|
||||||
SCM result = scm_i_mkbig ();
|
mpz_t z;
|
||||||
mpz_import (SCM_I_BIG_MPZ (result),
|
mpz_init (z);
|
||||||
|
mpz_import (z,
|
||||||
1, /* chunk */
|
1, /* chunk */
|
||||||
1, /* big-endian chunk ordering */
|
1, /* big-endian chunk ordering */
|
||||||
16, /* chunks are 16 bytes long */
|
16, /* chunks are 16 bytes long */
|
||||||
1, /* big-endian byte ordering */
|
1, /* big-endian byte ordering */
|
||||||
0, /* "nails" -- leading unused bits per chunk */
|
0, /* "nails" -- leading unused bits per chunk */
|
||||||
src);
|
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
|
/* convert a host ordered SCM integer to a 128 bit IPv6 address in
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue