1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-07-01 23:30:28 +02:00

Allocate a static tc16 for random states

* libguile/random.h (scm_t_rstate): Put a tag word in the beginning.
(scm_is_random_state, scm_to_random_state, scm_from_random_state): New
helpers.
(SCM_RSTATEP, SCM_RSTATE): Use the new helpers.
(scm_masktab): Make private.
* libguile/random.c: Adapt random states to not be a smob.
* libguile/eq.c:
* libguile/print.c:
* libguile/scm.h:
* module/oop/goops.scm: Add new random-state cases.  Fix a number of
classes for other types that were recently changed to not be smobs.
This commit is contained in:
Andy Wingo 2025-06-17 10:36:32 +02:00
parent 63317ff480
commit ccaff3da39
7 changed files with 57 additions and 45 deletions

View file

@ -46,7 +46,6 @@
#include "numbers.h"
#include "numbers.h"
#include "pairs.h"
#include "smob.h"
#include "srfi-4.h"
#include "stime.h"
#include "strings.h"
@ -185,6 +184,7 @@ scm_c_make_rstate (const char *seed, int n)
state = scm_gc_malloc_pointerless (scm_the_rng.rstate_size,
"random-state");
state->tag = scm_tc16_random_state;
state->rng = &scm_the_rng;
state->normal_next = 0.0;
state->rng->init_rstate (state, seed, n);
@ -198,6 +198,7 @@ scm_c_rstate_from_datum (SCM datum)
state = scm_gc_malloc_pointerless (scm_the_rng.rstate_size,
"random-state");
state->tag = scm_tc16_random_state;
state->rng = &scm_the_rng;
state->normal_next = 0.0;
state->rng->from_datum (state, datum);
@ -255,7 +256,7 @@ scm_c_exp1 (scm_t_rstate *state)
return - log (scm_c_uniform01 (state));
}
unsigned char scm_masktab[256];
static unsigned char scm_masktab[256];
static inline uint32_t
scm_i_mask32 (uint32_t m)
@ -371,19 +372,6 @@ scm_c_random_bignum (scm_t_rstate *state, SCM m)
return ret;
}
/*
* Scheme level representation of random states.
*/
scm_t_bits scm_tc16_rstate;
static SCM
make_rstate (scm_t_rstate *state)
{
SCM_RETURN_NEWSMOB (scm_tc16_rstate, state);
}
/*
* Scheme level interface.
*/
@ -444,7 +432,8 @@ SCM_DEFINE (scm_copy_random_state, "copy-random-state", 0, 1, 0,
if (SCM_UNBNDP (state))
state = SCM_VARIABLE_REF (scm_var_random_state);
SCM_VALIDATE_RSTATE (1, state);
return make_rstate (SCM_RSTATE (state)->rng->copy_rstate (SCM_RSTATE (state)));
return scm_from_random_state
(SCM_RSTATE (state)->rng->copy_rstate (SCM_RSTATE (state)));
}
#undef FUNC_NAME
@ -478,7 +467,7 @@ SCM_DEFINE (scm_seed_to_random_state, "seed->random-state", 1, 0, 0,
SCM_OUT_OF_RANGE (1, seed);
}
res = make_rstate (scm_c_make_rstate (c_str, len));
res = scm_from_random_state (scm_c_make_rstate (c_str, len));
free (c_str);
scm_remember_upto_here_1 (seed);
@ -493,7 +482,7 @@ SCM_DEFINE (scm_datum_to_random_state, "datum->random-state", 1, 0, 0,
"been obtained from @code{random-state->datum}.")
#define FUNC_NAME s_scm_datum_to_random_state
{
return make_rstate (scm_c_rstate_from_datum (datum));
return scm_from_random_state (scm_c_rstate_from_datum (datum));
}
#undef FUNC_NAME
@ -759,7 +748,7 @@ random_state_of_last_resort (void)
buf[i] = scm_to_int (scm_logand (seed, SCM_I_MAKINUM (255)));
seed = scm_ash (seed, SCM_I_MAKINUM (-8));
}
state = make_rstate (scm_c_make_rstate ((char *) buf, len));
state = scm_from_random_state (scm_c_make_rstate ((char *) buf, len));
free (buf);
}
return state;
@ -807,7 +796,7 @@ source of entropy, appropriate for use in non-security-critical applications.")
{
unsigned char buf[32];
if (read_dev_urandom (buf, sizeof(buf)))
return make_rstate (scm_c_make_rstate ((char *) buf, sizeof(buf)));
return scm_from_random_state (scm_c_make_rstate ((char *) buf, sizeof(buf)));
else
return random_state_of_last_resort ();
}
@ -829,8 +818,6 @@ scm_init_random ()
};
scm_the_rng = rng;
scm_tc16_rstate = scm_make_smob_type ("random-state", 0);
for (m = 1; m <= 0x100; m <<= 1)
for (i = m >> 1; i < m; ++i)
scm_masktab[i] = m - 1;