tinymt32.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * @file tinymt32.c
  3. *
  4. * @brief 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 "tinymt32.h"
  17. #define MIN_LOOP 8
  18. #define PRE_LOOP 8
  19. /**
  20. * This function represents a function used in the initialization
  21. * by init_by_array
  22. * @param x 32-bit integer
  23. * @return 32-bit integer
  24. */
  25. static uint32_t ini_func1(uint32_t x) {
  26. return (x ^ (x >> 27)) * UINT32_C(1664525);
  27. }
  28. /**
  29. * This function represents a function used in the initialization
  30. * by init_by_array
  31. * @param x 32-bit integer
  32. * @return 32-bit integer
  33. */
  34. static uint32_t ini_func2(uint32_t x) {
  35. return (x ^ (x >> 27)) * UINT32_C(1566083941);
  36. }
  37. /**
  38. * This function certificate the period of 2^127-1.
  39. * @param random tinymt state vector.
  40. */
  41. static void period_certification(tinymt32_t * random) {
  42. if ((random->status[0] & TINYMT32_MASK) == 0 &&
  43. random->status[1] == 0 &&
  44. random->status[2] == 0 &&
  45. random->status[3] == 0) {
  46. random->status[0] = 'T';
  47. random->status[1] = 'I';
  48. random->status[2] = 'N';
  49. random->status[3] = 'Y';
  50. }
  51. }
  52. /**
  53. * This function initializes the internal state array with a 32-bit
  54. * unsigned integer seed.
  55. * @param random tinymt state vector.
  56. * @param seed a 32-bit unsigned integer used as a seed.
  57. */
  58. void tinymt32_init(tinymt32_t * random, uint32_t seed) {
  59. random->status[0] = seed;
  60. random->status[1] = random->mat1;
  61. random->status[2] = random->mat2;
  62. random->status[3] = random->tmat;
  63. for (int i = 1; i < MIN_LOOP; i++) {
  64. random->status[i & 3] ^= i + UINT32_C(1812433253)
  65. * (random->status[(i - 1) & 3]
  66. ^ (random->status[(i - 1) & 3] >> 30));
  67. }
  68. period_certification(random);
  69. for (int i = 0; i < PRE_LOOP; i++) {
  70. tinymt32_next_state(random);
  71. }
  72. }
  73. /**
  74. * This function initializes the internal state array,
  75. * with an array of 32-bit unsigned integers used as seeds
  76. * @param random tinymt state vector.
  77. * @param init_key the array of 32-bit integers, used as a seed.
  78. * @param key_length the length of init_key.
  79. */
  80. void tinymt32_init_by_array(tinymt32_t * random, uint32_t init_key[],
  81. int key_length) {
  82. const int lag = 1;
  83. const int mid = 1;
  84. const int size = 4;
  85. int i, j;
  86. int count;
  87. uint32_t r;
  88. uint32_t * st = &random->status[0];
  89. st[0] = 0;
  90. st[1] = random->mat1;
  91. st[2] = random->mat2;
  92. st[3] = random->tmat;
  93. if (key_length + 1 > MIN_LOOP) {
  94. count = key_length + 1;
  95. } else {
  96. count = MIN_LOOP;
  97. }
  98. r = ini_func1(st[0] ^ st[mid % size]
  99. ^ st[(size - 1) % size]);
  100. st[mid % size] += r;
  101. r += key_length;
  102. st[(mid + lag) % size] += r;
  103. st[0] = r;
  104. count--;
  105. for (i = 1, j = 0; (j < count) && (j < key_length); j++) {
  106. r = ini_func1(st[i % size]
  107. ^ st[(i + mid) % size]
  108. ^ st[(i + size - 1) % size]);
  109. st[(i + mid) % size] += r;
  110. r += init_key[j] + i;
  111. st[(i + mid + lag) % size] += r;
  112. st[i % size] = r;
  113. i = (i + 1) % size;
  114. }
  115. for (; j < count; j++) {
  116. r = ini_func1(st[i % size]
  117. ^ st[(i + mid) % size]
  118. ^ st[(i + size - 1) % size]);
  119. st[(i + mid) % size] += r;
  120. r += i;
  121. st[(i + mid + lag) % size] += r;
  122. st[i % size] = r;
  123. i = (i + 1) % size;
  124. }
  125. for (j = 0; j < size; j++) {
  126. r = ini_func2(st[i % size]
  127. + st[(i + mid) % size]
  128. + st[(i + size - 1) % size]);
  129. st[(i + mid) % size] ^= r;
  130. r -= i;
  131. st[(i + mid + lag) % size] ^= r;
  132. st[i % size] = r;
  133. i = (i + 1) % size;
  134. }
  135. period_certification(random);
  136. for (i = 0; i < PRE_LOOP; i++) {
  137. tinymt32_next_state(random);
  138. }
  139. }