rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
Data Structures | Typedefs | Functions
tinymt32.h File Reference

Detailed Description

Tiny Mersenne Twister only 127 bit internal state.

Author
Mutsuo Saito (Hiroshima University)
Makoto Matsumoto (University of Tokyo)

Copyright (C) 2011 Mutsuo Saito, Makoto Matsumoto, Hiroshima University and The University of Tokyo. All rights reserved.

The 3-clause BSD License is applied to this software, see LICENSE.txt

Definition in file tinymt32.h.

Data Structures

struct  TINYMT32_T
 

Typedefs

typedef struct TINYMT32_T tinymt32_t
 

Functions

void tinymt32_init (tinymt32_t *random, uint32_t seed)
 
void tinymt32_init_by_array (tinymt32_t *random, uint32_t init_key[], int key_length)
 
static int tinymt32_get_mexp (tinymt32_t *random __attribute__((unused)))
 
static int tinymt32_get_mexp (tinymt32_t *random)
 
static void tinymt32_next_state (tinymt32_t *random)
 
static uint32_t tinymt32_temper (tinymt32_t *random)
 
static float tinymt32_temper_conv (tinymt32_t *random)
 
static float tinymt32_temper_conv_open (tinymt32_t *random)
 
static uint32_t tinymt32_generate_uint32 (tinymt32_t *random)
 
static float tinymt32_generate_float (tinymt32_t *random)
 
static float tinymt32_generate_float12 (tinymt32_t *random)
 
static float tinymt32_generate_float01 (tinymt32_t *random)
 
static float tinymt32_generate_floatOC (tinymt32_t *random)
 
static float tinymt32_generate_floatOO (tinymt32_t *random)
 
static double tinymt32_generate_32double (tinymt32_t *random)
 

Typedef Documentation

◆ tinymt32_t

typedef struct TINYMT32_T tinymt32_t

Definition at line 43 of file tinymt32.h.

Function Documentation

◆ tinymt32_generate_32double()

static double tinymt32_generate_32double ( tinymt32_t random)
inlinestatic

This function outputs double precision floating point number from internal state. The returned value has 32-bit precision. In other words, this function makes one double precision floating point number from one 32-bit unsigned integer.

Parameters
randomtinymt internal status
Returns
floating point number r (0.0 <= r < 1.0)

Definition at line 248 of file tinymt32.h.

248 {
249 tinymt32_next_state(random);
250 return tinymt32_temper(random) * (1.0 / 4294967296.0);
251}
static uint32_t tinymt32_temper(tinymt32_t *random)
Definition tinymt32.h:96
static void tinymt32_next_state(tinymt32_t *random)
Definition tinymt32.h:70
Here is the call graph for this function:

◆ tinymt32_generate_float()

static float tinymt32_generate_float ( tinymt32_t random)
inlinestatic

This function outputs floating point number from internal state. This function is implemented using multiplying by (1 / 2^24). floating point multiplication is faster than using union trick in my Intel CPU.

Parameters
randomtinymt internal status
Returns
floating point number r (0.0 <= r < 1.0)

Definition at line 191 of file tinymt32.h.

191 {
192 tinymt32_next_state(random);
193 return (float)(tinymt32_temper(random) >> 8) * TINYMT32_MUL;
194}

Referenced by configureRusefiLuaHooks(), SoftSparkLimiter::shouldSkip(), and tinymt32_generate_floatOC().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tinymt32_generate_float01()

static float tinymt32_generate_float01 ( tinymt32_t random)
inlinestatic

This function outputs floating point number from internal state. This function is implemented using union trick.

Parameters
randomtinymt internal status
Returns
floating point number r (0.0 <= r < 1.0)

Definition at line 213 of file tinymt32.h.

213 {
214 tinymt32_next_state(random);
215 return tinymt32_temper_conv(random) - 1.0f;
216}
static float tinymt32_temper_conv(tinymt32_t *random)
Definition tinymt32.h:119
Here is the call graph for this function:

◆ tinymt32_generate_float12()

static float tinymt32_generate_float12 ( tinymt32_t random)
inlinestatic

This function outputs floating point number from internal state. This function is implemented using union trick.

Parameters
randomtinymt internal status
Returns
floating point number r (1.0 <= r < 2.0)

Definition at line 202 of file tinymt32.h.

202 {
203 tinymt32_next_state(random);
204 return tinymt32_temper_conv(random);
205}
Here is the call graph for this function:

◆ tinymt32_generate_floatOC()

static float tinymt32_generate_floatOC ( tinymt32_t random)
inlinestatic

This function outputs floating point number from internal state. This function may return 1.0 and never returns 0.0.

Parameters
randomtinymt internal status
Returns
floating point number r (0.0 < r <= 1.0)

Definition at line 224 of file tinymt32.h.

224 {
225 tinymt32_next_state(random);
226 return 1.0f - tinymt32_generate_float(random);
227}
static float tinymt32_generate_float(tinymt32_t *random)
Definition tinymt32.h:191
Here is the call graph for this function:

◆ tinymt32_generate_floatOO()

static float tinymt32_generate_floatOO ( tinymt32_t random)
inlinestatic

This function outputs floating point number from internal state. This function returns neither 0.0 nor 1.0.

Parameters
randomtinymt internal status
Returns
floating point number r (0.0 < r < 1.0)

Definition at line 235 of file tinymt32.h.

235 {
236 tinymt32_next_state(random);
237 return tinymt32_temper_conv_open(random) - 1.0f;
238}
static float tinymt32_temper_conv_open(tinymt32_t *random)
Definition tinymt32.h:149
Here is the call graph for this function:

◆ tinymt32_generate_uint32()

static uint32_t tinymt32_generate_uint32 ( tinymt32_t random)
inlinestatic

This function outputs 32-bit unsigned integer from internal state.

Parameters
randomtinymt internal status
Returns
32-bit unsigned integer r (0 <= r < 2^32)

Definition at line 178 of file tinymt32.h.

178 {
179 tinymt32_next_state(random);
180 return tinymt32_temper(random);
181}
Here is the call graph for this function:

◆ tinymt32_get_mexp() [1/2]

static int tinymt32_get_mexp ( tinymt32_t *random   __attribute__(unused))
inlinestatic

This function always returns 127

Parameters
randomnot used
Returns
always 127

Definition at line 55 of file tinymt32.h.

56 {
57 return TINYMT32_MEXP;
58}

◆ tinymt32_get_mexp() [2/2]

static int tinymt32_get_mexp ( tinymt32_t random)
inlinestatic

Definition at line 60 of file tinymt32.h.

60 {
61 return TINYMT32_MEXP;
62}

◆ tinymt32_init()

void tinymt32_init ( tinymt32_t random,
uint32_t  seed 
)

This function initializes the internal state array with a 32-bit unsigned integer seed.

Parameters
randomtinymt state vector.
seeda 32-bit unsigned integer used as a seed.

Definition at line 62 of file tinymt32.c.

62 {
63 random->status[0] = seed;
64 random->status[1] = random->mat1;
65 random->status[2] = random->mat2;
66 random->status[3] = random->tmat;
67 for (unsigned int i = 1; i < MIN_LOOP; i++) {
68 random->status[i & 3] ^= i + UINT32_C(1812433253)
69 * (random->status[(i - 1) & 3]
70 ^ (random->status[(i - 1) & 3] >> 30));
71 }
73 for (unsigned int i = 0; i < PRE_LOOP; i++) {
74 tinymt32_next_state(random);
75 }
76}
uint32_t tmat
Definition tinymt32.h:40
uint32_t status[4]
Definition tinymt32.h:37
uint32_t mat2
Definition tinymt32.h:39
uint32_t mat1
Definition tinymt32.h:38
static void period_certification(tinymt32_t *random)
Definition tinymt32.c:44

Referenced by configureRusefiLuaHooks(), and initLaunchControl().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tinymt32_init_by_array()

void tinymt32_init_by_array ( tinymt32_t random,
uint32_t  init_key[],
int  key_length 
)

This function initializes the internal state array, with an array of 32-bit unsigned integers used as seeds

Parameters
randomtinymt state vector.
init_keythe array of 32-bit integers, used as a seed.
key_lengththe length of init_key.

Definition at line 85 of file tinymt32.c.

86 {
87 const unsigned int lag = 1;
88 const unsigned int mid = 1;
89 const unsigned int size = 4;
90 unsigned int i, j;
91 unsigned int count;
92 uint32_t r;
93 uint32_t * st = &random->status[0];
94
95 st[0] = 0;
96 st[1] = random->mat1;
97 st[2] = random->mat2;
98 st[3] = random->tmat;
99 if (key_length + 1 > MIN_LOOP) {
100 count = (unsigned int)key_length + 1;
101 } else {
102 count = MIN_LOOP;
103 }
104 r = ini_func1(st[0] ^ st[mid % size]
105 ^ st[(size - 1) % size]);
106 st[mid % size] += r;
107 r += (unsigned int)key_length;
108 st[(mid + lag) % size] += r;
109 st[0] = r;
110 count--;
111 for (i = 1, j = 0; (j < count) && (j < (unsigned int)key_length); j++) {
112 r = ini_func1(st[i % size]
113 ^ st[(i + mid) % size]
114 ^ st[(i + size - 1) % size]);
115 st[(i + mid) % size] += r;
116 r += init_key[j] + i;
117 st[(i + mid + lag) % size] += r;
118 st[i % size] = r;
119 i = (i + 1) % size;
120 }
121 for (; j < count; j++) {
122 r = ini_func1(st[i % size]
123 ^ st[(i + mid) % size]
124 ^ st[(i + size - 1) % size]);
125 st[(i + mid) % size] += r;
126 r += i;
127 st[(i + mid + lag) % size] += r;
128 st[i % size] = r;
129 i = (i + 1) % size;
130 }
131 for (j = 0; j < size; j++) {
132 r = ini_func2(st[i % size]
133 + st[(i + mid) % size]
134 + st[(i + size - 1) % size]);
135 st[(i + mid) % size] ^= r;
136 r -= i;
137 st[(i + mid + lag) % size] ^= r;
138 st[i % size] = r;
139 i = (i + 1) % size;
140 }
141 period_certification(random);
142 for (i = 0; i < PRE_LOOP; i++) {
143 tinymt32_next_state(random);
144 }
145}
static uint32_t ini_func2(uint32_t x)
Definition tinymt32.c:36
static uint32_t ini_func1(uint32_t x)
Definition tinymt32.c:26
composite packet size
uint16_t count
Definition tunerstudio.h:1
Here is the call graph for this function:

◆ tinymt32_next_state()

static void tinymt32_next_state ( tinymt32_t random)
inlinestatic

This function changes internal state of tinymt32. Users should not call this function directly.

Parameters
randomtinymt internal status

Definition at line 70 of file tinymt32.h.

70 {
71 uint32_t x;
72 uint32_t y;
73
74 y = random->status[3];
75 x = (random->status[0] & TINYMT32_MASK)
76 ^ random->status[1]
77 ^ random->status[2];
78 x ^= (x << TINYMT32_SH0);
79 y ^= (y >> TINYMT32_SH0) ^ x;
80 random->status[0] = random->status[1];
81 random->status[1] = random->status[2];
82 random->status[2] = x ^ (y << TINYMT32_SH1);
83 random->status[3] = y;
84 int32_t const a = -((int32_t)(y & 1)) & (int32_t)random->mat1;
85 int32_t const b = -((int32_t)(y & 1)) & (int32_t)random->mat2;
86 random->status[1] ^= (uint32_t)a;
87 random->status[2] ^= (uint32_t)b;
88}

Referenced by tinymt32_generate_32double(), tinymt32_generate_float(), tinymt32_generate_float01(), tinymt32_generate_float12(), tinymt32_generate_floatOC(), tinymt32_generate_floatOO(), tinymt32_generate_uint32(), tinymt32_init(), and tinymt32_init_by_array().

Here is the caller graph for this function:

◆ tinymt32_temper()

static uint32_t tinymt32_temper ( tinymt32_t random)
inlinestatic

This function outputs 32-bit unsigned integer from internal state. Users should not call this function directly.

Parameters
randomtinymt internal status
Returns
32-bit unsigned pseudorandom number

Definition at line 96 of file tinymt32.h.

96 {
97 uint32_t t0, t1;
98 t0 = random->status[3];
99#if defined(LINEARITY_CHECK)
100 t1 = random->status[0]
101 ^ (random->status[2] >> TINYMT32_SH8);
102#else
103 t1 = random->status[0]
104 + (random->status[2] >> TINYMT32_SH8);
105#endif
106 t0 ^= t1;
107 if ((t1 & 1) != 0) {
108 t0 ^= random->tmat;
109 }
110 return t0;
111}

Referenced by tinymt32_generate_32double(), tinymt32_generate_float(), and tinymt32_generate_uint32().

Here is the caller graph for this function:

◆ tinymt32_temper_conv()

static float tinymt32_temper_conv ( tinymt32_t random)
inlinestatic

This function outputs floating point number from internal state. Users should not call this function directly.

Parameters
randomtinymt internal status
Returns
floating point number r (1.0 <= r < 2.0)

Definition at line 119 of file tinymt32.h.

119 {
120 uint32_t t0, t1;
121 union {
122 uint32_t u;
123 float f;
124 } conv;
125
126 t0 = random->status[3];
127#if defined(LINEARITY_CHECK)
128 t1 = random->status[0]
129 ^ (random->status[2] >> TINYMT32_SH8);
130#else
131 t1 = random->status[0]
132 + (random->status[2] >> TINYMT32_SH8);
133#endif
134 t0 ^= t1;
135 if ((t1 & 1) != 0) {
136 conv.u = ((t0 ^ random->tmat) >> 9) | UINT32_C(0x3f800000);
137 } else {
138 conv.u = (t0 >> 9) | UINT32_C(0x3f800000);
139 }
140 return conv.f;
141}

Referenced by tinymt32_generate_float01(), and tinymt32_generate_float12().

Here is the caller graph for this function:

◆ tinymt32_temper_conv_open()

static float tinymt32_temper_conv_open ( tinymt32_t random)
inlinestatic

This function outputs floating point number from internal state. Users should not call this function directly.

Parameters
randomtinymt internal status
Returns
floating point number r (1.0 < r < 2.0)

Definition at line 149 of file tinymt32.h.

149 {
150 uint32_t t0, t1;
151 union {
152 uint32_t u;
153 float f;
154 } conv;
155
156 t0 = random->status[3];
157#if defined(LINEARITY_CHECK)
158 t1 = random->status[0]
159 ^ (random->status[2] >> TINYMT32_SH8);
160#else
161 t1 = random->status[0]
162 + (random->status[2] >> TINYMT32_SH8);
163#endif
164 t0 ^= t1;
165 if ((t1 & 1) != 0) {
166 conv.u = ((t0 ^ random->tmat) >> 9) | UINT32_C(0x3f800001);
167 } else {
168 conv.u = (t0 >> 9) | UINT32_C(0x3f800001);
169 }
170 return conv.f;
171}

Referenced by tinymt32_generate_floatOO().

Here is the caller graph for this function:

Go to the source code of this file.