← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
ckernel_counter_gaussian.h
Go to the documentation of this file.
1#ifndef CKERNEL_COUNTER_GAUSSIAN_H
2#define CKERNEL_COUNTER_GAUSSIAN_H
3
4#include <math.h>
5#include <stdint.h>
6
7/* SplitMix64 counter domain and FP64 Box-Muller. Reproducibility is for a
8 * given libm/build; the stream does not depend on any prior request. */
9static inline uint64_t ck_rng_mix64(uint64_t value) {
10 value += UINT64_C(0x9e3779b97f4a7c15);
11 value = (value ^ (value >> 30)) * UINT64_C(0xbf58476d1ce4e5b9);
12 value = (value ^ (value >> 27)) * UINT64_C(0x94d049bb133111eb);
13 return value ^ (value >> 31);
14}
15
16static inline uint64_t ck_rng_gaussian_domain(uint64_t seed, uint64_t stream)
17{
18 return ck_rng_mix64(seed ^ ck_rng_mix64(stream));
19}
20
21static inline double ck_rng_gaussian_f64(uint64_t domain, uint64_t index)
22{
23 const uint64_t counter = index * UINT64_C(2);
24 const uint64_t first = ck_rng_mix64(domain + counter);
25 const uint64_t second = ck_rng_mix64(domain + counter + UINT64_C(1));
26 const double uniform_positive = ((double)(first >> 11) + 0.5) * 0x1.0p-53;
27 const double uniform_angle = (double)(second >> 11) * 0x1.0p-53;
28 const double radius = sqrt(-2.0 * log(uniform_positive));
29 return radius * cos(6.283185307179586476925286766559 * uniform_angle);
30}
31
32#endif
static uint64_t ck_rng_gaussian_domain(uint64_t seed, uint64_t stream)
static double ck_rng_gaussian_f64(uint64_t domain, uint64_t index)
static uint64_t ck_rng_mix64(uint64_t value)