Coverage Report

Created: 2026-06-03 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ruby/missing/mt19937.c
Line
Count
Source
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
0
#define N 624
53
0
#define M 397
54
0
#define MATRIX_A 0x9908b0dfU  /* constant vector a */
55
0
#define UMASK 0x80000000U  /* most significant w-r bits */
56
0
#define LMASK 0x7fffffffU  /* least significant r bits */
57
0
#define MIXBITS(u,v) ( ((u) & UMASK) | ((v) & LMASK) )
58
0
#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
0
#define genrand_initialized(mt) ((mt)->next != 0)
70
0
#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
0
{
79
0
    int j;
80
0
    mt->state[0] = s & 0xffffffffU;
81
0
    for (j=1; j<N; j++) {
82
0
        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
0
        mt->state[j] &= 0xffffffff;  /* for >32 bit machines */
88
0
    }
89
0
    mt->left = 1;
90
0
    mt->next = mt->state + N;
91
0
}
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
0
{
100
0
    int i, j, k;
101
0
    init_genrand(mt, 19650218U);
102
0
    i=1; j=0;
103
0
    k = (N>key_length ? N : key_length);
104
0
    for (; k; k--) {
105
0
        mt->state[i] = (mt->state[i] ^ ((mt->state[i-1] ^ (mt->state[i-1] >> 30)) * 1664525U))
106
0
          + init_key[j] + j; /* non linear */
107
0
        mt->state[i] &= 0xffffffffU; /* for WORDSIZE > 32 machines */
108
0
        i++; j++;
109
0
        if (i>=N) { mt->state[0] = mt->state[N-1]; i=1; }
110
0
        if (j>=key_length) j=0;
111
0
    }
112
0
    for (k=N-1; k; k--) {
113
0
        mt->state[i] = (mt->state[i] ^ ((mt->state[i-1] ^ (mt->state[i-1] >> 30)) * 1566083941U))
114
0
          - i; /* non linear */
115
0
        mt->state[i] &= 0xffffffffU; /* for WORDSIZE > 32 machines */
116
0
        i++;
117
0
        if (i>=N) { mt->state[0] = mt->state[N-1]; i=1; }
118
0
    }
119
120
0
    mt->state[0] = 0x80000000U; /* MSB is 1; assuring non-zero initial array */
121
0
}
122
123
static void
124
next_state(struct MT *mt)
125
0
{
126
0
    uint32_t *p = mt->state;
127
0
    int j;
128
129
0
    mt->left = N;
130
0
    mt->next = mt->state;
131
132
0
    for (j=N-M+1; --j; p++)
133
0
        *p = p[M] ^ TWIST(p[0], p[1]);
134
135
0
    for (j=M; --j; p++)
136
0
        *p = p[M-N] ^ TWIST(p[0], p[1]);
137
138
0
    *p = p[M-N] ^ TWIST(p[0], mt->state[0]);
139
0
}
140
141
/* generates a random number on [0,0xffffffff]-interval */
142
static unsigned int
143
genrand_int32(struct MT *mt)
144
0
{
145
    /* mt must be initialized */
146
0
    unsigned int y;
147
148
0
    if (--mt->left <= 0) next_state(mt);
149
0
    y = *mt->next++;
150
151
    /* Tempering */
152
0
    y ^= (y >> 11);
153
0
    y ^= (y << 7) & 0x9d2c5680;
154
0
    y ^= (y << 15) & 0xefc60000;
155
0
    y ^= (y >> 18);
156
157
0
    return y;
158
0
}