2 * *****************************************************************************
4 * Copyright (c) 2018-2019 Gavin D. Howard and contributors.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * *****************************************************************************
32 * Parts of this code are adapted from the following:
34 * PCG, A Family of Better Random Number Generators.
36 * You can find the original source code at:
37 * https://github.com/imneme/pcg-c
39 * -----------------------------------------------------------------------------
41 * Parts of this code are also under the following license:
43 * Copyright (c) 2014-2017 Melissa O'Neill and PCG Project contributors
45 * Permission is hereby granted, free of charge, to any person obtaining a copy
46 * of this software and associated documentation files (the "Software"), to deal
47 * in the Software without restriction, including without limitation the rights
48 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
49 * copies of the Software, and to permit persons to whom the Software is
50 * furnished to do so, subject to the following conditions:
52 * The above copyright notice and this permission notice shall be included in
53 * all copies or substantial portions of the Software.
55 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
56 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
57 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
58 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
59 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
60 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
63 * *****************************************************************************
69 #if BC_ENABLE_EXTRA_MATH
85 static BcRandState bc_rand_addition(uint_fast64_t a, uint_fast64_t b) {
90 res.hi = (res.lo < a);
95 static BcRandState bc_rand_addition2(BcRandState a, BcRandState b) {
97 BcRandState temp, res;
99 res = bc_rand_addition(a.lo, b.lo);
100 temp = bc_rand_addition(a.hi, b.hi);
106 static BcRandState bc_rand_multiply(uint_fast64_t a, uint_fast64_t b) {
108 uint_fast64_t al, ah, bl, bh, c0, c1, c2, c3;
109 BcRandState carry, res;
111 al = BC_RAND_TRUNC32(a);
112 ah = BC_RAND_CHOP32(a);
113 bl = BC_RAND_TRUNC32(b);
114 bh = BC_RAND_CHOP32(b);
121 carry = bc_rand_addition(c1, c2);
123 res = bc_rand_addition(c0, (BC_RAND_TRUNC32(carry.lo)) << 32);
124 res.hi += BC_RAND_CHOP32(carry.lo) + c3 + (carry.hi << 32);
129 static BcRandState bc_rand_multiply2(BcRandState a, BcRandState b) {
131 BcRandState c0, c1, c2, carry;
133 c0 = bc_rand_multiply(a.lo, b.lo);
134 c1 = bc_rand_multiply(a.lo, b.hi);
135 c2 = bc_rand_multiply(a.hi, b.lo);
137 carry = bc_rand_addition2(c1, c2);
141 return bc_rand_addition2(c0, carry);
144 #endif // BC_RAND_BUILTIN
146 static void bc_rand_setModified(BcRNGData *r) {
149 r->inc |= (BcRandState) 1UL;
150 #else // BC_RAND_BUILTIN
151 r->inc.lo |= (uint_fast64_t) 1UL;
152 #endif // BC_RAND_BUILTIN
155 static void bc_rand_clearModified(BcRNGData *r) {
158 r->inc &= ~((BcRandState) 1UL);
159 #else // BC_RAND_BUILTIN
161 #endif // BC_RAND_BUILTIN
164 static void bc_rand_copy(BcRNGData *d, BcRNGData *s) {
165 bool unmod = BC_RAND_NOTMODIFIED(d);
166 memcpy(d, s, sizeof(BcRNGData));
167 if (!unmod) bc_rand_setModified(d);
168 else if (!BC_RAND_NOTMODIFIED(s)) bc_rand_clearModified(d);
171 static ulong bc_rand_frand(void *ptr) {
181 nread = read(fd, buf, sizeof(ulong));
183 if (BC_ERR(nread != sizeof(ulong))) bc_vm_err(BC_ERROR_FATAL_IO_ERR);
185 return *((ulong*) buf);
188 static ulong bc_rand_rand(void *ptr) {
195 for (i = 0; i < sizeof(ulong); ++i)
196 res |= ((ulong) (rand() & BC_RAND_SRAND_BITS)) << (i * CHAR_BIT);
201 static BcRandState bc_rand_inc(BcRNGData *r) {
207 #else // BC_RAND_BUILTIN
208 inc.lo = r->inc.lo | 1;
210 #endif // BC_RAND_BUILTIN
215 static void bc_rand_setInc(BcRNGData *r) {
219 #else // BC_RAND_BUILTIN
221 r->inc.hi |= (r->inc.lo & (1UL << (BC_LONG_BIT - 1))) >> (BC_LONG_BIT - 1);
223 #endif // BC_RAND_BUILTIN
226 static void bc_rand_seedState(BcRandState *state, ulong val1, ulong val2) {
229 *state = ((BcRandState) val1) | ((BcRandState) val2) << (BC_LONG_BIT);
230 #else // BC_RAND_BUILTIN
233 #endif // BC_RAND_BUILTIN
236 static void bc_rand_seedRNG(BcRNGData *r, ulong state1, ulong state2,
237 ulong inc1, ulong inc2)
239 bc_rand_seedState(&r->state, state1, state2);
240 bc_rand_seedState(&r->inc, inc1, inc2);
244 static void bc_rand_fill(BcRNGData *r, BcRandUlong fulong, void *ptr) {
246 ulong state1, state2, inc1, inc2;
248 state1 = fulong(ptr);
249 state2 = fulong(ptr);
254 bc_rand_seedRNG(r, state1, state2, inc1, inc2);
257 static void bc_rand_step(BcRNGData *r) {
258 BcRandState temp = bc_rand_mul2(r->state, bc_rand_multiplier);
259 r->state = bc_rand_add2(temp, bc_rand_inc(r));
262 static BcRand bc_rand_output(BcRNGData *r) {
263 return BC_RAND_ROT(BC_RAND_FOLD(r->state), BC_RAND_ROTAMT(r->state));
266 static void bc_rand_seedZeroes(BcRNG *r, BcRNGData *rng, size_t idx) {
270 if (r->v.len <= idx) return;
272 rng2 = bc_vec_item_rev(&r->v, idx);
274 if (BC_RAND_ZERO(rng2)) {
276 for (i = 1; i < r->v.len; ++i)
277 bc_rand_copy(bc_vec_item_rev(&r->v, i), rng);
281 static void bc_rand_srand(BcRNGData *rng) {
287 fd = open("/dev/urandom", O_RDONLY);
289 if (BC_NO_ERR(fd >= 0)) {
290 bc_rand_fill(rng, bc_rand_frand, &fd);
294 while (BC_ERR(BC_RAND_ZERO(rng))) bc_rand_fill(rng, bc_rand_rand, NULL);
299 static void bc_rand_propagate(BcRNG *r, BcRNGData *rng) {
301 if (r->v.len <= 1) return;
303 if (BC_RAND_NOTMODIFIED(rng)) {
308 for (i = 1; go && i < r->v.len; ++i) {
309 BcRNGData *rng2 = bc_vec_item_rev(&r->v, i);
310 go = BC_RAND_NOTMODIFIED(rng2);
311 bc_rand_copy(rng2, rng);
314 bc_rand_seedZeroes(r, rng, i);
316 else bc_rand_seedZeroes(r, rng, 1);
319 BcRand bc_rand_int(BcRNG *r) {
321 BcRNGData *rng = bc_vec_top(&r->v);
323 if (BC_ERR(BC_RAND_ZERO(rng))) bc_rand_srand(rng);
326 bc_rand_propagate(r, rng);
328 return bc_rand_output(rng);
331 BcRand bc_rand_bounded(BcRNG *r, BcRand bound) {
333 BcRand rand, threshold = (0 - bound) % bound;
336 rand = bc_rand_int(r);
337 } while (rand < threshold);
342 void bc_rand_seed(BcRNG *r, ulong state1, ulong state2, ulong inc1, ulong inc2)
344 BcRNGData *rng = bc_vec_top(&r->v);
346 bc_rand_seedState(&rng->inc, inc1, inc2);
348 bc_rand_setModified(rng);
350 if (!state1 && !state2) {
351 memcpy(&rng->state, &rng->inc, sizeof(BcRandState));
354 else bc_rand_seedState(&rng->state, state1, state2);
356 bc_rand_propagate(r, rng);
359 static BcRandState bc_rand_getInc(BcRNGData *r) {
365 #else // BC_RAND_BUILTIN
368 res.lo |= (res.hi & 1) << (BC_LONG_BIT - 1);
370 #endif // BC_RAND_BUILTIN
375 void bc_rand_getRands(BcRNG *r, BcRand *s1, BcRand *s2, BcRand *i1, BcRand *i2)
378 BcRNGData *rng = bc_vec_top(&r->v);
380 if (BC_ERR(BC_RAND_ZERO(rng))) bc_rand_srand(rng);
382 inc = bc_rand_getInc(rng);
384 *s1 = BC_RAND_TRUNC(rng->state);
385 *s2 = BC_RAND_CHOP(rng->state);
387 *i1 = BC_RAND_TRUNC(inc);
388 *i2 = BC_RAND_CHOP(inc);
391 void bc_rand_push(BcRNG *r) {
393 memset(&rng, 0, sizeof(BcRNGData));
394 if (r->v.len > 0) bc_rand_copy(&rng, bc_vec_top(&r->v));
395 bc_vec_push(&r->v, &rng);
398 void bc_rand_pop(BcRNG *r, bool reset) {
399 bc_vec_npop(&r->v, reset ? r->v.len - 1 : 1);
402 void bc_rand_init(BcRNG *r) {
403 BC_SIG_ASSERT_LOCKED;
404 bc_vec_init(&r->v, sizeof(BcRNGData), NULL);
409 void bc_rand_free(BcRNG *r) {
410 BC_SIG_ASSERT_LOCKED;
415 #endif // BC_ENABLE_EXTRA_MATH