ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/src/random.C
Revision: 1.2
Committed: Sun Sep 9 20:05:52 2007 UTC (19 years, 1 month ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.1: +5 -4 lines
Log Message:
- changed configurations to the c++ stdlib
- more #defines to enum
- removed getopt.h and link.h from the system as they were unused
- reworked logstreams
- added an itoa with old syntax
- made klines objects
- moved some global variables into appropriate classes
- fixed boost.foreach's compiler workaround #if's
- allow other files to add exceptions with ADD_EXCEPTION
- changed mynick_t to c++ object
- moved servers.h out of atheme.h
- corrected PING from inspircd 1.2

File Contents

# User Rev Content
1 pippijn 1.2 /**
2 pippijn 1.1 * random.C: SIMD oriented Fast Mersenne Twister(SFMT)
3     *
4 pippijn 1.2 * Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team (http://ermyth.schmorp.de)
5 pippijn 1.1 * Copyright © 2006, 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima
6     * University. All rights reserved.
7     *
8     * @author Mutsuo Saito (Hiroshima University)
9     * @author Makoto Matsumoto (Hiroshima University)
10     *
11     * The new BSD License is applied to this software, see doc/poddoc/license.pod
12     */
13 pippijn 1.2
14     static char const rcsid[] = "$Id: md5.C,v 1.3 2007-09-05 11:23:15 pippijn Exp $";
15    
16 pippijn 1.1 #include <string.h>
17     #include <assert.h>
18    
19     #include <common/random.h>
20    
21     /*-----------------
22     BASIC DEFINITIONS
23     -----------------*/
24     /** SFMT generator has an internal state array of 128-bit integers,
25     * and N is its size. */
26     #define N (216091 / 128 + 1)
27     /** N32 is the size of internal state array when regarded as an array
28     * of 32-bit integers.*/
29     #define N32 (N * 4)
30     /** N64 is the size of internal state array when regarded as an array
31     * of 64-bit integers.*/
32     #define N64 (N * 2)
33    
34     /*----------------------
35     the parameters of SFMT
36     following definitions are in paramsXXXX.h file.
37     ----------------------*/
38     // the pick up position of the array.
39     #define POS1 627
40     // the parameter of shift left as four 32-bit registers.
41     #define SL1 11
42     // the parameter of shift left as one 128-bit register.
43     // The 128-bit integer is shifted by (SL2 * 8) bits.
44     #define SL2 3
45     // the parameter of shift right as four 32-bit registers.
46     #define SR1 10
47     // the parameter of shift right as one 128-bit register.
48     // The 128-bit integer is shifted by (SL2 * 8) bits.
49     #define SR2 1
50     // A bitmask, used in the recursion. These parameters are introduced
51     // to break symmetry of SIMD.
52     #define MSK1 0xbff7bff7U
53     #define MSK2 0xbfffffffU
54     #define MSK3 0xbffffa7fU
55     #define MSK4 0xffddfbfbU
56     // These definitions are part of a 128-bit period certification vector.
57     #define PARITY1 0xf8000001U
58     #define PARITY2 0x89e80709U
59     #define PARITY3 0x3bd2b64bU
60     #define PARITY4 0x0c64b1e4U
61    
62     /* PARAMETERS FOR ALTIVEC */
63     #if defined(__APPLE__) /* For OSX */
64     #define ALTI_SL1 (vector unsigned int)(SL1, SL1, SL1, SL1)
65     #define ALTI_SR1 (vector unsigned int)(SR1, SR1, SR1, SR1)
66     #define ALTI_MSK (vector unsigned int)(MSK1, MSK2, MSK3, MSK4)
67     #define ALTI_MSK64 (vector unsigned int)(MSK2, MSK1, MSK4, MSK3)
68     #define ALTI_SL2_PERM (vector unsigned char)(3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10)
69     #define ALTI_SL2_PERM64 (vector unsigned char)(3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2)
70     #define ALTI_SR2_PERM (vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14)
71     #define ALTI_SR2_PERM64 (vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14)
72     #else // !__APPLE__
73     #define ALTI_SL1 {SL1, SL1, SL1, SL1}
74     #define ALTI_SR1 {SR1, SR1, SR1, SR1}
75     #define ALTI_MSK {MSK1, MSK2, MSK3, MSK4}
76     #define ALTI_MSK64 {MSK2, MSK1, MSK4, MSK3}
77     #define ALTI_SL2_PERM { 3,21,21,21, 7, 0, 1, 2,11, 4, 5, 6,15, 8, 9,10}
78     #define ALTI_SL2_PERM64 { 3, 4, 5, 6, 7,29,29,29,11,12,13,14,15, 0, 1, 2}
79     #define ALTI_SR2_PERM { 7, 0, 1, 2,11, 4, 5, 6,15, 8, 9,10,17,12,13,14}
80     #define ALTI_SR2_PERM64 {15, 0, 1, 2, 3, 4, 5, 6,17, 8, 9,10,11,12,13,14}
81     #endif // __APPLE__
82     #define IDSTR "SFMT-216091:627-11-3-10-1:bff7bff7-bfffffff-bffffa7f-ffddfbfb"
83    
84     #if defined(__BIG_ENDIAN__) && !defined(__amd64) && !defined(BIG_ENDIAN64)
85     #define BIG_ENDIAN64 1
86     #endif
87     #if defined(HAVE_ALTIVEC) && !defined(BIG_ENDIAN64)
88     #define BIG_ENDIAN64 1
89     #endif
90     #if defined(ONLY64) && !defined(BIG_ENDIAN64)
91     #if defined(__GNUC__)
92     #error "-DONLY64 must be specified with -DBIG_ENDIAN64"
93     #endif
94     #undef ONLY64
95     #endif
96    
97     /*--------------------------------------
98     FILE GLOBAL VARIABLES
99     internal state, index counter and flag
100     --------------------------------------*/
101     /** the 128-bit internal state array */
102     static w128_t sfmt[N];
103     /** the 32bit integer pointer to the 128-bit internal state array */
104     static uint32_t *psfmt32 = &sfmt[0].u[0];
105     #if !defined(BIG_ENDIAN64) || defined(ONLY64)
106     /** the 64bit integer pointer to the 128-bit internal state array */
107     static uint64_t *psfmt64 = reinterpret_cast<uint64_t *> (&sfmt[0].u[0]);
108     #endif
109     /** index counter to the 32-bit internal state array */
110     static int idx;
111     /** a flag: it is 0 if and only if the internal state is not yet
112     * initialized. */
113     static int initialized = 0;
114     /** a parity check vector which certificate the period of 2^{MEXP} */
115     static uint32_t parity[4] = { PARITY1, PARITY2, PARITY3, PARITY4 };
116    
117     /*----------------
118     STATIC FUNCTIONS
119     ----------------*/
120     inline static int idxof (int i);
121     inline static void rshift128 (w128_t *out, w128_t const *in, int shift);
122     inline static void lshift128 (w128_t *out, w128_t const *in, int shift);
123     inline static void gen_rand_all (void);
124     inline static void gen_rand_array (w128_t *array, int size);
125     inline static uint32_t func1 (uint32_t x);
126     inline static uint32_t func2 (uint32_t x);
127     inline static void period_certification (void);
128     #if defined(BIG_ENDIAN64) && !defined(ONLY64)
129     inline static void swap (w128_t *array, int size);
130     #endif
131    
132     #if defined(HAVE_ALTIVEC)
133     inline static vector unsigned int
134     vec_recursion (vector unsigned int a, vector unsigned int b, vector unsigned int c, vector unsigned int d) ALWAYSINLINE;
135    
136     /**
137     * This function represents the recursion formula in AltiVec and BIG ENDIAN.
138     * @param a a 128-bit part of the interal state array
139     * @param b a 128-bit part of the interal state array
140     * @param c a 128-bit part of the interal state array
141     * @param d a 128-bit part of the interal state array
142     * @return output
143     */
144     inline static vector unsigned int vec_recursion (vector unsigned int a, vector unsigned int b, vector unsigned int c, vector unsigned int d)
145     {
146     const vector unsigned int sl1 = ALTI_SL1;
147     const vector unsigned int sr1 = ALTI_SR1;
148     #ifdef ONLY64
149     const vector unsigned int mask = ALTI_MSK64;
150     const vector unsigned char perm_sl = ALTI_SL2_PERM64;
151     const vector unsigned char perm_sr = ALTI_SR2_PERM64;
152     #else
153     const vector unsigned int mask = ALTI_MSK;
154     const vector unsigned char perm_sl = ALTI_SL2_PERM;
155     const vector unsigned char perm_sr = ALTI_SR2_PERM;
156     #endif
157     vector unsigned int v, w, x, y, z;
158     x = vec_perm (a, (vector unsigned int) perm_sl, perm_sl);
159     v = a;
160     y = vec_sr (b, sr1);
161     z = vec_perm (c, (vector unsigned int) perm_sr, perm_sr);
162     w = vec_sl (d, sl1);
163     z = vec_xor (z, w);
164     y = vec_and (y, mask);
165     v = vec_xor (v, x);
166     z = vec_xor (z, y);
167     z = vec_xor (z, v);
168     return z;
169     }
170    
171     /**
172     * This function fills the internal state array with pseudorandom
173     * integers.
174     */
175     inline static void
176     gen_rand_all (void)
177     {
178     int i;
179     vector unsigned int r, r1, r2;
180    
181     r1 = sfmt[N - 2].s;
182     r2 = sfmt[N - 1].s;
183     for (i = 0; i < N - POS1; i++)
184     {
185     r = vec_recursion (sfmt[i].s, sfmt[i + POS1].s, r1, r2);
186     sfmt[i].s = r;
187     r1 = r2;
188     r2 = r;
189     }
190     for (; i < N; i++)
191     {
192     r = vec_recursion (sfmt[i].s, sfmt[i + POS1 - N].s, r1, r2);
193     sfmt[i].s = r;
194     r1 = r2;
195     r2 = r;
196     }
197     }
198    
199     /**
200     * This function fills the user-specified array with pseudorandom
201     * integers.
202     *
203     * @param array an 128-bit array to be filled by pseudorandom numbers.
204     * @param size number of 128-bit pesudorandom numbers to be generated.
205     */
206     inline static void
207     gen_rand_array (w128_t *array, int size)
208     {
209     int i, j;
210     vector unsigned int r, r1, r2;
211    
212     r1 = sfmt[N - 2].s;
213     r2 = sfmt[N - 1].s;
214     for (i = 0; i < N - POS1; i++)
215     {
216     r = vec_recursion (sfmt[i].s, sfmt[i + POS1].s, r1, r2);
217     array[i].s = r;
218     r1 = r2;
219     r2 = r;
220     }
221     for (; i < N; i++)
222     {
223     r = vec_recursion (sfmt[i].s, array[i + POS1 - N].s, r1, r2);
224     array[i].s = r;
225     r1 = r2;
226     r2 = r;
227     }
228     /* main loop */
229     for (; i < size - N; i++)
230     {
231     r = vec_recursion (array[i - N].s, array[i + POS1 - N].s, r1, r2);
232     array[i].s = r;
233     r1 = r2;
234     r2 = r;
235     }
236     for (j = 0; j < 2 * N - size; j++)
237     {
238     sfmt[j].s = array[j + size - N].s;
239     }
240     for (; i < size; i++)
241     {
242     r = vec_recursion (array[i - N].s, array[i + POS1 - N].s, r1, r2);
243     array[i].s = r;
244     sfmt[j++].s = r;
245     r1 = r2;
246     r2 = r;
247     }
248     }
249    
250     #ifndef ONLY64
251     #if defined(__APPLE__)
252     #define ALTI_SWAP (vector unsigned char) \
253     (4, 5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11)
254     #else
255     #define ALTI_SWAP {4, 5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11}
256     #endif
257     /**
258     * This function swaps high and low 32-bit of 64-bit integers in user
259     * specified array.
260     *
261     * @param array an 128-bit array to be swaped.
262     * @param size size of 128-bit array.
263     */
264     inline static void
265     swap (w128_t *array, int size)
266     {
267     int i;
268     const vector unsigned char perm = ALTI_SWAP;
269    
270     for (i = 0; i < size; i++)
271     {
272     array[i].s = vec_perm (array[i].s, (vector unsigned int) perm, perm);
273     }
274     }
275     #endif
276     #elif defined(HAVE_SSE2)
277     inline static __m128i
278     mm_recursion (__m128i *a, __m128i *b, __m128i c, __m128i d, __m128i mask);
279    
280     /**
281     * This function represents the recursion formula.
282     * @param a a 128-bit part of the interal state array
283     * @param b a 128-bit part of the interal state array
284     * @param c a 128-bit part of the interal state array
285     * @param d a 128-bit part of the interal state array
286     * @param mask 128-bit mask
287     * @return output
288     */
289     inline static __m128i mm_recursion (__m128i *a, __m128i *b, __m128i c, __m128i d, __m128i mask)
290     {
291     __m128i v, x, y, z;
292    
293     x = _mm_load_si128 (a);
294     y = _mm_srli_epi32 (*b, SR1);
295     z = _mm_srli_si128 (c, SR2);
296     v = _mm_slli_epi32 (d, SL1);
297     z = _mm_xor_si128 (z, x);
298     z = _mm_xor_si128 (z, v);
299     x = _mm_slli_si128 (x, SL2);
300     y = _mm_and_si128 (y, mask);
301     z = _mm_xor_si128 (z, x);
302     z = _mm_xor_si128 (z, y);
303     return z;
304     }
305    
306     /**
307     * This function fills the internal state array with pseudorandom
308     * integers.
309     */
310     inline static void
311     gen_rand_all (void)
312     {
313     int i;
314     __m128i r, r1, r2, mask;
315     mask = _mm_set_epi32 (MSK4, MSK3, MSK2, MSK1);
316    
317     r1 = _mm_load_si128 (&sfmt[N - 2].si);
318     r2 = _mm_load_si128 (&sfmt[N - 1].si);
319     for (i = 0; i < N - POS1; i++)
320     {
321     r = mm_recursion (&sfmt[i].si, &sfmt[i + POS1].si, r1, r2, mask);
322     _mm_store_si128 (&sfmt[i].si, r);
323     r1 = r2;
324     r2 = r;
325     }
326     for (; i < N; i++)
327     {
328     r = mm_recursion (&sfmt[i].si, &sfmt[i + POS1 - N].si, r1, r2, mask);
329     _mm_store_si128 (&sfmt[i].si, r);
330     r1 = r2;
331     r2 = r;
332     }
333     }
334    
335     /**
336     * This function fills the user-specified array with pseudorandom
337     * integers.
338     *
339     * @param array an 128-bit array to be filled by pseudorandom numbers.
340     * @param size number of 128-bit pesudorandom numbers to be generated.
341     */
342     inline static void
343     gen_rand_array (w128_t *array, int size)
344     {
345     int i, j;
346     __m128i r, r1, r2, mask;
347     mask = _mm_set_epi32 (MSK4, MSK3, MSK2, MSK1);
348    
349     r1 = _mm_load_si128 (&sfmt[N - 2].si);
350     r2 = _mm_load_si128 (&sfmt[N - 1].si);
351     for (i = 0; i < N - POS1; i++)
352     {
353     r = mm_recursion (&sfmt[i].si, &sfmt[i + POS1].si, r1, r2, mask);
354     _mm_store_si128 (&array[i].si, r);
355     r1 = r2;
356     r2 = r;
357     }
358     for (; i < N; i++)
359     {
360     r = mm_recursion (&sfmt[i].si, &array[i + POS1 - N].si, r1, r2, mask);
361     _mm_store_si128 (&array[i].si, r);
362     r1 = r2;
363     r2 = r;
364     }
365     /* main loop */
366     for (; i < size - N; i++)
367     {
368     r = mm_recursion (&array[i - N].si, &array[i + POS1 - N].si, r1, r2, mask);
369     _mm_store_si128 (&array[i].si, r);
370     r1 = r2;
371     r2 = r;
372     }
373     for (j = 0; j < 2 * N - size; j++)
374     {
375     r = _mm_load_si128 (&array[j + size - N].si);
376     _mm_store_si128 (&sfmt[j].si, r);
377     }
378     for (; i < size; i++)
379     {
380     r = mm_recursion (&array[i - N].si, &array[i + POS1 - N].si, r1, r2, mask);
381     _mm_store_si128 (&array[i].si, r);
382     _mm_store_si128 (&sfmt[j++].si, r);
383     r1 = r2;
384     r2 = r;
385     }
386     }
387     #endif
388    
389     /**
390     * This function simulate a 64-bit index of LITTLE ENDIAN
391     * in BIG ENDIAN machine.
392     */
393     #ifdef ONLY64
394     inline static int
395     idxof (int i)
396     {
397     return i ^ 1;
398     }
399     #else
400     inline static int
401     idxof (int i)
402     {
403     return i;
404     }
405     #endif
406     /**
407     * This function simulates SIMD 128-bit right shift by the standard C.
408     * The 128-bit integer given in in is shifted by (shift * 8) bits.
409     * This function simulates the LITTLE ENDIAN SIMD.
410     * @param out the output of this function
411     * @param in the 128-bit data to be shifted
412     * @param shift the shift value
413     */
414     #ifdef ONLY64
415     inline static void
416     rshift128 (w128_t *out, w128_t const *in, int shift)
417     {
418     uint64_t th, tl, oh, ol;
419    
420     th = ((uint64_t) in->u[2] << 32) | ((uint64_t) in->u[3]);
421     tl = ((uint64_t) in->u[0] << 32) | ((uint64_t) in->u[1]);
422    
423     oh = th >> (shift * 8);
424     ol = tl >> (shift * 8);
425     ol |= th << (64 - shift * 8);
426     out->u[0] = (uint32_t) (ol >> 32);
427     out->u[1] = (uint32_t) ol;
428     out->u[2] = (uint32_t) (oh >> 32);
429     out->u[3] = (uint32_t) oh;
430     }
431     #else
432     inline static void
433     rshift128 (w128_t *out, w128_t const *in, int shift)
434     {
435     uint64_t th, tl, oh, ol;
436    
437     th = ((uint64_t) in->u[3] << 32) | ((uint64_t) in->u[2]);
438     tl = ((uint64_t) in->u[1] << 32) | ((uint64_t) in->u[0]);
439    
440     oh = th >> (shift * 8);
441     ol = tl >> (shift * 8);
442     ol |= th << (64 - shift * 8);
443     out->u[1] = (uint32_t) (ol >> 32);
444     out->u[0] = (uint32_t) ol;
445     out->u[3] = (uint32_t) (oh >> 32);
446     out->u[2] = (uint32_t) oh;
447     }
448     #endif
449     /**
450     * This function simulates SIMD 128-bit left shift by the standard C.
451     * The 128-bit integer given in in is shifted by (shift * 8) bits.
452     * This function simulates the LITTLE ENDIAN SIMD.
453     * @param out the output of this function
454     * @param in the 128-bit data to be shifted
455     * @param shift the shift value
456     */
457     #ifdef ONLY64
458     inline static void
459     lshift128 (w128_t *out, w128_t const *in, int shift)
460     {
461     uint64_t th, tl, oh, ol;
462    
463     th = ((uint64_t) in->u[2] << 32) | ((uint64_t) in->u[3]);
464     tl = ((uint64_t) in->u[0] << 32) | ((uint64_t) in->u[1]);
465    
466     oh = th << (shift * 8);
467     ol = tl << (shift * 8);
468     oh |= tl >> (64 - shift * 8);
469     out->u[0] = (uint32_t) (ol >> 32);
470     out->u[1] = (uint32_t) ol;
471     out->u[2] = (uint32_t) (oh >> 32);
472     out->u[3] = (uint32_t) oh;
473     }
474     #else
475     inline static void
476     lshift128 (w128_t *out, w128_t const *in, int shift)
477     {
478     uint64_t th, tl, oh, ol;
479    
480     th = ((uint64_t) in->u[3] << 32) | ((uint64_t) in->u[2]);
481     tl = ((uint64_t) in->u[1] << 32) | ((uint64_t) in->u[0]);
482    
483     oh = th << (shift * 8);
484     ol = tl << (shift * 8);
485     oh |= tl >> (64 - shift * 8);
486     out->u[1] = (uint32_t) (ol >> 32);
487     out->u[0] = (uint32_t) ol;
488     out->u[3] = (uint32_t) (oh >> 32);
489     out->u[2] = (uint32_t) oh;
490     }
491     #endif
492    
493     /**
494     * This function represents the recursion formula.
495     * @param r output
496     * @param a a 128-bit part of the internal state array
497     * @param b a 128-bit part of the internal state array
498     * @param c a 128-bit part of the internal state array
499     * @param d a 128-bit part of the internal state array
500     */
501     #ifdef ONLY64
502     inline static void
503     do_recursion (w128_t *r, w128_t *a, w128_t *b, w128_t *c, w128_t *d)
504     {
505     w128_t x;
506     w128_t y;
507    
508     lshift128 (&x, a, SL2);
509     rshift128 (&y, c, SR2);
510     r->u[0] = a->u[0] ^ x.u[0] ^ ((b->u[0] >> SR1) & MSK2) ^ y.u[0] ^ (d->u[0] << SL1);
511     r->u[1] = a->u[1] ^ x.u[1] ^ ((b->u[1] >> SR1) & MSK1) ^ y.u[1] ^ (d->u[1] << SL1);
512     r->u[2] = a->u[2] ^ x.u[2] ^ ((b->u[2] >> SR1) & MSK4) ^ y.u[2] ^ (d->u[2] << SL1);
513     r->u[3] = a->u[3] ^ x.u[3] ^ ((b->u[3] >> SR1) & MSK3) ^ y.u[3] ^ (d->u[3] << SL1);
514     }
515     #else
516     inline static void
517     do_recursion (w128_t *r, w128_t *a, w128_t *b, w128_t *c, w128_t *d)
518     {
519     w128_t x;
520     w128_t y;
521    
522     lshift128 (&x, a, SL2);
523     rshift128 (&y, c, SR2);
524     r->u[0] = a->u[0] ^ x.u[0] ^ ((b->u[0] >> SR1) & MSK1) ^ y.u[0] ^ (d->u[0] << SL1);
525     r->u[1] = a->u[1] ^ x.u[1] ^ ((b->u[1] >> SR1) & MSK2) ^ y.u[1] ^ (d->u[1] << SL1);
526     r->u[2] = a->u[2] ^ x.u[2] ^ ((b->u[2] >> SR1) & MSK3) ^ y.u[2] ^ (d->u[2] << SL1);
527     r->u[3] = a->u[3] ^ x.u[3] ^ ((b->u[3] >> SR1) & MSK4) ^ y.u[3] ^ (d->u[3] << SL1);
528     }
529     #endif
530    
531     #if (!defined(HAVE_ALTIVEC)) && (!defined(HAVE_SSE2))
532     /**
533     * This function fills the internal state array with pseudorandom
534     * integers.
535     */
536     inline static void
537     gen_rand_all (void)
538     {
539     int i;
540     w128_t *r1, *r2;
541    
542     r1 = &sfmt[N - 2];
543     r2 = &sfmt[N - 1];
544     for (i = 0; i < N - POS1; i++)
545     {
546     do_recursion (&sfmt[i], &sfmt[i], &sfmt[i + POS1], r1, r2);
547     r1 = r2;
548     r2 = &sfmt[i];
549     }
550     for (; i < N; i++)
551     {
552     do_recursion (&sfmt[i], &sfmt[i], &sfmt[i + POS1 - N], r1, r2);
553     r1 = r2;
554     r2 = &sfmt[i];
555     }
556     }
557    
558     /**
559     * This function fills the user-specified array with pseudorandom
560     * integers.
561     *
562     * @param array an 128-bit array to be filled by pseudorandom numbers.
563     * @param size number of 128-bit pseudorandom numbers to be generated.
564     */
565     inline static void
566     gen_rand_array (w128_t *array, int size)
567     {
568     int i, j;
569     w128_t *r1, *r2;
570    
571     r1 = &sfmt[N - 2];
572     r2 = &sfmt[N - 1];
573     for (i = 0; i < N - POS1; i++)
574     {
575     do_recursion (&array[i], &sfmt[i], &sfmt[i + POS1], r1, r2);
576     r1 = r2;
577     r2 = &array[i];
578     }
579     for (; i < N; i++)
580     {
581     do_recursion (&array[i], &sfmt[i], &array[i + POS1 - N], r1, r2);
582     r1 = r2;
583     r2 = &array[i];
584     }
585     for (; i < size - N; i++)
586     {
587     do_recursion (&array[i], &array[i - N], &array[i + POS1 - N], r1, r2);
588     r1 = r2;
589     r2 = &array[i];
590     }
591     for (j = 0; j < 2 * N - size; j++)
592     {
593     sfmt[j] = array[j + size - N];
594     }
595     for (; i < size; i++, j++)
596     {
597     do_recursion (&array[i], &array[i - N], &array[i + POS1 - N], r1, r2);
598     r1 = r2;
599     r2 = &array[i];
600     sfmt[j] = array[i];
601     }
602     }
603     #endif
604    
605     #if defined(BIG_ENDIAN64) && !defined(ONLY64) && !defined(HAVE_ALTIVEC)
606     inline static void
607     swap (w128_t *array, int size)
608     {
609     int i;
610     uint32_t x, y;
611    
612     for (i = 0; i < size; i++)
613     {
614     x = array[i].u[0];
615     y = array[i].u[2];
616     array[i].u[0] = array[i].u[1];
617     array[i].u[2] = array[i].u[3];
618     array[i].u[1] = x;
619     array[i].u[3] = y;
620     }
621     }
622     #endif
623     /**
624     * This function represents a function used in the initialization
625     * by init_by_array
626     * @param x 32-bit integer
627     * @return 32-bit integer
628     */
629     inline static uint32_t
630     func1 (uint32_t x)
631     {
632     return (x ^ (x >> 27)) * (uint32_t) 1664525UL;
633     }
634    
635     /**
636     * This function represents a function used in the initialization
637     * by init_by_array
638     * @param x 32-bit integer
639     * @return 32-bit integer
640     */
641     inline static uint32_t
642     func2 (uint32_t x)
643     {
644     return (x ^ (x >> 27)) * (uint32_t) 1566083941UL;
645     }
646    
647     /**
648     * This function certificate the period of 2^{MEXP}
649     */
650     static void
651     period_certification (void)
652     {
653     int inner = 0;
654     int i, j;
655     uint32_t work;
656    
657     for (i = 0; i < 4; i++)
658     inner ^= psfmt32[idxof (i)] & parity[i];
659     for (i = 16; i > 0; i >>= 1)
660     inner ^= inner >> i;
661     inner &= 1;
662     /* check OK */
663     if (inner == 1)
664     {
665     return;
666     }
667     /* check NG, and modification */
668     for (i = 0; i < 4; i++)
669     {
670     work = 1;
671     for (j = 0; j < 32; j++)
672     {
673     if ((work & parity[i]) != 0)
674     {
675     psfmt32[idxof (i)] ^= work;
676     return;
677     }
678     work = work << 1;
679     }
680     }
681     }
682    
683     /*----------------
684     PUBLIC FUNCTIONS
685     ----------------*/
686     /**
687     * This function returns the identification string.
688     * The string shows the word size, the Mersenne exponent,
689     * and all parameters of this generator.
690     */
691     char const * const
692     get_idstring (void)
693     {
694     return IDSTR;
695     }
696    
697     /**
698     * This function returns the minimum size of array used for \b
699     * fill_array32() function.
700     * @return minimum size of array used for fill_array32() function.
701     */
702     int
703     get_min_array_size32 (void)
704     {
705     return N32;
706     }
707    
708     /**
709     * This function returns the minimum size of array used for \b
710     * fill_array64() function.
711     * @return minimum size of array used for fill_array64() function.
712     */
713     int
714     get_min_array_size64 (void)
715     {
716     return N64;
717     }
718    
719     #ifndef ONLY64
720     /**
721     * This function generates and returns 32-bit pseudorandom number.
722     * init_gen_rand or init_by_array must be called before this function.
723     * @return 32-bit pseudorandom number
724     */
725     uint32_t
726     gen_rand32 (void)
727     {
728     uint32_t r;
729    
730     assert (initialized);
731     if (idx >= N32)
732     {
733     gen_rand_all ();
734     idx = 0;
735     }
736     r = psfmt32[idx++];
737     return r;
738     }
739     #endif
740     /**
741     * This function generates and returns 64-bit pseudorandom number.
742     * init_gen_rand or init_by_array must be called before this function.
743     * The function gen_rand64 should not be called after gen_rand32,
744     * unless an initialization is again executed.
745     * @return 64-bit pseudorandom number
746     */
747     uint64_t
748     gen_rand64 (void)
749     {
750     #if defined(BIG_ENDIAN64) && !defined(ONLY64)
751     uint32_t r1, r2;
752     #else
753     uint64_t r;
754     #endif
755    
756     assert (initialized);
757     assert (idx % 2 == 0);
758    
759     if (idx >= N32)
760     {
761     gen_rand_all ();
762     idx = 0;
763     }
764     #if defined(BIG_ENDIAN64) && !defined(ONLY64)
765     r1 = psfmt32[idx];
766     r2 = psfmt32[idx + 1];
767     idx += 2;
768     return ((uint64_t) r2 << 32) | r1;
769     #else
770     r = psfmt64[idx / 2];
771     idx += 2;
772     return r;
773     #endif
774     }
775    
776     #ifndef ONLY64
777     /**
778     * This function generates pseudorandom 32-bit integers in the
779     * specified array[] by one call. The number of pseudorandom integers
780     * is specified by the argument size, which must be at least 624 and a
781     * multiple of four. The generation by this function is much faster
782     * than the following gen_rand function.
783     *
784     * For initialization, init_gen_rand or init_by_array must be called
785     * before the first call of this function. This function can not be
786     * used after calling gen_rand function, without initialization.
787     *
788     * @param array an array where pseudorandom 32-bit integers are filled
789     * by this function. The pointer to the array must be \b "aligned"
790     * (namely, must be a multiple of 16) in the SIMD version, since it
791     * refers to the address of a 128-bit integer. In the standard C
792     * version, the pointer is arbitrary.
793     *
794     * @param size the number of 32-bit pseudorandom integers to be
795     * generated. size must be a multiple of 4, and greater than or equal
796     * to (MEXP / 128 + 1) * 4.
797     *
798     * @note \b memalign or \b posix_memalign is available to get aligned
799     * memory. Mac OSX doesn't have these functions, but \b malloc of OSX
800     * returns the pointer to the aligned memory block.
801     */
802     void
803     fill_array32 (uint32_t * array, int size)
804     {
805     assert (initialized);
806     assert (idx == N32);
807     assert (size % 4 == 0);
808     assert (size >= N32);
809    
810     gen_rand_array (reinterpret_cast<w128_t *> (array), size / 4);
811     idx = N32;
812     }
813     #endif
814    
815     /**
816     * This function generates pseudorandom 64-bit integers in the
817     * specified array[] by one call. The number of pseudorandom integers
818     * is specified by the argument size, which must be at least 312 and a
819     * multiple of two. The generation by this function is much faster
820     * than the following gen_rand function.
821     *
822     * For initialization, init_gen_rand or init_by_array must be called
823     * before the first call of this function. This function can not be
824     * used after calling gen_rand function, without initialization.
825     *
826     * @param array an array where pseudorandom 64-bit integers are filled
827     * by this function. The pointer to the array must be "aligned"
828     * (namely, must be a multiple of 16) in the SIMD version, since it
829     * refers to the address of a 128-bit integer. In the standard C
830     * version, the pointer is arbitrary.
831     *
832     * @param size the number of 64-bit pseudorandom integers to be
833     * generated. size must be a multiple of 2, and greater than or equal
834     * to (MEXP / 128 + 1) * 2
835     *
836     * @note \b memalign or \b posix_memalign is available to get aligned
837     * memory. Mac OSX doesn't have these functions, but \b malloc of OSX
838     * returns the pointer to the aligned memory block.
839     */
840     void
841     fill_array64 (uint64_t * array, int size)
842     {
843     assert (initialized);
844     assert (idx == N32);
845     assert (size % 2 == 0);
846     assert (size >= N64);
847    
848     gen_rand_array (reinterpret_cast<w128_t *> (array), size / 2);
849     idx = N32;
850    
851     #if defined(BIG_ENDIAN64) && !defined(ONLY64)
852     swap (reinterpret_cast<w128_t *> (array), size / 2);
853     #endif
854     }
855    
856     /**
857     * This function initializes the internal state array with a 32-bit
858     * integer seed.
859     *
860     * @param seed a 32-bit integer used as the seed.
861     */
862     void
863     init_gen_rand (uint32_t seed)
864     {
865     int i;
866    
867     psfmt32[idxof (0)] = seed;
868     for (i = 1; i < N32; i++)
869     {
870     psfmt32[idxof (i)] = 1812433253UL * (psfmt32[idxof (i - 1)] ^ (psfmt32[idxof (i - 1)] >> 30)) + i;
871     }
872     idx = N32;
873     period_certification ();
874     initialized = 1;
875     }
876    
877     /**
878     * This function initializes the internal state array,
879     * with an array of 32-bit integers used as the seeds
880     * @param init_key the array of 32-bit integers, used as a seed.
881     * @param key_length the length of init_key.
882     */
883     void
884     init_by_array (uint32_t * init_key, int key_length)
885     {
886     int i, j, count;
887     uint32_t r;
888     int lag;
889     int mid;
890     int size = N * 4;
891    
892     if (size >= 623)
893     {
894     lag = 11;
895     }
896     else if (size >= 68)
897     {
898     lag = 7;
899     }
900     else if (size >= 39)
901     {
902     lag = 5;
903     }
904     else
905     {
906     lag = 3;
907     }
908     mid = (size - lag) / 2;
909    
910     memset (sfmt, 0x8b, sizeof (sfmt));
911     if (key_length + 1 > N32)
912     {
913     count = key_length + 1;
914     }
915     else
916     {
917     count = N32;
918     }
919     r = func1 (psfmt32[idxof (0)] ^ psfmt32[idxof (mid)] ^ psfmt32[idxof (N32 - 1)]);
920     psfmt32[idxof (mid)] += r;
921     r += key_length;
922     psfmt32[idxof (mid + lag)] += r;
923     psfmt32[idxof (0)] = r;
924    
925     count--;
926     for (i = 1, j = 0; (j < count) && (j < key_length); j++)
927     {
928     r = func1 (psfmt32[idxof (i)] ^ psfmt32[idxof ((i + mid) % N32)] ^ psfmt32[idxof ((i + N32 - 1) % N32)]);
929     psfmt32[idxof ((i + mid) % N32)] += r;
930     r += init_key[j] + i;
931     psfmt32[idxof ((i + mid + lag) % N32)] += r;
932     psfmt32[idxof (i)] = r;
933     i = (i + 1) % N32;
934     }
935     for (; j < count; j++)
936     {
937     r = func1 (psfmt32[idxof (i)] ^ psfmt32[idxof ((i + mid) % N32)] ^ psfmt32[idxof ((i + N32 - 1) % N32)]);
938     psfmt32[idxof ((i + mid) % N32)] += r;
939     r += i;
940     psfmt32[idxof ((i + mid + lag) % N32)] += r;
941     psfmt32[idxof (i)] = r;
942     i = (i + 1) % N32;
943     }
944     for (j = 0; j < N32; j++)
945     {
946     r = func2 (psfmt32[idxof (i)] + psfmt32[idxof ((i + mid) % N32)] + psfmt32[idxof ((i + N32 - 1) % N32)]);
947     psfmt32[idxof ((i + mid) % N32)] ^= r;
948     r -= i;
949     psfmt32[idxof ((i + mid + lag) % N32)] ^= r;
950     psfmt32[idxof (i)] = r;
951     i = (i + 1) % N32;
952     }
953    
954     idx = N32;
955     period_certification ();
956     initialized = 1;
957     }