summaryrefslogtreecommitdiff
path: root/opencl/random.cl
blob: 86b4b3b6e2de58e972c8f78bc17d29e05af02ed8 (plain)
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
/* This file is part of GEGL
 *
 * GEGL is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * GEGL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright 2013 Carlos Zubieta (czubieta.dev@gmail.com)
 */

/* XXX: this file should be kept in sync with gegl-random. */
#define XPRIME     103423
#define YPRIME     101359
#define NPRIME     101111

#define RANDOM_DATA_SIZE (15083+15091+15101)
#define PRIME_SIZE 533

static inline int
_gegl_cl_random_int (__global const int  *cl_random_data,
                     __global const long *cl_random_primes,
                     int                 seed,
                     int                 x,
                     int                 y,
                     int                 z,
                     int                 n)
{
  unsigned long idx = x * XPRIME + 
                      y * YPRIME * XPRIME + 
                      n * NPRIME * YPRIME * XPRIME;
#define ROUNDS 3
    /* 3 rounds gives a reasonably high cycle for */
    /*   our synthesized larger random set. */
  unsigned long seed_idx = seed % (PRIME_SIZE - 1 - ROUNDS);
  int prime0 = convert_int(cl_random_primes[seed_idx]),
      prime1 = convert_int(cl_random_primes[seed_idx+1]),
      prime2 = convert_int(cl_random_primes[seed_idx+2]);
  int r0 = cl_random_data[idx % prime0],
      r1 = cl_random_data[prime0 + (idx % (prime1))],
      r2 = cl_random_data[prime0 + prime1 + (idx % (prime2))];
  return r0 ^ r1 ^ r2;
}

int
gegl_cl_random_int (__global const int  *cl_random_data,
                    __global const long *cl_random_primes,
                    int                 seed,
                    int                 x,
                    int                 y,
                    int                 z,
                    int                 n)
{
  return _gegl_cl_random_int (cl_random_data, cl_random_primes,
                              seed, x, y, z, n);
}

int
gegl_cl_random_int_range (__global const int  *cl_random_data,
                          __global const long *cl_random_primes,
                          int                 seed,
                          int                 x,
                          int                 y,
                          int                 z,
                          int                 n,
                          int                 min,
                          int                 max)
{
  int ret = _gegl_cl_random_int (cl_random_data, cl_random_primes,
                                 seed, x, y, z, n);
  return (ret % (max-min)) + min;
}

int4
gegl_cl_random_int4 (__global const int  *cl_random_data,
                     __global const long *cl_random_primes,
                     int                 seed,
                     int                 x,
                     int                 y,
                     int                 z,
                     int                 n)
{
  int r0 = _gegl_cl_random_int(cl_random_data, cl_random_primes,
                               seed, x, y, z, n);
  int r1 = _gegl_cl_random_int(cl_random_data, cl_random_primes,
                               seed, x, y, z, n+1);
  int r2 = _gegl_cl_random_int(cl_random_data, cl_random_primes,
                               seed, x, y, z, n+2);
  int r3 = _gegl_cl_random_int(cl_random_data, cl_random_primes,
                               seed, x, y, z, n+3);
  return (int4)(r0, r1, r2, r3);
}

int4
gegl_cl_random_int4_range (__global const int  *cl_random_data,
                           __global const long *cl_random_primes,
                           int                 seed,
                           int                 x,
                           int                 y,
                           int                 z,
                           int                 n,
                           int                 min,
                           int                 max)
{
  int4 ret = gegl_cl_random_int4 (cl_random_data, cl_random_primes,
                                  seed, x, y, z, n);
  return (ret % (max-min)) + min;
}

#define G_RAND_FLOAT_TRANSFORM  0.00001525902189669642175f

float
gegl_cl_random_float (__global const int  *cl_random_data,
                      __global const long *cl_random_primes,
                      int                 seed,
                      int                 x,
                      int                 y,
                      int                 z,
                      int                 n)
{
  int u = _gegl_cl_random_int (cl_random_data, cl_random_primes,
                               seed, x, y, z, n);
  return convert_float(u & 0xffff) * G_RAND_FLOAT_TRANSFORM;
}

float
gegl_cl_random_float_range (__global const int  *cl_random_data,
                            __global const long *cl_random_primes,
                            int                 seed,
                            int                 x,
                            int                 y,
                            int                 z,
                            int                 n,
                            float               min,
                            float               max)
{
  float f = gegl_cl_random_float (cl_random_data, cl_random_primes,
                                  seed, x, y, z, n);
  return f * (max - min) + min;
}
/*
float4
gegl_cl_random_float4 (__global const int  *cl_random_data,
                       __global const long *cl_random_primes,
                       int                 seed,
                       int                 x,
                       int                 y,
                       int                 z,
                       int                 n)
{
  float r0 = gegl_cl_random_float(cl_random_data, cl_random_primes,
                                  seed x, y, z, n);
  float r1 = gegl_cl_random_float(cl_random_data, cl_random_primes,
                                  seed x, y, z, n+1);
  float r2 = gegl_cl_random_float(cl_random_data, cl_random_primes,
                                  seed x, y, z, n+2);
  float r3 = gegl_cl_random_float(cl_random_data, cl_random_primes,
                                  seed x, y, z, n+3);
  return (float4)(r0, r1, r2, r3);
}

float4
gegl_cl_random_float4_range (__global const int  *cl_random_data,
                             __global const long *cl_random_primes,
                             int                 seed,
                             int                 x,
                             int                 y,
                             int                 z,
                             int                 n,
                             float               min,
                             float               max)
{
  float4 f = gegl_cl_random_float4 (cl_random_data, cl_random_primes,
                                    seed, x, y, z, n);
  return f4 * (float4)((max - min) + min);
}
*/