SFMT.cpp 19.5 KB
Newer Older
1
/**
2
3
4
5
6
7
8
9
10
11
12
 * @file  SFMT.c
 * @brief SIMD oriented Fast Mersenne Twister(SFMT)
 *
 * @author Mutsuo Saito (Hiroshima University)
 * @author Makoto Matsumoto (Hiroshima University)
 *
 * Copyright (C) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima
 * University. All rights reserved.
 *
 * The new BSD License is applied to this software, see LICENSE.txt
 */
13
14
15
16
17
#include "sfmt/SFMT.h"
#include "sfmt/SFMT-params.h"

#include <cstring>
#include <cassert>
Peter Eastman's avatar
Peter Eastman committed
18
#include <iostream>
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

#if defined(__BIG_ENDIAN__) && !defined(__amd64) && !defined(BIG_ENDIAN64)
#define BIG_ENDIAN64 1
#endif
#if defined(HAVE_ALTIVEC) && !defined(BIG_ENDIAN64)
#define BIG_ENDIAN64 1
#endif
#if defined(ONLY64) && !defined(BIG_ENDIAN64)
  #if defined(__GNUC__)
    #error "-DONLY64 must be specified with -DBIG_ENDIAN64"
  #endif
#undef ONLY64
#endif
/*------------------------------------------------------
  128-bit SIMD data type for Altivec, SSE2 or standard C
  ------------------------------------------------------*/
#if defined(HAVE_ALTIVEC)
  #if !defined(__APPLE__)
    #include <altivec.h>
  #endif
/** 128-bit data structure */
union W128_T {
    vector unsigned int s;
    uint32_t u[4];
};
/** 128-bit data type */
typedef union W128_T w128_t;

#elif defined(HAVE_SSE2)
  #include <emmintrin.h>

/** 128-bit data structure */
union W128_T {
    __m128i si;
    uint32_t u[4];
};
/** 128-bit data type */
typedef union W128_T w128_t;

#else

/** 128-bit data structure */
struct W128_T {
    uint32_t u[4];
};
/** 128-bit data type */
typedef struct W128_T w128_t;

#endif

/*--------------------------------------
  FILE GLOBAL VARIABLES
71
  internal state, index counter and flag
72
  --------------------------------------*/
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
///** the 128-bit internal state array */
//static w128_t sfmt[N];
///** the 32bit integer pointer to the 128-bit internal state array */
//static uint32_t *psfmt32 = &sfmt[0].u[0];
//#if !defined(BIG_ENDIAN64) || defined(ONLY64)
///** the 64bit integer pointer to the 128-bit internal state array */
//static uint64_t *psfmt64 = (uint64_t *)&sfmt[0].u[0];
//#endif
///** index counter to the 32-bit internal state array */
//static int idx;
///** a flag: it is 0 if and only if the internal state is not yet
// * initialized. */
//static int initialized = 0;
///** a parity check vector which certificate the period of 2^{MEXP} */
//static uint32_t parity[4] = {PARITY1, PARITY2, PARITY3, PARITY4};

namespace OpenMM_SFMT {

class SFMTData {
public:
93
94
    /** Possibly incorrectly aligned memory for internal state array */
    char baseData[(N+1)*sizeof(w128_t)];
95
	/** the 128-bit internal state array */
96
	w128_t* sfmt;
97
98
99
100
101
102
103
104
105
106
107
108
109
110
	/** the 32bit integer pointer to the 128-bit internal state array */
	uint32_t *psfmt32;
	#if !defined(BIG_ENDIAN64) || defined(ONLY64)
	/** the 64bit integer pointer to the 128-bit internal state array */
	uint64_t *psfmt64;
	#endif
	/** index counter to the 32-bit internal state array */
	int idx;
	/** a flag: it is 0 if and only if the internal state is not yet
	 * initialized. */
	int initialized;
	/** a parity check vector which certificate the period of 2^{MEXP} */
	uint32_t parity[4];
	SFMTData() {
111
112
113
        char* offsetData = baseData+15;
        offsetData -= (long long)offsetData&0xF;
        sfmt = (w128_t*) offsetData;
114
		psfmt32 = &sfmt[0].u[0];
115
#if !defined(BIG_ENDIAN64) || defined(ONLY64)
116
		psfmt64 = (uint64_t *)&sfmt[0].u[0];
117
#endif
118
119
120
121
122
123
124
		initialized = 0;
		parity[0] = PARITY1;
		parity[1] = PARITY2;
		parity[2] = PARITY3;
		parity[3] = PARITY4;
	}
};
125

Peter Eastman's avatar
Peter Eastman committed
126
void SFMT::createCheckpoint(std::ostream& stream) {
Peter Eastman's avatar
Peter Eastman committed
127
    stream.write((char*) &data->baseData, sizeof(data->baseData));
Peter Eastman's avatar
Peter Eastman committed
128
129
130
131
132
    stream.write((char*) &data->sfmt, sizeof(data->sfmt));
    stream.write((char*) &data->idx, sizeof(data->idx));
}

void SFMT::loadCheckpoint(std::istream& stream) {
Peter Eastman's avatar
Peter Eastman committed
133
    stream.read((char*) &data->baseData, sizeof(data->baseData));
Peter Eastman's avatar
Peter Eastman committed
134
135
136
137
    stream.read((char*) &data->sfmt, sizeof(data->sfmt));
    stream.read((char*) &data->idx, sizeof(data->idx));
}

138
139
140
141
142
143
/*----------------
  STATIC FUNCTIONS
  ----------------*/
inline static int idxof(int i);
inline static void rshift128(w128_t *out,  w128_t const *in, int shift);
inline static void lshift128(w128_t *out,  w128_t const *in, int shift);
144
145
inline static void gen_rand_all(SFMT& sfmt);
inline static void gen_rand_array(w128_t *array, int size, SFMT& sfmt);
146
147
inline static uint32_t func1(uint32_t x);
inline static uint32_t func2(uint32_t x);
148
static void period_certification(SFMT& sfmt);
149
150
151
152
153
#if defined(BIG_ENDIAN64) && !defined(ONLY64)
inline static void swap(w128_t *array, int size);
#endif

#if defined(HAVE_ALTIVEC)
peastman's avatar
peastman committed
154
  #include "sfmt/SFMT-alti.h"
155
#elif defined(HAVE_SSE2)
peastman's avatar
peastman committed
156
  #include "sfmt/SFMT-sse2.h"
157
158
159
#endif

/**
160
 * This function simulate a 64-bit index of LITTLE ENDIAN
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
 * in BIG ENDIAN machine.
 */
#ifdef ONLY64
inline static int idxof(int i) {
    return i ^ 1;
}
#else
inline static int idxof(int i) {
    return i;
}
#endif
/**
 * This function simulates SIMD 128-bit right shift by the standard C.
 * The 128-bit integer given in 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
 */
#ifdef ONLY64
inline static void rshift128(w128_t *out, w128_t const *in, int shift) {
    uint64_t th, tl, oh, ol;

    th = ((uint64_t)in->u[2] << 32) | ((uint64_t)in->u[3]);
    tl = ((uint64_t)in->u[0] << 32) | ((uint64_t)in->u[1]);

    oh = th >> (shift * 8);
    ol = tl >> (shift * 8);
    ol |= th << (64 - shift * 8);
    out->u[0] = (uint32_t)(ol >> 32);
    out->u[1] = (uint32_t)ol;
    out->u[2] = (uint32_t)(oh >> 32);
    out->u[3] = (uint32_t)oh;
}
#else
inline static void rshift128(w128_t *out, w128_t const *in, int shift) {
    uint64_t th, tl, oh, ol;

    th = ((uint64_t)in->u[3] << 32) | ((uint64_t)in->u[2]);
    tl = ((uint64_t)in->u[1] << 32) | ((uint64_t)in->u[0]);

    oh = th >> (shift * 8);
    ol = tl >> (shift * 8);
    ol |= th << (64 - shift * 8);
    out->u[1] = (uint32_t)(ol >> 32);
    out->u[0] = (uint32_t)ol;
    out->u[3] = (uint32_t)(oh >> 32);
    out->u[2] = (uint32_t)oh;
}
#endif
/**
 * This function simulates SIMD 128-bit left shift by the standard C.
 * The 128-bit integer given in 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
 */
#ifdef ONLY64
inline static void lshift128(w128_t *out, w128_t const *in, int shift) {
    uint64_t th, tl, oh, ol;

    th = ((uint64_t)in->u[2] << 32) | ((uint64_t)in->u[3]);
    tl = ((uint64_t)in->u[0] << 32) | ((uint64_t)in->u[1]);

    oh = th << (shift * 8);
    ol = tl << (shift * 8);
    oh |= tl >> (64 - shift * 8);
    out->u[0] = (uint32_t)(ol >> 32);
    out->u[1] = (uint32_t)ol;
    out->u[2] = (uint32_t)(oh >> 32);
    out->u[3] = (uint32_t)oh;
}
#else
inline static void lshift128(w128_t *out, w128_t const *in, int shift) {
    uint64_t th, tl, oh, ol;

    th = ((uint64_t)in->u[3] << 32) | ((uint64_t)in->u[2]);
    tl = ((uint64_t)in->u[1] << 32) | ((uint64_t)in->u[0]);

    oh = th << (shift * 8);
    ol = tl << (shift * 8);
    oh |= tl >> (64 - shift * 8);
    out->u[1] = (uint32_t)(ol >> 32);
    out->u[0] = (uint32_t)ol;
    out->u[3] = (uint32_t)(oh >> 32);
    out->u[2] = (uint32_t)oh;
}
#endif

/**
 * This function represents the recursion formula.
 * @param r 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 d a 128-bit part of the internal state array
 */
#ifdef ONLY64
inline static void do_recursion(w128_t *r, w128_t *a, w128_t *b, w128_t *c,
				w128_t *d) {
    w128_t x;
    w128_t y;

    lshift128(&x, a, SL2);
    rshift128(&y, c, SR2);
267
    r->u[0] = a->u[0] ^ x.u[0] ^ ((b->u[0] >> SR1) & MSK2) ^ y.u[0]
268
	^ (d->u[0] << SL1);
269
    r->u[1] = a->u[1] ^ x.u[1] ^ ((b->u[1] >> SR1) & MSK1) ^ y.u[1]
270
	^ (d->u[1] << SL1);
271
    r->u[2] = a->u[2] ^ x.u[2] ^ ((b->u[2] >> SR1) & MSK4) ^ y.u[2]
272
	^ (d->u[2] << SL1);
273
    r->u[3] = a->u[3] ^ x.u[3] ^ ((b->u[3] >> SR1) & MSK3) ^ y.u[3]
274
275
276
277
278
279
280
281
282
283
	^ (d->u[3] << SL1);
}
#else
inline static void do_recursion(w128_t *r, w128_t *a, w128_t *b, w128_t *c,
				w128_t *d) {
    w128_t x;
    w128_t y;

    lshift128(&x, a, SL2);
    rshift128(&y, c, SR2);
284
    r->u[0] = a->u[0] ^ x.u[0] ^ ((b->u[0] >> SR1) & MSK1) ^ y.u[0]
285
	^ (d->u[0] << SL1);
286
    r->u[1] = a->u[1] ^ x.u[1] ^ ((b->u[1] >> SR1) & MSK2) ^ y.u[1]
287
	^ (d->u[1] << SL1);
288
    r->u[2] = a->u[2] ^ x.u[2] ^ ((b->u[2] >> SR1) & MSK3) ^ y.u[2]
289
	^ (d->u[2] << SL1);
290
    r->u[3] = a->u[3] ^ x.u[3] ^ ((b->u[3] >> SR1) & MSK4) ^ y.u[3]
291
292
293
294
295
296
297
298
299
	^ (d->u[3] << SL1);
}
#endif

#if (!defined(HAVE_ALTIVEC)) && (!defined(HAVE_SSE2))
/**
 * This function fills the internal state array with pseudorandom
 * integers.
 */
300
inline static void gen_rand_all(SFMT& sfmt) {
301
302
303
    int i;
    w128_t *r1, *r2;

304
305
306
    SFMTData& data = *sfmt.data;
    r1 = &(data.sfmt[N - 2]);
    r2 = &(data.sfmt[N - 1]);
307
    for (i = 0; i < N - POS1; i++) {
308
	do_recursion(&(data.sfmt[i]), &(data.sfmt[i]), &(data.sfmt[i + POS1]), r1, r2);
309
	r1 = r2;
310
	r2 = &(data.sfmt[i]);
311
312
    }
    for (; i < N; i++) {
313
	do_recursion(&(data.sfmt[i]), &(data.sfmt[i]), &(data.sfmt[i + POS1 - N]), r1, r2);
314
	r1 = r2;
315
	r2 = &(data.sfmt[i]);
316
317
318
319
320
321
322
    }
}

/**
 * This function fills the user-specified array with pseudorandom
 * integers.
 *
323
 * @param array an 128-bit array to be filled by pseudorandom numbers.
324
325
 * @param size number of 128-bit pseudorandom numbers to be generated.
 */
326
inline static void gen_rand_array(w128_t *array, int size, SFMT& sfmt) {
327
328
329
    int i, j;
    w128_t *r1, *r2;

330
331
332
    SFMTData& data = *sfmt.data;
    r1 = &(data.sfmt[N - 2]);
    r2 = &(data.sfmt[N - 1]);
333
    for (i = 0; i < N - POS1; i++) {
334
	do_recursion(&array[i], &(data.sfmt[i]), &(data.sfmt[i + POS1]), r1, r2);
335
336
337
338
	r1 = r2;
	r2 = &array[i];
    }
    for (; i < N; i++) {
339
	do_recursion(&array[i], &(data.sfmt[i]), &array[i + POS1 - N], r1, r2);
340
341
342
343
344
345
346
347
348
	r1 = r2;
	r2 = &array[i];
    }
    for (; i < size - N; i++) {
	do_recursion(&array[i], &array[i - N], &array[i + POS1 - N], r1, r2);
	r1 = r2;
	r2 = &array[i];
    }
    for (j = 0; j < 2 * N - size; j++) {
349
	data.sfmt[j] = array[j + size - N];
350
351
352
353
354
    }
    for (; i < size; i++, j++) {
	do_recursion(&array[i], &array[i - N], &array[i + POS1 - N], r1, r2);
	r1 = r2;
	r2 = &array[i];
355
	data.sfmt[j] = array[i];
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
    }
}
#endif

#if defined(BIG_ENDIAN64) && !defined(ONLY64) && !defined(HAVE_ALTIVEC)
inline static void swap(w128_t *array, int size) {
    int i;
    uint32_t x, y;

    for (i = 0; i < size; i++) {
	x = array[i].u[0];
	y = array[i].u[2];
	array[i].u[0] = array[i].u[1];
	array[i].u[2] = array[i].u[3];
	array[i].u[1] = x;
	array[i].u[3] = y;
    }
}
#endif
/**
 * 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 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 func2(uint32_t x) {
    return (x ^ (x >> 27)) * (uint32_t)1566083941UL;
}

/**
 * This function certificate the period of 2^{MEXP}
 */
398
static void period_certification(SFMT& sfmt) {
399
400
401
    int inner = 0;
    int i, j;
    uint32_t work;
402
    SFMTData& data = *sfmt.data;
403
404

    for (i = 0; i < 4; i++)
405
	inner ^= data.psfmt32[idxof(i)] & data.parity[i];
406
407
408
409
410
411
412
413
414
415
416
    for (i = 16; i > 0; i >>= 1)
	inner ^= inner >> i;
    inner &= 1;
    /* check OK */
    if (inner == 1) {
	return;
    }
    /* check NG, and modification */
    for (i = 0; i < 4; i++) {
	work = 1;
	for (j = 0; j < 32; j++) {
417
418
	    if ((work & data.parity[i]) != 0) {
		data.psfmt32[idxof(i)] ^= work;
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
		return;
	    }
	    work = work << 1;
	}
    }
}

/*----------------
  PUBLIC FUNCTIONS
  ----------------*/
/**
 * This function returns the identification string.
 * The string shows the word size, the Mersenne exponent,
 * and all parameters of this generator.
 */
const char *get_idstring(void) {
    return IDSTR;
}

/**
 * This function returns the minimum size of array used for \b
 * fill_array32() function.
 * @return minimum size of array used for fill_array32() function.
 */
int get_min_array_size32(void) {
    return N32;
}

/**
 * This function returns the minimum size of array used for \b
 * fill_array64() function.
 * @return minimum size of array used for fill_array64() function.
 */
int get_min_array_size64(void) {
    return N64;
}

#ifndef ONLY64
/**
 * This function generates and returns 32-bit pseudorandom number.
 * init_gen_rand or init_by_array must be called before this function.
 * @return 32-bit pseudorandom number
 */
462
uint32_t gen_rand32(SFMT& sfmt) {
463
    uint32_t r;
464
    SFMTData& data = *sfmt.data;
465

466
467
468
469
    assert(data.initialized);
    if (data.idx >= N32) {
	gen_rand_all(sfmt);
	data.idx = 0;
470
    }
471
    r = data.psfmt32[data.idx++];
472
473
474
475
476
477
478
    return r;
}
#endif
/**
 * This function generates and returns 64-bit pseudorandom number.
 * init_gen_rand or init_by_array must be called before this function.
 * The function gen_rand64 should not be called after gen_rand32,
479
 * unless an initialization is again executed.
480
481
 * @return 64-bit pseudorandom number
 */
482
uint64_t gen_rand64(SFMT& sfmt) {
483
484
485
486
487
#if defined(BIG_ENDIAN64) && !defined(ONLY64)
    uint32_t r1, r2;
#else
    uint64_t r;
#endif
488
    SFMTData& data = *sfmt.data;
489

490
491
    assert(data.initialized);
    assert(data.idx % 2 == 0);
492

493
494
495
    if (data.idx >= N32) {
	gen_rand_all(sfmt);
	data.idx = 0;
496
497
    }
#if defined(BIG_ENDIAN64) && !defined(ONLY64)
498
499
500
    r1 = data.psfmt32[data.idx];
    r2 = data.psfmt32[data.idx + 1];
    data.idx += 2;
501
502
    return ((uint64_t)r2 << 32) | r1;
#else
503
504
    r = data.psfmt64[data.idx / 2];
    data.idx += 2;
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
    return r;
#endif
}

#ifndef ONLY64
/**
 * This function generates pseudorandom 32-bit integers in the
 * specified array[] by one call. The number of pseudorandom integers
 * is specified by the argument size, which must be at least 624 and a
 * multiple of four.  The generation by this function is much faster
 * than the following gen_rand function.
 *
 * 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 gen_rand function, without initialization.
 *
 * @param array an array where pseudorandom 32-bit integers are filled
 * by this function.  The pointer to the array must be \b "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 32-bit pseudorandom integers to be
 * generated.  size must be a multiple of 4, and greater than or equal
 * to (MEXP / 128 + 1) * 4.
 *
 * @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.
 */
535
536
537
538
void fill_array32(uint32_t *array, int size, SFMT& sfmt) {
    SFMTData& data = *sfmt.data;
    assert(data.initialized);
    assert(data.idx == N32);
539
540
541
    assert(size % 4 == 0);
    assert(size >= N32);

542
543
    gen_rand_array((w128_t *)array, size / 4, sfmt);
    data.idx = N32;
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
}
#endif

/**
 * This function generates pseudorandom 64-bit integers in the
 * specified array[] by one call. The number of pseudorandom integers
 * is specified by the argument size, which must be at least 312 and a
 * multiple of two.  The generation by this function is much faster
 * than the following gen_rand function.
 *
 * 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 gen_rand function, without initialization.
 *
 * @param array an array where pseudorandom 64-bit integers 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 (MEXP / 128 + 1) * 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.
 */
572
573
574
575
void fill_array64(uint64_t *array, int size, SFMT& sfmt) {
    SFMTData& data = *sfmt.data;
    assert(data.initialized);
    assert(data.idx == N32);
576
577
578
    assert(size % 2 == 0);
    assert(size >= N64);

579
580
    gen_rand_array((w128_t *)array, size / 2, sfmt);
    data.idx = N32;
581
582
583
584
585
586
587
588
589
590
591
592

#if defined(BIG_ENDIAN64) && !defined(ONLY64)
    swap((w128_t *)array, size /2);
#endif
}

/**
 * This function initializes the internal state array with a 32-bit
 * integer seed.
 *
 * @param seed a 32-bit integer used as the seed.
 */
593
void init_gen_rand(uint32_t seed, SFMT& sfmt) {
594
    int i;
595
    SFMTData& data = *sfmt.data;
596

597
    data.psfmt32[idxof(0)] = seed;
598
    for (i = 1; i < N32; i++) {
599
600
    	data.psfmt32[idxof(i)] = 1812433253UL * (data.psfmt32[idxof(i - 1)]
					    ^ (data.psfmt32[idxof(i - 1)] >> 30))
601
602
	    + i;
    }
603
604
605
    data.idx = N32;
    period_certification(sfmt);
    data.initialized = 1;
606
607
608
609
610
611
612
613
}

/**
 * This function initializes the internal state array,
 * with an array of 32-bit integers used as the seeds
 * @param init_key the array of 32-bit integers, used as a seed.
 * @param key_length the length of init_key.
 */
614
void init_by_array(uint32_t *init_key, int key_length, SFMT& sfmt) {
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
    int i, j, count;
    uint32_t r;
    int lag;
    int mid;
    int size = N * 4;

    if (size >= 623) {
	lag = 11;
    } else if (size >= 68) {
	lag = 7;
    } else if (size >= 39) {
	lag = 5;
    } else {
	lag = 3;
    }
    mid = (size - lag) / 2;

632
633
    SFMTData& data = *sfmt.data;
    memset(data.sfmt, 0x8b, sizeof(data.sfmt));
634
635
636
637
638
    if (key_length + 1 > N32) {
	count = key_length + 1;
    } else {
	count = N32;
    }
639
640
641
    r = func1(data.psfmt32[idxof(0)] ^ data.psfmt32[idxof(mid)]
	      ^ data.psfmt32[idxof(N32 - 1)]);
    data.psfmt32[idxof(mid)] += r;
642
    r += key_length;
643
644
    data.psfmt32[idxof(mid + lag)] += r;
    data.psfmt32[idxof(0)] = r;
645
646
647

    count--;
    for (i = 1, j = 0; (j < count) && (j < key_length); j++) {
648
649
650
	r = func1(data.psfmt32[idxof(i)] ^ data.psfmt32[idxof((i + mid) % N32)]
		  ^ data.psfmt32[idxof((i + N32 - 1) % N32)]);
	data.psfmt32[idxof((i + mid) % N32)] += r;
651
	r += init_key[j] + i;
652
653
	data.psfmt32[idxof((i + mid + lag) % N32)] += r;
	data.psfmt32[idxof(i)] = r;
654
655
656
	i = (i + 1) % N32;
    }
    for (; j < count; j++) {
657
658
659
	r = func1(data.psfmt32[idxof(i)] ^ data.psfmt32[idxof((i + mid) % N32)]
		  ^ data.psfmt32[idxof((i + N32 - 1) % N32)]);
	data.psfmt32[idxof((i + mid) % N32)] += r;
660
	r += i;
661
662
	data.psfmt32[idxof((i + mid + lag) % N32)] += r;
	data.psfmt32[idxof(i)] = r;
663
664
665
	i = (i + 1) % N32;
    }
    for (j = 0; j < N32; j++) {
666
667
668
	r = func2(data.psfmt32[idxof(i)] + data.psfmt32[idxof((i + mid) % N32)]
		  + data.psfmt32[idxof((i + N32 - 1) % N32)]);
	data.psfmt32[idxof((i + mid) % N32)] ^= r;
669
	r -= i;
670
671
	data.psfmt32[idxof((i + mid + lag) % N32)] ^= r;
	data.psfmt32[idxof(i)] = r;
672
673
674
	i = (i + 1) % N32;
    }

675
676
677
    data.idx = N32;
    period_certification(sfmt);
    data.initialized = 1;
678
}
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696

/**
 * Create an SFMTData object.  This allows outside code to create these objects without needing to know their definition.
 */
SFMTData* createSFMTData(void) {
	return new SFMTData();
}

/**
 * Delete an SFMTData object that was created with createSFMTData().
 */
void deleteSFMTData(SFMTData* data) {
	delete data;
}

} // OpenMM_SFMT