Back to index

numactl  2.0.8~rc4
mt.c
Go to the documentation of this file.
00001 /* Mersenne twister implementation from Michael Brundage. Public Domain.
00002    MT is a very fast pseudo random number generator. This version works
00003    on 32bit words.  Changes by AK. */
00004 #include <stdlib.h>
00005 #include "mt.h"
00006 
00007 int mt_index;
00008 unsigned int mt_buffer[MT_LEN];
00009 
00010 void mt_init(void)
00011 {
00012     int i;
00013     srand(1);
00014     for (i = 0; i < MT_LEN; i++)
00015         mt_buffer[i] = rand();
00016     mt_index = 0;
00017 }
00018 
00019 #define MT_IA           397
00020 #define MT_IB           (MT_LEN - MT_IA)
00021 #define UPPER_MASK      0x80000000
00022 #define LOWER_MASK      0x7FFFFFFF
00023 #define MATRIX_A        0x9908B0DF
00024 #define TWIST(b,i,j)    ((b)[i] & UPPER_MASK) | ((b)[j] & LOWER_MASK)
00025 #define MAGIC(s)        (((s)&1)*MATRIX_A)
00026 
00027 void mt_refill(void)
00028 {
00029        int i;
00030        unsigned int s;
00031        unsigned int * b = mt_buffer;
00032 
00033        mt_index = 0;
00034         i = 0;
00035         for (; i < MT_IB; i++) {
00036             s = TWIST(b, i, i+1);
00037             b[i] = b[i + MT_IA] ^ (s >> 1) ^ MAGIC(s);
00038         }
00039         for (; i < MT_LEN-1; i++) {
00040             s = TWIST(b, i, i+1);
00041             b[i] = b[i - MT_IB] ^ (s >> 1) ^ MAGIC(s);
00042         }
00043 
00044         s = TWIST(b, MT_LEN-1, 0);
00045         b[MT_LEN-1] = b[MT_IA-1] ^ (s >> 1) ^ MAGIC(s);
00046 }
00047