summaryrefslogtreecommitdiff
path: root/opencl/random.cl
blob: 26cc1c3264b6d926c959269ced9459ba72d9c3cc (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
/* 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. */

typedef ushort4 GeglRandom;

unsigned int _gegl_cl_random_int (__global const int *cl_random_data,
                                  const GeglRandom  rand,
                                  int x,
                                  int y,
                                  int z,
                                  int n);

unsigned int gegl_cl_random_int  (__global const int *cl_random_data,
                                  const GeglRandom  rand,
                                  int x,
                                  int y,
                                  int z,
                                  int n);

int gegl_cl_random_int_range     (__global const int  *cl_random_data,
                                  const GeglRandom  rand,
                                  int x,
                                  int y,
                                  int z,
                                  int n,
                                  int min,
                                  int max);

float gegl_cl_random_float       (__global const int  *cl_random_data,
                                  const GeglRandom  rand,
                                  int x,
                                  int y,
                                  int z,
                                  int n);

float gegl_cl_random_float_range (__global const int  *cl_random_data,
                                  const GeglRandom  rand,
                                  int x,
                                  int y,
                                  int z,
                                  int n,
                                  float min,
                                  float max);

unsigned int
_gegl_cl_random_int (__global const int  *cl_random_data,
                     const GeglRandom    rand,
                     int                 x,
                     int                 y,
                     int                 z,
                     int                 n)
{
  const long XPRIME = 103423;
  const long YPRIME = 101359;
  const long NPRIME = 101111;

  unsigned long idx = x * XPRIME +
                      y * YPRIME * XPRIME +
                      n * NPRIME * YPRIME * XPRIME;

  int r0 = cl_random_data[idx % rand.x],
      r1 = cl_random_data[rand.x + (idx % rand.y)],
      r2 = cl_random_data[rand.x + rand.y + (idx % rand.z)];
  return r0 ^ r1 ^ r2;
}

unsigned int
gegl_cl_random_int (__global const int *cl_random_data,
                    const GeglRandom    rand,
                    int                 x,
                    int                 y,
                    int                 z,
                    int                 n)
{
  return _gegl_cl_random_int (cl_random_data, rand, x, y, z, n);
}

int
gegl_cl_random_int_range (__global const int  *cl_random_data,
                          const GeglRandom    rand,
                          int                 x,
                          int                 y,
                          int                 z,
                          int                 n,
                          int                 min,
                          int                 max)
{
  int ret = _gegl_cl_random_int (cl_random_data, rand, 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,
                      const GeglRandom    rand,
                      int                 x,
                      int                 y,
                      int                 z,
                      int                 n)
{
  int u = _gegl_cl_random_int (cl_random_data, rand, x, y, z, n);
  return (u & 0xffff) * G_RAND_FLOAT_TRANSFORM;
}

float
gegl_cl_random_float_range (__global const int *cl_random_data,
                            const GeglRandom    rand,
                            int                 x,
                            int                 y,
                            int                 z,
                            int                 n,
                            float               min,
                            float               max)
{
  float f = gegl_cl_random_float (cl_random_data, rand, x, y, z, n);
  return f * (max - min) + min;
}