38 #ifndef PRIVATE_LFSR113_CH 39 #define PRIVATE_LFSR113_CH 41 #define Lfsr113_NORM_cl_double 1.0 / 0x100000001L // 2^32 + 1 // 2.328306436538696e-10 42 #define Lfsr113_NORM_cl_float 2.3283063e-10f 53 for (
size_t i = 0; i < count; i++)
54 destStreams[i] = srcStreams[i];
65 b = (((currentState->
g[0] << 6) ^ currentState->
g[0]) >> 13);
66 currentState->
g[0] = (((currentState->
g[0] & 4294967294U) << 18) ^ b);
68 b = (((currentState->
g[1] << 2) ^ currentState->
g[1]) >> 27);
69 currentState->
g[1] = (((currentState->
g[1] & 4294967288U) << 2) ^ b);
71 b = (((currentState->
g[2] << 13) ^ currentState->
g[2]) >> 21);
72 currentState->
g[2] = (((currentState->
g[2] & 4294967280U) << 7) ^ b);
74 b = (((currentState->
g[3] << 3) ^ currentState->
g[3]) >> 12);
75 currentState->
g[3] = (((currentState->
g[3] & 4294967168U) << 13) ^ b);
77 return (currentState->
g[0] ^ currentState->
g[1] ^ currentState->
g[2] ^ currentState->
g[3]);
85 #define IMPLEMENT_GENERATE_FOR_TYPE(fptype) \ 87 fptype clrngLfsr113RandomU01_##fptype(clrngLfsr113Stream* stream) { \ 88 return clrngLfsr113NextState(&stream->current) * Lfsr113_NORM_##fptype; \ 91 cl_int clrngLfsr113RandomInteger_##fptype(clrngLfsr113Stream* stream, cl_int i, cl_int j) { \ 92 return i + (cl_int)((j - i + 1) * clrngLfsr113RandomU01_##fptype(stream)); \ 95 clrngStatus clrngLfsr113RandomU01Array_##fptype(clrngLfsr113Stream* stream, size_t count, fptype* buffer) { \ 97 return clrngSetErrorString(CLRNG_INVALID_VALUE); \ 99 return clrngSetErrorString(CLRNG_INVALID_VALUE); \ 100 for (size_t i = 0; i < count; i++) \ 101 buffer[i] = clrngLfsr113RandomU01_##fptype(stream); \ 102 return CLRNG_SUCCESS; \ 105 clrngStatus clrngLfsr113RandomIntegerArray_##fptype(clrngLfsr113Stream* stream, cl_int i, cl_int j, size_t count, cl_int* buffer) { \ 107 return clrngSetErrorString(CLRNG_INVALID_VALUE); \ 109 return clrngSetErrorString(CLRNG_INVALID_VALUE); \ 110 for (size_t k = 0; k < count; k++) \ 111 buffer[k] = clrngLfsr113RandomInteger_##fptype(stream, i, j); \ 112 return CLRNG_SUCCESS; \ 117 #if defined(CLRNG_SINGLE_PRECISION) || !defined(__CLRNG_DEVICE_API) 120 #if !defined(CLRNG_SINGLE_PRECISION) || !defined(__CLRNG_DEVICE_API) 125 #undef IMPLEMENT_GENERATE_FOR_TYPE 135 for (
size_t j = 0; j < count; j++) {
136 #ifdef __CLRNG_DEVICE_API 137 #ifdef CLRNG_ENABLE_SUBSTREAMS 150 #if defined(CLRNG_ENABLE_SUBSTREAMS) || !defined(__CLRNG_DEVICE_API) 157 for (
size_t j = 0; j < count; j++) {
176 cl_uint* subStreamState = stream->
substream.
g;
179 z = subStreamState[0] & (cl_uint)-2;
182 z = (z) ^ (z << 3) ^ (z << 4) ^ (z << 6) ^ (z << 7) ^
183 (z << 8) ^ (z << 10) ^ (z << 11) ^ (z << 13) ^ (z << 14) ^
184 (z << 16) ^ (z << 17) ^ (z << 18) ^ (z << 22) ^
185 (z << 24) ^ (z << 25) ^ (z << 26) ^ (z << 28) ^ (z << 30);
187 z ^= ((b >> 1) & 0x7FFFFFFF) ^
188 ((b >> 3) & 0x1FFFFFFF) ^
189 ((b >> 5) & 0x07FFFFFF) ^
190 ((b >> 6) & 0x03FFFFFF) ^
191 ((b >> 7) & 0x01FFFFFF) ^
192 ((b >> 9) & 0x007FFFFF) ^
193 ((b >> 13) & 0x0007FFFF) ^
194 ((b >> 14) & 0x0003FFFF) ^
195 ((b >> 15) & 0x0001FFFF) ^
196 ((b >> 17) & 0x00007FFF) ^
197 ((b >> 18) & 0x00003FFF) ^
198 ((b >> 20) & 0x00000FFF) ^
199 ((b >> 21) & 0x000007FF) ^
200 ((b >> 23) & 0x000001FF) ^
201 ((b >> 24) & 0x000000FF) ^
202 ((b >> 25) & 0x0000007F) ^
203 ((b >> 26) & 0x0000003F) ^
204 ((b >> 27) & 0x0000001F) ^
205 ((b >> 30) & 0x00000003);
206 subStreamState[0] = z;
209 z = subStreamState[1] & (cl_uint)-8;
216 b ^= (z << 22) ^ (z << 25) ^ (z << 27);
217 if ((z & 0x80000000) != 0) b ^= 0xABFFF000;
218 if ((z & 0x40000000) != 0) b ^= 0x55FFF800;
220 z = b ^ ((z >> 7) & 0x01FFFFFF) ^
221 ((z >> 20) & 0x00000FFF) ^
222 ((z >> 21) & 0x000007FF);
224 subStreamState[1] = z;
227 z = subStreamState[2] & (cl_uint)-16;
229 z = ((b >> 3) & 0x1FFFFFFF) ^
230 ((b >> 17) & 0x00007FFF) ^
231 (z << 10) ^ (z << 11) ^ (z << 25);
232 subStreamState[2] = z;
235 z = subStreamState[3] & (cl_uint)-128;
237 z = (z << 14) ^ (z << 16) ^ (z << 20) ^
238 ((b >> 5) & 0x07FFFFFF) ^
239 ((b >> 9) & 0x007FFFFF) ^
240 ((b >> 11) & 0x001FFFFF);
241 subStreamState[3] = z;
251 for (
size_t k = 0; k < count; k++) {
261 for (
size_t i = 0; i < count; i++) {
277 #endif // PRIVATE_Lfsr113_CH clrngStatus clrngLfsr113ForwardToNextSubstreams(size_t count, clrngLfsr113Stream *streams)
Definition: device/lfsr113.c.h:245
enum clrngStatus_ clrngStatus
Error codes.
clrngStatus clrngSetErrorString(cl_int err, const char *msg,...)
Set the current error string.
Definition: private.c:57
void lfsr113ResetNextSubStream(clrngLfsr113Stream *stream)
Definition: device/lfsr113.c.h:163
clrngLfsr113StreamState initial
Definition: lfsr113.h:66
clrngStatus clrngLfsr113RewindSubstreams(size_t count, clrngLfsr113Stream *streams)
Definition: device/lfsr113.c.h:151
#define IMPLEMENT_GENERATE_FOR_TYPE(fptype)
Definition: device/lfsr113.c.h:85
clrngStatus clrngLfsr113RewindStreams(size_t count, clrngLfsr113Stream *streams)
Definition: device/lfsr113.c.h:129
clrngStatus clrngLfsr113MakeOverSubstreams(clrngLfsr113Stream *stream, size_t count, clrngLfsr113Stream *substreams)
Definition: device/lfsr113.c.h:259
cl_uint g[4]
Seed for the first LFSR component.
Definition: lfsr113.h:55
clrngLfsr113StreamState substream
Definition: lfsr113.h:67
clrngStatus clrngLfsr113CopyOverStreams(size_t count, clrngLfsr113Stream *destStreams, const clrngLfsr113Stream *srcStreams)
Definition: device/lfsr113.c.h:45
clrngLfsr113StreamState current
Definition: lfsr113.h:65