forked from Cubitect/cubiomes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
javarnd.h
129 lines (107 loc) · 2.91 KB
/
javarnd.h
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
#ifndef JAVARND_H_
#define JAVARND_H_
#include <stdint.h>
/********************** C copy of the Java Random methods **********************
*/
static inline void setSeed(uint64_t *seed, uint64_t value)
{
*seed = (value ^ 0x5deece66d) & ((1ULL << 48) - 1);
}
static inline int next(uint64_t *seed, const int bits)
{
*seed = (*seed * 0x5deece66d + 0xb) & ((1ULL << 48) - 1);
return (int) ((int64_t)*seed >> (48 - bits));
}
static inline int nextInt(uint64_t *seed, const int n)
{
int bits, val;
const int m = n - 1;
if ((m & n) == 0) {
uint64_t x = n * (uint64_t)next(seed, 31);
return (int) ((int64_t) x >> 31);
}
do {
bits = next(seed, 31);
val = bits % n;
}
while (bits - val + m < 0);
return val;
}
static inline uint64_t nextLong(uint64_t *seed)
{
return ((uint64_t) next(seed, 32) << 32) + next(seed, 32);
}
static inline float nextFloat(uint64_t *seed)
{
return next(seed, 24) / (float) (1 << 24);
}
static inline double nextDouble(uint64_t *seed)
{
uint64_t x = (uint64_t)next(seed, 26);
x <<= 27;
x += next(seed, 27);
return (int64_t) x / (double) (1ULL << 53);
}
/* A macro to generate the ideal assembly for X = nextInt(S, 24)
* This is a macro and not an inline function, as many compilers can make use
* of the additional optimisation passes for the surrounding code.
*/
#define JAVA_NEXT_INT24(S,X) \
do { \
uint64_t a = (1ULL << 48) - 1; \
uint64_t c = 0x5deece66dULL * (S); \
c += 11; a &= c; \
(S) = a; \
a = (uint64_t) ((int64_t)a >> 17); \
c = 0xaaaaaaab * a; \
c = (uint64_t) ((int64_t)c >> 36); \
(X) = (int)a - (int)(c << 3) * 3; \
} while (0)
/* skipNextN
* ---------
* Jumps forwards in the random number sequence by simulating 'n' calls to next.
*/
static inline void skipNextN(uint64_t *seed, uint64_t n)
{
uint64_t m = 1;
uint64_t a = 0;
uint64_t im = 0x5deece66dULL;
uint64_t ia = 0xb;
uint64_t k;
for (k = n; k; k >>= 1)
{
if (k & 1)
{
m *= im;
a = im * a + ia;
}
ia = (im + 1) * ia;
im *= im;
}
*seed = *seed * m + a;
*seed &= 0xffffffffffffULL;
}
/* Find the modular inverse: (1/x) | mod m.
* Assumes x and m are positive (less than 2^63), co-prime.
*/
static inline __attribute__((const))
uint64_t mulInv(uint64_t x, uint64_t m)
{
uint64_t t, q, a, b, n;
if ((int64_t)m <= 1)
return 0; // no solution
n = m;
a = 0; b = 1;
while ((int64_t)x > 1)
{
if (m == 0)
return 0; // x and m are co-prime
q = x / m;
t = m; m = x % m; x = t;
t = a; a = b - q * a; b = t;
}
if ((int64_t)b < 0)
b += n;
return b;
}
#endif /* JAVARND_H_ */