1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-10 22:10:21 +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:
Andy Wingo 2010-07-26 14:57:46 +02:00
parent b606ff6af9
commit 99a0ee6620
2 changed files with 55 additions and 57 deletions

View file

@ -71,25 +71,34 @@ scm_t_rng scm_the_rng;
* (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
#ifndef M_PI
#define M_PI 3.14159265359
#endif
scm_t_uint32
scm_i_uniform32 (scm_t_i_rstate *state)
static scm_t_uint32
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;
state->w = w;
state->c = x >> 32L;
istate->w = w;
istate->c = x >> 32L;
return w;
}
void
scm_i_init_rstate (scm_t_i_rstate *state, const char *seed, int n)
static void
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 c = 0L;
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))
++c;
state->w = w;
state->c = c;
istate->w = w;
istate->c = c;
}
scm_t_i_rstate *
scm_i_copy_rstate (scm_t_i_rstate *state)
static scm_t_rstate *
scm_i_copy_rstate (scm_t_rstate *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");
void
scm_i_init_rstate_scm (scm_t_i_rstate *state, SCM value)
#define FUNC_NAME "scm_i_init_rstate_scm"
static void
scm_i_rstate_from_datum (scm_t_rstate *state, SCM value)
#define FUNC_NAME "scm_i_rstate_from_datum"
{
scm_t_i_rstate *istate = (scm_t_i_rstate*) state;
scm_t_uint32 w, c;
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_CADDR (value), c);
state->w = w;
state->c = c;
istate->w = w;
istate->c = c;
}
#undef FUNC_NAME
SCM
scm_i_expose_rstate (scm_t_i_rstate *state)
static SCM
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,
scm_from_uint32 (state->w),
scm_from_uint32 (state->c));
scm_from_uint32 (istate->w),
scm_from_uint32 (istate->c));
}
@ -164,14 +175,14 @@ scm_c_make_rstate (const char *seed, int n)
}
scm_t_rstate *
scm_c_make_rstate_scm (SCM external)
scm_c_rstate_from_datum (SCM datum)
{
scm_t_rstate *state;
state = scm_gc_malloc_pointerless (scm_the_rng.rstate_size,
"random-state");
state->reserved0 = 0;
scm_the_rng.init_rstate_scm (state, external);
scm_the_rng.from_datum (state, datum);
return state;
}
@ -400,25 +411,26 @@ SCM_DEFINE (scm_seed_to_random_state, "seed->random-state", 1, 0, 0,
}
#undef FUNC_NAME
SCM_DEFINE (scm_external_to_random_state, "external->random-state", 1, 0, 0,
(SCM external),
"Return a new random state using @var{external}.\n"
SCM_DEFINE (scm_datum_to_random_state, "datum->random-state", 1, 0, 0,
(SCM datum),
"Return a new random state using @var{datum}.\n"
"\n"
"@var{external} must be an external state representation obtained\n"
"from @code{random-state->external}.")
#define FUNC_NAME s_scm_external_to_random_state
"@var{datum} must be an external state representation obtained\n"
"from @code{random-state->datum}.")
#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
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),
"Return an external representation of @var{state}.")
#define FUNC_NAME s_scm_random_state_to_external
"Return a datum representation of @var{state} that may be\n"
"written out and read back with the Scheme reader.")
#define FUNC_NAME s_scm_random_state_to_datum
{
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
@ -618,11 +630,11 @@ scm_init_random ()
scm_t_rng rng =
{
sizeof (scm_t_i_rstate),
(scm_t_uint32 (*)()) scm_i_uniform32,
(void (*)()) scm_i_init_rstate,
(scm_t_rstate *(*)()) scm_i_copy_rstate,
(void (*)(scm_t_rstate *, SCM)) scm_i_init_rstate_scm,
(SCM (*)(scm_t_rstate *)) scm_i_expose_rstate
scm_i_uniform32,
scm_i_init_rstate,
scm_i_copy_rstate,
scm_i_rstate_from_datum,
scm_i_rstate_to_datum
};
scm_the_rng = rng;

View file

@ -49,34 +49,18 @@ typedef struct scm_t_rng {
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);
scm_t_rstate *(*copy_rstate) (scm_t_rstate *state);
void (*init_rstate_scm) (scm_t_rstate *state, SCM exposed);
SCM (*expose_rstate) (scm_t_rstate *state);
void (*from_datum) (scm_t_rstate *state, SCM datum);
SCM (*to_datum) (scm_t_rstate *state);
} scm_t_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
*/
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);
#define scm_c_uniform32(RSTATE) scm_the_rng.random_bits (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_copy_random_state (SCM state);
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_solid_sphere_x (SCM v, SCM state);
SCM_API SCM scm_random_hollow_sphere_x (SCM v, SCM state);