| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* num_rand.d - random numbers |
| 6 |
* |
| 7 |
* Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya |
| 8 |
* Copyright (c) 1990 Giuseppe Attardi |
| 9 |
* Copyright (c) 2001 Juan Jose Garcia Ripoll |
| 10 |
* Copyright (c) 2016 Daniel Kochmański |
| 11 |
* |
| 12 |
* See file 'LICENSE' for the copyright details. |
| 13 |
* |
| 14 |
*/ |
| 15 |
|
| 16 |
#include <ecl/ecl.h> |
| 17 |
#include <time.h> |
| 18 |
#include <stdio.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <ecl/internal.h> |
| 21 |
#include <fcntl.h> |
| 22 |
#if !defined(ECL_MS_WINDOWS_HOST) |
| 23 |
# include <unistd.h> |
| 24 |
#endif |
| 25 |
#if !defined(_MSC_VER) && !defined(__MINGW32__) |
| 26 |
# include <sys/stat.h> |
| 27 |
/* it isn't pulled in by fcntl.h */ |
| 28 |
#endif |
| 29 |
#ifdef _MSC_VER |
| 30 |
#include <stdint.h> |
| 31 |
#endif |
| 32 |
|
| 33 |
/* |
| 34 |
* Mersenne-Twister random number generator |
| 35 |
*/ |
| 36 |
|
| 37 |
#if ECL_FIXNUM_BITS > 32 |
| 38 |
/* |
| 39 |
* 64 bit version |
| 40 |
*/ |
| 41 |
|
| 42 |
/* Period parameters */ |
| 43 |
#define MT_N 312 |
| 44 |
#define MT_M 156 |
| 45 |
#define MATRIX_A 0xB5026F5AA96619E9ULL /* constant vector a */ |
| 46 |
#define UPPER_MASK 0xFFFFFFFF80000000ULL /* most significant 33 bits */ |
| 47 |
#define LOWER_MASK 0x7FFFFFFFULL /* least significant 31 bits */ |
| 48 |
#define ulong uint64_t |
| 49 |
|
| 50 |
cl_object |
| 51 |
init_genrand(ulong seed) |
| 52 |
{ |
| 53 |
int j; |
| 54 |
cl_object array = ecl_alloc_simple_vector((MT_N + 1), ecl_aet_b64); |
| 55 |
ulong *mt = array->vector.self.b64; |
| 56 |
mt[0] = seed; |
| 57 |
for (j=1; j<MT_N; j++) |
| 58 |
mt[j] = (6364136223846793005ULL * (mt[j-1] ^ (mt[j-1] >> 62)) + j); |
| 59 |
|
| 60 |
mt[MT_N] = MT_N+1; |
| 61 |
return array; |
| 62 |
} |
| 63 |
|
| 64 |
static ulong |
| 65 |
generate_int64(cl_object state) |
| 66 |
{ |
| 67 |
static ulong mag01[2]={0x0UL, MATRIX_A}; |
| 68 |
ulong y; |
| 69 |
ulong *mt = state->vector.self.b64; |
| 70 |
|
| 71 |
if (mt[MT_N] >= MT_N) { |
| 72 |
/* refresh data */ |
| 73 |
int kk; |
| 74 |
for (kk=0; kk < (MT_N - MT_M); kk++) { |
| 75 |
y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); |
| 76 |
mt[kk] = mt[kk + MT_M] ^ (y >> 1) ^ mag01[y & 0x1ULL]; |
| 77 |
} |
| 78 |
for (; kk < (MT_N - 1); kk++) { |
| 79 |
y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); |
| 80 |
mt[kk] = mt[kk+(MT_M-MT_N)] ^ (y >> 1) ^ mag01[y & 0x1ULL]; |
| 81 |
} |
| 82 |
y = (mt[MT_N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK); |
| 83 |
mt[MT_N-1] = mt[MT_M-1] ^ (y >> 1) ^ mag01[y & 0x1ULL]; |
| 84 |
mt[MT_N] = 0; |
| 85 |
} |
| 86 |
/* get random 64 bit num */ |
| 87 |
y = mt[mt[MT_N]++]; |
| 88 |
/* Tempering */ |
| 89 |
y ^= (y >> 29) & 0x5555555555555555ULL; |
| 90 |
y ^= (y << 17) & 0x71D67FFFEDA60000ULL; |
| 91 |
y ^= (y << 37) & 0xFFF7EEE000000000ULL; |
| 92 |
y ^= (y >> 43); |
| 93 |
|
| 94 |
return y; |
| 95 |
} |
| 96 |
|
| 97 |
static double |
| 98 |
generate_double(cl_object state) |
| 99 |
{ |
| 100 |
return (generate_int64(state) >> 11) * (1.0 / 9007199254740991.0); |
| 101 |
} |
| 102 |
|
| 103 |
static ecl_limb_t |
| 104 |
generate_limb(cl_object state) |
| 105 |
{ |
| 106 |
#if ECL_BIGNUM_LIMB_BITS <= 32 |
| 107 |
return generate_int64(state); |
| 108 |
#else |
| 109 |
# if ECL_BIGNUM_LIMB_BITS <= 64 |
| 110 |
return generate_int64(state); |
| 111 |
# else |
| 112 |
# if ECL_BIGNUM_LIMB_BITS <= 128 |
| 113 |
ecl_limb_t high = generate_int64(state); |
| 114 |
return (high << 64) | generate_int64(state); |
| 115 |
# endif |
| 116 |
# endif |
| 117 |
#endif |
| 118 |
} |
| 119 |
#else |
| 120 |
/* |
| 121 |
* 32 bit version |
| 122 |
*/ |
| 123 |
|
| 124 |
/* Period parameters */ |
| 125 |
#define MT_N 624 |
| 126 |
#define MT_M 397 |
| 127 |
#define MATRIX_A 0x9908b0dfUL /* constant vector a */ |
| 128 |
#define UPPER_MASK 0x80000000UL /* most significant w-r bits */ |
| 129 |
#define LOWER_MASK 0x7fffffffUL /* least significant r bits */ |
| 130 |
#define ulong uint32_t |
| 131 |
|
| 132 |
cl_object |
| 133 |
init_genrand(ulong seed) |
| 134 |
{ |
| 135 |
cl_object array = ecl_alloc_simple_vector((MT_N + 1), ecl_aet_b32); |
| 136 |
ulong *mt = array->vector.self.b32; |
| 137 |
int j; |
| 138 |
mt[0] = seed; |
| 139 |
for (j=1; j < MT_N; j++) |
| 140 |
mt[j] = (1812433253UL * (mt[j-1] ^ (mt[j-1] >> 30)) + j); |
| 141 |
|
| 142 |
mt[MT_N] = MT_N+1; |
| 143 |
return array; |
| 144 |
} |
| 145 |
|
| 146 |
static ulong |
| 147 |
generate_int32(cl_object state) |
| 148 |
{ |
| 149 |
static ulong mag01[2]={0x0UL, MATRIX_A}; |
| 150 |
ulong y; |
| 151 |
ulong *mt = state->vector.self.b32; |
| 152 |
if (mt[MT_N] >= MT_N) { |
| 153 |
/* refresh data */ |
| 154 |
int kk; |
| 155 |
for (kk=0; kk < (MT_N - MT_M); kk++) { |
| 156 |
y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); |
| 157 |
mt[kk] = mt[kk + MT_M] ^ (y >> 1) ^ mag01[y & 0x1UL]; |
| 158 |
} |
| 159 |
for (; kk < (MT_N - 1); kk++) { |
| 160 |
y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); |
| 161 |
mt[kk] = mt[kk+(MT_M-MT_N)] ^ (y >> 1) ^ mag01[y & 0x1UL]; |
| 162 |
} |
| 163 |
y = (mt[MT_N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK); |
| 164 |
mt[MT_N-1] = mt[MT_M-1] ^ (y >> 1) ^ mag01[y & 0x1UL]; |
| 165 |
mt[MT_N] = 0; |
| 166 |
} |
| 167 |
/* get random 32 bit num */ |
| 168 |
y = mt[mt[MT_N]++]; |
| 169 |
/* Tempering */ |
| 170 |
y ^= (y >> 11); |
| 171 |
y ^= (y << 7) & 0x9d2c5680UL; |
| 172 |
y ^= (y << 15) & 0xefc60000UL; |
| 173 |
y ^= (y >> 18); |
| 174 |
return y; |
| 175 |
} |
| 176 |
|
| 177 |
static double |
| 178 |
generate_double(cl_object state) |
| 179 |
{ |
| 180 |
return generate_int32(state) * (1.0 / 4294967296.0); |
| 181 |
} |
| 182 |
|
| 183 |
static ecl_limb_t |
| 184 |
generate_limb(cl_object state) |
| 185 |
{ |
| 186 |
#if ECL_BIGNUM_LIMB_BITS <= 32 |
| 187 |
return generate_int32(state); |
| 188 |
#else |
| 189 |
# if ECL_BIGNUM_LIMB_BITS <= 64 |
| 190 |
ecl_limb_t high = generate_int32(state); |
| 191 |
return (high << 32) | generate_int32(state); |
| 192 |
# else |
| 193 |
# if ECL_BIGNUM_LIMB_BITS <= 128 |
| 194 |
ecl_limb_t word0 = generate_int32(state); |
| 195 |
ecl_limb_t word1 = generate_int32(state); |
| 196 |
ecl_limb_t word2 = generate_int32(state); |
| 197 |
ecl_limb_t word3 = generate_int32(state); |
| 198 |
return (word3 << 96) | (word3 << 64) | (word1 << 32) || word0; |
| 199 |
# endif |
| 200 |
# endif |
| 201 |
#endif |
| 202 |
} |
| 203 |
#endif |
| 204 |
|
| 205 |
cl_object |
| 206 |
init_random_state(void) |
| 207 |
{ |
| 208 |
ulong seed; |
| 209 |
#if !defined(ECL_MS_WINDOWS_HOST) |
| 210 |
/* fopen() might read full 4kB blocks and discard |
| 211 |
* a lot of entropy, so use open() */ |
| 212 |
int file_handler = open("/dev/urandom", O_RDONLY); |
| 213 |
if (file_handler != -1) { |
| 214 |
read(file_handler, &seed, sizeof(ulong)); |
| 215 |
close(file_handler); |
| 216 |
} else |
| 217 |
#endif |
| 218 |
{ |
| 219 |
/* cant get urandom, use crappy source */ |
| 220 |
/* and/or fill rest of area */ |
| 221 |
seed = (rand() + time(0)); |
| 222 |
} |
| 223 |
|
| 224 |
return init_genrand(seed); |
| 225 |
} |
| 226 |
|
| 227 |
static cl_object |
| 228 |
random_integer(cl_object limit, cl_object state) |
| 229 |
{ |
| 230 |
cl_index bit_length = ecl_integer_length(limit); |
| 231 |
cl_object buffer; |
| 232 |
if (bit_length <= ECL_FIXNUM_BITS) |
| 233 |
bit_length = ECL_FIXNUM_BITS; |
| 234 |
buffer = ecl_ash(ecl_make_fixnum(1), bit_length); |
| 235 |
for (bit_length = ECL_BIGNUM_USIZE(buffer); bit_length; ) { |
| 236 |
ECL_BIGNUM_LIMBS(buffer)[--bit_length] = generate_limb(state); |
| 237 |
} |
| 238 |
return cl_mod(buffer, limit); |
| 239 |
} |
| 240 |
|
| 241 |
static cl_object |
| 242 |
rando(cl_object x, cl_object rs) |
| 243 |
{ |
| 244 |
cl_object z; |
| 245 |
if (!ecl_plusp(x)) { |
| 246 |
goto ERROR; |
| 247 |
} |
| 248 |
switch (ecl_t_of(x)) { |
| 249 |
case t_fixnum: |
| 250 |
#if ECL_FIXNUM_BITS <= 32 |
| 251 |
z = ecl_make_fixnum(generate_int32(rs->random.value) % ecl_fixnum(x)); |
| 252 |
break; |
| 253 |
#endif |
| 254 |
case t_bignum: |
| 255 |
z = random_integer(x, rs->random.value); |
| 256 |
break; |
| 257 |
case t_singlefloat: |
| 258 |
z = ecl_make_single_float(ecl_single_float(x) * |
| 259 |
(float)generate_double(rs->random.value)); |
| 260 |
break; |
| 261 |
case t_doublefloat: |
| 262 |
z = ecl_make_double_float(ecl_double_float(x) * |
| 263 |
generate_double(rs->random.value)); |
| 264 |
break; |
| 265 |
case t_longfloat: |
| 266 |
z = ecl_make_long_float(ecl_long_float(x) * |
| 267 |
(long double)generate_double(rs->random.value)); |
| 268 |
break; |
| 269 |
default: ERROR: { |
| 270 |
const char *type = "(OR (INTEGER (0) *) (FLOAT (0) *))"; |
| 271 |
FEwrong_type_nth_arg(@[random],1,x, ecl_read_from_cstring(type)); |
| 272 |
} |
| 273 |
} |
| 274 |
return z; |
| 275 |
} |
| 276 |
|
| 277 |
cl_object |
| 278 |
ecl_make_random_state(cl_object rs) |
| 279 |
{ |
| 280 |
cl_object z = ecl_alloc_object(t_random); |
| 281 |
const char *type |
| 282 |
= "(OR RANDOM-STATE FIXNUM (MEMBER T NIL))"; |
| 283 |
|
| 284 |
if (rs == ECL_T) { |
| 285 |
z->random.value = init_random_state(); |
| 286 |
return z; |
| 287 |
} else if (Null(rs)) { |
| 288 |
rs = ecl_symbol_value(@'*random-state*'); |
| 289 |
z->random.value = cl_copy_seq(rs->random.value); |
| 290 |
return z; |
| 291 |
} |
| 292 |
|
| 293 |
switch (ecl_t_of(rs)) { |
| 294 |
case t_random: |
| 295 |
z->random.value = cl_copy_seq(rs->random.value); |
| 296 |
break; |
| 297 |
case t_fixnum: |
| 298 |
z->random.value = init_genrand(ecl_fixnum(rs)); |
| 299 |
break; |
| 300 |
case t_vector: /* intentionaly undocumented (only for internal use) */ |
| 301 |
#if ECL_FIXNUM_BITS > 32 |
| 302 |
if (rs->vector.dim == 313 && rs->vector.elttype == ecl_aet_b64) { |
| 303 |
z = ecl_alloc_object(t_random); |
| 304 |
z->random.value = cl_copy_seq(rs); |
| 305 |
break; |
| 306 |
} |
| 307 |
#else /* 32 bit version */ |
| 308 |
if (rs->vector.dim == 625 && rs->vector.elttype == ecl_aet_b32) { |
| 309 |
z = ecl_alloc_object(t_random); |
| 310 |
z->random.value = cl_copy_seq(rs); |
| 311 |
break; |
| 312 |
} |
| 313 |
#endif |
| 314 |
default: |
| 315 |
FEwrong_type_only_arg(@[make-random-state], rs, |
| 316 |
ecl_read_from_cstring(type)); |
| 317 |
} |
| 318 |
|
| 319 |
return z; |
| 320 |
} |
| 321 |
|
| 322 |
@(defun random (x &optional (rs ecl_cmp_symbol_value(the_env, @'*random-state*'))) |
| 323 |
@ |
| 324 |
rs = ecl_check_cl_type(@'random', rs, t_random); |
| 325 |
@(return rando(x, rs)); |
| 326 |
@) |
| 327 |
|
| 328 |
@(defun make_random_state (&optional (rs ECL_NIL)) |
| 329 |
@ |
| 330 |
@(return ecl_make_random_state(rs)); |
| 331 |
@) |
| 332 |
|
| 333 |
cl_object |
| 334 |
cl_random_state_p(cl_object x) |
| 335 |
{ |
| 336 |
@(return (ECL_RANDOM_STATE_P(x) ? ECL_T : ECL_NIL)); |
| 337 |
} |
| 338 |
|
| 339 |
cl_object |
| 340 |
si_random_state_array(cl_object rs) { |
| 341 |
ecl_check_cl_type(@'ext::random-state-array', rs, t_random); |
| 342 |
return rs->random.value; |
| 343 |
} |