File Coverage

lib/Random/rand-common.h
Criterion Covered Total %
statement 25 36 69.4
branch 7 12 58.3
condition n/a
subroutine n/a
pod n/a
total 32 48 66.6


line stmt bran cond sub pod time code
1             static uint32_t _rand32();
2             static uint64_t _rand64();
3              
4             // Borrowed from https://www.pcg-random.org/posts/bounded-rands.html
5 100008           static uint32_t _bounded_rand(uint32_t range) {
6 100008           uint32_t x = _rand32();
7 100008           uint64_t m = (uint64_t)x * (uint64_t)range;
8 100008           uint32_t l = (uint32_t)m;
9              
10 100008 100         if (l < range) {
11 29964           uint32_t t = -range;
12 29964 50         if (t >= range) {
13 0           t -= range;
14 0 0         if (t >= range)
15 0           t %= range;
16             }
17 30001 100         while (l < t) {
18 37           x = _rand32();
19 37           m = (uint64_t)x * (uint64_t)range;
20 37           l = (uint32_t)m;
21             }
22             }
23              
24 100008           return m >> 32;
25             }
26              
27             // https://prng.di.unimi.it/#remarks
28 40029           static double _uint64_to_double(uint64_t num, bool inclusive) {
29             // A standard 64bit double floating-point number in IEEE floating point
30             // format has 52 bits of significand. Thus, the representation can actually
31             // store numbers with 53 significant binary digits.
32              
33             double scale;
34 40029 100         if (inclusive) {
35 10000           scale = 1.0 / ((1ULL << 53) - 1); // [0, 1]
36             } else {
37 30029           scale = 1.0 / (1ULL << 53); // [0, 1)
38             }
39              
40 40029           double ret = (num >> 11) * scale; // Top 53 bits divided by 1/2^53
41              
42             //printf("Double: %0.15f\n", ret);
43              
44 40029           return ret;
45             }
46              
47 0           static float _uint32_to_float(uint32_t num, bool inclusive) {
48             float scale;
49 0 0         if (inclusive) {
50 0           scale = 1.0f / ((1U << 24) - 1); // [0, 1]
51             } else {
52 0           scale = 1.0f / (1U << 24); // [0, 1)
53             }
54              
55 0           float ret = (num >> 8) * scale;
56              
57 0           return ret;
58             }
59              
60             // Why This Works
61             // (x + 0.5) offsets each uint32_t value into the center of its floating-point "bin," reducing bias.
62             // Multiplying by 1.0 / 4294967296.0 scales it into the range [0,1).
63 0           static double _uint32_to_double_old(uint32_t x) {
64 0           return (x + 0.5) * (1.0 / 4294967296.0); // 1/2^32
65             }
66              
67             // MurmurHash3 Finalizer (Passes SmallCrush)
68 8           static uint64_t _hash_mur3(uint64_t x) {
69 8           x ^= x >> 33;
70 8           x *= 0xff51afd7ed558ccd;
71 8           x ^= x >> 33;
72 8           x *= 0xc4ceb9fe1a85ec53;
73 8           x ^= x >> 33;
74 8           return x;
75             }