• R/O
  • HTTP
  • SSH
  • HTTPS

hengband: Commit

変愚蛮怒のメインリポジトリです


Commit MetaInfo

Revisãoe7354f67a7521178206c231cab6058879068bf8d (tree)
Hora2019-01-20 12:20:04
AutorHabu <habu@user...>
CommiterHabu

Mensagem de Log

[feature]RNGを xoroshiro128+ から xoshiro128** に変更

Mudança Sumário

Diff

--- a/src/z-rand.c
+++ b/src/z-rand.c
@@ -70,111 +70,43 @@ u32b Rand_state[RAND_DEG] = {
7070 };
7171
7272
73-typedef struct {
74- u32b dw[2];
75-} u64b;
76-
77-static u64b u64b_xor(u64b a, u64b b)
78-{
79- u64b result;
80-
81- result.dw[0] = a.dw[0] ^ b.dw[0];
82- result.dw[1] = a.dw[1] ^ b.dw[1];
83-
84- return result;
85-}
86-
87-static u64b u64b_shiftl(u64b x, int k)
88-{
89- u64b result;
90-
91- if (k < 32) {
92- result.dw[1] = (x.dw[1] << k) | (x.dw[0] >> (32 - k));
93- result.dw[0] = (x.dw[0] << k);
94- }
95- else {
96- result.dw[1] = (x.dw[0] << (k - 32));
97- result.dw[0] = 0;
98- }
99-
100- return result;
101-}
102-
103-static u64b u64b_rotl(u64b x, int k)
104-{
105- u64b result;
106-
107- if (k < 32) {
108- result.dw[0] = (x.dw[0] << k) | (x.dw[1] >> (32 - k));
109- result.dw[1] = (x.dw[1] << k) | (x.dw[0] >> (32 - k));
110- }
111- else {
112- result.dw[0] = (x.dw[0] >> (64 - k)) | (x.dw[1] << (k - 32));
113- result.dw[1] = (x.dw[1] >> (64 - k)) | (x.dw[0] << (k - 32));
114- }
115-
116- return result;
117-}
118-
119-static u64b u64b_add(u64b a, u64b b)
73+static u32b u32b_rotl(const u32b x, int k)
12074 {
121- u64b result;
122-
123- result.dw[0] = a.dw[0] + b.dw[0];
124- result.dw[1] = a.dw[1] + b.dw[1];
125-
126- if (result.dw[0] < a.dw[0])
127- result.dw[1] ++;
128-
129- return result;
75+ return (x << k) | (x >> (32 - k));
13076 }
13177
13278 /*
133- * Initialize Xorshift Algorithm state
79+ * Initialize RNG state
13480 */
135-static void Rand_Xorshift_seed(u32b seed, u32b* state)
81+static void Rand_seed(u32b seed, u32b* state)
13682 {
13783 int i;
13884
139- /* Initialize Xorshift Algorithm RNG */
14085 for (i = 1; i <= 4; ++ i) {
14186 seed = 1812433253UL * (seed ^ (seed >> 30)) + i;
14287 state[i-1] = seed;
14388 }
14489 }
14590
146-#if 0
14791 /*
148- * Xorshift Algorithm
92+ * Xoshiro128** Algorithm
14993 */
150-static u32b Rand_Xorshift(u32b* state)
94+static u32b Rand_Xoshiro128starstar(u32b *state)
15195 {
152- u32b t = state[0] ^ (state[0] << 11);
153-
154- state[0] = state[1];
155- state[1] = state[2];
156- state[2] = state[3];
96+ const u32b result = u32b_rotl(state[0] * 5, 7) * 9;
15797
158- state[3] = (state[3] ^ (state[3] >> 19)) ^ (t ^ (t >> 8));
98+ const u32b t = state[1] << 9;
15999
160- return state[3];
161-}
162-#endif
100+ state[2] ^= state[0];
101+ state[3] ^= state[1];
102+ state[1] ^= state[2];
103+ state[0] ^= state[3];
163104
164-/*
165- * Xoroshiro128+ Algorithm
166- */
167-static u32b Rand_Xoroshiro128plus(u32b* state)
168-{
169- const u64b s0 = *((u64b*)state);
170- u64b s1 = *((u64b*)state + 1);
171- const u64b result = u64b_add(s0, s1);
105+ state[2] ^= t;
172106
173- s1 = u64b_xor(s0, s1);
174- *((u64b*)state) = u64b_xor(u64b_xor(u64b_rotl(s0, 55), s1), u64b_shiftl(s1, 14));
175- *((u64b*)state + 1) = u64b_rotl(s1, 36);
107+ state[3] = u32b_rotl(state[3], 11);
176108
177- return result.dw[0];
109+ return result;
178110 }
179111
180112 static const u32b Rand_Xorshift_max = 0xFFFFFFFF;
@@ -184,7 +116,7 @@ static const u32b Rand_Xorshift_max = 0xFFFFFFFF;
184116 */
185117 void Rand_state_set(u32b seed)
186118 {
187- Rand_Xorshift_seed(seed, Rand_state);
119+ Rand_seed(seed, Rand_state);
188120 }
189121
190122 void Rand_state_init(void)
@@ -267,7 +199,7 @@ static s32b Rand_div_impl(s32b m, u32b* state)
267199 past = scaling * m;
268200
269201 do {
270- ret = Rand_Xoroshiro128plus(state);
202+ ret = Rand_Xoshiro128starstar(state);
271203 } while (ret >= past);
272204
273205 return ret / scaling;
@@ -465,7 +397,7 @@ s32b Rand_external(s32b m)
465397 {
466398 /* Initialize with new seed */
467399 u32b seed = (u32b)time(NULL);
468- Rand_Xorshift_seed(seed, Rand_state_external);
400+ Rand_seed(seed, Rand_state_external);
469401 initialized = TRUE;
470402 }
471403
Show on old repository browser