-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrng.mako
81 lines (66 loc) · 1.6 KB
/
rng.mako
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
#ifndef __CU_RNG_H
#define __CU_RNG_H
#include <float.h>
#define PI 3.14159265358979f
/*
* Return a uniformly distributed random number from the
* [0;1] range.
*/
%if rng == 'xs32':
__device__ float rng_xs32(unsigned int *state)
{
unsigned int x = *state;
x = x ^ (x >> 13);
x = x ^ (x << 17);
x = x ^ (x >> 5);
*state = x;
return x * 2.328306e-10f;
// return x / 4294967296.0f;
}
%endif
%if rng == 'kiss32':
__device__ float rng_kiss32(unsigned int *x, unsigned int *y,
unsigned int *z, unsigned int *w)
{
*x = 69069 * *x + 1234567; // CONG
*z = 36969 * (*z & 65535) + (*z >> 16); // znew
*w = 18000 * (*w & 65535) + (*w >> 16); // wnew & 6553?
*y ^= (*y << 17); // SHR3
*y ^= (*y >> 13);
*y ^= (*y << 5);
return ((((*z << 16) + *w) ^ *x) + *y) * 2.328306e-10f; // (MWC ^ CONG) + SHR3
}
%endif
%if rng == 'nr32':
// 32-bit PRNG from Numerical Recipes, 3rd Ed.
// Period: 3.11 * 10^37
__device__ float rng_nr32(unsigned int *u, unsigned int *v,
unsigned int *w1, unsigned int *w2)
{
unsigned int x, y;
*u = *u * 2891336453U + 1640531513U;
*v ^= *v >> 13;
*v ^= *v << 17;
*v ^= *v >> 5;
*w1 = 33378 * (*w1 & 0xffff) + (*w1 >> 16);
*w2 = 57225 * (*w2 & 0xffff) + (*w2 >> 16);
x = *u ^ (*u << 9);
x ^= x >> 17;
x ^= x << 6;
y = *w1 ^ (*w1 << 17);
y ^= y >> 15;
y ^= y << 5;
return ((x + *v) ^ (y + *w2)) * 2.328306e-10f;
}
%endif
/*
* Generate two normal variates given two uniform variates.
*/
__device__ void bm_trans(float& u1, float& u2)
{
float r = sqrtf(-2.0f * logf(u1 + FLT_EPSILON));
float phi = 2.0f * PI * u2;
u1 = r * cosf(phi);
u2 = r * sinf(phi);
}
#endif /* __CU_RNG_H */