-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc_rng.h
More file actions
74 lines (57 loc) · 1.98 KB
/
Copy pathlc_rng.h
File metadata and controls
74 lines (57 loc) · 1.98 KB
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
#ifndef LC_RNG_H
#define LC_RNG_H
#ifndef LC_RNG_API
#define LC_RNG_API extern
#endif
#include <stdint.h>
typedef float f32;
typedef double f64;
#ifndef LC_NODISCARD
#if __STDC_VERSION__ < 202000L && !defined(__cplusplus)
#if LC_CC_GNU
#define LC_NODISCARD __attribute__((__warn_unused_result__))
#else
#define LC_NODISCARD
#endif
#else
#define LC_NODISCARD [[nodiscard]]
#endif
#endif
#if defined(LC_CC_GNU) && LC_CC_GNU
#define LC_EXPECT(expr, cond) __builtin_expect(expr, cond)
#else
#define LC_EXPECT(expr, cond) expr
#endif
struct lc_rng_pcg32_state { uint64_t state; uint64_t inc; };
#define LC_RNG_PCG32_INITIALIZER \
((struct lc_rng_pcg32_state){ \
.state = UINT64_C(0x853C49E6748FEA9B), \
.inc = UINT64_C(0xDA3E39CB94B95BDB), \
})
LC_NODISCARD LC_RNG_API uint32_t lc_pcg32_random(struct lc_rng_pcg32_state *rng);
LC_NODISCARD LC_RNG_API f32 lc_pcg32_random_f32(struct lc_rng_pcg32_state *rng);
#ifdef LC_IMPLEMENTATION
LC_NODISCARD
LC_RNG_API uint32_t
lc_pcg32_random(struct lc_rng_pcg32_state *rng)
{
uint64_t old_state = rng->state;
// Advance internal state
rng->state = old_state * UINT64_C(6364136223846793005) + (rng->inc|1);
// Calculate output function (XSH RR), uses old state for max ILP
uint32_t xor_shifted = ((old_state >> UINT32_C(18)) ^ old_state) >> UINT32_C(27);
uint32_t rot = old_state >> UINT32_C(59);
return (xor_shifted >> rot) | (xor_shifted << ((-rot) & 31));
}
LC_NODISCARD
LC_RNG_API f32
lc_pcg32_random_f32(struct lc_rng_pcg32_state *rng)
{
uint32_t proto_exp_offset = lc_pcg32_random(rng);
if (LC_EXPECT(0 == proto_exp_offset, false))
return 0;
extern f32 ldexpf(f32 arg, int exp);
return ldexpf((f32)(rand() | 0x80000001), -32 - __builtin_clz(proto_exp_offset));
}
#endif
#endif