Ruby  3.4.0dev (2024-11-05 revision 348a53415339076afc4a02fcd09f3ae36e9c4c61)
mt19937.c
1 /*
2 This is based on trimmed version of MT19937. To get the original version,
3 contact <http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html>.
4 
5 The original copyright notice follows.
6 
7  A C-program for MT19937, with initialization improved 2002/2/10.
8  Coded by Takuji Nishimura and Makoto Matsumoto.
9  This is a faster version by taking Shawn Cokus's optimization,
10  Matthe Bellew's simplification, Isaku Wada's real version.
11 
12  Before using, initialize the state by using init_genrand(mt, seed)
13  or init_by_array(mt, init_key, key_length).
14 
15  Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
16  All rights reserved.
17 
18  Redistribution and use in source and binary forms, with or without
19  modification, are permitted provided that the following conditions
20  are met:
21 
22  1. Redistributions of source code must retain the above copyright
23  notice, this list of conditions and the following disclaimer.
24 
25  2. Redistributions in binary form must reproduce the above copyright
26  notice, this list of conditions and the following disclaimer in the
27  documentation and/or other materials provided with the distribution.
28 
29  3. The names of its contributors may not be used to endorse or promote
30  products derived from this software without specific prior written
31  permission.
32 
33  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
36  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
37  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
38  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
39  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
40  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
41  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
42  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
43  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 
45 
46  Any feedback is very welcome.
47  http://www.math.keio.ac.jp/matumoto/emt.html
48  email: matumoto@math.keio.ac.jp
49 */
50 
51 /* Period parameters */
52 #define N 624
53 #define M 397
54 #define MATRIX_A 0x9908b0dfU /* constant vector a */
55 #define UMASK 0x80000000U /* most significant w-r bits */
56 #define LMASK 0x7fffffffU /* least significant r bits */
57 #define MIXBITS(u,v) ( ((u) & UMASK) | ((v) & LMASK) )
58 #define TWIST(u,v) ((MIXBITS((u),(v)) >> 1) ^ ((v)&1U ? MATRIX_A : 0U))
59 
60 enum {MT_MAX_STATE = N};
61 
62 struct MT {
63  /* assume int is enough to store 32bits */
64  uint32_t state[N]; /* the array for the state vector */
65  uint32_t *next;
66  int left;
67 };
68 
69 #define genrand_initialized(mt) ((mt)->next != 0)
70 #define uninit_genrand(mt) ((mt)->next = 0)
71 
72 NO_SANITIZE("unsigned-integer-overflow", static void init_genrand(struct MT *mt, unsigned int s));
73 NO_SANITIZE("unsigned-integer-overflow", static void init_by_array(struct MT *mt, const uint32_t init_key[], int key_length));
74 
75 /* initializes state[N] with a seed */
76 static void
77 init_genrand(struct MT *mt, unsigned int s)
78 {
79  int j;
80  mt->state[0] = s & 0xffffffffU;
81  for (j=1; j<N; j++) {
82  mt->state[j] = (1812433253U * (mt->state[j-1] ^ (mt->state[j-1] >> 30)) + j);
83  /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
84  /* In the previous versions, MSBs of the seed affect */
85  /* only MSBs of the array state[]. */
86  /* 2002/01/09 modified by Makoto Matsumoto */
87  mt->state[j] &= 0xffffffff; /* for >32 bit machines */
88  }
89  mt->left = 1;
90  mt->next = mt->state + N;
91 }
92 
93 /* initialize by an array with array-length */
94 /* init_key is the array for initializing keys */
95 /* key_length is its length */
96 /* slight change for C++, 2004/2/26 */
97 static void
98 init_by_array(struct MT *mt, const uint32_t init_key[], int key_length)
99 {
100  int i, j, k;
101  init_genrand(mt, 19650218U);
102  i=1; j=0;
103  k = (N>key_length ? N : key_length);
104  for (; k; k--) {
105  mt->state[i] = (mt->state[i] ^ ((mt->state[i-1] ^ (mt->state[i-1] >> 30)) * 1664525U))
106  + init_key[j] + j; /* non linear */
107  mt->state[i] &= 0xffffffffU; /* for WORDSIZE > 32 machines */
108  i++; j++;
109  if (i>=N) { mt->state[0] = mt->state[N-1]; i=1; }
110  if (j>=key_length) j=0;
111  }
112  for (k=N-1; k; k--) {
113  mt->state[i] = (mt->state[i] ^ ((mt->state[i-1] ^ (mt->state[i-1] >> 30)) * 1566083941U))
114  - i; /* non linear */
115  mt->state[i] &= 0xffffffffU; /* for WORDSIZE > 32 machines */
116  i++;
117  if (i>=N) { mt->state[0] = mt->state[N-1]; i=1; }
118  }
119 
120  mt->state[0] = 0x80000000U; /* MSB is 1; assuring non-zero initial array */
121 }
122 
123 static void
124 next_state(struct MT *mt)
125 {
126  uint32_t *p = mt->state;
127  int j;
128 
129  mt->left = N;
130  mt->next = mt->state;
131 
132  for (j=N-M+1; --j; p++)
133  *p = p[M] ^ TWIST(p[0], p[1]);
134 
135  for (j=M; --j; p++)
136  *p = p[M-N] ^ TWIST(p[0], p[1]);
137 
138  *p = p[M-N] ^ TWIST(p[0], mt->state[0]);
139 }
140 
141 /* generates a random number on [0,0xffffffff]-interval */
142 static unsigned int
143 genrand_int32(struct MT *mt)
144 {
145  /* mt must be initialized */
146  unsigned int y;
147 
148  if (--mt->left <= 0) next_state(mt);
149  y = *mt->next++;
150 
151  /* Tempering */
152  y ^= (y >> 11);
153  y ^= (y << 7) & 0x9d2c5680;
154  y ^= (y << 15) & 0xefc60000;
155  y ^= (y >> 18);
156 
157  return y;
158 }
Definition: mt19937.c:62