forked from MersenneTwister-Lab/SFMT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SFMT-sse2-msc.h
321 lines (300 loc) · 9.71 KB
/
SFMT-sse2-msc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#pragma once
/**
* @file SFMT-sse2-msc.h
* @brief SIMD oriented Fast Mersenne Twister(SFMT) for Intel SSE2 for MSC
*
* @author Mutsuo Saito (Hiroshima University)
* @author Makoto Matsumoto (Hiroshima University)
*
* @note We assume LITTLE ENDIAN in this file
*
* Copyright (C) 2006, 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima
* University. All rights reserved.
* Copyright (C) 2013 Mutsuo Saito, Makoto Matsumoto and Hiroshima
* University.
*
* The new BSD License is applied to this software, see LICENSE.txt
*/
#ifndef SFMT_SSE2_MSC_H
#define SFMT_SSE2_MSC_H
/* This header file is included only when _MSC_VER is defined. */
#if _MSC_VER > 1700
inline static __m128i __vectorcall mm_recursion(__m128i a, __m128i b,
__m128i c, __m128i d);
/**
* This function represents the recursion formula.
* @param a a 128-bit part of the interal state array
* @param b a 128-bit part of the interal state array
* @param c a 128-bit part of the interal state array
* @param d a 128-bit part of the interal state array
* @return new value
*/
inline static __m128i __vectorcall mm_recursion(__m128i a, __m128i b,
__m128i c, __m128i d)
{
__m128i v, x, y, z;
y = _mm_srli_epi32(b, SFMT_SR1);
z = _mm_srli_si128(c, SFMT_SR2);
v = _mm_slli_epi32(d, SFMT_SL1);
z = _mm_xor_si128(z, a);
z = _mm_xor_si128(z, v);
x = _mm_slli_si128(a, SFMT_SL2);
y = _mm_and_si128(y, sse2_param_mask.si);
z = _mm_xor_si128(z, x);
return _mm_xor_si128(z, y);
}
/**
* This function fills the internal state array with pseudorandom
* integers.
* @param sfmt SFMT internal state
*/
void sfmt_gen_rand_all(sfmt_t * sfmt) {
int i;
__m128i r1, r2;
w128_t * pstate = sfmt->state;
r1 = pstate[SFMT_N - 2].si;
r2 = pstate[SFMT_N - 1].si;
for (i = 0; i < SFMT_N - SFMT_POS1; i++) {
pstate[i].si = mm_recursion(pstate[i].si,
pstate[i + SFMT_POS1].si, r1, r2);
r1 = r2;
r2 = pstate[i].si;
}
for (; i < SFMT_N; i++) {
pstate[i].si = mm_recursion(pstate[i].si,
pstate[i + SFMT_POS1 - SFMT_N].si,
r1, r2);
r1 = r2;
r2 = pstate[i].si;
}
}
/**
* This function fills the user-specified array with pseudorandom
* integers.
* @param sfmt SFMT internal state.
* @param array an 128-bit array to be filled by pseudorandom numbers.
* @param size number of 128-bit pseudorandom numbers to be generated.
*/
static void gen_rand_array(sfmt_t * sfmt, w128_t * array, int size)
{
int i, j;
__m128i r1, r2;
w128_t * pstate = sfmt->state;
r1 = pstate[SFMT_N - 2].si;
r2 = pstate[SFMT_N - 1].si;
for (i = 0; i < SFMT_N - SFMT_POS1; i++) {
array[i].si = mm_recursion(pstate[i].si,
pstate[i + SFMT_POS1].si, r1, r2);
r1 = r2;
r2 = array[i].si;
}
for (; i < SFMT_N; i++) {
array[i].si = mm_recursion(pstate[i].si,
array[i + SFMT_POS1 - SFMT_N].si, r1, r2);
r1 = r2;
r2 = array[i].si;
}
for (; i < size - SFMT_N; i++) {
array[i].si = mm_recursion(array[i - SFMT_N].si,
array[i + SFMT_POS1 - SFMT_N].si, r1, r2);
r1 = r2;
r2 = array[i].si;
}
for (j = 0; j < 2 * SFMT_N - size; j++) {
pstate[j] = array[j + size - SFMT_N];
}
for (; i < size; i++, j++) {
array[i].si = mm_recursion(array[i - SFMT_N].si,
array[i + SFMT_POS1 - SFMT_N].si, r1, r2);
r1 = r2;
r2 = array[i].si;
pstate[j] = array[i];
}
}
#elif defined(SFMT_USE_MACRO_FUNCTION_FOR_MSC)
/**
* This function represents the recursion formula.
* @param r an output
* @param a a 128-bit part of the interal state array
* @param b a 128-bit part of the interal state array
* @param c a 128-bit part of the interal state array
* @param d a 128-bit part of the interal state array
*/
#define mm_recursion(r, a, b, c, d) \
do { \
__m128i v, x, y, z; \
\
y = _mm_srli_epi32((b), SFMT_SR1); \
z = _mm_srli_si128((c), SFMT_SR2); \
v = _mm_slli_epi32((d), SFMT_SL1); \
z = _mm_xor_si128(z, (a)); \
z = _mm_xor_si128(z, v); \
x = _mm_slli_si128((a), SFMT_SL2); \
y = _mm_and_si128(y, sse2_param_mask.si); \
z = _mm_xor_si128(z, x); \
r = _mm_xor_si128(z, y); \
} while (0)
/**
* This function fills the internal state array with pseudorandom
* integers.
* @param sfmt SFMT internal state
*/
void sfmt_gen_rand_all(sfmt_t * sfmt) {
int i;
__m128i r1, r2;
w128_t * pstate = sfmt->state;
r1 = pstate[SFMT_N - 2].si;
r2 = pstate[SFMT_N - 1].si;
for (i = 0; i < SFMT_N - SFMT_POS1; i++) {
mm_recursion(pstate[i].si, pstate[i].si,
pstate[i + SFMT_POS1].si, r1, r2);
r1 = r2;
r2 = pstate[i].si;
}
for (; i < SFMT_N; i++) {
mm_recursion(pstate[i].si, pstate[i].si,
pstate[i + SFMT_POS1 - SFMT_N].si,
r1, r2);
r1 = r2;
r2 = pstate[i].si;
}
}
/**
* This function fills the user-specified array with pseudorandom
* integers.
* @param sfmt SFMT internal state.
* @param array an 128-bit array to be filled by pseudorandom numbers.
* @param size number of 128-bit pseudorandom numbers to be generated.
*/
static void gen_rand_array(sfmt_t * sfmt, w128_t * array, int size)
{
int i, j;
__m128i r1, r2;
w128_t * pstate = sfmt->state;
r1 = pstate[SFMT_N - 2].si;
r2 = pstate[SFMT_N - 1].si;
for (i = 0; i < SFMT_N - SFMT_POS1; i++) {
mm_recursion(array[i].si, pstate[i].si,
pstate[i + SFMT_POS1].si, r1, r2);
r1 = r2;
r2 = array[i].si;
}
for (; i < SFMT_N; i++) {
mm_recursion(array[i].si, pstate[i].si,
array[i + SFMT_POS1 - SFMT_N].si, r1, r2);
r1 = r2;
r2 = array[i].si;
}
for (; i < size - SFMT_N; i++) {
mm_recursion(array[i].si, array[i - SFMT_N].si,
array[i + SFMT_POS1 - SFMT_N].si, r1, r2);
r1 = r2;
r2 = array[i].si;
}
for (j = 0; j < 2 * SFMT_N - size; j++) {
pstate[j] = array[j + size - SFMT_N];
}
for (; i < size; i++, j++) {
mm_recursion(array[i].si, array[i - SFMT_N].si,
array[i + SFMT_POS1 - SFMT_N].si, r1, r2);
r1 = r2;
r2 = array[i].si;
pstate[j] = array[i];
}
}
#else
inline static void mm_recursion(__m128i * r, __m128i a, __m128i b,
__m128i c, __m128i * d);
/**
* This function represents the recursion formula.
* @param r an output
* @param a a 128-bit part of the interal state array
* @param b a 128-bit part of the interal state array
* @param c a 128-bit part of the interal state array
* @param d a 128-bit part of the interal state array
*/
inline static void mm_recursion(__m128i * r, __m128i a, __m128i b,
__m128i c, __m128i * d)
{
__m128i v, x, y, z;
y = _mm_srli_epi32(b, SFMT_SR1);
z = _mm_srli_si128(c, SFMT_SR2);
v = _mm_slli_epi32(*d, SFMT_SL1);
z = _mm_xor_si128(z, a);
z = _mm_xor_si128(z, v);
x = _mm_slli_si128(a, SFMT_SL2);
y = _mm_and_si128(y, sse2_param_mask.si);
z = _mm_xor_si128(z, x);
z = _mm_xor_si128(z, y);
*r = z;
}
/**
* This function fills the internal state array with pseudorandom
* integers.
* @param sfmt SFMT internal state
*/
void sfmt_gen_rand_all(sfmt_t * sfmt) {
int i;
__m128i r1, r2;
w128_t * pstate = sfmt->state;
r1 = pstate[SFMT_N - 2].si;
r2 = pstate[SFMT_N - 1].si;
for (i = 0; i < SFMT_N - SFMT_POS1; i++) {
mm_recursion(&pstate[i].si, pstate[i].si,
pstate[i + SFMT_POS1].si, r1, &r2);
r1 = r2;
r2 = pstate[i].si;
}
for (; i < SFMT_N; i++) {
mm_recursion(&pstate[i].si, pstate[i].si,
pstate[i + SFMT_POS1 - SFMT_N].si,
r1, &r2);
r1 = r2;
r2 = pstate[i].si;
}
}
/**
* This function fills the user-specified array with pseudorandom
* integers.
* @param sfmt SFMT internal state.
* @param array an 128-bit array to be filled by pseudorandom numbers.
* @param size number of 128-bit pseudorandom numbers to be generated.
*/
static void gen_rand_array(sfmt_t * sfmt, w128_t * array, int size)
{
int i, j;
__m128i r1, r2;
w128_t * pstate = sfmt->state;
r1 = pstate[SFMT_N - 2].si;
r2 = pstate[SFMT_N - 1].si;
for (i = 0; i < SFMT_N - SFMT_POS1; i++) {
mm_recursion(&array[i].si, pstate[i].si,
pstate[i + SFMT_POS1].si, r1, &r2);
r1 = r2;
r2 = array[i].si;
}
for (; i < SFMT_N; i++) {
mm_recursion(&array[i].si, pstate[i].si,
array[i + SFMT_POS1 - SFMT_N].si, r1, &r2);
r1 = r2;
r2 = array[i].si;
}
for (; i < size - SFMT_N; i++) {
mm_recursion(&array[i].si, array[i - SFMT_N].si,
array[i + SFMT_POS1 - SFMT_N].si, r1, &r2);
r1 = r2;
r2 = array[i].si;
}
for (j = 0; j < 2 * SFMT_N - size; j++) {
pstate[j] = array[j + size - SFMT_N];
}
for (; i < size; i++, j++) {
mm_recursion(&array[i].si, array[i - SFMT_N].si,
array[i + SFMT_POS1 - SFMT_N].si, r1, &r2);
r1 = r2;
r2 = array[i].si;
pstate[j] = array[i];
}
}
#endif
#endif