2 * Copyright (C) 2024 Mikulas Patocka
4 * This file is part of Ajla.
6 * Ajla is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software
8 * Foundation, either version 3 of the License, or (at your option) any later
11 * Ajla is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * Ajla. If not, see <https://www.gnu.org/licenses/>.
23 fn prng_init(seed : int) : prng_state;
24 fn prng_get_uint32(state : prng_state) : (prng_state, uint32);
25 fn prng_get_int(state : prng_state, bits : int) : (prng_state, int);
29 // https://arxiv.org/abs/1704.00358
35 const s : uint64 := #b5ad4eceda1ce2a9;
37 fn prng_init(seed : int) : prng_state
39 var x : uint64 := seed and #ffffffff;
40 var w : uint64 := seed shr 32 and #ffffffffffffffff;
49 fn prng_get_uint32(state : prng_state) : (prng_state, uint32)
56 x := x shr 32 or x shl 32;
59 return state, x and #ffffffff;
62 fn prng_get_int(implicit state : prng_state, bits : int) : (prng_state, int)
66 while bit + 32 <= bits do [
67 var p : int := prng_get_uint32();
72 var p : int := prng_get_uint32();
73 p and= (1 shl bits - bit) - 1;