-
Notifications
You must be signed in to change notification settings - Fork 0
/
xorshift.cl
77 lines (59 loc) · 1.79 KB
/
xorshift.cl
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
uint xorshift_int(uint4* ctx) {
uint t = ctx->x ^ (ctx->x << 11);
*ctx = ctx->yzww;
ctx->w = ctx->w ^ (ctx->w >> 19) ^ (t ^ (t >> 8));
return ctx->w;
}
float xorshift_float(uint4* ctx) {
return xorshift_int(ctx) * 2.3283064e-10;
}
#define M_MY_PI 3.14159265f
float2 normal_box_muller(uint4* ctx) {
float u1 = xorshift_float(ctx);
float u2 = xorshift_float(ctx);
float ln_u1 = sqrt(-2.0f * log(u1));
float p2_u2 = 2.0f * M_MY_PI * u2;
float2 res;
res.x = ln_u1 * cos(p2_u2);
res.y = ln_u1 * sin(p2_u2);
return res;
}
__kernel void uniform_int(__global uint4* context, __global uint* output, int len)
{
int num_per_thread = len / get_global_size(0);
int tid = get_global_id(0);
//read context from global memory
uint4 ctx = context[tid];
int base_offset = tid * num_per_thread;
for(int i = 0; i < num_per_thread; i++) {
output[base_offset + i] = xorshift_int(&ctx);
}
//save context back to global memory
context[tid] = ctx;
}
__kernel void uniform_float(__global uint4* context, __global float* output, int len)
{
int num_per_thread = len / get_global_size(0);
int tid = get_global_id(0);
//read context from global memory
uint4 ctx = context[tid];
int base_offset = tid * num_per_thread;
for(int i = 0; i < num_per_thread; i++) {
output[base_offset + i] = xorshift_float(&ctx);
}
//save context back to global memory
context[tid] = ctx;
}
__kernel void normal_float(__global uint4* context, __global float2* output, int len)
{
int num_per_thread = len / get_global_size(0) / 2;
int tid = get_global_id(0);
//read context from global memory
uint4 ctx = context[tid];
int base_offset = tid * num_per_thread;
for(int i = 0; i < num_per_thread; i++) {
output[base_offset + i] = normal_box_muller(&ctx);
}
//save context back to global memory
context[tid] = ctx;
}