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 / src / physics0 / pd0_rng.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "physics0/pd0_rng.h" |
| 18 | #include "omega_native.h" |
| 19 | |
| 20 | #include <string.h> |
| 21 | |
| 22 | uint64_t pd0_fnv1a64(const char *tag) { |
| 23 | size_t n = strlen(tag); |
| 24 | uint64_t h = 0xcbf29ce484222325ull; |
| 25 | if (n > 0) (void)omega2_pd0_om_fnv1a64(&h, (const uint8_t *)tag, n, n); |
| 26 | return h; |
| 27 | } |
| 28 | |
| 29 | uint64_t pd0_splitmix64(uint64_t *s) { |
| 30 | uint64_t st[1] = { *s }, out = 0; |
| 31 | (void)omega2_pd0_om_splitmix64(&out, st, 1); |
| 32 | *s = st[0]; |
| 33 | return out; |
| 34 | } |
| 35 | |
| 36 | void pd0_stream(pd0_rng *r, uint64_t seed, const char *tag) { r->s = seed ^ pd0_fnv1a64(tag); } |
| 37 | uint64_t pd0_next(pd0_rng *r) { return pd0_splitmix64(&r->s); } |
| 38 | int64_t pd0_uniform(pd0_rng *r) { |
| 39 | uint64_t st[1] = { r->s }, out = 0; |
| 40 | (void)omega2_pd0_om_uniform(&out, st, 1); |
| 41 | r->s = st[0]; |
| 42 | return (int64_t)out; |
| 43 | } |
| 44 | |
| 45 | int64_t pd0_const(pd0_rng *r, int64_t lo, int64_t hi) { |
| 46 | int64_t u = pd0_uniform(r); |
| 47 | return lo + ((hi - lo) * u) / PD0_MICRO; |
| 48 | } |
| 49 | |
| 50 | int64_t pd0_noise(pd0_rng *r, int64_t sigma_micro) { |
| 51 | int64_t u1 = pd0_uniform(r), u2 = pd0_uniform(r), u3 = pd0_uniform(r), u4 = pd0_uniform(r); |
| 52 | pd0_i128 c = (pd0_i128)((u1 + u2 + u3 + u4) - 2000000LL) * sigma_micro; |
| 53 | return (int64_t)(c / PD0_IH_SD); |
| 54 | } |
| 55 | |
| 56 | int64_t pd0_mul(int64_t a, int64_t b) { |
| 57 | pd0_i128 p = (pd0_i128)a * (pd0_i128)b; |
| 58 | return (int64_t)(p / PD0_MICRO); |
| 59 | } |
| 60 | |
| 61 | int64_t pd0_div(int64_t a, int64_t b) { return a / b; } |