_mt.c

filter: Statementstmt 86.1% 81 / 94 Branchbran 68.1% 30 / 44 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.4% 111 / 138 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« RdGen.xs.inc ↑ Summary CaseResampling.xs »
1 #include "mt.h"
2  
3 #include <errno.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6  
7 #ifdef _WIN32
8 # ifndef WIN32_LEAN_AND_MEAN
9 # define WIN32_LEAN_AND_MEAN
10 # endif
11 # include <windows.h>
12 #elif !defined(__VMS) && (defined(__unix__) || defined(__unix) || defined(__APPLE__) || defined(__linux__) || defined(__CYGWIN__))
13 # include <sys/types.h>
14 # include <fcntl.h>
15 # include <unistd.h>
16 # define CS_HAVE_URANDOM 1
17 #endif
18  
19 /* This code is based on mt19937ar.c, written by Takuji Nishimura and
20 Makoto Matsumoto (20020126). Further details are available at
21 <http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html>.
22  
23 REFERENCE
24 M. Matsumoto and T. Nishimura,
25 "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform
26 Pseudo-Random Number Generator",
27 ACM Transactions on Modeling and Computer Simulation,
28 Vol. 8, No. 1, January 1998, pp 3--30. */
29  
30 18 void mt_init_seed( struct mt *m, uint32_t seed )
31 {
32 int i;
33 uint32_t *mt;
34  
35 18 mt = m->mt;
36 18 mt[0] = seed & 0xffffffff;
37 11232 ▶ for ( i = 1; i < N; i++ )
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 1; i < N; i++ ) 11214 18 11232
38 11214 mt[i] = 1812433253 * (mt[i-1]^(mt[i-1]>>30)) + i;
39 18 m->mti = N;
40 18 }
41  
42 3 struct mt *mt_setup(uint32_t seed)
43 {
44 3 struct mt *self = malloc(sizeof(struct mt));
45  
46 3 ▶ if (self) {
Branch: 1 branch
BranchTrueFalseTotal
if (self) { 3 0 3
47 3 mt_init_seed( self, seed );
48 3 self->auto_seeded = 0;
49 3 self->pid = 0;
50 }
51  
52 3 return self;
53 }
54  
55 15 struct mt *mt_setup_array( uint32_t *array, int n )
56 {
57 int i, j, k;
58 15 struct mt *self = malloc(sizeof(struct mt));
59 uint32_t *mt;
60  
61 15 ▶ if (self) {
Branch: 1 branch
BranchTrueFalseTotal
if (self) { 15 0 15
62 15 mt_init_seed( self, 19650218UL );
63 15 self->auto_seeded = 0;
64 15 self->pid = 0;
65  
66 15 i = 1; j = 0;
67 15 k = ( N > n ? N : n );
68 15 mt = self->mt;
69  
70 9375 ▶ for (; k; k--) {
Branch: 1 branch
BranchTrueFalseTotal
for (; k; k--) { 9360 15 9375
71 9360 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
72 9360 + array[j] + j;
73 9360 i++; j++;
74 9360 ▶ if (i>=N) { mt[0] = mt[N-1]; i=1; }
Branch: 1 branch
BranchTrueFalseTotal
if (i>=N) { mt[0] = mt[N-1]; i=1; } 15 9345 9360
75 9360 ▶ if (j>=n) j=0;
Branch: 1 branch
BranchTrueFalseTotal
if (j>=n) j=0; 2204 7156 9360
76 }
77 9360 ▶ for (k=N-1; k; k--) {
Branch: 1 branch
BranchTrueFalseTotal
for (k=N-1; k; k--) { 9345 15 9360
78 9345 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL)) - i;
79 9345 i++;
80 9345 ▶ if (i>=N) { mt[0] = mt[N-1]; i=1; }
Branch: 1 branch
BranchTrueFalseTotal
if (i>=N) { mt[0] = mt[N-1]; i=1; } 15 9330 9345
81 }
82  
83 15 mt[0] = 0x80000000UL;
84 }
85  
86 15 return self;
87 }
88  
89 18 void mt_free(struct mt *self)
90 {
91 18 ▶ if (self)
Branch: 1 branch
BranchTrueFalseTotal
if (self) 18 0 18
92 18 free(self);
93 18 }
94  
95 14 int mt_os_entropy(uint32_t *out, int nwords)
96 {
97 14 ▶ if (out == NULL || nwords <= 0)
Branch: 2 branches
BranchTrueFalseTotal
if (out == NULL || nwords <= 0) 14 0 14
if (out == NULL || nwords <= 0) 0 14 14
98 0 return 0;
99 #ifdef _WIN32
100 {
101 typedef long (__stdcall *cs_bcrypt_fn)(void *, unsigned char *, unsigned long, unsigned long);
102 typedef unsigned char (__stdcall *cs_rtl_fn)(void *, unsigned long);
103 HMODULE hBcrypt;
104 HMODULE hAdvapi;
105 unsigned long nbytes = (unsigned long)((size_t)nwords * sizeof(uint32_t));
106  
107 hBcrypt = LoadLibraryA("bcrypt.dll");
108 if (hBcrypt != NULL) {
109 cs_bcrypt_fn fn = (cs_bcrypt_fn)GetProcAddress(hBcrypt, "BCryptGenRandom");
110 if (fn != NULL && fn(NULL, (unsigned char *)out, nbytes, 0x00000002UL) == 0) {
111 FreeLibrary(hBcrypt);
112 return 1;
113 }
114 FreeLibrary(hBcrypt);
115 }
116  
117 hAdvapi = LoadLibraryA("advapi32.dll");
118 if (hAdvapi != NULL) {
119 cs_rtl_fn fn = (cs_rtl_fn)GetProcAddress(hAdvapi, "SystemFunction036");
120 if (fn != NULL && fn(out, nbytes) != 0) {
121 FreeLibrary(hAdvapi);
122 return 1;
123 }
124 FreeLibrary(hAdvapi);
125 }
126 return 0;
127 }
128 #elif defined(CS_HAVE_URANDOM)
129 {
130 14 int saved_errno = errno;
131 int fd;
132 14 unsigned char *ptr = (unsigned char *)out;
133 14 size_t need = (size_t)nwords * sizeof(uint32_t);
134 14 size_t got = 0;
135  
136 14 fd = open("/dev/urandom", O_RDONLY);
137 14 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 0 14 14
138 0 errno = saved_errno;
139 0 return 0;
140 }
141 28 ▶ while (got < need) {
Branch: 1 branch
BranchTrueFalseTotal
while (got < need) { 14 14 28
142 14 ssize_t r = read(fd, ptr + got, need - got);
143 14 ▶ if (r < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (r < 0) { 0 14 14
144 0 ▶ if (errno == EINTR)
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) 0 0 0
145 0 continue;
146 0 break;
147 }
148 14 ▶ if (r == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (r == 0) 0 14 14
149 0 break;
150 14 got += (size_t)r;
151 }
152 14 close(fd);
153 14 errno = saved_errno;
154 14 return (got == need) ? 1 : 0;
155 }
156 #else
157 return 0;
158 #endif
159 }
160  
161 /* Returns a uniformly distributed 32-bit pseudorandom integer */
162 91378 uint32_t mt_genrand_int32(struct mt *self)
163 {
164 int kk;
165 uint32_t y;
166 static uint32_t mag01[2] = {0x0, 0x9908b0df};
167 static const uint32_t UP_MASK = 0x80000000, LOW_MASK = 0x7fffffff;
168  
169 91378 ▶ if (self->mti >= N) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->mti >= N) { 159 91219 91378
170 36252 ▶ for (kk = 0; kk < N-M; kk++) {
Branch: 1 branch
BranchTrueFalseTotal
for (kk = 0; kk < N-M; kk++) { 36093 159 36252
171 36093 y = (self->mt[kk] & UP_MASK) | (self->mt[kk+1] & LOW_MASK);
172 36093 self->mt[kk] = self->mt[kk+M] ^ (y >> 1) ^ mag01[y & 1];
173 }
174  
175 63123 ▶ for (; kk < N-1; kk++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; kk < N-1; kk++) { 62964 159 63123
176 62964 y = (self->mt[kk] & UP_MASK) | (self->mt[kk+1] & LOW_MASK);
177 62964 self->mt[kk] = self->mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 1];
178 }
179  
180 159 y = (self->mt[N-1] & UP_MASK) | (self->mt[0] & LOW_MASK);
181 159 self->mt[N-1] = self->mt[M-1] ^ (y >> 1) ^ mag01[y & 1];
182  
183 159 self->mti = 0;
184 }
185  
186 91378 y = self->mt[self->mti++];
187 91378 y ^= y >> 11;
188 91378 y ^= y << 7 & 0x9d2c5680;
189 91378 y ^= y << 15 & 0xefc60000;
190 91378 y ^= y >> 18;
191  
192 91378 return y;
193 }
194  
195 /* Returns an unbiased pseudorandom integer in [0, range) using Lemire's method */
196 91371 uint32_t mt_genrand_bounded(struct mt *self, uint32_t range)
197 {
198 uint32_t x, l;
199 cs_u64 m;
200 91371 ▶ if (range <= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (range <= 1) 0 91371 91371
201 0 return 0;
202 91371 x = mt_genrand_int32(self);
203 91371 m = (cs_u64)x * (cs_u64)range;
204 91371 l = (uint32_t)m;
205 91371 ▶ if (l < range) {
Branch: 1 branch
BranchTrueFalseTotal
if (l < range) { 0 91371 91371
206 0 uint32_t t = (uint32_t)(0u - range) % range;
207 0 ▶ while (l < t) {
Branch: 1 branch
BranchTrueFalseTotal
while (l < t) { 0 0 0
208 0 x = mt_genrand_int32(self);
209 0 m = (cs_u64)x * (cs_u64)range;
210 0 l = (uint32_t)m;
211 }
212 }
213 91371 return (uint32_t)(m >> 32);
214 }
215  
216 /* Returns a pseudorandom number which is uniformly distributed in [0,1) */
217 7 double mt_genrand(struct mt *self)
218 {
219 7 return mt_genrand_int32(self) * (1.0 / 4294967296.0);
220 }
« RdGen.xs.inc ↑ Summary CaseResampling.xs »