tinymt32.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #ifndef TINYMT32_H
  2. #define TINYMT32_H
  3. /**
  4. * @file tinymt32.h
  5. *
  6. * @brief Tiny Mersenne Twister only 127 bit internal state
  7. *
  8. * @author Mutsuo Saito (Hiroshima University)
  9. * @author Makoto Matsumoto (University of Tokyo)
  10. *
  11. * Copyright (C) 2011 Mutsuo Saito, Makoto Matsumoto,
  12. * Hiroshima University and The University of Tokyo.
  13. * All rights reserved.
  14. *
  15. * The 3-clause BSD License is applied to this software, see
  16. * LICENSE.txt
  17. */
  18. #include <stdint.h>
  19. #include <inttypes.h>
  20. #define TINYMT32_MEXP 127
  21. #define TINYMT32_SH0 1
  22. #define TINYMT32_SH1 10
  23. #define TINYMT32_SH8 8
  24. #define TINYMT32_MASK UINT32_C(0x7fffffff)
  25. #define TINYMT32_MUL (1.0f / 16777216.0f)
  26. #if defined(__cplusplus)
  27. extern "C" {
  28. #endif
  29. /**
  30. * tinymt32 internal state vector and parameters
  31. */
  32. struct TINYMT32_T {
  33. uint32_t status[4];
  34. uint32_t mat1;
  35. uint32_t mat2;
  36. uint32_t tmat;
  37. };
  38. typedef struct TINYMT32_T tinymt32_t;
  39. void tinymt32_init(tinymt32_t * random, uint32_t seed);
  40. void tinymt32_init_by_array(tinymt32_t * random, uint32_t init_key[],
  41. int key_length);
  42. #if defined(__GNUC__)
  43. /**
  44. * This function always returns 127
  45. * @param random not used
  46. * @return always 127
  47. */
  48. inline static int tinymt32_get_mexp(
  49. tinymt32_t * random __attribute__((unused))) {
  50. return TINYMT32_MEXP;
  51. }
  52. #else
  53. inline static int tinymt32_get_mexp(tinymt32_t * random) {
  54. return TINYMT32_MEXP;
  55. }
  56. #endif
  57. /**
  58. * This function changes internal state of tinymt32.
  59. * Users should not call this function directly.
  60. * @param random tinymt internal status
  61. */
  62. inline static void tinymt32_next_state(tinymt32_t * random) {
  63. uint32_t x;
  64. uint32_t y;
  65. y = random->status[3];
  66. x = (random->status[0] & TINYMT32_MASK)
  67. ^ random->status[1]
  68. ^ random->status[2];
  69. x ^= (x << TINYMT32_SH0);
  70. y ^= (y >> TINYMT32_SH0) ^ x;
  71. random->status[0] = random->status[1];
  72. random->status[1] = random->status[2];
  73. random->status[2] = x ^ (y << TINYMT32_SH1);
  74. random->status[3] = y;
  75. random->status[1] ^= -((int32_t)(y & 1)) & random->mat1;
  76. random->status[2] ^= -((int32_t)(y & 1)) & random->mat2;
  77. }
  78. /**
  79. * This function outputs 32-bit unsigned integer from internal state.
  80. * Users should not call this function directly.
  81. * @param random tinymt internal status
  82. * @return 32-bit unsigned pseudorandom number
  83. */
  84. inline static uint32_t tinymt32_temper(tinymt32_t * random) {
  85. uint32_t t0, t1;
  86. t0 = random->status[3];
  87. #if defined(LINEARITY_CHECK)
  88. t1 = random->status[0]
  89. ^ (random->status[2] >> TINYMT32_SH8);
  90. #else
  91. t1 = random->status[0]
  92. + (random->status[2] >> TINYMT32_SH8);
  93. #endif
  94. t0 ^= t1;
  95. t0 ^= -((int32_t)(t1 & 1)) & random->tmat;
  96. return t0;
  97. }
  98. /**
  99. * This function outputs floating point number from internal state.
  100. * Users should not call this function directly.
  101. * @param random tinymt internal status
  102. * @return floating point number r (1.0 <= r < 2.0)
  103. */
  104. inline static float tinymt32_temper_conv(tinymt32_t * random) {
  105. uint32_t t0, t1;
  106. union {
  107. uint32_t u;
  108. float f;
  109. } conv;
  110. t0 = random->status[3];
  111. #if defined(LINEARITY_CHECK)
  112. t1 = random->status[0]
  113. ^ (random->status[2] >> TINYMT32_SH8);
  114. #else
  115. t1 = random->status[0]
  116. + (random->status[2] >> TINYMT32_SH8);
  117. #endif
  118. t0 ^= t1;
  119. conv.u = ((t0 ^ (-((int32_t)(t1 & 1)) & random->tmat)) >> 9)
  120. | UINT32_C(0x3f800000);
  121. return conv.f;
  122. }
  123. /**
  124. * This function outputs floating point number from internal state.
  125. * Users should not call this function directly.
  126. * @param random tinymt internal status
  127. * @return floating point number r (1.0 < r < 2.0)
  128. */
  129. inline static float tinymt32_temper_conv_open(tinymt32_t * random) {
  130. uint32_t t0, t1;
  131. union {
  132. uint32_t u;
  133. float f;
  134. } conv;
  135. t0 = random->status[3];
  136. #if defined(LINEARITY_CHECK)
  137. t1 = random->status[0]
  138. ^ (random->status[2] >> TINYMT32_SH8);
  139. #else
  140. t1 = random->status[0]
  141. + (random->status[2] >> TINYMT32_SH8);
  142. #endif
  143. t0 ^= t1;
  144. conv.u = ((t0 ^ (-((int32_t)(t1 & 1)) & random->tmat)) >> 9)
  145. | UINT32_C(0x3f800001);
  146. return conv.f;
  147. }
  148. /**
  149. * This function outputs 32-bit unsigned integer from internal state.
  150. * @param random tinymt internal status
  151. * @return 32-bit unsigned integer r (0 <= r < 2^32)
  152. */
  153. inline static uint32_t tinymt32_generate_uint32(tinymt32_t * random) {
  154. tinymt32_next_state(random);
  155. return tinymt32_temper(random);
  156. }
  157. /**
  158. * This function outputs floating point number from internal state.
  159. * This function is implemented using multiplying by (1 / 2^24).
  160. * floating point multiplication is faster than using union trick in
  161. * my Intel CPU.
  162. * @param random tinymt internal status
  163. * @return floating point number r (0.0 <= r < 1.0)
  164. */
  165. inline static float tinymt32_generate_float(tinymt32_t * random) {
  166. tinymt32_next_state(random);
  167. return (tinymt32_temper(random) >> 8) * TINYMT32_MUL;
  168. }
  169. /**
  170. * This function outputs floating point number from internal state.
  171. * This function is implemented using union trick.
  172. * @param random tinymt internal status
  173. * @return floating point number r (1.0 <= r < 2.0)
  174. */
  175. inline static float tinymt32_generate_float12(tinymt32_t * random) {
  176. tinymt32_next_state(random);
  177. return tinymt32_temper_conv(random);
  178. }
  179. /**
  180. * This function outputs floating point number from internal state.
  181. * This function is implemented using union trick.
  182. * @param random tinymt internal status
  183. * @return floating point number r (0.0 <= r < 1.0)
  184. */
  185. inline static float tinymt32_generate_float01(tinymt32_t * random) {
  186. tinymt32_next_state(random);
  187. return tinymt32_temper_conv(random) - 1.0f;
  188. }
  189. /**
  190. * This function outputs floating point number from internal state.
  191. * This function may return 1.0 and never returns 0.0.
  192. * @param random tinymt internal status
  193. * @return floating point number r (0.0 < r <= 1.0)
  194. */
  195. inline static float tinymt32_generate_floatOC(tinymt32_t * random) {
  196. tinymt32_next_state(random);
  197. return 1.0f - tinymt32_generate_float(random);
  198. }
  199. /**
  200. * This function outputs floating point number from internal state.
  201. * This function returns neither 0.0 nor 1.0.
  202. * @param random tinymt internal status
  203. * @return floating point number r (0.0 < r < 1.0)
  204. */
  205. inline static float tinymt32_generate_floatOO(tinymt32_t * random) {
  206. tinymt32_next_state(random);
  207. return tinymt32_temper_conv_open(random) - 1.0f;
  208. }
  209. /**
  210. * This function outputs double precision floating point number from
  211. * internal state. The returned value has 32-bit precision.
  212. * In other words, this function makes one double precision floating point
  213. * number from one 32-bit unsigned integer.
  214. * @param random tinymt internal status
  215. * @return floating point number r (0.0 <= r < 1.0)
  216. */
  217. inline static double tinymt32_generate_32double(tinymt32_t * random) {
  218. tinymt32_next_state(random);
  219. return tinymt32_temper(random) * (1.0 / 4294967296.0);
  220. }
  221. #if defined(__cplusplus)
  222. }
  223. #endif
  224. #endif