forked from MersenneTwister-Lab/MTGP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmtgp32-ref.c
More file actions
591 lines (560 loc) · 15.4 KB
/
mtgp32-ref.c
File metadata and controls
591 lines (560 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/**
* @file mtgp32-ref.c
*
* @brief Mersenne Twister for Graphic Processors (MTGP32), which
* generates 32-bit unsigned integers and single precision floating
* point numbers based on IEEE 754 format.
*
* @author Mutsuo Saito (Hiroshima University)
* @author Makoto Matsumoto (Hiroshima University)
*
* Copyright (C) 2009 Mutsuo Saito, Makoto Matsumoto and
* Hiroshima University. All rights reserved.
*
* The new BSD License is applied to this software, see LICENSE.txt
*/
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include "mtgp32-ref.h"
static uint32_t ini_func1(uint32_t x);
static uint32_t ini_func2(uint32_t x);
static void next_state(mtgp32_ref_t *mtgp32);
static const uint32_t non_zero = 0x4d544750;
/**
* This function represents a function used in the initialization
* by init_by_array
* @param[in] x 32-bit integer
* @return 32-bit integer
*/
static uint32_t ini_func1(uint32_t x) {
return (x ^ (x >> 27)) * UINT32_C(1664525);
}
/**
* This function represents a function used in the initialization
* by init_by_array
* @param[in] x 32-bit integer
* @return 32-bit integer
*/
static uint32_t ini_func2(uint32_t x) {
return (x ^ (x >> 27)) * UINT32_C(1566083941);
}
/**
* The state transition function.
* @param[in,out] mtgp32 the all in one structure
*/
static void next_state(mtgp32_ref_t *mtgp32) {
uint32_t *array = mtgp32->status->array;
int idx;
int size = mtgp32->status->size;
uint32_t x;
uint32_t y;
uint32_t yl;
mtgp32->status->idx += 1;
if (mtgp32->status->idx >= size) {
mtgp32->status->idx = 0;
}
idx = mtgp32->status->idx;
x = (array[idx] & mtgp32->params.mask) ^ array[(idx + 1) % size];
y = array[(idx + mtgp32->params.pos) % size];
x ^= x << mtgp32->params.sh1;
y = x ^ (y >> mtgp32->params.sh2);
yl = y & 0x0f;
if (yl & 1) {
y ^= mtgp32->params.tbl[0];
}
if (yl & 2) {
y ^= mtgp32->params.tbl[1];
}
if (yl & 4) {
y ^= mtgp32->params.tbl[2];
}
if (yl & 8) {
y ^= mtgp32->params.tbl[3];
}
array[idx] = y;
}
/**
* The tempering function.
* @param[in] tmp_tbl the pre-computed tempering table.
* @param[in] r the value to be tempered.
* @param[in] t the tempering helper value.
* @return the tempered value.
*/
static uint32_t temper(const uint32_t tmp_tbl[4], uint32_t r, uint32_t t) {
t ^= t >> 16;
t ^= t >> 8;
if (t & 1) {
r ^= tmp_tbl[0];
}
if (t & 2) {
r ^= tmp_tbl[1];
}
if (t & 4) {
r ^= tmp_tbl[2];
}
if (t & 8) {
r ^= tmp_tbl[3];
}
return r;
}
/*----------------
PUBLIC FUNCTIONS
----------------*/
/**
* This function allocates and initializes the internal state array
* with a 32-bit integer seed. The allocated memory should be freed by
* calling mtgp32_free(). \b para should be one of the elements in
* the parameter table (mtgp32-param-ref.c).
*
* @param[out] mtgp32 MTGP structure.
* @param[in] para parameter structure
* @param[in] seed a 32-bit integer used as the seed.
* @return memory allocation result. if 0 O.K.
*/
int mtgp32_init(mtgp32_ref_t *mtgp32,
const mtgp32_params_ref_t *para, uint32_t seed) {
int i;
int size = para->mexp / 32 + 1;
uint32_t hidden_seed;
uint32_t tmp;
mtgp32_status_ref_t *st;
st = (mtgp32_status_ref_t *)malloc(sizeof(mtgp32_status_ref_t)
+ sizeof(uint32_t) * size);
if (st == NULL) {
return -1;
}
hidden_seed = para->tbl[2] ^ (para->tbl[3] << 16);
tmp = hidden_seed;
tmp += tmp >> 16;
tmp += tmp >> 8;
memset(st->array, tmp & 0xff, sizeof(uint32_t) * size);
mtgp32->params = *para;
mtgp32->status = st;
st->size = size;
st->idx = size - 1;
st->array[0] = seed;
st->array[1] = hidden_seed;
for (i = 1; i < size; i++) {
st->array[i] ^= UINT32_C(1812433253) * (st->array[i - 1]
^ (st->array[i - 1] >> 30))
+ i;
}
return 0;
}
/**
* This function allocates and initializes the internal state array
* with a 32-bit integer array. The allocated memory should be freed by
* calling mtgp32_free(). \b para should be one of the elements in
* the parameter table (mtgp32-param-ref.c).
*
* @param[out] mtgp32 MTGP structure.
* @param[in] para parameter structure
* @param[in] array a 32-bit integer array used as a seed.
* @param[in] length length of the array.
* @return memory allocation result. if 0 O.K.
*/
int mtgp32_init_by_array(mtgp32_ref_t *mtgp32,
const mtgp32_params_ref_t *para,
const uint32_t *array, int length) {
int i, j, count;
uint32_t r;
int lag;
int mid;
int size = para->mexp / 32 + 1;
uint32_t hidden_seed;
uint32_t tmp;
mtgp32_status_ref_t *st;
st = (mtgp32_status_ref_t *)malloc(sizeof(mtgp32_status_ref_t)
+ sizeof(uint32_t) * size);
if (st == NULL) {
return -1;
}
if (size >= 623) {
lag = 11;
} else if (size >= 68) {
lag = 7;
} else if (size >= 39) {
lag = 5;
} else {
lag = 3;
}
mid = (size - lag) / 2;
hidden_seed = para->tbl[2] ^ (para->tbl[3] << 16);
tmp = hidden_seed;
tmp += tmp >> 16;
tmp += tmp >> 8;
memset(st->array, tmp & 0xff, sizeof(uint32_t) * size);
mtgp32->params = *para;
mtgp32->status = st;
st->size = size;
st->idx = size - 1;
st->array[0] = hidden_seed;
if (length + 1 > size) {
count = length + 1;
} else {
count = size;
}
r = ini_func1(st->array[0] ^ st->array[mid] ^ st->array[size - 1]);
st->array[mid] += r;
r += length;
st->array[(mid + lag) % size] += r;
st->array[0] = r;
i = 1;
count--;
for (i = 1, j = 0; (j < count) && (j < length); j++) {
r = ini_func1(st->array[i] ^ st->array[(i + mid) % size]
^ st->array[(i + size - 1) % size]);
st->array[(i + mid) % size] += r;
r += array[j] + i;
st->array[(i + mid + lag) % size] += r;
st->array[i] = r;
i = (i + 1) % size;
}
for (; j < count; j++) {
r = ini_func1(st->array[i] ^ st->array[(i + mid) % size]
^ st->array[(i + size - 1) % size]);
st->array[(i + mid) % size] += r;
r += i;
st->array[(i + mid + lag) % size] += r;
st->array[i] = r;
i = (i + 1) % size;
}
for (j = 0; j < size; j++) {
r = ini_func2(st->array[i] + st->array[(i + mid) % size]
+ st->array[(i + size - 1) % size]);
st->array[(i + mid) % size] ^= r;
r -= i;
st->array[(i + mid + lag) % size] ^= r;
st->array[i] = r;
i = (i + 1) % size;
}
if (st->array[size - 1] == 0) {
st->array[size - 1] = non_zero;
}
return 0;
}
/**
* This function allocates and initializes the internal state array
* with a character array. The allocated memory should be freed by
* calling mtgp32_free(). \b para should be one of the elements in
* the parameter table (mtgp32-param-ref.c).
* This is the same algorithm with mtgp32_init_by_array(), but hope to
* be more useful.
*
* @param[out] mtgp32 MTGP structure.
* @param[in] para parameter structure
* @param[in] array a character array used as a seed. (terminated by zero.)
* @return memory allocation result. if 0 O.K.
*/
int mtgp32_init_by_str(mtgp32_ref_t *mtgp32,
const mtgp32_params_ref_t *para, const char *array) {
int i, j, count;
uint32_t r;
int lag;
int mid;
int size = para->mexp / 32 + 1;
int length = strlen(array);
uint32_t hidden_seed;
uint32_t tmp;
mtgp32_status_ref_t *st;
st = (mtgp32_status_ref_t *)malloc(sizeof(mtgp32_status_ref_t)
+ sizeof(uint32_t) * size);
if (st == NULL) {
return -1;
}
if (size >= 623) {
lag = 11;
} else if (size >= 68) {
lag = 7;
} else if (size >= 39) {
lag = 5;
} else {
lag = 3;
}
mid = (size - lag) / 2;
hidden_seed = para->tbl[2] ^ (para->tbl[3] << 16);
tmp = hidden_seed;
tmp += tmp >> 16;
tmp += tmp >> 8;
memset(st->array, tmp & 0xff, sizeof(uint32_t) * size);
mtgp32->params = *para;
mtgp32->status = st;
st->size = size;
st->idx = size - 1;
st->array[0] = hidden_seed;
if (length + 1 > size) {
count = length + 1;
} else {
count = size;
}
r = ini_func1(st->array[0] ^ st->array[mid] ^ st->array[size - 1]);
st->array[mid] += r;
r += length;
st->array[(mid + lag) % size] += r;
st->array[0] = r;
i = 1;
count--;
for (i = 1, j = 0; (j < count) && (j < length); j++) {
r = ini_func1(st->array[i] ^ st->array[(i + mid) % size]
^ st->array[(i + size - 1) % size]);
st->array[(i + mid) % size] += r;
r += array[j] + i;
st->array[(i + mid + lag) % size] += r;
st->array[i] = r;
i = (i + 1) % size;
}
for (; j < count; j++) {
r = ini_func1(st->array[i] ^ st->array[(i + mid) % size]
^ st->array[(i + size - 1) % size]);
st->array[(i + mid) % size] += r;
r += i;
st->array[(i + mid + lag) % size] += r;
st->array[i] = r;
i = (i + 1) % size;
}
for (j = 0; j < size; j++) {
r = ini_func2(st->array[i] + st->array[(i + mid) % size]
+ st->array[(i + size - 1) % size]);
st->array[(i + mid) % size] ^= r;
r -= i;
st->array[(i + mid + lag) % size] ^= r;
st->array[i] = r;
i = (i + 1) % size;
}
if (st->array[size - 1] == 0) {
st->array[size - 1] = non_zero;
}
return 0;
}
/**
* This releases the memory allocated by mtgp32_init(), mtgp32_init_by_array(),
* mtgp32_init_by_str().
*
* @param[in,out] mtgp32 MTGP all in one structure.
*/
void mtgp32_free(mtgp32_ref_t *mtgp32) {
free(mtgp32->status);
}
/**
* This function prints the Mersenne exponent and SHA1 of characteristic
* polynomial of generators state transition function.
*
* @param[in] mtgp32 MTGP all in one structure.
* @param[in,out] fp FILE pointer.
*/
void mtgp32_print_idstring(const mtgp32_ref_t *mtgp32, FILE *fp) {
int i;
fprintf(fp, "mtgp32:%d:", mtgp32->params.mexp);
for (i = 0; i < 20; i++) {
fprintf(fp, "%02x", (unsigned int)mtgp32->params.poly_sha1[i]);
}
fprintf(fp, "\n");
}
/**
* This function generates and returns 32-bit unsigned integer.
* mtgp32_init(), mtgp32_init_by_array() or mtgp32_init_by_str() must
* be called before this function.
*
* @param[in,out] mtgp32 MTGP all in one structure.
* @return 32-bit unsigned integer.
*/
uint32_t mtgp32_genrand_uint32(mtgp32_ref_t *mtgp32) {
next_state(mtgp32);
return temper(mtgp32->params.tmp_tbl,
mtgp32->status->array[mtgp32->status->idx],
mtgp32->status->array[(mtgp32->status->idx
+ mtgp32->params.pos - 1)
% mtgp32->status->size]);
}
/**
* This function generates and returns single precision pseudorandom
* number which distributes uniformly in the range [1, 2).
* mtgp32_init(), mtgp32_init_by_array() or mtgp32_init_by_str() must
* be called before this function.
*
* @param[in,out] mtgp32 MTGP all in one structure.
* @return single precision floating point pseudorandom number
*/
float mtgp32_genrand_close1_open2(mtgp32_ref_t *mtgp32) {
union {
uint32_t u;
float f;
} x;
x.u = mtgp32_genrand_uint32(mtgp32);
x.u = (x.u >> 9) | UINT32_C(0x3f800000);
return x.f;
}
/**
* This function generates and returns single precision pseudorandom
* number which distributes uniformly in the range [0, 1).
* mtgp32_init(), mtgp32_init_by_array() or mtgp32_init_by_str() must
* be called before this function.
*
* @param[in,out] mtgp32 MTGP all in one structure.
* @return single precision floating point pseudorandom number
*/
float mtgp32_genrand_close_open(mtgp32_ref_t *mtgp32) {
return mtgp32_genrand_close1_open2(mtgp32) - 1.0F;
}
/**
* This function generates and returns single precision pseudorandom
* number which distributes uniformly in the range (0, 1].
* mtgp32_init(), mtgp32_init_by_array() or mtgp32_init_by_str() must
* be called before this function.
*
* @param[in,out] mtgp32 MTGP all in one structure.
* @return single precision floating point pseudorandom number
*/
float mtgp32_genrand_open_close(mtgp32_ref_t *mtgp32) {
return 2.0F - mtgp32_genrand_close1_open2(mtgp32);
}
/**
* This function generates and returns single precision pseudorandom
* number which distributes uniformly in the range (0, 1).
* mtgp32_init(), mtgp32_init_by_array() or mtgp32_init_by_str() must
* be called before this function.
*
* @param[in,out] mtgp32 MTGP all in one structure.
* @return single precision floating point pseudorandom number
*/
float mtgp32_genrand_open_open(mtgp32_ref_t *mtgp32) {
union {
uint32_t u;
float f;
} x;
x.u = mtgp32_genrand_uint32(mtgp32);
x.u = (x.u >> 9) | UINT32_C(0x3f800001);
return x.f - 1.0F;
}
#if defined(MAIN)
#include <errno.h>
void print_uint32(mtgp32_ref_t *mtgp32, int count);
void print_close1_open2(mtgp32_ref_t *mtgp32, int count);
void print_close_open(mtgp32_ref_t *mtgp32, int count);
void print_open_close(mtgp32_ref_t *mtgp32, int count);
void print_open_open(mtgp32_ref_t *mtgp32, int count);
void print_uint32(mtgp32_ref_t *mtgp32, int count) {
int i;
for (i = 0; i < count; i++) {
printf("%10"PRIu32" ", mtgp32_genrand_uint32(mtgp32));
if (i % 5 == 4) {
printf("\n");
}
}
if (i % 5 != 0) {
printf("\n");
}
}
void print_close1_open2(mtgp32_ref_t *mtgp32, int count) {
int i;
for (i = 0; i < count; i++) {
printf("%.8f ", mtgp32_genrand_close1_open2(mtgp32));
if (i % 5 == 4) {
printf("\n");
}
}
if (i % 5 != 0) {
printf("\n");
}
printf("\n");
}
void print_close_open(mtgp32_ref_t *mtgp32, int count) {
int i;
for (i = 0; i < count; i++) {
printf("%.8f ", mtgp32_genrand_close_open(mtgp32));
if (i % 5 == 4) {
printf("\n");
}
}
if (i % 5 != 0) {
printf("\n");
}
}
void print_open_close(mtgp32_ref_t *mtgp32, int count) {
int i;
for (i = 0; i < count; i++) {
printf("%.8f ", mtgp32_genrand_open_close(mtgp32));
if (i % 5 == 4) {
printf("\n");
}
}
if (i % 5 != 0) {
printf("\n");
}
}
void print_open_open(mtgp32_ref_t *mtgp32, int count) {
int i;
for (i = 0; i < count; i++) {
printf("%.8f ", mtgp32_genrand_open_open(mtgp32));
if (i % 5 == 4) {
printf("\n");
}
}
if (i % 5 != 0) {
printf("\n");
}
}
int main(int argc, char *argv[]) {
int mexp;
int no;
uint32_t seed = 1;
uint32_t seed_ar[4] = {1, 2, 3, 4};
char seed_str[] = "\01\02\03\04";
mtgp32_params_ref_t *params;
mtgp32_ref_t mtgp32;
if (argc <= 2) {
printf("%s: mexp no.\n", argv[0]);
return 1;
}
mexp = strtol(argv[1], NULL, 10);
if (errno) {
printf("%s: mexp no.\n", argv[0]);
return 2;
}
no = strtol(argv[2], NULL, 10);
if (errno) {
printf("%s: mexp no.\n", argv[0]);
return 3;
}
switch (mexp) {
case 11213:
params = mtgp32_params_ref_11213;
break;
case 23209:
params = mtgp32_params_ref_23209;
break;
case 44497:
params = mtgp32_params_ref_44497;
break;
default:
printf("%s: mexp no.\n", argv[0]);
printf("mexp should be 11213, 23209 or 44497 only\n");
return 4;
}
if (no >= 128 || no < 0) {
printf("%s: mexp no.\n", argv[0]);
printf("no must be between 0 and 127\n");
return 5;
}
params += no;
mtgp32_init(&mtgp32, params, seed);
mtgp32_print_idstring(&mtgp32, stdout);
printf("init:\n");
print_uint32(&mtgp32, 1000);
mtgp32_init_by_array(&mtgp32, params, seed_ar, 4);
printf("init_array:\n");
print_uint32(&mtgp32, 1000);
mtgp32_init_by_str(&mtgp32, params, seed_str);
printf("init_str:\n");
print_uint32(&mtgp32, 1000);
print_close1_open2(&mtgp32, 1000);
print_close_open(&mtgp32, 1000);
print_open_close(&mtgp32, 1000);
print_open_open(&mtgp32, 1000);
mtgp32_free(&mtgp32);
return 0;
}
#endif