_mt.c

filter: Statementstmt 87.2% 82 / 94 Branchbran 70.4% 31 / 44 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.8% 113 / 138 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« _stats.c ↑ 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 23 void mt_init_seed( struct mt *m, uint32_t seed )
31 {
32 int i;
33 uint32_t *mt;
34  
35 23 mt = m->mt;
36 23 mt[0] = seed & 0xffffffff;
37 14352 ▶ for ( i = 1; i < N; i++ )
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 1; i < N; i++ ) 14329 23 14352
38 14329 mt[i] = 1812433253 * (mt[i-1]^(mt[i-1]>>30)) + i;
39 23 m->mti = N;
40 23 }
41  
42 7 struct mt *mt_setup(uint32_t seed)
43 {
44 7 struct mt *self = malloc(sizeof(struct mt));
45  
46 7 ▶ if (self) {
Branch: 1 branch
BranchTrueFalseTotal
if (self) { 7 0 7
47 7 mt_init_seed( self, seed );
48 7 self->auto_seeded = 0;
49 7 self->pid = 0;
50 }
51  
52 7 return self;
53 }
54  
55 16 struct mt *mt_setup_array( uint32_t *array, int n )
56 {
57 int i, j, k;
58 16 struct mt *self = malloc(sizeof(struct mt));
59 uint32_t *mt;
60  
61 16 ▶ if (self) {
Branch: 1 branch
BranchTrueFalseTotal
if (self) { 16 0 16
62 16 mt_init_seed( self, 19650218UL );
63 16 self->auto_seeded = 0;
64 16 self->pid = 0;
65  
66 16 i = 1; j = 0;
67 16 k = ( N > n ? N : n );
68 16 mt = self->mt;
69  
70 10000 ▶ for (; k; k--) {
Branch: 1 branch
BranchTrueFalseTotal
for (; k; k--) { 9984 16 10000
71 9984 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
72 9984 + array[j] + j;
73 9984 i++; j++;
74 9984 ▶ if (i>=N) { mt[0] = mt[N-1]; i=1; }
Branch: 1 branch
BranchTrueFalseTotal
if (i>=N) { mt[0] = mt[N-1]; i=1; } 16 9968 9984
75 9984 ▶ if (j>=n) j=0;
Branch: 1 branch
BranchTrueFalseTotal
if (j>=n) j=0; 2360 7624 9984
76 }
77 9984 ▶ for (k=N-1; k; k--) {
Branch: 1 branch
BranchTrueFalseTotal
for (k=N-1; k; k--) { 9968 16 9984
78 9968 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL)) - i;
79 9968 i++;
80 9968 ▶ if (i>=N) { mt[0] = mt[N-1]; i=1; }
Branch: 1 branch
BranchTrueFalseTotal
if (i>=N) { mt[0] = mt[N-1]; i=1; } 16 9952 9968
81 }
82  
83 16 mt[0] = 0x80000000UL;
84 }
85  
86 16 return self;
87 }
88  
89 23 void mt_free(struct mt *self)
90 {
91 23 ▶ if (self)
Branch: 1 branch
BranchTrueFalseTotal
if (self) 23 0 23
92 23 free(self);
93 23 }
94  
95 15 int mt_os_entropy(uint32_t *out, int nwords)
96 {
97 15 ▶ if (out == NULL || nwords <= 0)
Branch: 2 branches
BranchTrueFalseTotal
if (out == NULL || nwords <= 0) 15 0 15
if (out == NULL || nwords <= 0) 0 15 15
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 15 int saved_errno = errno;
131 int fd;
132 15 unsigned char *ptr = (unsigned char *)out;
133 15 size_t need = (size_t)nwords * sizeof(uint32_t);
134 15 size_t got = 0;
135  
136 15 fd = open("/dev/urandom", O_RDONLY);
137 15 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 0 15 15
138 0 errno = saved_errno;
139 0 return 0;
140 }
141 30 ▶ while (got < need) {
Branch: 1 branch
BranchTrueFalseTotal
while (got < need) { 15 15 30
142 15 ssize_t r = read(fd, ptr + got, need - got);
143 15 ▶ if (r < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (r < 0) { 0 15 15
144 0 ▶ if (errno == EINTR)
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) 0 0 0
145 0 continue;
146 0 break;
147 }
148 15 ▶ if (r == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (r == 0) 0 15 15
149 0 break;
150 15 got += (size_t)r;
151 }
152 15 close(fd);
153 15 errno = saved_errno;
154 15 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 14541138 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 14541138 ▶ if (self->mti >= N) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->mti >= N) { 23318 14517820 14541138
170 5316504 ▶ for (kk = 0; kk < N-M; kk++) {
Branch: 1 branch
BranchTrueFalseTotal
for (kk = 0; kk < N-M; kk++) { 5293186 23318 5316504
171 5293186 y = (self->mt[kk] & UP_MASK) | (self->mt[kk+1] & LOW_MASK);
172 5293186 self->mt[kk] = self->mt[kk+M] ^ (y >> 1) ^ mag01[y & 1];
173 }
174  
175 9257246 ▶ for (; kk < N-1; kk++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; kk < N-1; kk++) { 9233928 23318 9257246
176 9233928 y = (self->mt[kk] & UP_MASK) | (self->mt[kk+1] & LOW_MASK);
177 9233928 self->mt[kk] = self->mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 1];
178 }
179  
180 23318 y = (self->mt[N-1] & UP_MASK) | (self->mt[0] & LOW_MASK);
181 23318 self->mt[N-1] = self->mt[M-1] ^ (y >> 1) ^ mag01[y & 1];
182  
183 23318 self->mti = 0;
184 }
185  
186 14541138 y = self->mt[self->mti++];
187 14541138 y ^= y >> 11;
188 14541138 y ^= y << 7 & 0x9d2c5680;
189 14541138 y ^= y << 15 & 0xefc60000;
190 14541138 y ^= y >> 18;
191  
192 14541138 return y;
193 }
194  
195 /* Returns an unbiased pseudorandom integer in [0, range) using Lemire's method */
196 14523151 uint32_t mt_genrand_bounded(struct mt *self, uint32_t range)
197 {
198 uint32_t x, l;
199 cs_u64 m;
200 14523151 ▶ if (range <= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (range <= 1) 20 14523131 14523151
201 20 return 0;
202 14523131 x = mt_genrand_int32(self);
203 14523131 m = (cs_u64)x * (cs_u64)range;
204 14523131 l = (uint32_t)m;
205 14523131 ▶ if (l < range) {
Branch: 1 branch
BranchTrueFalseTotal
if (l < range) { 0 14523131 14523131
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 14523131 return (uint32_t)(m >> 32);
214 }
215  
216 /* Returns a pseudorandom number which is uniformly distributed in [0,1) */
217 18007 double mt_genrand(struct mt *self)
218 {
219 18007 return mt_genrand_int32(self) * (1.0 / 4294967296.0);
220 }
« _stats.c ↑ Summary CaseResampling.xs »