変愚蛮怒のメインリポジトリです
Revision | f0195c4416327f63ee22856f025ecf6920cc9370 (tree) |
---|---|
Zeit | 2017-09-01 23:59:19 |
Autor | Habu <habu@user...> |
Commiter | Habu |
Merge branch 'xoroshiro128plus'
@@ -70,6 +70,65 @@ u32b Rand_state[RAND_DEG] = { | ||
70 | 70 | }; |
71 | 71 | |
72 | 72 | |
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) | |
120 | +{ | |
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; | |
130 | +} | |
131 | + | |
73 | 132 | /* |
74 | 133 | * Initialize Xorshift Algorithm state |
75 | 134 | */ |
@@ -84,6 +143,7 @@ static void Rand_Xorshift_seed(u32b seed, u32b* state) | ||
84 | 143 | } |
85 | 144 | } |
86 | 145 | |
146 | +#if 0 | |
87 | 147 | /* |
88 | 148 | * Xorshift Algorithm |
89 | 149 | */ |
@@ -99,6 +159,23 @@ static u32b Rand_Xorshift(u32b* state) | ||
99 | 159 | |
100 | 160 | return state[3]; |
101 | 161 | } |
162 | +#endif | |
163 | + | |
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); | |
172 | + | |
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); | |
176 | + | |
177 | + return result.dw[0]; | |
178 | +} | |
102 | 179 | |
103 | 180 | static const u32b Rand_Xorshift_max = 0xFFFFFFFF; |
104 | 181 |
@@ -189,7 +266,7 @@ static s32b Rand_div_impl(s32b m, u32b* state) | ||
189 | 266 | past = scaling * m; |
190 | 267 | |
191 | 268 | do { |
192 | - ret = Rand_Xorshift(state); | |
269 | + ret = Rand_Xoroshiro128plus(state); | |
193 | 270 | } while (ret >= past); |
194 | 271 | |
195 | 272 | return ret / scaling; |