1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
|
/**
* @file dSFMT.c
* @brief double precision SIMD-oriented Fast Mersenne Twister (dSFMT)
* based on IEEE 754 format.
*
* @author Mutsuo Saito (Hiroshima University)
* @author Makoto Matsumoto (Hiroshima University)
*
* Copyright (C) 2007,2008 Mutsuo Saito, Makoto Matsumoto and Hiroshima
* University. All rights reserved.
*
* The new BSD License is applied to this software, see LICENSE.txt
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "dSFMT.h"
/** dsfmt internal state vector */
dsfmt_t dsfmt_global_data;
/** dsfmt initialized flag */
int dsfmt_global_is_initialized = 0;
/** dsfmt mexp for check */
static const int dsfmt_mexp = DSFMT_MEXP;
/*----------------
STATIC FUNCTIONS
----------------*/
inline static uint32_t ini_func1(uint32_t x);
inline static uint32_t ini_func2(uint32_t x);
inline static void gen_rand_array_c1o2(dsfmt_t *dsfmt, w128_t array[],
int size);
inline static void gen_rand_array_c0o1(dsfmt_t *dsfmt, w128_t array[],
int size);
inline static void gen_rand_array_o0c1(dsfmt_t *dsfmt, w128_t array[],
int size);
inline static void gen_rand_array_o0o1(dsfmt_t *dsfmt, w128_t array[],
int size);
inline static int idxof(int i);
static void initial_mask(dsfmt_t *dsfmt);
static void period_certification(dsfmt_t *dsfmt);
#if !defined(HAVE_SSE2) && !defined(HAVE_ALTIVEC)
inline static void lshift128(w128_t *out, const w128_t *in, int shift);
#endif
#if defined(HAVE_SSE2)
# include <emmintrin.h>
/** mask data for sse2 */
static __m128i sse2_param_mask;
/** low mask data for sse2 */
static __m128i sse2_low_mask;
/** high constant data for sse2 */
static __m128i sse2_high_const;
/** 1 in 64bit for sse2 */
static __m128i sse2_int_one;
/** 2.0 double for sse2 */
static __m128d sse2_double_two;
/** -1.0 double for sse2 */
static __m128d sse2_double_m_one;
static void setup_const(void);
#endif
/**
* This function simulate a 32-bit array index overlapped to 64-bit
* array of LITTLE ENDIAN in BIG ENDIAN machine.
*/
#if defined(DSFMT_BIG_ENDIAN)
inline static int idxof(int i) {
return i ^ 1;
}
#else
inline static int idxof(int i) {
return i;
}
#endif
/**
* This function represents the recursion formula.
* @param rr output
* @param a a 128-bit part of the internal state array
* @param b a 128-bit part of the internal state array
* @param c a 128-bit part of the internal state array
* @param lung a 128-bit part of the internal state array
*/
#if defined(HAVE_ALTIVEC)
inline static void do_recursion(w128_t *rr,
w128_t *a,
w128_t *b,
w128_t *reg,
w128_t *lung) {
vector unsigned int r, s, t, u, v, w, x, y, z;
const vector unsigned char sl1 = ALTI_SL1;
const vector unsigned char sl1_perm = ALTI_SL1_PERM;
const vector unsigned int sl1_msk = ALTI_SL1_MSK;
const vector unsigned char sl2_perm = ALTI_SL2_PERM;
const vector unsigned char sr1 = ALTI_SR1;
const vector unsigned int sr1_msk = ALTI_SR1_MSK;
const vector unsigned char sr2_perm = ALTI_SR2_PERM;
const vector unsigned char perm = ALTI_PERM;
const vector unsigned int low_mask = ALTI_LOW_MSK;
const vector unsigned int high_const = ALTI_HIGH_CONST;
x = vec_perm(a->s, (vector unsigned int)sl2_perm, sl2_perm);
y = vec_srl(b->s, sr1);
y = vec_and(y, sr1_msk);
z = vec_perm(reg->s, (vector unsigned int)sl1_perm, sl1_perm);
z = vec_sll(z, sl1);
z = vec_and(z, sl1_msk);
w = vec_perm(reg->s, (vector unsigned int)sr2_perm, sr2_perm);
v = vec_perm(lung->s, (vector unsigned int)perm, perm);
s = vec_xor(a->s, x);
t = vec_xor(y, z);
u = vec_xor(w, v);
r = vec_xor(s, t);
r = vec_xor(r, u);
r = vec_and(r, low_mask);
lung->s = vec_xor(lung->s, r);
rr->s = vec_or(r, high_const);
}
#elif defined(HAVE_SSE2)
/**
* This function setup some constant variables for SSE2.
*/
static void setup_const(void) {
static int first = 1;
if (!first) {
return;
}
sse2_param_mask = _mm_set_epi32(DSFMT_MSK32_3, DSFMT_MSK32_4,
DSFMT_MSK32_1, DSFMT_MSK32_2);
sse2_low_mask = _mm_set_epi32(DSFMT_LOW_MASK32_1, DSFMT_LOW_MASK32_2,
DSFMT_LOW_MASK32_1, DSFMT_LOW_MASK32_2);
sse2_int_one = _mm_set_epi32(0, 1, 0, 1);
sse2_high_const = _mm_set_epi32(DSFMT_HIGH_CONST32, 0,
DSFMT_HIGH_CONST32, 0);
sse2_double_two = _mm_set_pd(2.0, 2.0);
sse2_double_m_one = _mm_set_pd(-1.0, -1.0);
first = 0;
}
/**
* This function represents the recursion formula.
* @param r output 128-bit
* @param a a 128-bit part of the internal state array
* @param b a 128-bit part of the internal state array
* @param c a 128-bit part of the internal state array
* @param d a 128-bit part of the internal state array (I/O)
*/
inline static void do_recursion(w128_t *r, w128_t *a, w128_t *b,
w128_t *c, w128_t *d) {
__m128i v, w, x, y, z;
z = a->si;
y = _mm_srli_epi64(b->si, DSFMT_SR1);
y = _mm_and_si128(y, sse2_param_mask);
w = _mm_slli_epi64(c->si, DSFMT_SL1);
x = _mm_srli_epi64(c->si, DSFMT_SR2);
v = _mm_shuffle_epi32(d->si, SSE2_SHUFF);
w = _mm_xor_si128(w, x);
v = _mm_xor_si128(v, z);
z = _mm_slli_si128(z, DSFMT_SL2);
w = _mm_xor_si128(w, y);
v = _mm_xor_si128(v, z);
v = _mm_xor_si128(v, w);
v = _mm_and_si128(v, sse2_low_mask);
d->si = _mm_xor_si128(d->si, v);
r->si = _mm_or_si128(v, sse2_high_const);
}
#else /* standard C */
/**
* This function simulates SIMD 128-bit left shift by the standard C.
* The 128-bit integer given in \b in is shifted by (shift * 8) bits.
* This function simulates the LITTLE ENDIAN SIMD.
* @param out the output of this function
* @param in the 128-bit data to be shifted
* @param shift the shift value
*/
inline static void lshift128(w128_t *out, const w128_t *in, int shift) {
out->u[0] = in->u[0] << (shift * 8);
out->u[1] = in->u[1] << (shift * 8);
out->u[1] |= in->u[0] >> (64 - shift * 8);
}
/**
* This function represents the recursion formula.
* @param r output 128-bit
* @param a a 128-bit part of the internal state array
* @param b a 128-bit part of the internal state array
* @param c a 128-bit part of the internal state array
* @param lung a 128-bit part of the internal state array (I/O)
*/
inline static void do_recursion(w128_t *r, w128_t *a, w128_t *b, w128_t *c,
w128_t *lung) {
w128_t x;
lshift128(&x, a, DSFMT_SL2);
r->u[0] = a->u[0] ^ x.u[0] ^ ((b->u[0] >> DSFMT_SR1) & DSFMT_MSK1)
^ (c->u[0] >> DSFMT_SR2) ^ (c->u[0] << DSFMT_SL1) ^ lung->u[1];
r->u[1] = a->u[1] ^ x.u[1] ^ ((b->u[1] >> DSFMT_SR1) & DSFMT_MSK2)
^ (c->u[1] >> DSFMT_SR2) ^ (c->u[1] << DSFMT_SL1) ^ lung->u[0];
r->u[0] &= DSFMT_LOW_MASK;
r->u[1] &= DSFMT_LOW_MASK;
lung->u[0] ^= r->u[0];
lung->u[1] ^= r->u[1];
r->u[0] |= DSFMT_HIGH_CONST;
r->u[1] |= DSFMT_HIGH_CONST;
}
#endif
#if defined(HAVE_SSE2)
/**
* This function converts the double precision floating point numbers which
* distribute uniformly in the range [1, 2) to those which distribute uniformly
* in the range [0, 1).
* @param w 128bit stracture of double precision floating point numbers (I/O)
*/
inline static void convert_c0o1(w128_t *w) {
w->sd = _mm_add_pd(w->sd, sse2_double_m_one);
}
/**
* This function converts the double precision floating point numbers which
* distribute uniformly in the range [1, 2) to those which distribute uniformly
* in the range (0, 1].
* @param w 128bit stracture of double precision floating point numbers (I/O)
*/
inline static void convert_o0c1(w128_t *w) {
w->sd = _mm_sub_pd(sse2_double_two, w->sd);
}
/**
* This function converts the double precision floating point numbers which
* distribute uniformly in the range [1, 2) to those which distribute uniformly
* in the range (0, 1).
* @param w 128bit stracture of double precision floating point numbers (I/O)
*/
inline static void convert_o0o1(w128_t *w) {
w->si = _mm_or_si128(w->si, sse2_int_one);
w->sd = _mm_add_pd(w->sd, sse2_double_m_one);
}
#else /* standard C and altivec */
/**
* This function converts the double precision floating point numbers which
* distribute uniformly in the range [1, 2) to those which distribute uniformly
* in the range [0, 1).
* @param w 128bit stracture of double precision floating point numbers (I/O)
*/
inline static void convert_c0o1(w128_t *w) {
w->d[0] -= 1.0;
w->d[1] -= 1.0;
}
/**
* This function converts the double precision floating point numbers which
* distribute uniformly in the range [1, 2) to those which distribute uniformly
* in the range (0, 1].
* @param w 128bit stracture of double precision floating point numbers (I/O)
*/
inline static void convert_o0c1(w128_t *w) {
w->d[0] = 2.0 - w->d[0];
w->d[1] = 2.0 - w->d[1];
}
/**
* This function converts the double precision floating point numbers which
* distribute uniformly in the range [1, 2) to those which distribute uniformly
* in the range (0, 1).
* @param w 128bit stracture of double precision floating point numbers (I/O)
*/
inline static void convert_o0o1(w128_t *w) {
w->u[0] |= 1;
w->u[1] |= 1;
w->d[0] -= 1.0;
w->d[1] -= 1.0;
}
#endif
/**
* This function fills the user-specified array with double precision
* floating point pseudorandom numbers of the IEEE 754 format.
* @param dsfmt dsfmt state vector.
* @param array an 128-bit array to be filled by pseudorandom numbers.
* @param size number of 128-bit pseudorandom numbers to be generated.
*/
inline static void gen_rand_array_c1o2(dsfmt_t *dsfmt, w128_t array[],
int size) {
int i, j;
w128_t lung;
lung = dsfmt->status[DSFMT_N];
do_recursion(&array[0], &dsfmt->status[0], &dsfmt->status[DSFMT_POS1],
&dsfmt->status[DSFMT_N - 1], &lung);
for (i = 1; i < DSFMT_N - DSFMT_POS1; i++) {
do_recursion(&array[i], &dsfmt->status[i],
&dsfmt->status[i + DSFMT_POS1], &array[i - 1],
&lung);
}
for (; i < DSFMT_N; i++) {
do_recursion(&array[i], &dsfmt->status[i],
&array[i + DSFMT_POS1 - DSFMT_N], &array[i - 1], &lung);
}
for (; i < size - DSFMT_N; i++) {
do_recursion(&array[i], &array[i - DSFMT_N],
&array[i + DSFMT_POS1 - DSFMT_N], &array[i - 1], &lung);
}
for (j = 0; j < 2 * DSFMT_N - size; j++) {
dsfmt->status[j] = array[j + size - DSFMT_N];
}
for (; i < size; i++, j++) {
do_recursion(&array[i], &array[i - DSFMT_N],
&array[i + DSFMT_POS1 - DSFMT_N], &array[i - 1], &lung);
dsfmt->status[j] = array[i];
}
dsfmt->status[DSFMT_N] = lung;
}
/**
* This function fills the user-specified array with double precision
* floating point pseudorandom numbers of the IEEE 754 format.
* @param dsfmt dsfmt state vector.
* @param array an 128-bit array to be filled by pseudorandom numbers.
* @param size number of 128-bit pseudorandom numbers to be generated.
*/
inline static void gen_rand_array_c0o1(dsfmt_t *dsfmt, w128_t array[],
int size) {
int i, j;
w128_t lung;
lung = dsfmt->status[DSFMT_N];
do_recursion(&array[0], &dsfmt->status[0], &dsfmt->status[DSFMT_POS1],
&dsfmt->status[DSFMT_N - 1], &lung);
for (i = 1; i < DSFMT_N - DSFMT_POS1; i++) {
do_recursion(&array[i], &dsfmt->status[i],
&dsfmt->status[i + DSFMT_POS1], &array[i - 1],
&lung);
}
for (; i < DSFMT_N; i++) {
do_recursion(&array[i], &dsfmt->status[i],
&array[i + DSFMT_POS1 - DSFMT_N], &array[i - 1], &lung);
}
for (; i < size - DSFMT_N; i++) {
do_recursion(&array[i], &array[i - DSFMT_N],
&array[i + DSFMT_POS1 - DSFMT_N], &array[i - 1], &lung);
convert_c0o1(&array[i - DSFMT_N]);
}
for (j = 0; j < 2 * DSFMT_N - size; j++) {
dsfmt->status[j] = array[j + size - DSFMT_N];
}
for (; i < size; i++, j++) {
do_recursion(&array[i], &array[i - DSFMT_N],
&array[i + DSFMT_POS1 - DSFMT_N], &array[i - 1], &lung);
dsfmt->status[j] = array[i];
convert_c0o1(&array[i - DSFMT_N]);
}
for (i = size - DSFMT_N; i < size; i++) {
convert_c0o1(&array[i]);
}
dsfmt->status[DSFMT_N] = lung;
}
/**
* This function fills the user-specified array with double precision
* floating point pseudorandom numbers of the IEEE 754 format.
* @param dsfmt dsfmt state vector.
* @param array an 128-bit array to be filled by pseudorandom numbers.
* @param size number of 128-bit pseudorandom numbers to be generated.
*/
inline static void gen_rand_array_o0o1(dsfmt_t *dsfmt, w128_t array[],
int size) {
int i, j;
w128_t lung;
lung = dsfmt->status[DSFMT_N];
do_recursion(&array[0], &dsfmt->status[0], &dsfmt->status[DSFMT_POS1],
&dsfmt->status[DSFMT_N - 1], &lung);
for (i = 1; i < DSFMT_N - DSFMT_POS1; i++) {
do_recursion(&array[i], &dsfmt->status[i],
&dsfmt->status[i + DSFMT_POS1], &array[i - 1],
&lung);
}
for (; i < DSFMT_N; i++) {
do_recursion(&array[i], &dsfmt->status[i],
&array[i + DSFMT_POS1 - DSFMT_N], &array[i - 1], &lung);
}
for (; i < size - DSFMT_N; i++) {
do_recursion(&array[i], &array[i - DSFMT_N],
&array[i + DSFMT_POS1 - DSFMT_N], &array[i - 1], &lung);
convert_o0o1(&array[i - DSFMT_N]);
}
for (j = 0; j < 2 * DSFMT_N - size; j++) {
dsfmt->status[j] = array[j + size - DSFMT_N];
}
for (; i < size; i++, j++) {
do_recursion(&array[i], &array[i - DSFMT_N],
&array[i + DSFMT_POS1 - DSFMT_N], &array[i - 1], &lung);
dsfmt->status[j] = array[i];
convert_o0o1(&array[i - DSFMT_N]);
}
for (i = size - DSFMT_N; i < size; i++) {
convert_o0o1(&array[i]);
}
dsfmt->status[DSFMT_N] = lung;
}
/**
* This function fills the user-specified array with double precision
* floating point pseudorandom numbers of the IEEE 754 format.
* @param dsfmt dsfmt state vector.
* @param array an 128-bit array to be filled by pseudorandom numbers.
* @param size number of 128-bit pseudorandom numbers to be generated.
*/
inline static void gen_rand_array_o0c1(dsfmt_t *dsfmt, w128_t array[],
int size) {
int i, j;
w128_t lung;
lung = dsfmt->status[DSFMT_N];
do_recursion(&array[0], &dsfmt->status[0], &dsfmt->status[DSFMT_POS1],
&dsfmt->status[DSFMT_N - 1], &lung);
for (i = 1; i < DSFMT_N - DSFMT_POS1; i++) {
do_recursion(&array[i], &dsfmt->status[i],
&dsfmt->status[i + DSFMT_POS1], &array[i - 1],
&lung);
}
for (; i < DSFMT_N; i++) {
do_recursion(&array[i], &dsfmt->status[i],
&array[i + DSFMT_POS1 - DSFMT_N], &array[i - 1], &lung);
}
for (; i < size - DSFMT_N; i++) {
do_recursion(&array[i], &array[i - DSFMT_N],
&array[i + DSFMT_POS1 - DSFMT_N], &array[i - 1], &lung);
convert_o0c1(&array[i - DSFMT_N]);
}
for (j = 0; j < 2 * DSFMT_N - size; j++) {
dsfmt->status[j] = array[j + size - DSFMT_N];
}
for (; i < size; i++, j++) {
do_recursion(&array[i], &array[i - DSFMT_N],
&array[i + DSFMT_POS1 - DSFMT_N], &array[i - 1], &lung);
dsfmt->status[j] = array[i];
convert_o0c1(&array[i - DSFMT_N]);
}
for (i = size - DSFMT_N; i < size; i++) {
convert_o0c1(&array[i]);
}
dsfmt->status[DSFMT_N] = lung;
}
/**
* This function represents a function used in the initialization
* by init_by_array
* @param x 32-bit integer
* @return 32-bit integer
*/
static uint32_t ini_func1(uint32_t x) {
return (x ^ (x >> 27)) * (uint32_t)1664525UL;
}
/**
* This function represents a function used in the initialization
* by init_by_array
* @param x 32-bit integer
* @return 32-bit integer
*/
static uint32_t ini_func2(uint32_t x) {
return (x ^ (x >> 27)) * (uint32_t)1566083941UL;
}
/**
* This function initializes the internal state array to fit the IEEE
* 754 format.
* @param dsfmt dsfmt state vector.
*/
static void initial_mask(dsfmt_t *dsfmt) {
int i;
uint64_t *psfmt;
psfmt = &dsfmt->status[0].u[0];
for (i = 0; i < (DSFMT_N + 1) * 2; i++) {
psfmt[i] = (psfmt[i] & DSFMT_LOW_MASK) | DSFMT_HIGH_CONST;
}
}
/**
* This function certificate the period of 2^{SFMT_MEXP}-1.
* @param dsfmt dsfmt state vector.
*/
static void period_certification(dsfmt_t *dsfmt) {
int i, j;
uint64_t pcv[2] = {DSFMT_PCV1, DSFMT_PCV2};
uint64_t inner;
uint64_t new_lung[2];
uint64_t work;
uint64_t fix[2];
fix[0] = (((DSFMT_HIGH_CONST >> DSFMT_SR1) & DSFMT_MSK2)
^ (DSFMT_HIGH_CONST >> DSFMT_SR2)) | DSFMT_HIGH_CONST;
fix[1] = (((DSFMT_HIGH_CONST >> DSFMT_SR1) & DSFMT_MSK1)
^ (DSFMT_HIGH_CONST >> DSFMT_SR2)) | DSFMT_HIGH_CONST;
fix[0] = fix[0] ^ (DSFMT_HIGH_CONST >> (64 - 8 * DSFMT_SL2));
new_lung[0] = dsfmt->status[DSFMT_N].u[0] ^ fix[0];
new_lung[1] = dsfmt->status[DSFMT_N].u[1] ^ fix[1];
inner = new_lung[0] & pcv[0];
inner ^= new_lung[1] & pcv[1];
for (i = 32; i > 0; i >>= 1) {
inner ^= inner >> i;
}
inner &= 1;
/* check OK */
if (inner == 1) {
return;
}
/* check NG, and modification */
for (i = 0; i < 2; i++) {
work = 1;
for (j = 0; j < 52; j++) {
if ((work & pcv[i]) != 0) {
dsfmt->status[DSFMT_N].u[i] ^= work;
return;
}
work = work << 1;
}
}
}
/*----------------
PUBLIC FUNCTIONS
----------------*/
/**
* This function returns the identification string. The string shows
* the Mersenne exponent, and all parameters of this generator.
* @return id string.
*/
const char *dsfmt_get_idstring(void) {
return DSFMT_IDSTR;
}
/**
* This function returns the minimum size of array used for \b
* fill_array functions.
* @return minimum size of array used for fill_array functions.
*/
int dsfmt_get_min_array_size(void) {
return DSFMT_N64;
}
/**
* This function fills the internal state array with double precision
* floating point pseudorandom numbers of the IEEE 754 format.
* @param dsfmt dsfmt state vector.
*/
void dsfmt_gen_rand_all(dsfmt_t *dsfmt) {
int i;
w128_t lung;
lung = dsfmt->status[DSFMT_N];
do_recursion(&dsfmt->status[0], &dsfmt->status[0],
&dsfmt->status[DSFMT_POS1], &dsfmt->status[DSFMT_N -1], &lung);
for (i = 1; i < DSFMT_N - DSFMT_POS1; i++) {
do_recursion(&dsfmt->status[i], &dsfmt->status[i],
&dsfmt->status[i + DSFMT_POS1], &dsfmt->status[i - 1],
&lung);
}
for (; i < DSFMT_N; i++) {
do_recursion(&dsfmt->status[i], &dsfmt->status[i],
&dsfmt->status[i + DSFMT_POS1 - DSFMT_N],
&dsfmt->status[i - 1], &lung);
}
dsfmt->status[DSFMT_N] = lung;
}
/**
* This function generates double precision floating point
* pseudorandom numbers which distribute in the range [1, 2) to the
* specified array[] by one call. The number of pseudorandom numbers
* is specified by the argument \b size, which must be at least (SFMT_MEXP
* / 128) * 2 and a multiple of two. The function
* get_min_array_size() returns this minimum size. The generation by
* this function is much faster than the following fill_array_xxx functions.
*
* For initialization, init_gen_rand() or init_by_array() must be called
* before the first call of this function. This function can not be
* used after calling genrand_xxx functions, without initialization.
*
* @param dsfmt dsfmt state vector.
* @param array an array where pseudorandom numbers are filled
* by this function. The pointer to the array must be "aligned"
* (namely, must be a multiple of 16) in the SIMD version, since it
* refers to the address of a 128-bit integer. In the standard C
* version, the pointer is arbitrary.
*
* @param size the number of 64-bit pseudorandom integers to be
* generated. size must be a multiple of 2, and greater than or equal
* to (SFMT_MEXP / 128) * 2.
*
* @note \b memalign or \b posix_memalign is available to get aligned
* memory. Mac OSX doesn't have these functions, but \b malloc of OSX
* returns the pointer to the aligned memory block.
*/
void dsfmt_fill_array_close1_open2(dsfmt_t *dsfmt, double array[], int size) {
assert(size % 2 == 0);
assert(size >= DSFMT_N64);
gen_rand_array_c1o2(dsfmt, (w128_t *)array, size / 2);
}
/**
* This function generates double precision floating point
* pseudorandom numbers which distribute in the range (0, 1] to the
* specified array[] by one call. This function is the same as
* fill_array_close1_open2() except the distribution range.
*
* @param dsfmt dsfmt state vector.
* @param array an array where pseudorandom numbers are filled
* by this function.
* @param size the number of pseudorandom numbers to be generated.
* see also \sa fill_array_close1_open2()
*/
void dsfmt_fill_array_open_close(dsfmt_t *dsfmt, double array[], int size) {
assert(size % 2 == 0);
assert(size >= DSFMT_N64);
gen_rand_array_o0c1(dsfmt, (w128_t *)array, size / 2);
}
/**
* This function generates double precision floating point
* pseudorandom numbers which distribute in the range [0, 1) to the
* specified array[] by one call. This function is the same as
* fill_array_close1_open2() except the distribution range.
*
* @param array an array where pseudorandom numbers are filled
* by this function.
* @param dsfmt dsfmt state vector.
* @param size the number of pseudorandom numbers to be generated.
* see also \sa fill_array_close1_open2()
*/
void dsfmt_fill_array_close_open(dsfmt_t *dsfmt, double array[], int size) {
assert(size % 2 == 0);
assert(size >= DSFMT_N64);
gen_rand_array_c0o1(dsfmt, (w128_t *)array, size / 2);
}
/**
* This function generates double precision floating point
* pseudorandom numbers which distribute in the range (0, 1) to the
* specified array[] by one call. This function is the same as
* fill_array_close1_open2() except the distribution range.
*
* @param dsfmt dsfmt state vector.
* @param array an array where pseudorandom numbers are filled
* by this function.
* @param size the number of pseudorandom numbers to be generated.
* see also \sa fill_array_close1_open2()
*/
void dsfmt_fill_array_open_open(dsfmt_t *dsfmt, double array[], int size) {
assert(size % 2 == 0);
assert(size >= DSFMT_N64);
gen_rand_array_o0o1(dsfmt, (w128_t *)array, size / 2);
}
#if defined(__INTEL_COMPILER)
# pragma warning(disable:981)
#endif
/**
* This function initializes the internal state array with a 32-bit
* integer seed.
* @param dsfmt dsfmt state vector.
* @param seed a 32-bit integer used as the seed.
* @param mexp caller's mersenne expornent
*/
void dsfmt_chk_init_gen_rand(dsfmt_t *dsfmt, uint32_t seed, int mexp) {
int i;
uint32_t *psfmt;
/* make sure caller program is compiled with the same MEXP */
if (mexp != dsfmt_mexp) {
fprintf(stderr, "DSFMT_MEXP doesn't match with dSFMT.c\n");
exit(1);
}
psfmt = &dsfmt->status[0].u32[0];
psfmt[idxof(0)] = seed;
for (i = 1; i < (DSFMT_N + 1) * 4; i++) {
psfmt[idxof(i)] = 1812433253UL
* (psfmt[idxof(i - 1)] ^ (psfmt[idxof(i - 1)] >> 30)) + i;
}
initial_mask(dsfmt);
period_certification(dsfmt);
dsfmt->idx = DSFMT_N64;
#if defined(HAVE_SSE2)
setup_const();
#endif
}
/**
* This function initializes the internal state array,
* with an array of 32-bit integers used as the seeds
* @param dsfmt dsfmt state vector.
* @param init_key the array of 32-bit integers, used as a seed.
* @param key_length the length of init_key.
* @param mexp caller's mersenne expornent
*/
void dsfmt_chk_init_by_array(dsfmt_t *dsfmt, uint32_t init_key[],
int key_length, int mexp) {
int i, j, count;
uint32_t r;
uint32_t *psfmt32;
int lag;
int mid;
int size = (DSFMT_N + 1) * 4; /* pulmonary */
/* make sure caller program is compiled with the same MEXP */
if (mexp != dsfmt_mexp) {
fprintf(stderr, "DSFMT_MEXP doesn't match with dSFMT.c\n");
exit(1);
}
if (size >= 623) {
lag = 11;
} else if (size >= 68) {
lag = 7;
} else if (size >= 39) {
lag = 5;
} else {
lag = 3;
}
mid = (size - lag) / 2;
psfmt32 = &dsfmt->status[0].u32[0];
memset(dsfmt->status, 0x8b, sizeof(dsfmt->status));
if (key_length + 1 > size) {
count = key_length + 1;
} else {
count = size;
}
r = ini_func1(psfmt32[idxof(0)] ^ psfmt32[idxof(mid % size)]
^ psfmt32[idxof((size - 1) % size)]);
psfmt32[idxof(mid % size)] += r;
r += key_length;
psfmt32[idxof((mid + lag) % size)] += r;
psfmt32[idxof(0)] = r;
i = 1;
count--;
for (i = 1, j = 0; (j < count) && (j < key_length); j++) {
r = ini_func1(psfmt32[idxof(i)]
^ psfmt32[idxof((i + mid) % size)]
^ psfmt32[idxof((i + size - 1) % size)]);
psfmt32[idxof((i + mid) % size)] += r;
r += init_key[j] + i;
psfmt32[idxof((i + mid + lag) % size)] += r;
psfmt32[idxof(i)] = r;
i = (i + 1) % size;
}
for (; j < count; j++) {
r = ini_func1(psfmt32[idxof(i)]
^ psfmt32[idxof((i + mid) % size)]
^ psfmt32[idxof((i + size - 1) % size)]);
psfmt32[idxof((i + mid) % size)] += r;
r += i;
psfmt32[idxof((i + mid + lag) % size)] += r;
psfmt32[idxof(i)] = r;
i = (i + 1) % size;
}
for (j = 0; j < size; j++) {
r = ini_func2(psfmt32[idxof(i)]
+ psfmt32[idxof((i + mid) % size)]
+ psfmt32[idxof((i + size - 1) % size)]);
psfmt32[idxof((i + mid) % size)] ^= r;
r -= i;
psfmt32[idxof((i + mid + lag) % size)] ^= r;
psfmt32[idxof(i)] = r;
i = (i + 1) % size;
}
initial_mask(dsfmt);
period_certification(dsfmt);
dsfmt->idx = DSFMT_N64;
#if defined(HAVE_SSE2)
setup_const();
#endif
}
#if defined(__INTEL_COMPILER)
# pragma warning(default:981)
#endif
|