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
omega-mv / gpu / physics0 / rng.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note gpu/physics0/rng.om: PD-0 deterministic SplitMix64 + FNV-1a64 + micro-unit arithmetic. |
| 3 | note Converted from src/physics0/pd0_rng.c (Vigna constants, matching turing_splitmix64). |
| 4 | |
| 5 | fn pd0_fnv1a64 tag view uchar * to ulong |
| 6 | h = ulong 0xcbf29ce484222325 |
| 7 | n = len tag |
| 8 | for i ulong n bound 256 |
| 9 | h = xor h ulong get tag i |
| 10 | h = mul h ulong 0x100000001b3 |
| 11 | end |
| 12 | ret h |
| 13 | end |
| 14 | |
| 15 | fn pd0_splitmix64 r edit ulong 1 to ulong |
| 16 | s = add get r 0 ulong 0x9e3779b97f4a7c15 |
| 17 | put r 0 s |
| 18 | z = mul xor s shr s 30 ulong 0xbf58476d1ce4e5b9 |
| 19 | z = mul xor z shr z 27 ulong 0x94d049bb133111eb |
| 20 | ret xor z shr z 31 |
| 21 | end |
| 22 | |
| 23 | fn pd0_stream r edit ulong 1 seed ulong tag view uchar * to void |
| 24 | h = pd0_fnv1a64 tag |
| 25 | put r 0 xor seed h |
| 26 | ret |
| 27 | end |
| 28 | |
| 29 | fn pd0_uniform r edit ulong 1 to long |
| 30 | v = pd0_splitmix64 r |
| 31 | ret long rem v ulong 1000001 |
| 32 | end |
| 33 | |
| 34 | fn pd0_const r edit ulong 1 lo long hi long to long |
| 35 | u = pd0_uniform r |
| 36 | span = sub hi lo |
| 37 | ret add lo div mul span u PD0_MICRO |
| 38 | end |
| 39 | |
| 40 | fn pd0_noise r edit ulong 1 sigma_micro long to long |
| 41 | u1 = pd0_uniform r |
| 42 | u2 = pd0_uniform r |
| 43 | u3 = pd0_uniform r |
| 44 | u4 = pd0_uniform r |
| 45 | s = sub add add u1 u2 add u3 u4 long 2000000 |
| 46 | ret div mul s sigma_micro PD0_IH_SD |
| 47 | end |
| 48 | |
| 49 | fn pd0_abs_u64 x long to ulong |
| 50 | if lt x 0 |
| 51 | ret ulong neg x |
| 52 | end |
| 53 | ret ulong x |
| 54 | end |
| 55 | |
| 56 | fn pd0_mul a long b long to long |
| 57 | note Exact (a * b) / 1000000 truncating toward zero without i64 intermediate overflow. |
| 58 | neg_res = false |
| 59 | if lt a 0 |
| 60 | neg_res = not neg_res |
| 61 | end |
| 62 | if lt b 0 |
| 63 | neg_res = not neg_res |
| 64 | end |
| 65 | ua = pd0_abs_u64 a |
| 66 | ub = pd0_abs_u64 b |
| 67 | m = ulong 1000000 |
| 68 | qa = div ua m |
| 69 | ra = rem ua m |
| 70 | qb = div ub m |
| 71 | rb = rem ub m |
| 72 | t0 = mul mul qa qb m |
| 73 | t1 = add mul qa rb mul qb ra |
| 74 | t2 = div mul ra rb m |
| 75 | res = add add t0 t1 t2 |
| 76 | if neg_res |
| 77 | ret neg long res |
| 78 | end |
| 79 | ret long res |
| 80 | end |
| 81 | |
| 82 | fn pd0_div a long b long to long |
| 83 | ret div a b |
| 84 | end |