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