# bend-ndarray's random numbers, pure: the Philox4x32-10 counter-based generator (Salmon et al., "Parallel random # numbers: as easy as 1, 2, 3", the generator of Random123, JAX and PyTorch) and the draws built on it, one array # element at a time. Element i of a draw (row-major) is a function of the seed, the draw's number and i alone, so an # array is generated in any order, in parallel, with the same result. import Base # ---- 32 x 32 -> 64-bit products, from 16-bit halves ---- type HL is Data: HL{hi: U32, lo: U32} def lo16(+x: U32) -> U32: U32.and(x, 65535) def hi16(+x: U32) -> U32: U32.div(x, 65536) def mulhilo_mid(+mid: U32, +hh: U32, +lh: U32, +hl: U32, +lo: U32) -> HL: HL{(hh + lh + hl + hi16(mid) : U32), lo} # ---- Philox4x32-10 ---- type Q4 is Data: Q4{a: U32, b: U32, c: U32, d: U32} def mulhilo_go(+al: U32, +ah: U32, +bl: U32, +bh: U32, +lo: U32) -> HL: mulhilo_mid((hi16((al * bl : U32)) + lo16((al * bh : U32)) + lo16((ah * bl : U32)) : U32), (ah * bh : U32), hi16((al * bh : U32)), hi16((ah * bl : U32)), lo) def mulhilo(+a: U32, +b: U32) -> HL: mulhilo_go(lo16(a) , hi16(a), lo16(b), hi16(b), (a * b : U32)) def round_mix(p0: HL, p1: HL, +c1: U32, +c3: U32, +k0: U32, +k1: U32) -> Q4: match p0 p1: case HL{h0, l0} HL{h1, l1}: Q4{U32.xor(U32.xor(h1, c1), k0), l1, U32.xor(U32.xor(h0, c3), k1), l0} def round(q: Q4, +k0: U32, +k1: U32) -> Q4: match q: case Q4{c0, c1, c2, c3}: round_mix(mulhilo(3528531795, c0), mulhilo(3449720151, c2), c1, c3, k0, k1) def rounds(n: Nat, q: Q4, +k0: U32, +k1: U32) -> Q4: match n: case 0n: q case 1n+m: rounds(m, round(q, k0, k1), (k0 + 2654435769 : U32), (k1 + 3144134277 : U32)) # the four words of counter (c0, c1, c2, c3) under key (k0, k1) def philox(q: Q4, +k0: U32, +k1: U32) -> Q4: rounds(10n, q, k0, k1) # ---- the generator ---- # a seed (as two words) and how many draws it has made type Gen is Data: Gen{k0: U32, k1: U32, n: U32} # the generator for a 32-bit seed def rng(+seed: U32) -> Gen: Gen{seed, 0, 0} # the generator for a 64-bit seed, as its high and low words def rng64(+hi: U32, +lo: U32) -> Gen: Gen{lo, hi, 0} def next(g: Gen) -> Gen: match g: case Gen{k0, k1, +n}: Gen{k0, k1, (n + 1 : U32)} # the words of element i of draw n, try j (an integer draw that rejects a word tries again) def words(+k0: U32, +k1: U32, +n: U32, +i: U32, +j: U32) -> Q4: philox(Q4{i, j, n, 0}, k0, k1) def qa(q: Q4) -> U32: match q: case Q4{a, b, c, d}: a def qb(q: Q4) -> U32: match q: case Q4{a, b, c, d}: b # a word to a float32 in [0, 1): its top 24 bits times 2^-24 (NumPy's float32 method) def unit(+x: U32) -> F32: F32.mul(U32.to_f32(U32.div(x, 256)), 5.9604645e-8) # in (0, 1]: for the logarithm of Box-Muller def unit_pos(+x: U32) -> F32: F32.mul(U32.to_f32((U32.div(x, 256) + 1 : U32)), 5.9604645e-8) def bits(x: F32) -> U32: match x: case F32{w}: U32{w} # ---- integers in [low, low + r), without bias (Lemire's method: reject the few low products that would favour some # values; the rejected word is replaced by the next lane, then by the next try's words) ---- # 2^32 mod r: products whose low word is below it are rejected def reject_below(+r: U32) -> U32: U32.mod((0 - r : U32), r) def lemire_pick(p: HL, +r: U32, more: Unit -> U32) -> U32: match p: case HL{+hi, +lo}: Bool.pick(U32, U32.is_lt(lo, reject_below(r)), more(Unit{}), hi) def lemire_lanes(q: Q4, +r: U32, more: Unit -> U32) -> U32: match q: case Q4{a, +b, +c, +d}: lemire_pick(mulhilo(a, r), r, u => lemire_pick(mulhilo(b, r), r, v => lemire_pick(mulhilo(c, r), r, w => lemire_pick(mulhilo(d, r), r, more)))) # an offset in [0, r) for element i, trying up to fuel blocks of four words (0 if all were rejected: probability # below 2^-(4 fuel)) def lemire(fuel: Nat, +k0: U32, +k1: U32, +n: U32, +i: U32, +j: U32, +r: U32) -> U32: match fuel: case 0n: 0 case 1n+f: lemire_lanes(words(k0, k1, n, i, j), r, u => lemire(f, k0, k1, n, i, (j + 1 : U32), r)) # ---- the distributions, as cells ---- type Dist is Data: DUnit{} DUniform{low: F32, span: F32} DNormal{mean: F32, std: F32} DInts{low: U32, r: U32, mask: U32} DKey{} def normal01(q: Q4) -> F32: match q: case Q4{a, b, c, d}: F32.mul(F32.sqrt(F32.mul(F32.neg(2.0), F32.log(unit_pos(a)))), F32.cos(F32.mul(6.2831855, unit(b)))) # element i's cell of draw n def cell(d: Dist, +k0: U32, +k1: U32, +n: U32, +i: U32) -> U32: match d: case DUnit{}: bits(unit(qa(words(k0, k1, n, i, 0)))) case DUniform{+low, span}: bits(F32.add(low, F32.mul(span, unit(qa(words(k0, k1, n, i, 0)))))) case DNormal{mean, std}: bits(F32.add(mean, F32.mul(std, normal01(words(k0, k1, n, i, 0))))) case DInts{low, +r, mask}: U32.and((low + lemire(32n, k0, k1, n, i, 0, r) : U32), mask) case DKey{}: qa(words(k0, k1, n, i, 0)) def key_of(q: Q4) -> HL: match q: case Q4{a, b, c, d}: HL{a, b} # ---- a permutation: indices sorted by random 64-bit keys (merge sort, the earlier of equal keys first) ---- type KI is Data: KI{key: HL, idx: Nat} def key2(+k0: U32, +k1: U32, +n: U32, +i: U32) -> HL: key_of(words(k0, k1, n, i, 0)) def key_le(x: HL, y: HL) -> Bool: match x y: case HL{+xh, xl} HL{+yh, yl}: Bool.or(U32.is_lt(xh, yh), Bool.and(U32.is_eq(xh, yh), U32.is_le(xl, yl))) def ki_le(x: KI, y: KI) -> Bool: match x y: case KI{a, i} KI{b, j}: key_le(a, b) def merge_pick(le: Bool, +x: KI, +xt: List<&2, KI>, +y: KI, +yt: List<&2, KI>, rest_x: Unit -> List<&2, KI>, rest_y: Unit -> List<&2, KI>) -> List<&2, KI>: match le: case True{}: x <> rest_x(Unit{}) case False{}: y <> rest_y(Unit{}) def merge(xs: List<&2, KI>, ys: List<&2, KI>) -> List<&2, KI>: match xs ys: case Nil{} _: ys case _ Nil{}: xs case +x <> +xt +y <> +yt: merge_pick(ki_le(x, y), x, xt, y, yt, u => merge(xt, y <> yt), u => merge(x <> xt, yt)) def evens(xs: List<&2, KI>) -> List<&2, KI>: match xs: case []: [] case x <> Nil{}: [x] case x <> y <> t: x <> evens(t) def odds(xs: List<&2, KI>) -> List<&2, KI>: match xs: case []: [] case x <> Nil{}: [] case x <> y <> t: y <> odds(t) def msort(fuel: Nat, +xs: List<&2, KI>) -> List<&2, KI>: match fuel xs: case 0n ys: ys case 1n+f Nil{}: [] case 1n+f x <> Nil{}: [x] case 1n+ +f ys: a b = msort(f, evens(ys)) msort(f, odds(ys)) merge(a, b) def idxs(xs: List<&2, KI>) -> List<&2, Nat>: match xs: case []: [] case KI{k, i} <> t: i <> idxs(t) # indices start .. start + c - 1, each with its key (the 64-bit key of draw n's element of that index) def keyed(c: Nat, +k0: U32, +k1: U32, +n: U32, +start: Nat) -> List<&2, KI>: match c: case 0n: [] case 1n+d: KI{key2(k0, k1, n, U32.from_nat(start)), start} <> keyed(d, k0, k1, n, Nat.add(start, 1n)) # 0 .. m - 1 in a random order: each index with the 64-bit key of draw n's element of that index, sorted by key def shuffled(+k0: U32, +k1: U32, +n: U32, +m: Nat) -> List<&2, Nat>: idxs(msort(m, keyed(m, k0, k1, n, 0n)))