[4019] | 1 | /**
|
---|
| 2 | * @file tinymt64.c
|
---|
| 3 | *
|
---|
| 4 | * @brief 64-bit Tiny Mersenne Twister only 127 bit internal state
|
---|
| 5 | *
|
---|
| 6 | * @author Mutsuo Saito (Hiroshima University)
|
---|
| 7 | * @author Makoto Matsumoto (The University of Tokyo)
|
---|
| 8 | *
|
---|
| 9 | * Copyright (C) 2011 Mutsuo Saito, Makoto Matsumoto,
|
---|
| 10 | * Hiroshima University and The University of Tokyo.
|
---|
| 11 | * All rights reserved.
|
---|
| 12 | *
|
---|
| 13 | * The 3-clause BSD License is applied to this software, see
|
---|
| 14 | * LICENSE.txt
|
---|
| 15 | */
|
---|
| 16 | #include "tinymt64.h"
|
---|
| 17 |
|
---|
| 18 | #define MIN_LOOP 8
|
---|
| 19 |
|
---|
| 20 | /**
|
---|
| 21 | * This function represents a function used in the initialization
|
---|
| 22 | * by init_by_array
|
---|
| 23 | * @param[in] x 64-bit integer
|
---|
| 24 | * @return 64-bit integer
|
---|
| 25 | */
|
---|
| 26 | static uint64_t ini_func1(uint64_t x) {
|
---|
| 27 | return (x ^ (x >> 59)) * UINT64_C(2173292883993);
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | /**
|
---|
| 31 | * This function represents a function used in the initialization
|
---|
| 32 | * by init_by_array
|
---|
| 33 | * @param[in] x 64-bit integer
|
---|
| 34 | * @return 64-bit integer
|
---|
| 35 | */
|
---|
| 36 | static uint64_t ini_func2(uint64_t x) {
|
---|
| 37 | return (x ^ (x >> 59)) * UINT64_C(58885565329898161);
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | /**
|
---|
| 41 | * This function certificate the period of 2^127-1.
|
---|
| 42 | * @param random tinymt state vector.
|
---|
| 43 | */
|
---|
| 44 | static void period_certification(tinymt64_t * random) {
|
---|
| 45 | if ((random->status[0] & TINYMT64_MASK) == 0 &&
|
---|
| 46 | random->status[1] == 0) {
|
---|
| 47 | random->status[0] = 'T';
|
---|
| 48 | random->status[1] = 'M';
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | /**
|
---|
| 53 | * This function initializes the internal state array with a 64-bit
|
---|
| 54 | * unsigned integer seed.
|
---|
| 55 | * @param random tinymt state vector.
|
---|
| 56 | * @param seed a 64-bit unsigned integer used as a seed.
|
---|
| 57 | */
|
---|
| 58 | void tinymt64_init(tinymt64_t * random, uint64_t seed) {
|
---|
| 59 | int i;
|
---|
| 60 | random->status[0] = seed ^ ((uint64_t)random->mat1 << 32);
|
---|
| 61 | random->status[1] = random->mat2 ^ random->tmat;
|
---|
| 62 | for (i = 1; i < MIN_LOOP; i++) {
|
---|
| 63 | random->status[i & 1] ^= i + UINT64_C(6364136223846793005)
|
---|
| 64 | * (random->status[(i - 1) & 1]
|
---|
| 65 | ^ (random->status[(i - 1) & 1] >> 62));
|
---|
| 66 | }
|
---|
| 67 | period_certification(random);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | /**
|
---|
| 71 | * This function initializes the internal state array,
|
---|
| 72 | * with an array of 64-bit unsigned integers used as seeds
|
---|
| 73 | * @param random tinymt state vector.
|
---|
| 74 | * @param init_key the array of 64-bit integers, used as a seed.
|
---|
| 75 | * @param key_length the length of init_key.
|
---|
| 76 | */
|
---|
| 77 | void tinymt64_init_by_array(tinymt64_t * random, const uint64_t init_key[],
|
---|
| 78 | int key_length) {
|
---|
| 79 | const int lag = 1;
|
---|
| 80 | const int mid = 1;
|
---|
| 81 | const int size = 4;
|
---|
| 82 | int i, j;
|
---|
| 83 | int count;
|
---|
| 84 | uint64_t r;
|
---|
| 85 | uint64_t st[4];
|
---|
| 86 |
|
---|
| 87 | st[0] = 0;
|
---|
| 88 | st[1] = random->mat1;
|
---|
| 89 | st[2] = random->mat2;
|
---|
| 90 | st[3] = random->tmat;
|
---|
| 91 | if (key_length + 1 > MIN_LOOP) {
|
---|
| 92 | count = key_length + 1;
|
---|
| 93 | } else {
|
---|
| 94 | count = MIN_LOOP;
|
---|
| 95 | }
|
---|
| 96 | r = ini_func1(st[0] ^ st[mid % size]
|
---|
| 97 | ^ st[(size - 1) % size]);
|
---|
| 98 | st[mid % size] += r;
|
---|
| 99 | r += key_length;
|
---|
| 100 | st[(mid + lag) % size] += r;
|
---|
| 101 | st[0] = r;
|
---|
| 102 | count--;
|
---|
| 103 | for (i = 1, j = 0; (j < count) && (j < key_length); j++) {
|
---|
| 104 | r = ini_func1(st[i] ^ st[(i + mid) % size] ^ st[(i + size - 1) % size]);
|
---|
| 105 | st[(i + mid) % size] += r;
|
---|
| 106 | r += init_key[j] + i;
|
---|
| 107 | st[(i + mid + lag) % size] += r;
|
---|
| 108 | st[i] = r;
|
---|
| 109 | i = (i + 1) % size;
|
---|
| 110 | }
|
---|
| 111 | for (; j < count; j++) {
|
---|
| 112 | r = ini_func1(st[i] ^ st[(i + mid) % size] ^ st[(i + size - 1) % size]);
|
---|
| 113 | st[(i + mid) % size] += r;
|
---|
| 114 | r += i;
|
---|
| 115 | st[(i + mid + lag) % size] += r;
|
---|
| 116 | st[i] = r;
|
---|
| 117 | i = (i + 1) % size;
|
---|
| 118 | }
|
---|
| 119 | for (j = 0; j < size; j++) {
|
---|
| 120 | r = ini_func2(st[i] + st[(i + mid) % size] + st[(i + size - 1) % size]);
|
---|
| 121 | st[(i + mid) % size] ^= r;
|
---|
| 122 | r -= i;
|
---|
| 123 | st[(i + mid + lag) % size] ^= r;
|
---|
| 124 | st[i] = r;
|
---|
| 125 | i = (i + 1) % size;
|
---|
| 126 | }
|
---|
| 127 | random->status[0] = st[0] ^ st[1];
|
---|
| 128 | random->status[1] = st[2] ^ st[3];
|
---|
| 129 | period_certification(random);
|
---|
| 130 | }
|
---|