annotate src/SFMT/SFMT.c @ 198:5055ec9de278

competed move-table printing component of pokemon printing function.
author Robert McIntyre <rlm@mit.edu>
date Fri, 23 Mar 2012 00:30:34 -0500
parents f9f4f1b99eed
children
rev   line source
rlm@1 1 /**
rlm@1 2 * @file SFMT.c
rlm@1 3 * @brief SIMD oriented Fast Mersenne Twister(SFMT)
rlm@1 4 *
rlm@1 5 * @author Mutsuo Saito (Hiroshima University)
rlm@1 6 * @author Makoto Matsumoto (Hiroshima University)
rlm@1 7 *
rlm@1 8 * Copyright (C) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima
rlm@1 9 * University. All rights reserved.
rlm@1 10 *
rlm@1 11 * The new BSD License is applied to this software, see LICENSE.txt
rlm@1 12 */
rlm@1 13 #include <string.h>
rlm@1 14 #include <assert.h>
rlm@1 15 #include "SFMT.h"
rlm@1 16 #include "SFMT-params.h"
rlm@1 17
rlm@1 18 #if defined(__BIG_ENDIAN__) && !defined(__amd64) && !defined(BIG_ENDIAN64)
rlm@1 19 #define BIG_ENDIAN64 1
rlm@1 20 #endif
rlm@1 21 #if defined(HAVE_ALTIVEC) && !defined(BIG_ENDIAN64)
rlm@1 22 #define BIG_ENDIAN64 1
rlm@1 23 #endif
rlm@1 24 #if defined(ONLY64) && !defined(BIG_ENDIAN64)
rlm@1 25 #if defined(__GNUC__)
rlm@1 26 #error "-DONLY64 must be specified with -DBIG_ENDIAN64"
rlm@1 27 #endif
rlm@1 28 #undef ONLY64
rlm@1 29 #endif
rlm@1 30 /*------------------------------------------------------
rlm@1 31 128-bit SIMD data type for Altivec, SSE2 or standard C
rlm@1 32 ------------------------------------------------------*/
rlm@1 33 #if defined(HAVE_ALTIVEC)
rlm@1 34 #if !defined(__APPLE__)
rlm@1 35 #include <altivec.h>
rlm@1 36 #endif
rlm@1 37 /** 128-bit data structure */
rlm@1 38 union W128_T {
rlm@1 39 vector unsigned int s;
rlm@1 40 uint32_t u[4];
rlm@1 41 };
rlm@1 42 /** 128-bit data type */
rlm@1 43 typedef union W128_T w128_t;
rlm@1 44
rlm@1 45 #elif defined(HAVE_SSE2)
rlm@1 46 #include <emmintrin.h>
rlm@1 47
rlm@1 48 /** 128-bit data structure */
rlm@1 49 union W128_T {
rlm@1 50 __m128i si;
rlm@1 51 uint32_t u[4];
rlm@1 52 };
rlm@1 53 /** 128-bit data type */
rlm@1 54 typedef union W128_T w128_t;
rlm@1 55
rlm@1 56 #else
rlm@1 57
rlm@1 58 /** 128-bit data structure */
rlm@1 59 struct W128_T {
rlm@1 60 uint32_t u[4];
rlm@1 61 };
rlm@1 62 /** 128-bit data type */
rlm@1 63 typedef struct W128_T w128_t;
rlm@1 64
rlm@1 65 #endif
rlm@1 66
rlm@1 67 /*--------------------------------------
rlm@1 68 FILE GLOBAL VARIABLES
rlm@1 69 internal state, index counter and flag
rlm@1 70 --------------------------------------*/
rlm@1 71 /** the 128-bit internal state array */
rlm@1 72 static w128_t sfmt[N];
rlm@1 73 /** the 32bit integer pointer to the 128-bit internal state array */
rlm@1 74 static uint32_t *psfmt32 = &sfmt[0].u[0];
rlm@1 75 #if !defined(BIG_ENDIAN64) || defined(ONLY64)
rlm@1 76 /** the 64bit integer pointer to the 128-bit internal state array */
rlm@1 77 static uint64_t *psfmt64 = (uint64_t *)&sfmt[0].u[0];
rlm@1 78 #endif
rlm@1 79 /** index counter to the 32-bit internal state array */
rlm@1 80 static int idx;
rlm@1 81 /** a flag: it is 0 if and only if the internal state is not yet
rlm@1 82 * initialized. */
rlm@1 83 static int initialized = 0;
rlm@1 84 /** a parity check vector which certificate the period of 2^{MEXP} */
rlm@1 85 static uint32_t parity[4] = {PARITY1, PARITY2, PARITY3, PARITY4};
rlm@1 86
rlm@1 87 /*----------------
rlm@1 88 STATIC FUNCTIONS
rlm@1 89 ----------------*/
rlm@1 90 inline static int idxof(int i);
rlm@1 91 inline static void rshift128(w128_t *out, w128_t const *in, int shift);
rlm@1 92 inline static void lshift128(w128_t *out, w128_t const *in, int shift);
rlm@1 93 inline static void gen_rand_all(void);
rlm@1 94 inline static void gen_rand_array(w128_t *array, int size);
rlm@1 95 inline static uint32_t func1(uint32_t x);
rlm@1 96 inline static uint32_t func2(uint32_t x);
rlm@1 97 static void period_certification(void);
rlm@1 98 #if defined(BIG_ENDIAN64) && !defined(ONLY64)
rlm@1 99 inline static void swap(w128_t *array, int size);
rlm@1 100 #endif
rlm@1 101
rlm@1 102 #if defined(HAVE_ALTIVEC)
rlm@1 103 #include "SFMT-alti.h"
rlm@1 104 #elif defined(HAVE_SSE2)
rlm@1 105 #include "SFMT-sse2.h"
rlm@1 106 #endif
rlm@1 107
rlm@1 108 /**
rlm@1 109 * This function simulate a 64-bit index of LITTLE ENDIAN
rlm@1 110 * in BIG ENDIAN machine.
rlm@1 111 */
rlm@1 112 #ifdef ONLY64
rlm@1 113 inline static int idxof(int i) {
rlm@1 114 return i ^ 1;
rlm@1 115 }
rlm@1 116 #else
rlm@1 117 inline static int idxof(int i) {
rlm@1 118 return i;
rlm@1 119 }
rlm@1 120 #endif
rlm@1 121 /**
rlm@1 122 * This function simulates SIMD 128-bit right shift by the standard C.
rlm@1 123 * The 128-bit integer given in in is shifted by (shift * 8) bits.
rlm@1 124 * This function simulates the LITTLE ENDIAN SIMD.
rlm@1 125 * @param out the output of this function
rlm@1 126 * @param in the 128-bit data to be shifted
rlm@1 127 * @param shift the shift value
rlm@1 128 */
rlm@1 129 #ifdef ONLY64
rlm@1 130 inline static void rshift128(w128_t *out, w128_t const *in, int shift) {
rlm@1 131 uint64_t th, tl, oh, ol;
rlm@1 132
rlm@1 133 th = ((uint64_t)in->u[2] << 32) | ((uint64_t)in->u[3]);
rlm@1 134 tl = ((uint64_t)in->u[0] << 32) | ((uint64_t)in->u[1]);
rlm@1 135
rlm@1 136 oh = th >> (shift * 8);
rlm@1 137 ol = tl >> (shift * 8);
rlm@1 138 ol |= th << (64 - shift * 8);
rlm@1 139 out->u[0] = (uint32_t)(ol >> 32);
rlm@1 140 out->u[1] = (uint32_t)(ol & 0xffffffff);
rlm@1 141 out->u[2] = (uint32_t)(oh >> 32);
rlm@1 142 out->u[3] = (uint32_t)(oh & 0xffffffff);
rlm@1 143 }
rlm@1 144 #else
rlm@1 145 inline static void rshift128(w128_t *out, w128_t const *in, int shift) {
rlm@1 146 uint64_t th, tl, oh, ol;
rlm@1 147
rlm@1 148 th = ((uint64_t)in->u[3] << 32) | ((uint64_t)in->u[2]);
rlm@1 149 tl = ((uint64_t)in->u[1] << 32) | ((uint64_t)in->u[0]);
rlm@1 150
rlm@1 151 oh = th >> (shift * 8);
rlm@1 152 ol = tl >> (shift * 8);
rlm@1 153 ol |= th << (64 - shift * 8);
rlm@1 154 out->u[1] = (uint32_t)(ol >> 32);
rlm@1 155 out->u[0] = (uint32_t)(ol & 0xffffffff);
rlm@1 156 out->u[3] = (uint32_t)(oh >> 32);
rlm@1 157 out->u[2] = (uint32_t)(oh & 0xffffffff);
rlm@1 158 }
rlm@1 159 #endif
rlm@1 160 /**
rlm@1 161 * This function simulates SIMD 128-bit left shift by the standard C.
rlm@1 162 * The 128-bit integer given in in is shifted by (shift * 8) bits.
rlm@1 163 * This function simulates the LITTLE ENDIAN SIMD.
rlm@1 164 * @param out the output of this function
rlm@1 165 * @param in the 128-bit data to be shifted
rlm@1 166 * @param shift the shift value
rlm@1 167 */
rlm@1 168 #ifdef ONLY64
rlm@1 169 inline static void lshift128(w128_t *out, w128_t const *in, int shift) {
rlm@1 170 uint64_t th, tl, oh, ol;
rlm@1 171
rlm@1 172 th = ((uint64_t)in->u[2] << 32) | ((uint64_t)in->u[3]);
rlm@1 173 tl = ((uint64_t)in->u[0] << 32) | ((uint64_t)in->u[1]);
rlm@1 174
rlm@1 175 oh = th << (shift * 8);
rlm@1 176 ol = tl << (shift * 8);
rlm@1 177 oh |= tl >> (64 - shift * 8);
rlm@1 178 out->u[0] = (uint32_t)(ol >> 32);
rlm@1 179 out->u[1] = (uint32_t)(ol & 0xffffffff);
rlm@1 180 out->u[2] = (uint32_t)(oh >> 32);
rlm@1 181 out->u[3] = (uint32_t)(oh & 0xffffffff);
rlm@1 182 }
rlm@1 183 #else
rlm@1 184 inline static void lshift128(w128_t *out, w128_t const *in, int shift) {
rlm@1 185 uint64_t th, tl, oh, ol;
rlm@1 186
rlm@1 187 th = ((uint64_t)in->u[3] << 32) | ((uint64_t)in->u[2]);
rlm@1 188 tl = ((uint64_t)in->u[1] << 32) | ((uint64_t)in->u[0]);
rlm@1 189
rlm@1 190 oh = th << (shift * 8);
rlm@1 191 ol = tl << (shift * 8);
rlm@1 192 oh |= tl >> (64 - shift * 8);
rlm@1 193 out->u[1] = (uint32_t)(ol >> 32);
rlm@1 194 out->u[0] = (uint32_t)(ol & 0xffffffff);
rlm@1 195 out->u[3] = (uint32_t)(oh >> 32);
rlm@1 196 out->u[2] = (uint32_t)(oh & 0xffffffff);
rlm@1 197 }
rlm@1 198 #endif
rlm@1 199
rlm@1 200 /**
rlm@1 201 * This function represents the recursion formula.
rlm@1 202 * @param r output
rlm@1 203 * @param a a 128-bit part of the internal state array
rlm@1 204 * @param b a 128-bit part of the internal state array
rlm@1 205 * @param c a 128-bit part of the internal state array
rlm@1 206 * @param d a 128-bit part of the internal state array
rlm@1 207 */
rlm@1 208 #if (!defined(HAVE_ALTIVEC)) && (!defined(HAVE_SSE2))
rlm@1 209 #ifdef ONLY64
rlm@1 210 inline static void do_recursion(w128_t *r, w128_t *a, w128_t *b, w128_t *c,
rlm@1 211 w128_t *d) {
rlm@1 212 w128_t x;
rlm@1 213 w128_t y;
rlm@1 214
rlm@1 215 lshift128(&x, a, SL2);
rlm@1 216 rshift128(&y, c, SR2);
rlm@1 217 r->u[0] = a->u[0] ^ x.u[0] ^ ((b->u[0] >> SR1) & MSK2) ^ y.u[0]
rlm@1 218 ^ (d->u[0] << SL1);
rlm@1 219 r->u[1] = a->u[1] ^ x.u[1] ^ ((b->u[1] >> SR1) & MSK1) ^ y.u[1]
rlm@1 220 ^ (d->u[1] << SL1);
rlm@1 221 r->u[2] = a->u[2] ^ x.u[2] ^ ((b->u[2] >> SR1) & MSK4) ^ y.u[2]
rlm@1 222 ^ (d->u[2] << SL1);
rlm@1 223 r->u[3] = a->u[3] ^ x.u[3] ^ ((b->u[3] >> SR1) & MSK3) ^ y.u[3]
rlm@1 224 ^ (d->u[3] << SL1);
rlm@1 225 }
rlm@1 226 #else
rlm@1 227 inline static void do_recursion(w128_t *r, w128_t *a, w128_t *b, w128_t *c,
rlm@1 228 w128_t *d) {
rlm@1 229 w128_t x;
rlm@1 230 w128_t y;
rlm@1 231
rlm@1 232 lshift128(&x, a, SL2);
rlm@1 233 rshift128(&y, c, SR2);
rlm@1 234 r->u[0] = a->u[0] ^ x.u[0] ^ ((b->u[0] >> SR1) & MSK1) ^ y.u[0]
rlm@1 235 ^ (d->u[0] << SL1);
rlm@1 236 r->u[1] = a->u[1] ^ x.u[1] ^ ((b->u[1] >> SR1) & MSK2) ^ y.u[1]
rlm@1 237 ^ (d->u[1] << SL1);
rlm@1 238 r->u[2] = a->u[2] ^ x.u[2] ^ ((b->u[2] >> SR1) & MSK3) ^ y.u[2]
rlm@1 239 ^ (d->u[2] << SL1);
rlm@1 240 r->u[3] = a->u[3] ^ x.u[3] ^ ((b->u[3] >> SR1) & MSK4) ^ y.u[3]
rlm@1 241 ^ (d->u[3] << SL1);
rlm@1 242 }
rlm@1 243 #endif
rlm@1 244 #endif
rlm@1 245
rlm@1 246 #if (!defined(HAVE_ALTIVEC)) && (!defined(HAVE_SSE2))
rlm@1 247 /**
rlm@1 248 * This function fills the internal state array with pseudorandom
rlm@1 249 * integers.
rlm@1 250 */
rlm@1 251 inline static void gen_rand_all(void) {
rlm@1 252 int i;
rlm@1 253 w128_t *r1, *r2;
rlm@1 254
rlm@1 255 r1 = &sfmt[N - 2];
rlm@1 256 r2 = &sfmt[N - 1];
rlm@1 257 for (i = 0; i < N - POS1; i++) {
rlm@1 258 do_recursion(&sfmt[i], &sfmt[i], &sfmt[i + POS1], r1, r2);
rlm@1 259 r1 = r2;
rlm@1 260 r2 = &sfmt[i];
rlm@1 261 }
rlm@1 262 for (; i < N; i++) {
rlm@1 263 do_recursion(&sfmt[i], &sfmt[i], &sfmt[i + POS1 - N], r1, r2);
rlm@1 264 r1 = r2;
rlm@1 265 r2 = &sfmt[i];
rlm@1 266 }
rlm@1 267 }
rlm@1 268
rlm@1 269 /**
rlm@1 270 * This function fills the user-specified array with pseudorandom
rlm@1 271 * integers.
rlm@1 272 *
rlm@1 273 * @param array an 128-bit array to be filled by pseudorandom numbers.
rlm@1 274 * @param size number of 128-bit pseudorandom numbers to be generated.
rlm@1 275 */
rlm@1 276 inline static void gen_rand_array(w128_t *array, int size) {
rlm@1 277 int i, j;
rlm@1 278 w128_t *r1, *r2;
rlm@1 279
rlm@1 280 r1 = &sfmt[N - 2];
rlm@1 281 r2 = &sfmt[N - 1];
rlm@1 282 for (i = 0; i < N - POS1; i++) {
rlm@1 283 do_recursion(&array[i], &sfmt[i], &sfmt[i + POS1], r1, r2);
rlm@1 284 r1 = r2;
rlm@1 285 r2 = &array[i];
rlm@1 286 }
rlm@1 287 for (; i < N; i++) {
rlm@1 288 do_recursion(&array[i], &sfmt[i], &array[i + POS1 - N], r1, r2);
rlm@1 289 r1 = r2;
rlm@1 290 r2 = &array[i];
rlm@1 291 }
rlm@1 292 for (; i < size - N; i++) {
rlm@1 293 do_recursion(&array[i], &array[i - N], &array[i + POS1 - N], r1, r2);
rlm@1 294 r1 = r2;
rlm@1 295 r2 = &array[i];
rlm@1 296 }
rlm@1 297 for (j = 0; j < 2 * N - size; j++) {
rlm@1 298 sfmt[j] = array[j + size - N];
rlm@1 299 }
rlm@1 300 for (; i < size; i++, j++) {
rlm@1 301 do_recursion(&array[i], &array[i - N], &array[i + POS1 - N], r1, r2);
rlm@1 302 r1 = r2;
rlm@1 303 r2 = &array[i];
rlm@1 304 sfmt[j] = array[i];
rlm@1 305 }
rlm@1 306 }
rlm@1 307 #endif
rlm@1 308
rlm@1 309 #if defined(BIG_ENDIAN64) && !defined(ONLY64) && !defined(HAVE_ALTIVEC)
rlm@1 310 inline static void swap(w128_t *array, int size) {
rlm@1 311 int i;
rlm@1 312 uint32_t x, y;
rlm@1 313
rlm@1 314 for (i = 0; i < size; i++) {
rlm@1 315 x = array[i].u[0];
rlm@1 316 y = array[i].u[2];
rlm@1 317 array[i].u[0] = array[i].u[1];
rlm@1 318 array[i].u[2] = array[i].u[3];
rlm@1 319 array[i].u[1] = x;
rlm@1 320 array[i].u[3] = y;
rlm@1 321 }
rlm@1 322 }
rlm@1 323 #endif
rlm@1 324 /**
rlm@1 325 * This function represents a function used in the initialization
rlm@1 326 * by init_by_array
rlm@1 327 * @param x 32-bit integer
rlm@1 328 * @return 32-bit integer
rlm@1 329 */
rlm@1 330 static uint32_t func1(uint32_t x) {
rlm@1 331 return (x ^ (x >> 27)) * (uint32_t)1664525UL;
rlm@1 332 }
rlm@1 333
rlm@1 334 /**
rlm@1 335 * This function represents a function used in the initialization
rlm@1 336 * by init_by_array
rlm@1 337 * @param x 32-bit integer
rlm@1 338 * @return 32-bit integer
rlm@1 339 */
rlm@1 340 static uint32_t func2(uint32_t x) {
rlm@1 341 return (x ^ (x >> 27)) * (uint32_t)1566083941UL;
rlm@1 342 }
rlm@1 343
rlm@1 344 /**
rlm@1 345 * This function certificate the period of 2^{MEXP}
rlm@1 346 */
rlm@1 347 static void period_certification(void) {
rlm@1 348 int inner = 0;
rlm@1 349 int i, j;
rlm@1 350 uint32_t work;
rlm@1 351
rlm@1 352 for (i = 0; i < 4; i++)
rlm@1 353 inner ^= psfmt32[idxof(i)] & parity[i];
rlm@1 354 for (i = 16; i > 0; i >>= 1)
rlm@1 355 inner ^= inner >> i;
rlm@1 356 inner &= 1;
rlm@1 357 /* check OK */
rlm@1 358 if (inner == 1) {
rlm@1 359 return;
rlm@1 360 }
rlm@1 361 /* check NG, and modification */
rlm@1 362 for (i = 0; i < 4; i++) {
rlm@1 363 work = 1;
rlm@1 364 for (j = 0; j < 32; j++) {
rlm@1 365 if ((work & parity[i]) != 0) {
rlm@1 366 psfmt32[idxof(i)] ^= work;
rlm@1 367 return;
rlm@1 368 }
rlm@1 369 work = work << 1;
rlm@1 370 }
rlm@1 371 }
rlm@1 372 }
rlm@1 373
rlm@1 374 /*----------------
rlm@1 375 PUBLIC FUNCTIONS
rlm@1 376 ----------------*/
rlm@1 377 /**
rlm@1 378 * This function returns the identification string.
rlm@1 379 * The string shows the word size, the Mersenne exponent,
rlm@1 380 * and all parameters of this generator.
rlm@1 381 */
rlm@1 382 const char *get_idstring(void) {
rlm@1 383 return IDSTR;
rlm@1 384 }
rlm@1 385
rlm@1 386 /**
rlm@1 387 * This function returns the minimum size of array used for \b
rlm@1 388 * fill_array32() function.
rlm@1 389 * @return minimum size of array used for fill_array32() function.
rlm@1 390 */
rlm@1 391 int get_min_array_size32(void) {
rlm@1 392 return N32;
rlm@1 393 }
rlm@1 394
rlm@1 395 /**
rlm@1 396 * This function returns the minimum size of array used for \b
rlm@1 397 * fill_array64() function.
rlm@1 398 * @return minimum size of array used for fill_array64() function.
rlm@1 399 */
rlm@1 400 int get_min_array_size64(void) {
rlm@1 401 return N64;
rlm@1 402 }
rlm@1 403
rlm@1 404 #ifndef ONLY64
rlm@1 405 /**
rlm@1 406 * This function generates and returns 32-bit pseudorandom number.
rlm@1 407 * init_gen_rand or init_by_array must be called before this function.
rlm@1 408 * @return 32-bit pseudorandom number
rlm@1 409 */
rlm@1 410 uint32_t gen_rand32(void) {
rlm@1 411 uint32_t r;
rlm@1 412
rlm@1 413 assert(initialized);
rlm@1 414 if (idx >= N32) {
rlm@1 415 gen_rand_all();
rlm@1 416 idx = 0;
rlm@1 417 }
rlm@1 418 r = psfmt32[idx++];
rlm@1 419 return r;
rlm@1 420 }
rlm@1 421 #endif
rlm@1 422 /**
rlm@1 423 * This function generates and returns 64-bit pseudorandom number.
rlm@1 424 * init_gen_rand or init_by_array must be called before this function.
rlm@1 425 * The function gen_rand64 should not be called after gen_rand32,
rlm@1 426 * unless an initialization is again executed.
rlm@1 427 * @return 64-bit pseudorandom number
rlm@1 428 */
rlm@1 429 uint64_t gen_rand64(void) {
rlm@1 430 #if defined(BIG_ENDIAN64) && !defined(ONLY64)
rlm@1 431 uint32_t r1, r2;
rlm@1 432 #else
rlm@1 433 uint64_t r;
rlm@1 434 #endif
rlm@1 435
rlm@1 436 assert(initialized);
rlm@1 437 assert(idx % 2 == 0);
rlm@1 438
rlm@1 439 if (idx >= N32) {
rlm@1 440 gen_rand_all();
rlm@1 441 idx = 0;
rlm@1 442 }
rlm@1 443 #if defined(BIG_ENDIAN64) && !defined(ONLY64)
rlm@1 444 r1 = psfmt32[idx];
rlm@1 445 r2 = psfmt32[idx + 1];
rlm@1 446 idx += 2;
rlm@1 447 return ((uint64_t)r2 << 32) | r1;
rlm@1 448 #else
rlm@1 449 r = psfmt64[idx / 2];
rlm@1 450 idx += 2;
rlm@1 451 return r;
rlm@1 452 #endif
rlm@1 453 }
rlm@1 454
rlm@1 455 #ifndef ONLY64
rlm@1 456 /**
rlm@1 457 * This function generates pseudorandom 32-bit integers in the
rlm@1 458 * specified array[] by one call. The number of pseudorandom integers
rlm@1 459 * is specified by the argument size, which must be at least 624 and a
rlm@1 460 * multiple of four. The generation by this function is much faster
rlm@1 461 * than the following gen_rand function.
rlm@1 462 *
rlm@1 463 * For initialization, init_gen_rand or init_by_array must be called
rlm@1 464 * before the first call of this function. This function can not be
rlm@1 465 * used after calling gen_rand function, without initialization.
rlm@1 466 *
rlm@1 467 * @param array an array where pseudorandom 32-bit integers are filled
rlm@1 468 * by this function. The pointer to the array must be \b "aligned"
rlm@1 469 * (namely, must be a multiple of 16) in the SIMD version, since it
rlm@1 470 * refers to the address of a 128-bit integer. In the standard C
rlm@1 471 * version, the pointer is arbitrary.
rlm@1 472 *
rlm@1 473 * @param size the number of 32-bit pseudorandom integers to be
rlm@1 474 * generated. size must be a multiple of 4, and greater than or equal
rlm@1 475 * to (MEXP / 128 + 1) * 4.
rlm@1 476 *
rlm@1 477 * @note \b memalign or \b posix_memalign is available to get aligned
rlm@1 478 * memory. Mac OSX doesn't have these functions, but \b malloc of OSX
rlm@1 479 * returns the pointer to the aligned memory block.
rlm@1 480 */
rlm@1 481 void fill_array32(uint32_t *array, int size) {
rlm@1 482 assert(initialized);
rlm@1 483 assert(idx == N32);
rlm@1 484 assert(size % 4 == 0);
rlm@1 485 assert(size >= N32);
rlm@1 486
rlm@1 487 gen_rand_array((w128_t *)array, size / 4);
rlm@1 488 idx = N32;
rlm@1 489 }
rlm@1 490 #endif
rlm@1 491
rlm@1 492 /**
rlm@1 493 * This function generates pseudorandom 64-bit integers in the
rlm@1 494 * specified array[] by one call. The number of pseudorandom integers
rlm@1 495 * is specified by the argument size, which must be at least 312 and a
rlm@1 496 * multiple of two. The generation by this function is much faster
rlm@1 497 * than the following gen_rand function.
rlm@1 498 *
rlm@1 499 * For initialization, init_gen_rand or init_by_array must be called
rlm@1 500 * before the first call of this function. This function can not be
rlm@1 501 * used after calling gen_rand function, without initialization.
rlm@1 502 *
rlm@1 503 * @param array an array where pseudorandom 64-bit integers are filled
rlm@1 504 * by this function. The pointer to the array must be "aligned"
rlm@1 505 * (namely, must be a multiple of 16) in the SIMD version, since it
rlm@1 506 * refers to the address of a 128-bit integer. In the standard C
rlm@1 507 * version, the pointer is arbitrary.
rlm@1 508 *
rlm@1 509 * @param size the number of 64-bit pseudorandom integers to be
rlm@1 510 * generated. size must be a multiple of 2, and greater than or equal
rlm@1 511 * to (MEXP / 128 + 1) * 2
rlm@1 512 *
rlm@1 513 * @note \b memalign or \b posix_memalign is available to get aligned
rlm@1 514 * memory. Mac OSX doesn't have these functions, but \b malloc of OSX
rlm@1 515 * returns the pointer to the aligned memory block.
rlm@1 516 */
rlm@1 517 void fill_array64(uint64_t *array, int size) {
rlm@1 518 assert(initialized);
rlm@1 519 assert(idx == N32);
rlm@1 520 assert(size % 2 == 0);
rlm@1 521 assert(size >= N64);
rlm@1 522
rlm@1 523 gen_rand_array((w128_t *)array, size / 2);
rlm@1 524 idx = N32;
rlm@1 525
rlm@1 526 #if defined(BIG_ENDIAN64) && !defined(ONLY64)
rlm@1 527 swap((w128_t *)array, size /2);
rlm@1 528 #endif
rlm@1 529 }
rlm@1 530
rlm@1 531 /**
rlm@1 532 * This function initializes the internal state array with a 32-bit
rlm@1 533 * integer seed.
rlm@1 534 *
rlm@1 535 * @param seed a 32-bit integer used as the seed.
rlm@1 536 */
rlm@1 537 void init_gen_rand(uint32_t seed) {
rlm@1 538 int i;
rlm@1 539
rlm@1 540 psfmt32[idxof(0)] = seed;
rlm@1 541 for (i = 1; i < N32; i++) {
rlm@1 542 psfmt32[idxof(i)] = 1812433253UL * (psfmt32[idxof(i - 1)]
rlm@1 543 ^ (psfmt32[idxof(i - 1)] >> 30))
rlm@1 544 + i;
rlm@1 545 }
rlm@1 546 idx = N32;
rlm@1 547 period_certification();
rlm@1 548 initialized = 1;
rlm@1 549 }
rlm@1 550
rlm@1 551 /**
rlm@1 552 * This function initializes the internal state array,
rlm@1 553 * with an array of 32-bit integers used as the seeds
rlm@1 554 * @param init_key the array of 32-bit integers, used as a seed.
rlm@1 555 * @param key_length the length of init_key.
rlm@1 556 */
rlm@1 557 void init_by_array(uint32_t *init_key, int key_length) {
rlm@1 558 int i, j, count;
rlm@1 559 uint32_t r;
rlm@1 560 int lag;
rlm@1 561 int mid;
rlm@1 562 int size = N * 4;
rlm@1 563
rlm@1 564 if (size >= 623) {
rlm@1 565 lag = 11;
rlm@1 566 } else if (size >= 68) {
rlm@1 567 lag = 7;
rlm@1 568 } else if (size >= 39) {
rlm@1 569 lag = 5;
rlm@1 570 } else {
rlm@1 571 lag = 3;
rlm@1 572 }
rlm@1 573 mid = (size - lag) / 2;
rlm@1 574
rlm@1 575 memset(sfmt, 0x8b, sizeof(sfmt));
rlm@1 576 if (key_length + 1 > N32) {
rlm@1 577 count = key_length + 1;
rlm@1 578 } else {
rlm@1 579 count = N32;
rlm@1 580 }
rlm@1 581 r = func1(psfmt32[idxof(0)] ^ psfmt32[idxof(mid)]
rlm@1 582 ^ psfmt32[idxof(N32 - 1)]);
rlm@1 583 psfmt32[idxof(mid)] += r;
rlm@1 584 r += key_length;
rlm@1 585 psfmt32[idxof(mid + lag)] += r;
rlm@1 586 psfmt32[idxof(0)] = r;
rlm@1 587
rlm@1 588 count--;
rlm@1 589 for (i = 1, j = 0; (j < count) && (j < key_length); j++) {
rlm@1 590 r = func1(psfmt32[idxof(i)] ^ psfmt32[idxof((i + mid) % N32)]
rlm@1 591 ^ psfmt32[idxof((i + N32 - 1) % N32)]);
rlm@1 592 psfmt32[idxof((i + mid) % N32)] += r;
rlm@1 593 r += init_key[j] + i;
rlm@1 594 psfmt32[idxof((i + mid + lag) % N32)] += r;
rlm@1 595 psfmt32[idxof(i)] = r;
rlm@1 596 i = (i + 1) % N32;
rlm@1 597 }
rlm@1 598 for (; j < count; j++) {
rlm@1 599 r = func1(psfmt32[idxof(i)] ^ psfmt32[idxof((i + mid) % N32)]
rlm@1 600 ^ psfmt32[idxof((i + N32 - 1) % N32)]);
rlm@1 601 psfmt32[idxof((i + mid) % N32)] += r;
rlm@1 602 r += i;
rlm@1 603 psfmt32[idxof((i + mid + lag) % N32)] += r;
rlm@1 604 psfmt32[idxof(i)] = r;
rlm@1 605 i = (i + 1) % N32;
rlm@1 606 }
rlm@1 607 for (j = 0; j < N32; j++) {
rlm@1 608 r = func2(psfmt32[idxof(i)] + psfmt32[idxof((i + mid) % N32)]
rlm@1 609 + psfmt32[idxof((i + N32 - 1) % N32)]);
rlm@1 610 psfmt32[idxof((i + mid) % N32)] ^= r;
rlm@1 611 r -= i;
rlm@1 612 psfmt32[idxof((i + mid + lag) % N32)] ^= r;
rlm@1 613 psfmt32[idxof(i)] = r;
rlm@1 614 i = (i + 1) % N32;
rlm@1 615 }
rlm@1 616
rlm@1 617 idx = N32;
rlm@1 618 period_certification();
rlm@1 619 initialized = 1;
rlm@1 620 }