interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / seed / physics0 / rng.om

61 lines · 1653 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Handwritten PD0 stream primitive. See README.md for the bounded byte-tag API.
3note Single stream, explicit global storage; no allocator, C call or implicit wrapping.
4global rngtag uchar 256
5global rngstate ulong 1
6
7note Low 64 bits of addition: each intermediate is less than 2^34.
8fn rngadd a ulong b ulong to ulong
9lo = add and a 4294967295 and b 4294967295
10hi = add add shr a 32 shr b 32 shr lo 32
11ret or shl and hi 4294967295 32 and lo 4294967295
12end
13
14note Low 64 bits of multiplication in base 65536.
15note Each column has at most four 16-bit products plus carry, below 2^35.
16note Only the masked digit is shifted, so every shift fits ulong exactly.
17fn rngmul a ulong b ulong to ulong
18out = ulong 0
19carry = ulong 0
20for k ulong 4
21sum = carry
22for j ulong add k 1
23av = and shr a mul j 16 65535
24bv = and shr b mul sub k j 16 65535
25sum = add sum mul av bv
26end
27out = or out shl and sum 65535 mul k 16
28carry = shr sum 16
29end
30ret out
31end
32
33note Return 2 for capacity or embedded NUL, preserving state on refusal.
34note Valid tags are exactly n nonzero bytes, equivalent to a C tag plus terminator.
35fn rnginit seed ulong n ulong to int
36if gt n len rngtag
37ret 2
38end
39hash = ulong 14695981039346656037
40for i ulong n
41c = get rngtag i
42if eq c uchar 0
43ret 2
44end
45hash = rngmul xor hash ulong c 1099511628211
46end
47put rngstate 0 xor seed hash
48ret 0
49end
50
51fn rngnext to ulong
52state = rngadd get rngstate 0 11400714819323198485
53put rngstate 0 state
54z = rngmul xor state shr state 30 13787848793156543929
55z = rngmul xor z shr z 27 10723151780598845931
56ret xor z shr z 31
57end
58
59fn rnguniform to ulong
60ret rem rngnext 1000001
61end