mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-06-12 06:41:13 +02:00
refactor datum->random-state / random-state->datum
* libguile/random.c (scm_t_i_rstate): Move here from random.h, along with prototypes for functions (scm_i_uniform32, scm_i_init_rstate, scm_i_copy_rstate): Change to take a stock scm_t_rstate as an arg, and cast it. This way we don't cast the pointers below. (scm_i_rstate_from_datum, scm_i_rstate_from_datum): Same and rename from scm_i_init_rstate_scm / scm_i_expose_rstate. (scm_c_rstate_from_datum): Rename from scm_c_make_rstate_scm. (scm_datum_to_random_state, scm_random_state_to_datum): Rename from scm_external_to_random_state and scm_random_state_to_external. (scm_init_random): Remove casts. * libguile/random.h (scm_t_rng): Rename init_rstate_scm, expose_rstate vmethods to from_datum, to_datum. Remove internal definitions. Rename to scm_c_rstate_from_datum, and provide scm_random_state_to_datum and scm_datum_to_random_state.
This commit is contained in:
parent
b606ff6af9
commit
99a0ee6620
2 changed files with 55 additions and 57 deletions
|
@ -71,25 +71,34 @@ scm_t_rng scm_the_rng;
|
||||||
* (http://stat.fsu.edu/~geo/diehard.html)
|
* (http://stat.fsu.edu/~geo/diehard.html)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
typedef struct scm_t_i_rstate {
|
||||||
|
scm_t_rstate rstate;
|
||||||
|
scm_t_uint32 w;
|
||||||
|
scm_t_uint32 c;
|
||||||
|
} scm_t_i_rstate;
|
||||||
|
|
||||||
|
|
||||||
#define A 2131995753UL
|
#define A 2131995753UL
|
||||||
|
|
||||||
#ifndef M_PI
|
#ifndef M_PI
|
||||||
#define M_PI 3.14159265359
|
#define M_PI 3.14159265359
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
scm_t_uint32
|
static scm_t_uint32
|
||||||
scm_i_uniform32 (scm_t_i_rstate *state)
|
scm_i_uniform32 (scm_t_rstate *state)
|
||||||
{
|
{
|
||||||
scm_t_uint64 x = (scm_t_uint64) A * state->w + state->c;
|
scm_t_i_rstate *istate = (scm_t_i_rstate*) state;
|
||||||
|
scm_t_uint64 x = (scm_t_uint64) A * istate->w + istate->c;
|
||||||
scm_t_uint32 w = x & 0xffffffffUL;
|
scm_t_uint32 w = x & 0xffffffffUL;
|
||||||
state->w = w;
|
istate->w = w;
|
||||||
state->c = x >> 32L;
|
istate->c = x >> 32L;
|
||||||
return w;
|
return w;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
static void
|
||||||
scm_i_init_rstate (scm_t_i_rstate *state, const char *seed, int n)
|
scm_i_init_rstate (scm_t_rstate *state, const char *seed, int n)
|
||||||
{
|
{
|
||||||
|
scm_t_i_rstate *istate = (scm_t_i_rstate*) state;
|
||||||
scm_t_uint32 w = 0L;
|
scm_t_uint32 w = 0L;
|
||||||
scm_t_uint32 c = 0L;
|
scm_t_uint32 c = 0L;
|
||||||
int i, m;
|
int i, m;
|
||||||
|
@ -103,12 +112,12 @@ scm_i_init_rstate (scm_t_i_rstate *state, const char *seed, int n)
|
||||||
}
|
}
|
||||||
if ((w == 0 && c == 0) || (w == -1 && c == A - 1))
|
if ((w == 0 && c == 0) || (w == -1 && c == A - 1))
|
||||||
++c;
|
++c;
|
||||||
state->w = w;
|
istate->w = w;
|
||||||
state->c = c;
|
istate->c = c;
|
||||||
}
|
}
|
||||||
|
|
||||||
scm_t_i_rstate *
|
static scm_t_rstate *
|
||||||
scm_i_copy_rstate (scm_t_i_rstate *state)
|
scm_i_copy_rstate (scm_t_rstate *state)
|
||||||
{
|
{
|
||||||
scm_t_rstate *new_state;
|
scm_t_rstate *new_state;
|
||||||
|
|
||||||
|
@ -119,10 +128,11 @@ scm_i_copy_rstate (scm_t_i_rstate *state)
|
||||||
|
|
||||||
SCM_SYMBOL(scm_i_rstate_tag, "multiply-with-carry");
|
SCM_SYMBOL(scm_i_rstate_tag, "multiply-with-carry");
|
||||||
|
|
||||||
void
|
static void
|
||||||
scm_i_init_rstate_scm (scm_t_i_rstate *state, SCM value)
|
scm_i_rstate_from_datum (scm_t_rstate *state, SCM value)
|
||||||
#define FUNC_NAME "scm_i_init_rstate_scm"
|
#define FUNC_NAME "scm_i_rstate_from_datum"
|
||||||
{
|
{
|
||||||
|
scm_t_i_rstate *istate = (scm_t_i_rstate*) state;
|
||||||
scm_t_uint32 w, c;
|
scm_t_uint32 w, c;
|
||||||
long length;
|
long length;
|
||||||
|
|
||||||
|
@ -133,17 +143,18 @@ scm_i_init_rstate_scm (scm_t_i_rstate *state, SCM value)
|
||||||
SCM_VALIDATE_UINT_COPY (SCM_ARG1, SCM_CADR (value), w);
|
SCM_VALIDATE_UINT_COPY (SCM_ARG1, SCM_CADR (value), w);
|
||||||
SCM_VALIDATE_UINT_COPY (SCM_ARG1, SCM_CADDR (value), c);
|
SCM_VALIDATE_UINT_COPY (SCM_ARG1, SCM_CADDR (value), c);
|
||||||
|
|
||||||
state->w = w;
|
istate->w = w;
|
||||||
state->c = c;
|
istate->c = c;
|
||||||
}
|
}
|
||||||
#undef FUNC_NAME
|
#undef FUNC_NAME
|
||||||
|
|
||||||
SCM
|
static SCM
|
||||||
scm_i_expose_rstate (scm_t_i_rstate *state)
|
scm_i_rstate_to_datum (scm_t_rstate *state)
|
||||||
{
|
{
|
||||||
|
scm_t_i_rstate *istate = (scm_t_i_rstate*) state;
|
||||||
return scm_list_3 (scm_i_rstate_tag,
|
return scm_list_3 (scm_i_rstate_tag,
|
||||||
scm_from_uint32 (state->w),
|
scm_from_uint32 (istate->w),
|
||||||
scm_from_uint32 (state->c));
|
scm_from_uint32 (istate->c));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -164,14 +175,14 @@ scm_c_make_rstate (const char *seed, int n)
|
||||||
}
|
}
|
||||||
|
|
||||||
scm_t_rstate *
|
scm_t_rstate *
|
||||||
scm_c_make_rstate_scm (SCM external)
|
scm_c_rstate_from_datum (SCM datum)
|
||||||
{
|
{
|
||||||
scm_t_rstate *state;
|
scm_t_rstate *state;
|
||||||
|
|
||||||
state = scm_gc_malloc_pointerless (scm_the_rng.rstate_size,
|
state = scm_gc_malloc_pointerless (scm_the_rng.rstate_size,
|
||||||
"random-state");
|
"random-state");
|
||||||
state->reserved0 = 0;
|
state->reserved0 = 0;
|
||||||
scm_the_rng.init_rstate_scm (state, external);
|
scm_the_rng.from_datum (state, datum);
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -400,25 +411,26 @@ SCM_DEFINE (scm_seed_to_random_state, "seed->random-state", 1, 0, 0,
|
||||||
}
|
}
|
||||||
#undef FUNC_NAME
|
#undef FUNC_NAME
|
||||||
|
|
||||||
SCM_DEFINE (scm_external_to_random_state, "external->random-state", 1, 0, 0,
|
SCM_DEFINE (scm_datum_to_random_state, "datum->random-state", 1, 0, 0,
|
||||||
(SCM external),
|
(SCM datum),
|
||||||
"Return a new random state using @var{external}.\n"
|
"Return a new random state using @var{datum}.\n"
|
||||||
"\n"
|
"\n"
|
||||||
"@var{external} must be an external state representation obtained\n"
|
"@var{datum} must be an external state representation obtained\n"
|
||||||
"from @code{random-state->external}.")
|
"from @code{random-state->datum}.")
|
||||||
#define FUNC_NAME s_scm_external_to_random_state
|
#define FUNC_NAME s_scm_datum_to_random_state
|
||||||
{
|
{
|
||||||
return make_rstate (scm_c_make_rstate_scm (external));
|
return make_rstate (scm_c_rstate_from_datum (datum));
|
||||||
}
|
}
|
||||||
#undef FUNC_NAME
|
#undef FUNC_NAME
|
||||||
|
|
||||||
SCM_DEFINE (scm_random_state_to_external, "random-state->external", 1, 0, 0,
|
SCM_DEFINE (scm_random_state_to_datum, "random-state->datum", 1, 0, 0,
|
||||||
(SCM state),
|
(SCM state),
|
||||||
"Return an external representation of @var{state}.")
|
"Return a datum representation of @var{state} that may be\n"
|
||||||
#define FUNC_NAME s_scm_random_state_to_external
|
"written out and read back with the Scheme reader.")
|
||||||
|
#define FUNC_NAME s_scm_random_state_to_datum
|
||||||
{
|
{
|
||||||
SCM_VALIDATE_RSTATE (1, state);
|
SCM_VALIDATE_RSTATE (1, state);
|
||||||
return scm_the_rng.expose_rstate (SCM_RSTATE (state));
|
return scm_the_rng.to_datum (SCM_RSTATE (state));
|
||||||
}
|
}
|
||||||
#undef FUNC_NAME
|
#undef FUNC_NAME
|
||||||
|
|
||||||
|
@ -618,11 +630,11 @@ scm_init_random ()
|
||||||
scm_t_rng rng =
|
scm_t_rng rng =
|
||||||
{
|
{
|
||||||
sizeof (scm_t_i_rstate),
|
sizeof (scm_t_i_rstate),
|
||||||
(scm_t_uint32 (*)()) scm_i_uniform32,
|
scm_i_uniform32,
|
||||||
(void (*)()) scm_i_init_rstate,
|
scm_i_init_rstate,
|
||||||
(scm_t_rstate *(*)()) scm_i_copy_rstate,
|
scm_i_copy_rstate,
|
||||||
(void (*)(scm_t_rstate *, SCM)) scm_i_init_rstate_scm,
|
scm_i_rstate_from_datum,
|
||||||
(SCM (*)(scm_t_rstate *)) scm_i_expose_rstate
|
scm_i_rstate_to_datum
|
||||||
};
|
};
|
||||||
scm_the_rng = rng;
|
scm_the_rng = rng;
|
||||||
|
|
||||||
|
|
|
@ -49,34 +49,18 @@ typedef struct scm_t_rng {
|
||||||
scm_t_uint32 (*random_bits) (scm_t_rstate *state); /* gives 32 random bits */
|
scm_t_uint32 (*random_bits) (scm_t_rstate *state); /* gives 32 random bits */
|
||||||
void (*init_rstate) (scm_t_rstate *state, const char *seed, int n);
|
void (*init_rstate) (scm_t_rstate *state, const char *seed, int n);
|
||||||
scm_t_rstate *(*copy_rstate) (scm_t_rstate *state);
|
scm_t_rstate *(*copy_rstate) (scm_t_rstate *state);
|
||||||
void (*init_rstate_scm) (scm_t_rstate *state, SCM exposed);
|
void (*from_datum) (scm_t_rstate *state, SCM datum);
|
||||||
SCM (*expose_rstate) (scm_t_rstate *state);
|
SCM (*to_datum) (scm_t_rstate *state);
|
||||||
} scm_t_rng;
|
} scm_t_rng;
|
||||||
|
|
||||||
SCM_API scm_t_rng scm_the_rng;
|
SCM_API scm_t_rng scm_the_rng;
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Default RNG
|
|
||||||
*/
|
|
||||||
typedef struct scm_t_i_rstate {
|
|
||||||
scm_t_rstate rstate;
|
|
||||||
scm_t_uint32 w;
|
|
||||||
scm_t_uint32 c;
|
|
||||||
} scm_t_i_rstate;
|
|
||||||
|
|
||||||
SCM_INTERNAL scm_t_uint32 scm_i_uniform32 (scm_t_i_rstate *);
|
|
||||||
SCM_INTERNAL void scm_i_init_rstate (scm_t_i_rstate *, const char *seed, int n);
|
|
||||||
SCM_INTERNAL scm_t_i_rstate *scm_i_copy_rstate (scm_t_i_rstate *);
|
|
||||||
SCM_INTERNAL void scm_i_init_rstate_scm (scm_t_i_rstate *state, SCM value);
|
|
||||||
SCM_INTERNAL SCM scm_i_expose_rstate (scm_t_i_rstate *state);
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Random number library functions
|
* Random number library functions
|
||||||
*/
|
*/
|
||||||
SCM_API scm_t_rstate *scm_c_make_rstate (const char *, int);
|
SCM_API scm_t_rstate *scm_c_make_rstate (const char *, int);
|
||||||
SCM_API scm_t_rstate *scm_c_make_rstate_scm (SCM external);
|
SCM_API scm_t_rstate *scm_c_rstate_from_datum (SCM datum);
|
||||||
SCM_API scm_t_rstate *scm_c_default_rstate (void);
|
SCM_API scm_t_rstate *scm_c_default_rstate (void);
|
||||||
#define scm_c_uniform32(RSTATE) scm_the_rng.random_bits (RSTATE)
|
#define scm_c_uniform32(RSTATE) scm_the_rng.random_bits (RSTATE)
|
||||||
SCM_API double scm_c_uniform01 (scm_t_rstate *);
|
SCM_API double scm_c_uniform01 (scm_t_rstate *);
|
||||||
|
@ -99,6 +83,8 @@ SCM_API SCM scm_var_random_state;
|
||||||
SCM_API SCM scm_random (SCM n, SCM state);
|
SCM_API SCM scm_random (SCM n, SCM state);
|
||||||
SCM_API SCM scm_copy_random_state (SCM state);
|
SCM_API SCM scm_copy_random_state (SCM state);
|
||||||
SCM_API SCM scm_seed_to_random_state (SCM seed);
|
SCM_API SCM scm_seed_to_random_state (SCM seed);
|
||||||
|
SCM_API SCM scm_datum_to_random_state (SCM datum);
|
||||||
|
SCM_API SCM scm_random_state_to_datum (SCM state);
|
||||||
SCM_API SCM scm_random_uniform (SCM state);
|
SCM_API SCM scm_random_uniform (SCM state);
|
||||||
SCM_API SCM scm_random_solid_sphere_x (SCM v, SCM state);
|
SCM_API SCM scm_random_solid_sphere_x (SCM v, SCM state);
|
||||||
SCM_API SCM scm_random_hollow_sphere_x (SCM v, SCM state);
|
SCM_API SCM scm_random_hollow_sphere_x (SCM v, SCM state);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue