Use 'scm_from_utf8_{string,symbol,keyword}' for C string literals.
Partial fix for <https://bugs.gnu.org/33044>.
Reported by Tom de Vries <tdevries@suse.de>.
Fix several instances of the mistake of using 'scm_from_locale_*' for C
strings that originally came from a C string literal. Change several
uses of 'scm_from_latin1_*' as well, to promote the practice of writing
code that works for arbitrary C string literals.
Also add missing years to the copyright notices of changed files, based
on the git history.
* libguile/debug-malloc.c, libguile/deprecation.c, libguile/error.c,
libguile/eval.c, libguile/expand.c, libguile/extensions.c,
libguile/filesys.c, libguile/init.c, libguile/load.c,
libguile/modules.c, libguile/pairs.c, libguile/posix.c,
libguile/print.c, libguile/random.c, libguile/read.c,
libguile/regex-posix.c, libguile/snarf.h, libguile/srfi-13.c,
libguile/stacks.c, libguile/stime.c, libguile/strports.c,
libguile/values.c: Use 'scm_from_utf8_*' where appropriate.
2018-10-16 02:34:18 -04:00
|
|
|
|
/* Copyright 1999-2001,2003,2005-2006,2009-2010,2012-2014,2017-2019
|
2018-06-20 20:01:49 +02:00
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
|
|
|
|
This file is part of Guile.
|
|
|
|
|
|
|
|
|
|
|
|
Guile is free software: you can redistribute it and/or modify it
|
|
|
|
|
|
under the terms of the GNU Lesser General Public License as published
|
|
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
|
|
Guile is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
|
|
License for more details.
|
|
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
|
License along with Guile. If not, see
|
|
|
|
|
|
<https://www.gnu.org/licenses/>. */
|
1999-01-10 07:57:27 +00:00
|
|
|
|
|
1999-12-12 02:36:16 +00:00
|
|
|
|
|
2013-02-25 13:38:55 -05:00
|
|
|
|
/* Original Author: Mikael Djurfeldt <djurfeldt@nada.kth.se> */
|
1999-01-10 07:57:27 +00:00
|
|
|
|
|
2008-09-13 15:35:27 +02:00
|
|
|
|
#ifdef HAVE_CONFIG_H
|
2003-03-25 23:59:05 +00:00
|
|
|
|
# include <config.h>
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
1999-01-10 07:57:27 +00:00
|
|
|
|
#include <math.h>
|
2018-06-20 18:31:24 +02:00
|
|
|
|
#include <stdio.h>
|
2000-06-14 01:33:02 +00:00
|
|
|
|
#include <string.h>
|
2013-02-25 13:38:55 -05:00
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
2018-06-20 18:31:24 +02:00
|
|
|
|
#include <gmp.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include "arrays.h"
|
|
|
|
|
|
#include "feature.h"
|
2018-06-20 17:19:31 +02:00
|
|
|
|
#include "generalized-arrays.h"
|
2018-06-20 18:31:24 +02:00
|
|
|
|
#include "generalized-vectors.h"
|
|
|
|
|
|
#include "gsubr.h"
|
2018-06-20 17:19:31 +02:00
|
|
|
|
#include "list.h"
|
|
|
|
|
|
#include "modules.h"
|
|
|
|
|
|
#include "numbers.h"
|
2018-06-20 18:31:24 +02:00
|
|
|
|
#include "numbers.h"
|
2018-06-20 17:19:31 +02:00
|
|
|
|
#include "pairs.h"
|
2018-06-20 18:31:24 +02:00
|
|
|
|
#include "smob.h"
|
|
|
|
|
|
#include "srfi-4.h"
|
2018-06-20 17:19:31 +02:00
|
|
|
|
#include "stime.h"
|
|
|
|
|
|
#include "strings.h"
|
|
|
|
|
|
#include "symbols.h"
|
|
|
|
|
|
#include "variable.h"
|
|
|
|
|
|
#include "vectors.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include "random.h"
|
1999-01-10 07:57:27 +00:00
|
|
|
|
|
2018-06-20 18:31:24 +02:00
|
|
|
|
|
1999-01-10 07:57:27 +00:00
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
* A plugin interface for RNGs
|
|
|
|
|
|
*
|
|
|
|
|
|
* Using this interface, it is possible for the application to tell
|
|
|
|
|
|
* libguile to use a different RNG. This is desirable if it is
|
|
|
|
|
|
* necessary to use the same RNG everywhere in the application in
|
|
|
|
|
|
* order to prevent interference, if the application uses RNG
|
|
|
|
|
|
* hardware, or if the application has special demands on the RNG.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Look in random.h and how the default generator is "plugged in" in
|
|
|
|
|
|
* scm_init_random().
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2001-06-14 19:50:43 +00:00
|
|
|
|
scm_t_rng scm_the_rng;
|
1999-01-10 07:57:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
* The prepackaged RNG
|
|
|
|
|
|
*
|
|
|
|
|
|
* This is the MWC (Multiply With Carry) random number generator
|
|
|
|
|
|
* described by George Marsaglia at the Department of Statistics and
|
|
|
|
|
|
* Supercomputer Computations Research Institute, The Florida State
|
|
|
|
|
|
* University (http://stat.fsu.edu/~geo).
|
|
|
|
|
|
*
|
|
|
|
|
|
* It uses 64 bits, has a period of 4578426017172946943 (4.6e18), and
|
|
|
|
|
|
* passes all tests in the DIEHARD test suite
|
|
|
|
|
|
* (http://stat.fsu.edu/~geo/diehard.html)
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2010-07-26 14:57:46 +02:00
|
|
|
|
typedef struct scm_t_i_rstate {
|
|
|
|
|
|
scm_t_rstate rstate;
|
2018-06-21 08:39:03 +02:00
|
|
|
|
uint32_t w;
|
|
|
|
|
|
uint32_t c;
|
2010-07-26 14:57:46 +02:00
|
|
|
|
} scm_t_i_rstate;
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-01-10 07:57:27 +00:00
|
|
|
|
#define A 2131995753UL
|
|
|
|
|
|
|
2001-06-26 17:53:09 +00:00
|
|
|
|
#ifndef M_PI
|
|
|
|
|
|
#define M_PI 3.14159265359
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2018-06-21 08:39:03 +02:00
|
|
|
|
static uint32_t
|
2010-07-26 14:57:46 +02:00
|
|
|
|
scm_i_uniform32 (scm_t_rstate *state)
|
1999-01-10 07:57:27 +00:00
|
|
|
|
{
|
2010-07-26 14:57:46 +02:00
|
|
|
|
scm_t_i_rstate *istate = (scm_t_i_rstate*) state;
|
2018-06-21 08:39:03 +02:00
|
|
|
|
uint64_t x = (uint64_t) A * istate->w + istate->c;
|
|
|
|
|
|
uint32_t w = x & 0xffffffffUL;
|
2010-07-26 14:57:46 +02:00
|
|
|
|
istate->w = w;
|
|
|
|
|
|
istate->c = x >> 32L;
|
1999-01-10 07:57:27 +00:00
|
|
|
|
return w;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2010-07-26 14:57:46 +02:00
|
|
|
|
static void
|
|
|
|
|
|
scm_i_init_rstate (scm_t_rstate *state, const char *seed, int n)
|
1999-01-10 07:57:27 +00:00
|
|
|
|
{
|
2010-07-26 14:57:46 +02:00
|
|
|
|
scm_t_i_rstate *istate = (scm_t_i_rstate*) state;
|
2018-06-21 08:39:03 +02:00
|
|
|
|
uint32_t w = 0L;
|
|
|
|
|
|
uint32_t c = 0L;
|
1999-01-10 07:57:27 +00:00
|
|
|
|
int i, m;
|
|
|
|
|
|
for (i = 0; i < n; ++i)
|
|
|
|
|
|
{
|
|
|
|
|
|
m = i % 8;
|
|
|
|
|
|
if (m < 4)
|
|
|
|
|
|
w += seed[i] << (8 * m);
|
|
|
|
|
|
else
|
|
|
|
|
|
c += seed[i] << (8 * (m - 4));
|
|
|
|
|
|
}
|
2005-01-23 23:58:43 +00:00
|
|
|
|
if ((w == 0 && c == 0) || (w == -1 && c == A - 1))
|
1999-01-10 07:57:27 +00:00
|
|
|
|
++c;
|
2010-07-26 14:57:46 +02:00
|
|
|
|
istate->w = w;
|
|
|
|
|
|
istate->c = c;
|
1999-01-10 07:57:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2010-07-26 14:57:46 +02:00
|
|
|
|
static scm_t_rstate *
|
|
|
|
|
|
scm_i_copy_rstate (scm_t_rstate *state)
|
1999-01-10 07:57:27 +00:00
|
|
|
|
{
|
2006-06-08 22:01:47 +00:00
|
|
|
|
scm_t_rstate *new_state;
|
|
|
|
|
|
|
2010-07-26 16:36:15 +02:00
|
|
|
|
new_state = scm_gc_malloc_pointerless (state->rng->rstate_size,
|
2006-06-08 22:01:47 +00:00
|
|
|
|
"random-state");
|
2010-07-26 16:36:15 +02:00
|
|
|
|
return memcpy (new_state, state, state->rng->rstate_size);
|
1999-01-10 07:57:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2010-07-22 18:26:00 +02:00
|
|
|
|
SCM_SYMBOL(scm_i_rstate_tag, "multiply-with-carry");
|
|
|
|
|
|
|
2010-07-26 14:57:46 +02:00
|
|
|
|
static void
|
|
|
|
|
|
scm_i_rstate_from_datum (scm_t_rstate *state, SCM value)
|
|
|
|
|
|
#define FUNC_NAME "scm_i_rstate_from_datum"
|
2010-07-22 18:26:00 +02:00
|
|
|
|
{
|
2010-07-26 14:57:46 +02:00
|
|
|
|
scm_t_i_rstate *istate = (scm_t_i_rstate*) state;
|
2018-06-21 08:39:03 +02:00
|
|
|
|
uint32_t w, c;
|
2010-07-22 18:26:00 +02:00
|
|
|
|
long length;
|
|
|
|
|
|
|
|
|
|
|
|
SCM_VALIDATE_LIST_COPYLEN (SCM_ARG1, value, length);
|
|
|
|
|
|
SCM_ASSERT (length == 3, value, SCM_ARG1, FUNC_NAME);
|
|
|
|
|
|
SCM_ASSERT (scm_is_eq (SCM_CAR (value), scm_i_rstate_tag),
|
|
|
|
|
|
value, SCM_ARG1, FUNC_NAME);
|
2010-07-22 21:26:19 +02:00
|
|
|
|
SCM_VALIDATE_UINT_COPY (SCM_ARG1, SCM_CADR (value), w);
|
|
|
|
|
|
SCM_VALIDATE_UINT_COPY (SCM_ARG1, SCM_CADDR (value), c);
|
2010-07-22 18:26:00 +02:00
|
|
|
|
|
2010-07-26 14:57:46 +02:00
|
|
|
|
istate->w = w;
|
|
|
|
|
|
istate->c = c;
|
2010-07-22 18:26:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
2010-07-26 14:57:46 +02:00
|
|
|
|
static SCM
|
|
|
|
|
|
scm_i_rstate_to_datum (scm_t_rstate *state)
|
2010-07-22 18:26:00 +02:00
|
|
|
|
{
|
2010-07-26 14:57:46 +02:00
|
|
|
|
scm_t_i_rstate *istate = (scm_t_i_rstate*) state;
|
2010-07-22 18:26:00 +02:00
|
|
|
|
return scm_list_3 (scm_i_rstate_tag,
|
2010-07-26 14:57:46 +02:00
|
|
|
|
scm_from_uint32 (istate->w),
|
|
|
|
|
|
scm_from_uint32 (istate->c));
|
2010-07-22 18:26:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
1999-01-10 07:57:27 +00:00
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
* Random number library functions
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2001-06-14 19:50:43 +00:00
|
|
|
|
scm_t_rstate *
|
* strings.h, strings.c: (scm_i_string_chars, scm_i_string_length,
scm_i_string_writable_chars, scm_i_string_stop_writing): New, to
replace SCM_I_STRING_CHARS and SCM_I_STRING_LENGTH. Updated all
uses.
(scm_i_make_string, scm_c_make_string): New, to replace
scm_allocate_string. Updated all uses.
(SCM_STRINGP, SCM_STRING_CHARS, SCM_STRING_UCHARS,
SCM_STRING_LENGTH): Deprecated.
(scm_allocate_string, scm_take_str, scm_take0str, scm_mem2string,
scm_str2string, scm_makfrom0str, scm_makfrom0str_opt):
Discouraged. Replaced all uses with scm_from_locale_string or
similar, as appropriate.
(scm_c_string_length, scm_c_string_ref, scm_c_string_set_x,
scm_c_substring, scm_c_substring_shared, scm_c_substring_copy,
scm_substring_shared, scm_substring_copy): New.
* symbols.c, symbols.h (SCM_SYMBOLP, SCM_SYMBOL_FUNC,
SCM_SET_SYMBOL_FUNC, SCM_SYMBOL_PROPS, SCM_SET_SYMBOL_PROPS,
SCM_SYMBOL_HASH, SCM_SYMBOL_INTERNED_P, scm_mem2symbol,
scm_str2symbol, scm_mem2uninterned_symbol): Discouraged.
(SCM_SYMBOL_LENGTH, SCM_SYMBOL_CHARS, scm_c_symbol2str):
Deprecated.
(SCM_MAKE_SYMBOL_TAG, SCM_SET_SYMBOL_LENGTH, SCM_SET_SYMBOL_CHARS,
SCM_PROP_SLOTS, SCM_SET_PROP_SLOTS): Removed.
(scm_is_symbol, scm_from_locale_symbol, scm_from_locale_symboln):
New, to replace scm_str2symbol and scm_mem2symbol, respectively.
Updated all uses.
(scm_gensym): Generate only the number suffix in the buffer, just
string-append the prefix.
2004-08-19 17:19:44 +00:00
|
|
|
|
scm_c_make_rstate (const char *seed, int n)
|
1999-01-21 09:20:39 +00:00
|
|
|
|
{
|
2006-06-08 22:01:47 +00:00
|
|
|
|
scm_t_rstate *state;
|
|
|
|
|
|
|
|
|
|
|
|
state = scm_gc_malloc_pointerless (scm_the_rng.rstate_size,
|
|
|
|
|
|
"random-state");
|
2010-07-26 16:36:15 +02:00
|
|
|
|
state->rng = &scm_the_rng;
|
|
|
|
|
|
state->normal_next = 0.0;
|
|
|
|
|
|
state->rng->init_rstate (state, seed, n);
|
1999-01-21 09:20:39 +00:00
|
|
|
|
return state;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2010-07-22 18:26:00 +02:00
|
|
|
|
scm_t_rstate *
|
2010-07-26 14:57:46 +02:00
|
|
|
|
scm_c_rstate_from_datum (SCM datum)
|
2010-07-22 18:26:00 +02:00
|
|
|
|
{
|
|
|
|
|
|
scm_t_rstate *state;
|
|
|
|
|
|
|
|
|
|
|
|
state = scm_gc_malloc_pointerless (scm_the_rng.rstate_size,
|
|
|
|
|
|
"random-state");
|
2010-07-26 16:36:15 +02:00
|
|
|
|
state->rng = &scm_the_rng;
|
|
|
|
|
|
state->normal_next = 0.0;
|
|
|
|
|
|
state->rng->from_datum (state, datum);
|
2010-07-22 18:26:00 +02:00
|
|
|
|
return state;
|
|
|
|
|
|
}
|
2001-03-06 01:22:37 +00:00
|
|
|
|
|
2001-06-14 19:50:43 +00:00
|
|
|
|
scm_t_rstate *
|
1999-07-25 19:25:01 +00:00
|
|
|
|
scm_c_default_rstate ()
|
2001-03-06 01:22:37 +00:00
|
|
|
|
#define FUNC_NAME "scm_c_default_rstate"
|
1999-07-25 19:25:01 +00:00
|
|
|
|
{
|
2003-04-06 13:48:57 +00:00
|
|
|
|
SCM state = SCM_VARIABLE_REF (scm_var_random_state);
|
2001-03-06 01:22:37 +00:00
|
|
|
|
if (!SCM_RSTATEP (state))
|
|
|
|
|
|
SCM_MISC_ERROR ("*random-state* contains bogus random state", SCM_EOL);
|
1999-07-25 19:25:01 +00:00
|
|
|
|
return SCM_RSTATE (state);
|
|
|
|
|
|
}
|
2001-03-06 01:22:37 +00:00
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
1999-07-25 19:25:01 +00:00
|
|
|
|
|
2010-07-26 16:36:15 +02:00
|
|
|
|
double
|
2001-06-14 19:50:43 +00:00
|
|
|
|
scm_c_uniform01 (scm_t_rstate *state)
|
1999-01-10 07:57:27 +00:00
|
|
|
|
{
|
2010-07-26 16:36:15 +02:00
|
|
|
|
double x = (double) state->rng->random_bits (state) / (double) 0xffffffffUL;
|
|
|
|
|
|
return ((x + (double) state->rng->random_bits (state))
|
1999-01-11 07:13:18 +00:00
|
|
|
|
/ (double) 0xffffffffUL);
|
1999-01-10 07:57:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double
|
2001-06-14 19:50:43 +00:00
|
|
|
|
scm_c_normal01 (scm_t_rstate *state)
|
1999-01-10 07:57:27 +00:00
|
|
|
|
{
|
2010-07-26 16:36:15 +02:00
|
|
|
|
if (state->normal_next != 0.0)
|
1999-01-10 07:57:27 +00:00
|
|
|
|
{
|
2010-07-26 16:36:15 +02:00
|
|
|
|
double ret = state->normal_next;
|
|
|
|
|
|
|
|
|
|
|
|
state->normal_next = 0.0;
|
|
|
|
|
|
|
|
|
|
|
|
return ret;
|
1999-01-10 07:57:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
double r, a, n;
|
|
|
|
|
|
|
1999-07-25 19:25:01 +00:00
|
|
|
|
r = sqrt (-2.0 * log (scm_c_uniform01 (state)));
|
|
|
|
|
|
a = 2.0 * M_PI * scm_c_uniform01 (state);
|
1999-01-10 07:57:27 +00:00
|
|
|
|
|
|
|
|
|
|
n = r * sin (a);
|
2010-07-26 16:36:15 +02:00
|
|
|
|
state->normal_next = r * cos (a);
|
1999-01-10 07:57:27 +00:00
|
|
|
|
|
|
|
|
|
|
return n;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double
|
2001-06-14 19:50:43 +00:00
|
|
|
|
scm_c_exp1 (scm_t_rstate *state)
|
1999-01-10 07:57:27 +00:00
|
|
|
|
{
|
1999-07-25 19:25:01 +00:00
|
|
|
|
return - log (scm_c_uniform01 (state));
|
1999-01-10 07:57:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned char scm_masktab[256];
|
|
|
|
|
|
|
2018-06-21 08:39:03 +02:00
|
|
|
|
static inline uint32_t
|
|
|
|
|
|
scm_i_mask32 (uint32_t m)
|
1999-01-10 07:57:27 +00:00
|
|
|
|
{
|
2010-08-01 21:53:29 +02:00
|
|
|
|
return (m < 0x100
|
1999-01-10 07:57:27 +00:00
|
|
|
|
? scm_masktab[m]
|
|
|
|
|
|
: (m < 0x10000
|
1999-01-11 07:13:18 +00:00
|
|
|
|
? scm_masktab[m >> 8] << 8 | 0xff
|
1999-01-10 07:57:27 +00:00
|
|
|
|
: (m < 0x1000000
|
1999-01-11 07:13:18 +00:00
|
|
|
|
? scm_masktab[m >> 16] << 16 | 0xffff
|
2018-06-21 08:39:03 +02:00
|
|
|
|
: ((uint32_t) scm_masktab[m >> 24]) << 24 | 0xffffff)));
|
2010-08-01 21:53:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-06-21 08:39:03 +02:00
|
|
|
|
uint32_t
|
|
|
|
|
|
scm_c_random (scm_t_rstate *state, uint32_t m)
|
2010-08-01 21:53:29 +02:00
|
|
|
|
{
|
2018-06-21 08:39:03 +02:00
|
|
|
|
uint32_t r, mask = scm_i_mask32 (m);
|
2010-07-26 16:36:15 +02:00
|
|
|
|
while ((r = state->rng->random_bits (state) & mask) >= m);
|
1999-01-10 07:57:27 +00:00
|
|
|
|
return r;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-06-21 08:39:03 +02:00
|
|
|
|
uint64_t
|
|
|
|
|
|
scm_c_random64 (scm_t_rstate *state, uint64_t m)
|
2010-08-01 21:53:29 +02:00
|
|
|
|
{
|
2018-06-21 08:39:03 +02:00
|
|
|
|
uint64_t r;
|
|
|
|
|
|
uint32_t mask;
|
2010-08-01 21:53:29 +02:00
|
|
|
|
|
Use stdint.h limit macros
* libguile/__scm.h: Include <stdint.h>, now that we rely on C99.
(SCM_T_UINT8_MAX, SCM_T_INT8_MIN, SCM_T_INT8_MAX, SCM_T_UINT16_MAX)
(SCM_T_INT16_MIN, SCM_T_INT16_MAX, SCM_T_UINT32_MAX, SCM_T_INT32_MIN)
(SCM_T_INT32_MAX, SCM_T_UINT64_MAX, SCM_T_INT64_MIN, SCM_T_INT64_MAX)
(SCM_T_UINTMAX_MAX, SCM_T_INTMAX_MIN, SCM_T_INTMAX_MAX)
(SCM_T_UINTPTR_MAX, SCM_T_INTPTR_MIN, SCM_T_INTPTR_MAX): Define in
terms of equivalent stdint.h definitions.
* libguile/gen-scmconfig.c:
* libguile/instructions.c:
* libguile/numbers.c:
* libguile/random.c:
* libguile/tags.h:
* test-suite/standalone/test-conversion.c: Adapt to use C99 names.
2018-06-20 14:55:49 +02:00
|
|
|
|
if (m <= UINT32_MAX)
|
2018-06-21 08:39:03 +02:00
|
|
|
|
return scm_c_random (state, (uint32_t) m);
|
2010-08-01 21:53:29 +02:00
|
|
|
|
|
|
|
|
|
|
mask = scm_i_mask32 (m >> 32);
|
2018-06-21 08:39:03 +02:00
|
|
|
|
while ((r = ((uint64_t) (state->rng->random_bits (state) & mask) << 32)
|
2010-08-01 21:53:29 +02:00
|
|
|
|
| state->rng->random_bits (state)) >= m)
|
|
|
|
|
|
;
|
|
|
|
|
|
return r;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2003-03-03 13:19:46 +00:00
|
|
|
|
/*
|
|
|
|
|
|
SCM scm_c_random_bignum (scm_t_rstate *state, SCM m)
|
|
|
|
|
|
|
|
|
|
|
|
Takes a random state (source of random bits) and a bignum m.
|
|
|
|
|
|
Returns a bignum b, 0 <= b < m.
|
|
|
|
|
|
|
|
|
|
|
|
It does this by allocating a bignum b with as many base 65536 digits
|
|
|
|
|
|
as m, filling b with random bits (in 32 bit chunks) up to the most
|
|
|
|
|
|
significant 1 in m, and, finally checking if the resultant b is too
|
|
|
|
|
|
large (>= m). If too large, we simply repeat the process again. (It
|
|
|
|
|
|
is important to throw away all generated random bits if b >= m,
|
|
|
|
|
|
otherwise we'll end up with a distorted distribution.)
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
1999-01-10 07:57:27 +00:00
|
|
|
|
SCM
|
2001-06-14 19:50:43 +00:00
|
|
|
|
scm_c_random_bignum (scm_t_rstate *state, SCM m)
|
1999-01-10 07:57:27 +00:00
|
|
|
|
{
|
2003-04-04 21:50:22 +00:00
|
|
|
|
SCM result = scm_i_mkbig ();
|
|
|
|
|
|
const size_t m_bits = mpz_sizeinbase (SCM_I_BIG_MPZ (m), 2);
|
2018-06-21 08:39:03 +02:00
|
|
|
|
/* 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;
|
|
|
|
|
|
const uint32_t num_full_chunks =
|
|
|
|
|
|
m_bits / (sizeof (uint32_t) * SCM_CHAR_BIT);
|
|
|
|
|
|
const uint32_t num_chunks = num_full_chunks + ((end_bits) ? 1 : 0);
|
2003-04-04 21:50:22 +00:00
|
|
|
|
|
|
|
|
|
|
/* we know the result will be this big */
|
|
|
|
|
|
mpz_realloc2 (SCM_I_BIG_MPZ (result), m_bits);
|
|
|
|
|
|
|
|
|
|
|
|
random_chunks =
|
2018-06-21 08:39:03 +02:00
|
|
|
|
(uint32_t *) scm_gc_calloc (num_chunks * sizeof (uint32_t),
|
2003-04-04 21:50:22 +00:00
|
|
|
|
"random bignum chunks");
|
|
|
|
|
|
|
2003-04-06 09:41:07 +00:00
|
|
|
|
do
|
1999-01-10 07:57:27 +00:00
|
|
|
|
{
|
2018-06-21 08:39:03 +02:00
|
|
|
|
uint32_t *current_chunk = random_chunks + (num_chunks - 1);
|
|
|
|
|
|
uint32_t chunks_left = num_chunks;
|
2003-04-04 21:50:22 +00:00
|
|
|
|
|
|
|
|
|
|
mpz_set_ui (SCM_I_BIG_MPZ (result), 0);
|
|
|
|
|
|
|
|
|
|
|
|
if (end_bits)
|
|
|
|
|
|
{
|
|
|
|
|
|
/* generate a mask with ones in the end_bits position, i.e. if
|
|
|
|
|
|
end_bits is 3, then we'd have a mask of ...0000000111 */
|
2018-06-21 08:39:03 +02:00
|
|
|
|
const uint32_t rndbits = state->rng->random_bits (state);
|
|
|
|
|
|
int rshift = (sizeof (uint32_t) * SCM_CHAR_BIT) - end_bits;
|
|
|
|
|
|
uint32_t mask = ((uint32_t)-1) >> rshift;
|
|
|
|
|
|
uint32_t highest_bits = rndbits & mask;
|
2003-04-04 21:50:22 +00:00
|
|
|
|
*current_chunk-- = highest_bits;
|
|
|
|
|
|
chunks_left--;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
while (chunks_left)
|
|
|
|
|
|
{
|
2018-06-21 08:39:03 +02:00
|
|
|
|
/* now fill in the remaining uint32_t sized chunks */
|
2010-07-26 16:36:15 +02:00
|
|
|
|
*current_chunk-- = state->rng->random_bits (state);
|
2003-04-04 21:50:22 +00:00
|
|
|
|
chunks_left--;
|
|
|
|
|
|
}
|
|
|
|
|
|
mpz_import (SCM_I_BIG_MPZ (result),
|
|
|
|
|
|
num_chunks,
|
|
|
|
|
|
-1,
|
2018-06-21 08:39:03 +02:00
|
|
|
|
sizeof (uint32_t),
|
2003-04-04 21:50:22 +00:00
|
|
|
|
0,
|
|
|
|
|
|
0,
|
|
|
|
|
|
random_chunks);
|
2003-04-06 09:41:07 +00:00
|
|
|
|
/* 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);
|
2003-04-04 21:50:22 +00:00
|
|
|
|
scm_gc_free (random_chunks,
|
2018-06-21 08:39:03 +02:00
|
|
|
|
num_chunks * sizeof (uint32_t),
|
2003-04-04 21:50:22 +00:00
|
|
|
|
"random bignum chunks");
|
2003-04-07 01:51:10 +00:00
|
|
|
|
return scm_i_normbig (result);
|
1999-01-10 07:57:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
* Scheme level representation of random states.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2001-06-14 19:50:43 +00:00
|
|
|
|
scm_t_bits scm_tc16_rstate;
|
1999-01-10 07:57:27 +00:00
|
|
|
|
|
|
|
|
|
|
static SCM
|
2001-06-14 19:50:43 +00:00
|
|
|
|
make_rstate (scm_t_rstate *state)
|
1999-01-10 07:57:27 +00:00
|
|
|
|
{
|
1999-07-07 09:44:01 +00:00
|
|
|
|
SCM_RETURN_NEWSMOB (scm_tc16_rstate, state);
|
1999-01-10 07:57:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
* Scheme level interface.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
Use 'scm_from_utf8_{string,symbol,keyword}' for C string literals.
Partial fix for <https://bugs.gnu.org/33044>.
Reported by Tom de Vries <tdevries@suse.de>.
Fix several instances of the mistake of using 'scm_from_locale_*' for C
strings that originally came from a C string literal. Change several
uses of 'scm_from_latin1_*' as well, to promote the practice of writing
code that works for arbitrary C string literals.
Also add missing years to the copyright notices of changed files, based
on the git history.
* libguile/debug-malloc.c, libguile/deprecation.c, libguile/error.c,
libguile/eval.c, libguile/expand.c, libguile/extensions.c,
libguile/filesys.c, libguile/init.c, libguile/load.c,
libguile/modules.c, libguile/pairs.c, libguile/posix.c,
libguile/print.c, libguile/random.c, libguile/read.c,
libguile/regex-posix.c, libguile/snarf.h, libguile/srfi-13.c,
libguile/stacks.c, libguile/stime.c, libguile/strports.c,
libguile/values.c: Use 'scm_from_utf8_*' where appropriate.
2018-10-16 02:34:18 -04:00
|
|
|
|
SCM_GLOBAL_VARIABLE_INIT (scm_var_random_state, "*random-state*",
|
|
|
|
|
|
scm_seed_to_random_state
|
|
|
|
|
|
(scm_from_utf8_string
|
|
|
|
|
|
("URL:http://stat.fsu.edu/~geo/diehard.html")));
|
1999-01-10 07:57:27 +00:00
|
|
|
|
|
2000-01-05 19:05:23 +00:00
|
|
|
|
SCM_DEFINE (scm_random, "random", 1, 1, 0,
|
1999-12-12 02:36:16 +00:00
|
|
|
|
(SCM n, SCM state),
|
2002-07-20 14:08:34 +00:00
|
|
|
|
"Return a number in [0, N).\n"
|
2000-01-25 21:29:57 +00:00
|
|
|
|
"\n"
|
2001-11-11 15:01:52 +00:00
|
|
|
|
"Accepts a positive integer or real n and returns a\n"
|
|
|
|
|
|
"number of the same type between zero (inclusive) and\n"
|
|
|
|
|
|
"N (exclusive). The values returned have a uniform\n"
|
2000-01-25 21:29:57 +00:00
|
|
|
|
"distribution.\n"
|
|
|
|
|
|
"\n"
|
2001-02-17 11:29:16 +00:00
|
|
|
|
"The optional argument @var{state} must be of the type produced\n"
|
|
|
|
|
|
"by @code{seed->random-state}. It defaults to the value of the\n"
|
|
|
|
|
|
"variable @var{*random-state*}. This object is used to maintain\n"
|
|
|
|
|
|
"the state of the pseudo-random-number generator and is altered\n"
|
|
|
|
|
|
"as a side effect of the random operation.")
|
1999-12-12 02:36:16 +00:00
|
|
|
|
#define FUNC_NAME s_scm_random
|
1999-01-10 07:57:27 +00:00
|
|
|
|
{
|
|
|
|
|
|
if (SCM_UNBNDP (state))
|
2001-05-15 14:57:22 +00:00
|
|
|
|
state = SCM_VARIABLE_REF (scm_var_random_state);
|
2002-07-20 14:08:34 +00:00
|
|
|
|
SCM_VALIDATE_RSTATE (2, state);
|
2004-07-23 15:43:02 +00:00
|
|
|
|
if (SCM_I_INUMP (n))
|
1999-01-10 07:57:27 +00:00
|
|
|
|
{
|
2010-11-19 11:29:26 +01:00
|
|
|
|
scm_t_bits m = (scm_t_bits) SCM_I_INUM (n);
|
2010-07-27 11:32:31 +02:00
|
|
|
|
SCM_ASSERT_RANGE (1, n, SCM_I_INUM (n) > 0);
|
2010-11-19 11:29:26 +01:00
|
|
|
|
#if SCM_SIZEOF_UINTPTR_T <= 4
|
2010-07-27 11:32:31 +02:00
|
|
|
|
return scm_from_uint32 (scm_c_random (SCM_RSTATE (state),
|
2018-06-21 08:39:03 +02:00
|
|
|
|
(uint32_t) m));
|
2010-11-19 11:29:26 +01:00
|
|
|
|
#elif SCM_SIZEOF_UINTPTR_T <= 8
|
2010-08-01 21:53:29 +02:00
|
|
|
|
return scm_from_uint64 (scm_c_random64 (SCM_RSTATE (state),
|
2018-06-21 08:39:03 +02:00
|
|
|
|
(uint64_t) m));
|
2010-07-27 11:32:31 +02:00
|
|
|
|
#else
|
2010-11-19 11:29:26 +01:00
|
|
|
|
#error "Cannot deal with this platform's scm_t_bits size"
|
2010-07-27 11:32:31 +02:00
|
|
|
|
#endif
|
1999-01-10 07:57:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
if (SCM_REALP (n))
|
2004-08-03 15:06:12 +00:00
|
|
|
|
return scm_from_double (SCM_REAL_VALUE (n)
|
|
|
|
|
|
* scm_c_uniform01 (SCM_RSTATE (state)));
|
2003-04-04 21:50:22 +00:00
|
|
|
|
|
* validate.h, deprecated.h (SCM_VALIDATE_INUM, SCM_VALIDATE_INUM_COPY,
SCM_VALIDATE_BIGINT, SCM_VALIDATE_INUM_MIN,
SCM_VALIDATE_INUM_MIN_COPY,
SCM_VALIDATE_INUM_MIN_DEF_COPY,SCM_VALIDATE_INUM_DEF,
SCM_VALIDATE_INUM_DEF_COPY, SCM_VALIDATE_INUM_RANGE,
SCM_VALIDATE_INUM_RANGE_COPY): Deprecated because they make the
fixnum/bignum distinction visible. Changed all uses to scm_to_size_t
or similar.
2004-07-10 14:35:36 +00:00
|
|
|
|
if (!SCM_BIGP (n))
|
|
|
|
|
|
SCM_WRONG_TYPE_ARG (1, n);
|
1999-07-25 19:25:01 +00:00
|
|
|
|
return scm_c_random_bignum (SCM_RSTATE (state), n);
|
1999-01-10 07:57:27 +00:00
|
|
|
|
}
|
1999-12-12 02:36:16 +00:00
|
|
|
|
#undef FUNC_NAME
|
1999-01-10 07:57:27 +00:00
|
|
|
|
|
2000-01-05 19:05:23 +00:00
|
|
|
|
SCM_DEFINE (scm_copy_random_state, "copy-random-state", 0, 1, 0,
|
1999-12-12 02:36:16 +00:00
|
|
|
|
(SCM state),
|
2001-02-17 11:29:16 +00:00
|
|
|
|
"Return a copy of the random state @var{state}.")
|
1999-12-12 02:36:16 +00:00
|
|
|
|
#define FUNC_NAME s_scm_copy_random_state
|
1999-01-10 07:57:27 +00:00
|
|
|
|
{
|
|
|
|
|
|
if (SCM_UNBNDP (state))
|
2001-05-15 14:57:22 +00:00
|
|
|
|
state = SCM_VARIABLE_REF (scm_var_random_state);
|
2002-07-20 14:08:34 +00:00
|
|
|
|
SCM_VALIDATE_RSTATE (1, state);
|
2010-07-26 16:36:15 +02:00
|
|
|
|
return make_rstate (SCM_RSTATE (state)->rng->copy_rstate (SCM_RSTATE (state)));
|
1999-01-10 07:57:27 +00:00
|
|
|
|
}
|
1999-12-12 02:36:16 +00:00
|
|
|
|
#undef FUNC_NAME
|
1999-01-10 07:57:27 +00:00
|
|
|
|
|
2000-01-05 19:05:23 +00:00
|
|
|
|
SCM_DEFINE (scm_seed_to_random_state, "seed->random-state", 1, 0, 0,
|
1999-12-12 02:36:16 +00:00
|
|
|
|
(SCM seed),
|
2001-02-17 11:29:16 +00:00
|
|
|
|
"Return a new random state using @var{seed}.")
|
1999-12-12 02:36:16 +00:00
|
|
|
|
#define FUNC_NAME s_scm_seed_to_random_state
|
1999-01-21 09:20:39 +00:00
|
|
|
|
{
|
* socket.c, rw.c, deprecated.h, validate.h
(SCM_VALIDATE_STRING_COPY): Deprecated. Replaced all uses with
SCM_VALIDATE_STRING plus SCM_I_STRING_CHARS or
scm_to_locale_string, etc.
(SCM_VALIDATE_SUBSTRING_SPEC_COPY): Deprecated. Replaced as
above, plus scm_i_get_substring_spec.
* regex-posix.c, read.c, random.c, ramap.c, print.c, numbers.c,
hash.c, gc.c, gc-card.c, convert.i.c, backtrace.c, strop.c,
strorder.c, strports.c, struct.c, symbols.c, unif.c, ports.c: Use
SCM_I_STRING_CHARS, SCM_I_STRING_UCHARS, and SCM_I_STRING_LENGTH
instead of SCM_STRING_CHARS, SCM_STRING_UCHARS, and
SCM_STRING_LENGTH, respectively. Also, replaced scm_return_first
with more explicit scm_remember_upto_here_1, etc, or introduced
them in the first place.
2004-08-12 17:45:03 +00:00
|
|
|
|
SCM res;
|
2018-10-19 21:54:34 -04:00
|
|
|
|
char *c_str;
|
|
|
|
|
|
size_t len;
|
|
|
|
|
|
|
1999-01-21 09:20:39 +00:00
|
|
|
|
if (SCM_NUMBERP (seed))
|
|
|
|
|
|
seed = scm_number_to_string (seed, SCM_UNDEFINED);
|
2002-07-20 14:08:34 +00:00
|
|
|
|
SCM_VALIDATE_STRING (1, seed);
|
2018-10-19 21:54:34 -04:00
|
|
|
|
|
|
|
|
|
|
if (scm_i_is_narrow_string (seed))
|
|
|
|
|
|
/* This special case of a narrow string, where latin1 is used, is
|
|
|
|
|
|
for backward compatibility during the 2.2 stable series. In
|
|
|
|
|
|
future major releases, we should use UTF-8 uniformly. */
|
|
|
|
|
|
c_str = scm_to_latin1_stringn (seed, &len);
|
|
|
|
|
|
else
|
|
|
|
|
|
c_str = scm_to_utf8_stringn (seed, &len);
|
|
|
|
|
|
|
|
|
|
|
|
/* 'scm_to_*_stringn' returns a 'size_t' for the length in bytes, but
|
|
|
|
|
|
'scm_c_make_rstate' accepts an 'int'. Make sure the length fits in
|
|
|
|
|
|
an 'int'. */
|
|
|
|
|
|
if (len > INT_MAX)
|
|
|
|
|
|
{
|
|
|
|
|
|
free (c_str);
|
|
|
|
|
|
SCM_OUT_OF_RANGE (1, seed);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
res = make_rstate (scm_c_make_rstate (c_str, len));
|
|
|
|
|
|
free (c_str);
|
|
|
|
|
|
|
* socket.c, rw.c, deprecated.h, validate.h
(SCM_VALIDATE_STRING_COPY): Deprecated. Replaced all uses with
SCM_VALIDATE_STRING plus SCM_I_STRING_CHARS or
scm_to_locale_string, etc.
(SCM_VALIDATE_SUBSTRING_SPEC_COPY): Deprecated. Replaced as
above, plus scm_i_get_substring_spec.
* regex-posix.c, read.c, random.c, ramap.c, print.c, numbers.c,
hash.c, gc.c, gc-card.c, convert.i.c, backtrace.c, strop.c,
strorder.c, strports.c, struct.c, symbols.c, unif.c, ports.c: Use
SCM_I_STRING_CHARS, SCM_I_STRING_UCHARS, and SCM_I_STRING_LENGTH
instead of SCM_STRING_CHARS, SCM_STRING_UCHARS, and
SCM_STRING_LENGTH, respectively. Also, replaced scm_return_first
with more explicit scm_remember_upto_here_1, etc, or introduced
them in the first place.
2004-08-12 17:45:03 +00:00
|
|
|
|
scm_remember_upto_here_1 (seed);
|
|
|
|
|
|
return res;
|
|
|
|
|
|
|
1999-01-21 09:20:39 +00:00
|
|
|
|
}
|
1999-12-12 02:36:16 +00:00
|
|
|
|
#undef FUNC_NAME
|
1999-01-21 09:20:39 +00:00
|
|
|
|
|
2010-07-26 14:57:46 +02:00
|
|
|
|
SCM_DEFINE (scm_datum_to_random_state, "datum->random-state", 1, 0, 0,
|
|
|
|
|
|
(SCM datum),
|
2010-07-26 15:12:42 +02:00
|
|
|
|
"Return a new random state using @var{datum}, which should have\n"
|
fix typos in the manual bits generated from source comments.
* libguile/bitvectors.c, libguile/chars.c,
libguile/deprecated.c, libguile/numbers.c, libguile/random.c,
libguile/read.c, libguile/root.c, libguile/srfi-1.c,
libguile/srfi-13.c, libguile/srfi-14.c, libguile/uniform.c:
Fix typos, add missing newlines.
2011-02-07 00:29:51 +01:00
|
|
|
|
"been obtained from @code{random-state->datum}.")
|
2010-07-26 14:57:46 +02:00
|
|
|
|
#define FUNC_NAME s_scm_datum_to_random_state
|
2010-07-22 18:26:00 +02:00
|
|
|
|
{
|
2010-07-26 14:57:46 +02:00
|
|
|
|
return make_rstate (scm_c_rstate_from_datum (datum));
|
2010-07-22 18:26:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
2010-07-26 14:57:46 +02:00
|
|
|
|
SCM_DEFINE (scm_random_state_to_datum, "random-state->datum", 1, 0, 0,
|
2010-07-22 18:26:00 +02:00
|
|
|
|
(SCM state),
|
2010-07-26 14:57:46 +02:00
|
|
|
|
"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
|
2010-07-22 18:26:00 +02:00
|
|
|
|
{
|
|
|
|
|
|
SCM_VALIDATE_RSTATE (1, state);
|
2010-07-26 16:36:15 +02:00
|
|
|
|
return SCM_RSTATE (state)->rng->to_datum (SCM_RSTATE (state));
|
2010-07-22 18:26:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
2000-01-05 19:05:23 +00:00
|
|
|
|
SCM_DEFINE (scm_random_uniform, "random:uniform", 0, 1, 0,
|
1999-12-12 02:36:16 +00:00
|
|
|
|
(SCM state),
|
2001-04-03 13:19:05 +00:00
|
|
|
|
"Return a uniformly distributed inexact real random number in\n"
|
|
|
|
|
|
"[0,1).")
|
1999-12-12 02:36:16 +00:00
|
|
|
|
#define FUNC_NAME s_scm_random_uniform
|
1999-01-10 07:57:27 +00:00
|
|
|
|
{
|
|
|
|
|
|
if (SCM_UNBNDP (state))
|
2001-05-15 14:57:22 +00:00
|
|
|
|
state = SCM_VARIABLE_REF (scm_var_random_state);
|
2002-07-20 14:08:34 +00:00
|
|
|
|
SCM_VALIDATE_RSTATE (1, state);
|
2004-08-03 15:06:12 +00:00
|
|
|
|
return scm_from_double (scm_c_uniform01 (SCM_RSTATE (state)));
|
1999-01-10 07:57:27 +00:00
|
|
|
|
}
|
1999-12-12 02:36:16 +00:00
|
|
|
|
#undef FUNC_NAME
|
1999-01-10 07:57:27 +00:00
|
|
|
|
|
2000-01-05 19:05:23 +00:00
|
|
|
|
SCM_DEFINE (scm_random_normal, "random:normal", 0, 1, 0,
|
1999-12-12 02:36:16 +00:00
|
|
|
|
(SCM state),
|
2001-04-03 13:19:05 +00:00
|
|
|
|
"Return an inexact real in a normal distribution. The\n"
|
|
|
|
|
|
"distribution used has mean 0 and standard deviation 1. For a\n"
|
|
|
|
|
|
"normal distribution with mean m and standard deviation d use\n"
|
|
|
|
|
|
"@code{(+ m (* d (random:normal)))}.")
|
1999-12-12 02:36:16 +00:00
|
|
|
|
#define FUNC_NAME s_scm_random_normal
|
* acconfig.h: add HAVE_ARRAYS.
* configure.in: add --disable-arrays option, probably temporary.
* the following changes allow guile to be built with the array
"module" omitted. some of this stuff is just tc7 type support,
which wouldn't be needed if uniform array types were converted
to smobs.
* tag.c (scm_utag_bvect ... scm_utag_cvect): don't define unless
HAVE_ARRAYS.
(scm_tag): don't check array types unless HAVE_ARRAYS.
* sort.c (scm_restricted_vector_sort_x, scm_sorted_p):
remove the unused array types.
* (scm_stable_sort, scm_sort): don't support vectors if not
HAVE_ARRAYS. a bit excessive.
* random.c (vector_scale, vector_sum_squares,
scm_random_solid_sphere_x, scm_random_hollow_sphere_x,
scm_random_normal_vector_x): don't define unless HAVE_ARRAYS.
* gh_data.c (makvect, gh_chars2byvect, gh_shorts2svect,
gh_longs2ivect, gh_ulongs2uvect, gh_floats2fvect, gh_doubles2dvect,
gh_uniform_vector_length, gh_uniform_vector_ref):
don't define unless HAVE_ARRAYS.
(gh_scm2chars, gh_scm2shorts, gh_scm2longs, gh_scm2floats,
gh_scm2doubles):
don't check vector types if not HAVE_ARRAYS.
* eq.c (scm_equal_p), eval.c (SCM_CEVAL), print.c (scm_iprin1),
gc.c (scm_gc_mark, scm_gc_sweep), objects.c (scm_class_of):
don't support the array types unless HAVE_ARRAYS is defined.
* tags.h: make nine tc7 types conditional on HAVE_ARRAYS.
* read.c (scm_lreadr): don't check for #* unless HAVE_ARRAYS is
defined (this should use read-hash-extend).
* ramap.c, unif.c: don't check whether ARRAYS is defined.
* vectors.c (scm_vector_set_length_x): moved here from unif.c. call
scm_uniform_element_size if HAVE_ARRAYS.
vectors.h: prototype too.
* unif.c (scm_uniform_element_size): new procedure.
* init.c (scm_boot_guile_1): don't call scm_init_ramap or
scm_init_unif unless HAVE_ARRAYS is defined.
* __scm.h: don't define ARRAYS.
* Makefile.am (EXTRA_libguile_la_SOURCES): unif.c and ramap.c
moved here from libguile_la_SOURCES.
* Makefile.am (ice9_sources): add arrays.scm.
* boot-9.scm: load arrays.scm if 'array is provided.
* arrays.scm: new file with stuff from boot-9.scm.
1999-11-19 18:16:19 +00:00
|
|
|
|
{
|
|
|
|
|
|
if (SCM_UNBNDP (state))
|
2001-05-15 14:57:22 +00:00
|
|
|
|
state = SCM_VARIABLE_REF (scm_var_random_state);
|
2002-07-20 14:08:34 +00:00
|
|
|
|
SCM_VALIDATE_RSTATE (1, state);
|
2004-08-03 15:06:12 +00:00
|
|
|
|
return scm_from_double (scm_c_normal01 (SCM_RSTATE (state)));
|
* acconfig.h: add HAVE_ARRAYS.
* configure.in: add --disable-arrays option, probably temporary.
* the following changes allow guile to be built with the array
"module" omitted. some of this stuff is just tc7 type support,
which wouldn't be needed if uniform array types were converted
to smobs.
* tag.c (scm_utag_bvect ... scm_utag_cvect): don't define unless
HAVE_ARRAYS.
(scm_tag): don't check array types unless HAVE_ARRAYS.
* sort.c (scm_restricted_vector_sort_x, scm_sorted_p):
remove the unused array types.
* (scm_stable_sort, scm_sort): don't support vectors if not
HAVE_ARRAYS. a bit excessive.
* random.c (vector_scale, vector_sum_squares,
scm_random_solid_sphere_x, scm_random_hollow_sphere_x,
scm_random_normal_vector_x): don't define unless HAVE_ARRAYS.
* gh_data.c (makvect, gh_chars2byvect, gh_shorts2svect,
gh_longs2ivect, gh_ulongs2uvect, gh_floats2fvect, gh_doubles2dvect,
gh_uniform_vector_length, gh_uniform_vector_ref):
don't define unless HAVE_ARRAYS.
(gh_scm2chars, gh_scm2shorts, gh_scm2longs, gh_scm2floats,
gh_scm2doubles):
don't check vector types if not HAVE_ARRAYS.
* eq.c (scm_equal_p), eval.c (SCM_CEVAL), print.c (scm_iprin1),
gc.c (scm_gc_mark, scm_gc_sweep), objects.c (scm_class_of):
don't support the array types unless HAVE_ARRAYS is defined.
* tags.h: make nine tc7 types conditional on HAVE_ARRAYS.
* read.c (scm_lreadr): don't check for #* unless HAVE_ARRAYS is
defined (this should use read-hash-extend).
* ramap.c, unif.c: don't check whether ARRAYS is defined.
* vectors.c (scm_vector_set_length_x): moved here from unif.c. call
scm_uniform_element_size if HAVE_ARRAYS.
vectors.h: prototype too.
* unif.c (scm_uniform_element_size): new procedure.
* init.c (scm_boot_guile_1): don't call scm_init_ramap or
scm_init_unif unless HAVE_ARRAYS is defined.
* __scm.h: don't define ARRAYS.
* Makefile.am (EXTRA_libguile_la_SOURCES): unif.c and ramap.c
moved here from libguile_la_SOURCES.
* Makefile.am (ice9_sources): add arrays.scm.
* boot-9.scm: load arrays.scm if 'array is provided.
* arrays.scm: new file with stuff from boot-9.scm.
1999-11-19 18:16:19 +00:00
|
|
|
|
}
|
1999-12-12 02:36:16 +00:00
|
|
|
|
#undef FUNC_NAME
|
* acconfig.h: add HAVE_ARRAYS.
* configure.in: add --disable-arrays option, probably temporary.
* the following changes allow guile to be built with the array
"module" omitted. some of this stuff is just tc7 type support,
which wouldn't be needed if uniform array types were converted
to smobs.
* tag.c (scm_utag_bvect ... scm_utag_cvect): don't define unless
HAVE_ARRAYS.
(scm_tag): don't check array types unless HAVE_ARRAYS.
* sort.c (scm_restricted_vector_sort_x, scm_sorted_p):
remove the unused array types.
* (scm_stable_sort, scm_sort): don't support vectors if not
HAVE_ARRAYS. a bit excessive.
* random.c (vector_scale, vector_sum_squares,
scm_random_solid_sphere_x, scm_random_hollow_sphere_x,
scm_random_normal_vector_x): don't define unless HAVE_ARRAYS.
* gh_data.c (makvect, gh_chars2byvect, gh_shorts2svect,
gh_longs2ivect, gh_ulongs2uvect, gh_floats2fvect, gh_doubles2dvect,
gh_uniform_vector_length, gh_uniform_vector_ref):
don't define unless HAVE_ARRAYS.
(gh_scm2chars, gh_scm2shorts, gh_scm2longs, gh_scm2floats,
gh_scm2doubles):
don't check vector types if not HAVE_ARRAYS.
* eq.c (scm_equal_p), eval.c (SCM_CEVAL), print.c (scm_iprin1),
gc.c (scm_gc_mark, scm_gc_sweep), objects.c (scm_class_of):
don't support the array types unless HAVE_ARRAYS is defined.
* tags.h: make nine tc7 types conditional on HAVE_ARRAYS.
* read.c (scm_lreadr): don't check for #* unless HAVE_ARRAYS is
defined (this should use read-hash-extend).
* ramap.c, unif.c: don't check whether ARRAYS is defined.
* vectors.c (scm_vector_set_length_x): moved here from unif.c. call
scm_uniform_element_size if HAVE_ARRAYS.
vectors.h: prototype too.
* unif.c (scm_uniform_element_size): new procedure.
* init.c (scm_boot_guile_1): don't call scm_init_ramap or
scm_init_unif unless HAVE_ARRAYS is defined.
* __scm.h: don't define ARRAYS.
* Makefile.am (EXTRA_libguile_la_SOURCES): unif.c and ramap.c
moved here from libguile_la_SOURCES.
* Makefile.am (ice9_sources): add arrays.scm.
* boot-9.scm: load arrays.scm if 'array is provided.
* arrays.scm: new file with stuff from boot-9.scm.
1999-11-19 18:16:19 +00:00
|
|
|
|
|
1999-01-10 07:57:27 +00:00
|
|
|
|
static void
|
2004-10-27 19:28:05 +00:00
|
|
|
|
vector_scale_x (SCM v, double c)
|
1999-01-10 07:57:27 +00:00
|
|
|
|
{
|
2017-10-31 13:28:44 +01:00
|
|
|
|
scm_t_array_handle handle;
|
|
|
|
|
|
scm_t_array_dim const * dims;
|
|
|
|
|
|
ssize_t i, inc, ubnd;
|
2005-01-02 20:49:04 +00:00
|
|
|
|
|
2017-10-31 13:28:44 +01:00
|
|
|
|
scm_array_get_handle (v, &handle);
|
|
|
|
|
|
dims = scm_array_handle_dims (&handle);
|
|
|
|
|
|
if (1 == scm_array_handle_rank (&handle))
|
|
|
|
|
|
{
|
|
|
|
|
|
ubnd = dims[0].ubnd;
|
|
|
|
|
|
inc = dims[0].inc;
|
2005-01-02 20:49:04 +00:00
|
|
|
|
|
2017-10-31 13:28:44 +01:00
|
|
|
|
if (handle.element_type == SCM_ARRAY_ELEMENT_TYPE_F64)
|
|
|
|
|
|
{
|
|
|
|
|
|
double *elts = (double *)(handle.writable_elements) + handle.base;
|
|
|
|
|
|
for (i = dims[0].lbnd; i <= ubnd; ++i, elts += inc)
|
|
|
|
|
|
*elts *= c;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (handle.element_type == SCM_ARRAY_ELEMENT_TYPE_SCM)
|
|
|
|
|
|
{
|
|
|
|
|
|
SCM *elts = (SCM *)(handle.writable_elements) + handle.base;
|
|
|
|
|
|
for (i = dims[0].lbnd; i <= ubnd; ++i, elts += inc)
|
|
|
|
|
|
SCM_REAL_VALUE (*elts) *= c;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2004-10-27 19:28:05 +00:00
|
|
|
|
}
|
2017-10-31 13:28:44 +01:00
|
|
|
|
scm_array_handle_release (&handle);
|
|
|
|
|
|
scm_misc_error (NULL, "must be a rank-1 array of type #t or 'f64", scm_list_1 (v));
|
1999-01-10 07:57:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static double
|
|
|
|
|
|
vector_sum_squares (SCM v)
|
|
|
|
|
|
{
|
|
|
|
|
|
double x, sum = 0.0;
|
2017-10-31 13:28:44 +01:00
|
|
|
|
scm_t_array_handle handle;
|
|
|
|
|
|
scm_t_array_dim const * dims;
|
|
|
|
|
|
ssize_t i, inc, ubnd;
|
2005-01-02 20:49:04 +00:00
|
|
|
|
|
2017-10-31 13:28:44 +01:00
|
|
|
|
scm_array_get_handle (v, &handle);
|
|
|
|
|
|
dims = scm_array_handle_dims (&handle);
|
|
|
|
|
|
if (1 == scm_array_handle_rank (&handle))
|
|
|
|
|
|
{
|
|
|
|
|
|
ubnd = dims[0].ubnd;
|
|
|
|
|
|
inc = dims[0].inc;
|
|
|
|
|
|
if (handle.element_type == SCM_ARRAY_ELEMENT_TYPE_F64)
|
|
|
|
|
|
{
|
|
|
|
|
|
const double *elts = (const double *)(handle.elements) + handle.base;
|
|
|
|
|
|
for (i = dims[0].lbnd; i <= ubnd; ++i, elts += inc)
|
|
|
|
|
|
{
|
|
|
|
|
|
x = *elts;
|
|
|
|
|
|
sum += x * x;
|
|
|
|
|
|
}
|
|
|
|
|
|
return sum;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (handle.element_type == SCM_ARRAY_ELEMENT_TYPE_SCM)
|
|
|
|
|
|
{
|
|
|
|
|
|
const SCM *elts = (const SCM *)(handle.elements) + handle.base;
|
|
|
|
|
|
for (i = dims[0].lbnd; i <= ubnd; ++i, elts += inc)
|
|
|
|
|
|
{
|
|
|
|
|
|
x = SCM_REAL_VALUE (*elts);
|
|
|
|
|
|
sum += x * x;
|
|
|
|
|
|
}
|
|
|
|
|
|
return sum;
|
|
|
|
|
|
}
|
2004-10-27 19:28:05 +00:00
|
|
|
|
}
|
2017-10-31 13:28:44 +01:00
|
|
|
|
scm_array_handle_release (&handle);
|
|
|
|
|
|
scm_misc_error (NULL, "must be an array of type #t or 'f64", scm_list_1 (v));
|
1999-01-10 07:57:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* For the uniform distribution on the solid sphere, note that in
|
|
|
|
|
|
* this distribution the length r of the vector has cumulative
|
|
|
|
|
|
* distribution r^n; i.e., u=r^n is uniform [0,1], so r can be
|
|
|
|
|
|
* generated as r=u^(1/n).
|
|
|
|
|
|
*/
|
2000-01-05 19:05:23 +00:00
|
|
|
|
SCM_DEFINE (scm_random_solid_sphere_x, "random:solid-sphere!", 1, 1, 0,
|
1999-12-12 02:36:16 +00:00
|
|
|
|
(SCM v, SCM state),
|
2005-01-15 00:03:53 +00:00
|
|
|
|
"Fills @var{vect} with inexact real random numbers the sum of\n"
|
|
|
|
|
|
"whose squares is less than 1.0. Thinking of @var{vect} as\n"
|
|
|
|
|
|
"coordinates in space of dimension @var{n} @math{=}\n"
|
|
|
|
|
|
"@code{(vector-length @var{vect})}, the coordinates are\n"
|
|
|
|
|
|
"uniformly distributed within the unit @var{n}-sphere.")
|
1999-12-12 02:36:16 +00:00
|
|
|
|
#define FUNC_NAME s_scm_random_solid_sphere_x
|
1999-01-10 07:57:27 +00:00
|
|
|
|
{
|
|
|
|
|
|
if (SCM_UNBNDP (state))
|
2001-05-15 14:57:22 +00:00
|
|
|
|
state = SCM_VARIABLE_REF (scm_var_random_state);
|
2002-07-20 14:08:34 +00:00
|
|
|
|
SCM_VALIDATE_RSTATE (2, state);
|
1999-01-10 07:57:27 +00:00
|
|
|
|
scm_random_normal_vector_x (v, state);
|
2004-10-27 19:28:05 +00:00
|
|
|
|
vector_scale_x (v,
|
|
|
|
|
|
pow (scm_c_uniform01 (SCM_RSTATE (state)),
|
2013-04-09 18:09:49 +02:00
|
|
|
|
1.0 / scm_c_array_length (v))
|
2004-10-27 19:28:05 +00:00
|
|
|
|
/ sqrt (vector_sum_squares (v)));
|
1999-01-10 07:57:27 +00:00
|
|
|
|
return SCM_UNSPECIFIED;
|
|
|
|
|
|
}
|
1999-12-12 02:36:16 +00:00
|
|
|
|
#undef FUNC_NAME
|
1999-01-10 07:57:27 +00:00
|
|
|
|
|
2013-04-09 18:09:49 +02:00
|
|
|
|
SCM_DEFINE (scm_random_hollow_sphere_x, "random:hollow-sphere!", 1, 1, 0,
|
1999-12-12 02:36:16 +00:00
|
|
|
|
(SCM v, SCM state),
|
2000-01-25 21:29:57 +00:00
|
|
|
|
"Fills vect with inexact real random numbers\n"
|
|
|
|
|
|
"the sum of whose squares is equal to 1.0.\n"
|
2001-11-11 15:01:52 +00:00
|
|
|
|
"Thinking of vect as coordinates in space of\n"
|
2000-01-25 21:29:57 +00:00
|
|
|
|
"dimension n = (vector-length vect), the coordinates\n"
|
2001-11-11 15:01:52 +00:00
|
|
|
|
"are uniformly distributed over the surface of the\n"
|
2001-11-13 23:44:29 +00:00
|
|
|
|
"unit n-sphere.")
|
1999-12-12 02:36:16 +00:00
|
|
|
|
#define FUNC_NAME s_scm_random_hollow_sphere_x
|
1999-01-10 07:57:27 +00:00
|
|
|
|
{
|
|
|
|
|
|
if (SCM_UNBNDP (state))
|
2001-05-15 14:57:22 +00:00
|
|
|
|
state = SCM_VARIABLE_REF (scm_var_random_state);
|
2002-07-20 14:08:34 +00:00
|
|
|
|
SCM_VALIDATE_RSTATE (2, state);
|
1999-01-10 07:57:27 +00:00
|
|
|
|
scm_random_normal_vector_x (v, state);
|
2004-10-27 19:28:05 +00:00
|
|
|
|
vector_scale_x (v, 1 / sqrt (vector_sum_squares (v)));
|
1999-01-10 07:57:27 +00:00
|
|
|
|
return SCM_UNSPECIFIED;
|
|
|
|
|
|
}
|
1999-12-12 02:36:16 +00:00
|
|
|
|
#undef FUNC_NAME
|
1999-01-10 07:57:27 +00:00
|
|
|
|
|
1999-12-12 02:36:16 +00:00
|
|
|
|
|
2017-10-31 13:28:44 +01:00
|
|
|
|
SCM_DEFINE (scm_random_normal_vector_x, "random:normal-vector!", 1, 1, 0,
|
1999-12-12 02:36:16 +00:00
|
|
|
|
(SCM v, SCM state),
|
2000-01-25 21:29:57 +00:00
|
|
|
|
"Fills vect with inexact real random numbers that are\n"
|
|
|
|
|
|
"independent and standard normally distributed\n"
|
2001-02-16 14:55:54 +00:00
|
|
|
|
"(i.e., with mean 0 and variance 1).")
|
1999-12-12 02:36:16 +00:00
|
|
|
|
#define FUNC_NAME s_scm_random_normal_vector_x
|
1999-01-10 07:57:27 +00:00
|
|
|
|
{
|
2005-01-02 20:49:04 +00:00
|
|
|
|
scm_t_array_handle handle;
|
2017-10-31 13:28:44 +01:00
|
|
|
|
scm_t_array_dim const * dims;
|
|
|
|
|
|
ssize_t i;
|
2004-10-27 19:28:05 +00:00
|
|
|
|
|
1999-01-10 07:57:27 +00:00
|
|
|
|
if (SCM_UNBNDP (state))
|
2001-05-15 14:57:22 +00:00
|
|
|
|
state = SCM_VARIABLE_REF (scm_var_random_state);
|
2002-07-20 14:08:34 +00:00
|
|
|
|
SCM_VALIDATE_RSTATE (2, state);
|
2005-01-02 20:49:04 +00:00
|
|
|
|
|
2017-02-13 13:41:45 +01:00
|
|
|
|
scm_array_get_handle (v, &handle);
|
|
|
|
|
|
if (1 != scm_array_handle_rank (&handle))
|
|
|
|
|
|
{
|
|
|
|
|
|
scm_array_handle_release (&handle);
|
|
|
|
|
|
scm_wrong_type_arg_msg (NULL, 0, v, "rank 1 array");
|
|
|
|
|
|
}
|
2017-10-31 13:28:44 +01:00
|
|
|
|
|
|
|
|
|
|
dims = scm_array_handle_dims (&handle);
|
2005-01-02 20:49:04 +00:00
|
|
|
|
|
2013-04-11 18:11:35 +02:00
|
|
|
|
if (handle.element_type == SCM_ARRAY_ELEMENT_TYPE_SCM)
|
2004-10-27 19:28:05 +00:00
|
|
|
|
{
|
2005-01-02 20:49:04 +00:00
|
|
|
|
SCM *elts = scm_array_handle_writable_elements (&handle);
|
2017-10-31 13:28:44 +01:00
|
|
|
|
for (i = dims->lbnd; i <= dims->ubnd; i++, elts += dims->inc)
|
|
|
|
|
|
*elts = scm_from_double (scm_c_normal01 (SCM_RSTATE (state)));
|
2004-10-27 19:28:05 +00:00
|
|
|
|
}
|
1999-01-10 07:57:27 +00:00
|
|
|
|
else
|
2004-10-27 19:28:05 +00:00
|
|
|
|
{
|
|
|
|
|
|
/* must be a f64vector. */
|
2005-01-02 20:49:04 +00:00
|
|
|
|
double *elts = scm_array_handle_f64_writable_elements (&handle);
|
2017-10-31 13:28:44 +01:00
|
|
|
|
for (i = dims->lbnd; i <= dims->ubnd; i++, elts += dims->inc)
|
|
|
|
|
|
*elts = scm_c_normal01 (SCM_RSTATE (state));
|
2004-10-27 19:28:05 +00:00
|
|
|
|
}
|
2005-01-02 20:49:04 +00:00
|
|
|
|
|
2005-01-06 18:56:34 +00:00
|
|
|
|
scm_array_handle_release (&handle);
|
1999-01-10 07:57:27 +00:00
|
|
|
|
return SCM_UNSPECIFIED;
|
|
|
|
|
|
}
|
1999-12-12 02:36:16 +00:00
|
|
|
|
#undef FUNC_NAME
|
1999-01-10 07:57:27 +00:00
|
|
|
|
|
2017-10-31 13:28:44 +01:00
|
|
|
|
SCM_DEFINE (scm_random_exp, "random:exp", 0, 1, 0,
|
1999-12-12 02:36:16 +00:00
|
|
|
|
(SCM state),
|
2001-04-03 13:19:05 +00:00
|
|
|
|
"Return an inexact real in an exponential distribution with mean\n"
|
|
|
|
|
|
"1. For an exponential distribution with mean u use (* u\n"
|
|
|
|
|
|
"(random:exp)).")
|
1999-12-12 02:36:16 +00:00
|
|
|
|
#define FUNC_NAME s_scm_random_exp
|
1999-01-10 07:57:27 +00:00
|
|
|
|
{
|
|
|
|
|
|
if (SCM_UNBNDP (state))
|
2001-05-15 14:57:22 +00:00
|
|
|
|
state = SCM_VARIABLE_REF (scm_var_random_state);
|
2002-07-20 14:08:34 +00:00
|
|
|
|
SCM_VALIDATE_RSTATE (1, state);
|
2004-08-03 15:06:12 +00:00
|
|
|
|
return scm_from_double (scm_c_exp1 (SCM_RSTATE (state)));
|
1999-01-10 07:57:27 +00:00
|
|
|
|
}
|
1999-12-12 02:36:16 +00:00
|
|
|
|
#undef FUNC_NAME
|
1999-01-10 07:57:27 +00:00
|
|
|
|
|
2013-02-25 13:33:14 -05:00
|
|
|
|
/* Return a new random-state seeded from the time, date, process ID, an
|
|
|
|
|
|
address from a freshly allocated heap cell, an address from the local
|
|
|
|
|
|
stack frame, and a high-resolution timer if available. This is only
|
|
|
|
|
|
to be used as a last resort, when no better source of entropy is
|
|
|
|
|
|
available. */
|
2012-01-18 02:36:17 -05:00
|
|
|
|
static SCM
|
|
|
|
|
|
random_state_of_last_resort (void)
|
|
|
|
|
|
{
|
|
|
|
|
|
SCM state;
|
|
|
|
|
|
SCM time_of_day = scm_gettimeofday ();
|
|
|
|
|
|
SCM sources = scm_list_n
|
|
|
|
|
|
(scm_from_unsigned_integer (SCM_UNPACK (time_of_day)), /* heap addr */
|
2013-02-25 13:38:55 -05:00
|
|
|
|
/* Avoid scm_getpid, since it depends on HAVE_POSIX. */
|
|
|
|
|
|
scm_from_unsigned_integer (getpid ()), /* process ID */
|
2012-01-18 02:36:17 -05:00
|
|
|
|
scm_get_internal_real_time (), /* high-resolution process timer */
|
|
|
|
|
|
scm_from_unsigned_integer ((scm_t_bits) &time_of_day), /* stack addr */
|
|
|
|
|
|
scm_car (time_of_day), /* seconds since midnight 1970-01-01 UTC */
|
|
|
|
|
|
scm_cdr (time_of_day), /* microsecond component of the above clock */
|
|
|
|
|
|
SCM_UNDEFINED);
|
2013-02-25 13:33:14 -05:00
|
|
|
|
|
2012-01-18 02:36:17 -05:00
|
|
|
|
/* Concatenate the sources bitwise to form the seed */
|
2013-02-25 13:33:28 -05:00
|
|
|
|
SCM seed = SCM_INUM0;
|
2012-01-18 02:36:17 -05:00
|
|
|
|
while (scm_is_pair (sources))
|
|
|
|
|
|
{
|
|
|
|
|
|
seed = scm_logxor (seed, scm_ash (scm_car (sources),
|
|
|
|
|
|
scm_integer_length (seed)));
|
|
|
|
|
|
sources = scm_cdr (sources);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* FIXME The following code belongs in `scm_seed_to_random_state',
|
|
|
|
|
|
and here we should simply do:
|
|
|
|
|
|
|
|
|
|
|
|
return scm_seed_to_random_state (seed);
|
|
|
|
|
|
|
|
|
|
|
|
Unfortunately, `scm_seed_to_random_state' only preserves around 32
|
|
|
|
|
|
bits of entropy from the provided seed. I don't know if it's okay
|
|
|
|
|
|
to fix that in 2.0, so for now we have this workaround. */
|
|
|
|
|
|
{
|
|
|
|
|
|
int i, len;
|
|
|
|
|
|
unsigned char *buf;
|
|
|
|
|
|
len = scm_to_int (scm_ceiling_quotient (scm_integer_length (seed),
|
|
|
|
|
|
SCM_I_MAKINUM (8)));
|
|
|
|
|
|
buf = (unsigned char *) malloc (len);
|
|
|
|
|
|
for (i = len-1; i >= 0; --i)
|
|
|
|
|
|
{
|
|
|
|
|
|
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));
|
|
|
|
|
|
free (buf);
|
|
|
|
|
|
}
|
|
|
|
|
|
return state;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Attempt to fill buffer with random bytes from /dev/urandom.
|
|
|
|
|
|
Return 1 if successful, else return 0. */
|
|
|
|
|
|
static int
|
|
|
|
|
|
read_dev_urandom (unsigned char *buf, size_t len)
|
|
|
|
|
|
{
|
|
|
|
|
|
size_t res = 0;
|
|
|
|
|
|
FILE *f = fopen ("/dev/urandom", "r");
|
|
|
|
|
|
if (f)
|
|
|
|
|
|
{
|
|
|
|
|
|
res = fread(buf, 1, len, f);
|
|
|
|
|
|
fclose (f);
|
|
|
|
|
|
}
|
|
|
|
|
|
return (res == len);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Fill a buffer with random bytes seeded from a platform-specific
|
|
|
|
|
|
source of entropy. /dev/urandom is used if available. Note that
|
|
|
|
|
|
this function provides no guarantees about the amount of entropy
|
|
|
|
|
|
present in the returned bytes. */
|
|
|
|
|
|
void
|
|
|
|
|
|
scm_i_random_bytes_from_platform (unsigned char *buf, size_t len)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (read_dev_urandom (buf, len))
|
|
|
|
|
|
return;
|
|
|
|
|
|
else /* FIXME: support other platform sources */
|
|
|
|
|
|
{
|
|
|
|
|
|
/* When all else fails, use this (rather weak) fallback */
|
|
|
|
|
|
SCM random_state = random_state_of_last_resort ();
|
|
|
|
|
|
int i;
|
|
|
|
|
|
for (i = len-1; i >= 0; --i)
|
|
|
|
|
|
buf[i] = scm_to_int (scm_random (SCM_I_MAKINUM (256), random_state));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SCM_DEFINE (scm_random_state_from_platform, "random-state-from-platform", 0, 0, 0,
|
|
|
|
|
|
(void),
|
|
|
|
|
|
"Construct a new random state seeded from a platform-specific\n\
|
|
|
|
|
|
source of entropy, appropriate for use in non-security-critical applications.")
|
|
|
|
|
|
#define FUNC_NAME s_scm_random_state_from_platform
|
|
|
|
|
|
{
|
|
|
|
|
|
unsigned char buf[32];
|
|
|
|
|
|
if (read_dev_urandom (buf, sizeof(buf)))
|
|
|
|
|
|
return make_rstate (scm_c_make_rstate ((char *) buf, sizeof(buf)));
|
|
|
|
|
|
else
|
|
|
|
|
|
return random_state_of_last_resort ();
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
1999-01-10 07:57:27 +00:00
|
|
|
|
void
|
|
|
|
|
|
scm_init_random ()
|
|
|
|
|
|
{
|
|
|
|
|
|
int i, m;
|
|
|
|
|
|
/* plug in default RNG */
|
2001-06-14 19:50:43 +00:00
|
|
|
|
scm_t_rng rng =
|
1999-01-10 07:57:27 +00:00
|
|
|
|
{
|
2001-06-14 19:50:43 +00:00
|
|
|
|
sizeof (scm_t_i_rstate),
|
2010-07-26 14:57:46 +02:00
|
|
|
|
scm_i_uniform32,
|
|
|
|
|
|
scm_i_init_rstate,
|
|
|
|
|
|
scm_i_copy_rstate,
|
|
|
|
|
|
scm_i_rstate_from_datum,
|
|
|
|
|
|
scm_i_rstate_to_datum
|
1999-01-10 07:57:27 +00:00
|
|
|
|
};
|
|
|
|
|
|
scm_the_rng = rng;
|
2017-10-31 13:28:44 +01:00
|
|
|
|
|
2000-12-08 17:32:56 +00:00
|
|
|
|
scm_tc16_rstate = scm_make_smob_type ("random-state", 0);
|
1999-01-10 07:57:27 +00:00
|
|
|
|
|
|
|
|
|
|
for (m = 1; m <= 0x100; m <<= 1)
|
|
|
|
|
|
for (i = m >> 1; i < m; ++i)
|
|
|
|
|
|
scm_masktab[i] = m - 1;
|
2017-10-31 13:28:44 +01:00
|
|
|
|
|
2018-06-20 17:19:31 +02:00
|
|
|
|
#include "random.x"
|
1999-01-10 07:57:27 +00:00
|
|
|
|
|
|
|
|
|
|
scm_add_feature ("random");
|
|
|
|
|
|
}
|