Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/seed/physics0/rng.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / seed / physics0 / rng.om` 61 lines ยท 1653 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note Handwritten PD0 stream primitive. See README.md for the bounded byte-tag API. 3 note Single stream, explicit global storage; no allocator, C call or implicit wrapping. 4 global rngtag uchar 256 5 global rngstate ulong 1 6 7 note Low 64 bits of addition: each intermediate is less than 2^34. 8 fn rngadd a ulong b ulong to ulong 9 lo = add and a `4294967295` and b `4294967295` 10 hi = add add shr a 32 shr b 32 shr lo 32 11 ret or shl and hi `4294967295` 32 and lo `4294967295` 12 end 13 14 note Low 64 bits of multiplication in base 65536. 15 note Each column has at most four 16-bit products plus carry, below 2^35. 16 note Only the masked digit is shifted, so every shift fits ulong exactly. 17 fn rngmul a ulong b ulong to ulong 18 out = ulong 0 19 carry = ulong 0 20 for k ulong 4 21 sum = carry 22 for j ulong add k 1 23 av = and shr a mul j 16 65535 24 bv = and shr b mul sub k j 16 65535 25 sum = add sum mul av bv 26 end 27 out = or out shl and sum 65535 mul k 16 28 carry = shr sum 16 29 end 30 ret out 31 end 32 33 note Return 2 for capacity or embedded NUL, preserving state on refusal. 34 note Valid tags are exactly n nonzero bytes, equivalent to a C tag plus terminator. 35 fn rnginit seed ulong n ulong to int 36 if gt n len rngtag 37 ret 2 38 end 39 hash = ulong `14695981039346656037` 40 for i ulong n 41 c = get rngtag i 42 if eq c uchar 0 43 ret 2 44 end 45 hash = rngmul xor hash ulong c `1099511628211` 46 end 47 put rngstate 0 xor seed hash 48 ret 0 49 end 50 51 fn rngnext to ulong 52 state = rngadd get rngstate 0 `11400714819323198485` 53 put rngstate 0 state 54 z = rngmul xor state shr state 30 `13787848793156543929` 55 z = rngmul xor z shr z 27 `10723151780598845931` 56 ret xor z shr z 31 57 end 58 59 fn rnguniform to ulong 60 ret rem rngnext `1000001` 61 end