summaryrefslogtreecommitdiff
path: root/opencl/random.cl.h
blob: 9261d71be84ef31b86d456f7bf74fdcad2723a1d (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
static const char* random_cl_source =
"/* This file is part of GEGL                                                  \n"
" *                                                                            \n"
" * GEGL is free software; you can redistribute it and/or                      \n"
" * modify it under the terms of the GNU Lesser General Public                 \n"
" * License as published by the Free Software Foundation; either               \n"
" * version 3 of the License, or (at your option) any later version.           \n"
" *                                                                            \n"
" * GEGL is distributed in the hope that it will be useful,                    \n"
" * but WITHOUT ANY WARRANTY; without even the implied warranty of             \n"
" * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU          \n"
" * Lesser General Public License for more details.                            \n"
" *                                                                            \n"
" * You should have received a copy of the GNU Lesser General Public           \n"
" * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.       \n"
" *                                                                            \n"
" * Copyright 2013 Carlos Zubieta (czubieta.dev@gmail.com)                     \n"
" */                                                                           \n"
"                                                                              \n"
"/* XXX: this file should be kept in sync with gegl-random. */                 \n"
"                                                                              \n"
"typedef ushort4 GeglRandom;                                                   \n"
"                                                                              \n"
"unsigned int _gegl_cl_random_int (__global const int *cl_random_data,         \n"
"                                  const GeglRandom  rand,                     \n"
"                                  int x,                                      \n"
"                                  int y,                                      \n"
"                                  int z,                                      \n"
"                                  int n);                                     \n"
"                                                                              \n"
"unsigned int gegl_cl_random_int  (__global const int *cl_random_data,         \n"
"                                  const GeglRandom  rand,                     \n"
"                                  int x,                                      \n"
"                                  int y,                                      \n"
"                                  int z,                                      \n"
"                                  int n);                                     \n"
"                                                                              \n"
"int gegl_cl_random_int_range     (__global const int  *cl_random_data,        \n"
"                                  const GeglRandom  rand,                     \n"
"                                  int x,                                      \n"
"                                  int y,                                      \n"
"                                  int z,                                      \n"
"                                  int n,                                      \n"
"                                  int min,                                    \n"
"                                  int max);                                   \n"
"                                                                              \n"
"float gegl_cl_random_float       (__global const int  *cl_random_data,        \n"
"                                  const GeglRandom  rand,                     \n"
"                                  int x,                                      \n"
"                                  int y,                                      \n"
"                                  int z,                                      \n"
"                                  int n);                                     \n"
"                                                                              \n"
"float gegl_cl_random_float_range (__global const int  *cl_random_data,        \n"
"                                  const GeglRandom  rand,                     \n"
"                                  int x,                                      \n"
"                                  int y,                                      \n"
"                                  int z,                                      \n"
"                                  int n,                                      \n"
"                                  float min,                                  \n"
"                                  float max);                                 \n"
"                                                                              \n"
"unsigned int                                                                  \n"
"_gegl_cl_random_int (__global const int  *cl_random_data,                     \n"
"                     const GeglRandom    rand,                                \n"
"                     int                 x,                                   \n"
"                     int                 y,                                   \n"
"                     int                 z,                                   \n"
"                     int                 n)                                   \n"
"{                                                                             \n"
"  const long XPRIME = 103423;                                                 \n"
"  const long YPRIME = 101359;                                                 \n"
"  const long NPRIME = 101111;                                                 \n"
"                                                                              \n"
"  unsigned long idx = x * XPRIME +                                            \n"
"                      y * YPRIME * XPRIME +                                   \n"
"                      n * NPRIME * YPRIME * XPRIME;                           \n"
"                                                                              \n"
"  int r0 = cl_random_data[idx % rand.x],                                      \n"
"      r1 = cl_random_data[rand.x + (idx % rand.y)],                           \n"
"      r2 = cl_random_data[rand.x + rand.y + (idx % rand.z)];                  \n"
"  return r0 ^ r1 ^ r2;                                                        \n"
"}                                                                             \n"
"                                                                              \n"
"unsigned int                                                                  \n"
"gegl_cl_random_int (__global const int *cl_random_data,                       \n"
"                    const GeglRandom    rand,                                 \n"
"                    int                 x,                                    \n"
"                    int                 y,                                    \n"
"                    int                 z,                                    \n"
"                    int                 n)                                    \n"
"{                                                                             \n"
"  return _gegl_cl_random_int (cl_random_data, rand, x, y, z, n);              \n"
"}                                                                             \n"
"                                                                              \n"
"int                                                                           \n"
"gegl_cl_random_int_range (__global const int  *cl_random_data,                \n"
"                          const GeglRandom    rand,                           \n"
"                          int                 x,                              \n"
"                          int                 y,                              \n"
"                          int                 z,                              \n"
"                          int                 n,                              \n"
"                          int                 min,                            \n"
"                          int                 max)                            \n"
"{                                                                             \n"
"  int ret = _gegl_cl_random_int (cl_random_data, rand, x, y, z, n);           \n"
"  return (ret % (max-min)) + min;                                             \n"
"}                                                                             \n"
"                                                                              \n"
"                                                                              \n"
"#define G_RAND_FLOAT_TRANSFORM  0.00001525902189669642175f                    \n"
"                                                                              \n"
"float                                                                         \n"
"gegl_cl_random_float (__global const int *cl_random_data,                     \n"
"                      const GeglRandom    rand,                               \n"
"                      int                 x,                                  \n"
"                      int                 y,                                  \n"
"                      int                 z,                                  \n"
"                      int                 n)                                  \n"
"{                                                                             \n"
"  int u = _gegl_cl_random_int (cl_random_data, rand, x, y, z, n);             \n"
"  return (u & 0xffff) * G_RAND_FLOAT_TRANSFORM;                               \n"
"}                                                                             \n"
"                                                                              \n"
"float                                                                         \n"
"gegl_cl_random_float_range (__global const int *cl_random_data,               \n"
"                            const GeglRandom    rand,                         \n"
"                            int                 x,                            \n"
"                            int                 y,                            \n"
"                            int                 z,                            \n"
"                            int                 n,                            \n"
"                            float               min,                          \n"
"                            float               max)                          \n"
"{                                                                             \n"
"  float f = gegl_cl_random_float (cl_random_data, rand, x, y, z, n);          \n"
"  return f * (max - min) + min;                                               \n"
"}                                                                             \n"
;