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 / ladder / pd0_rng.h
| 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 | /* SplitMix64 (Vigna's published constants) and FNV-1a 64, per spec section 3. Header-only. */ |
| 18 | #ifndef PD0_RNG_H |
| 19 | #define PD0_RNG_H |
| 20 | #include <stdint.h> |
| 21 | #include <string.h> |
| 22 | typedef struct { uint64_t s; } pd0_rng; |
| 23 | static inline uint64_t pd0_rng_next(pd0_rng *r) |
| 24 | { |
| 25 | uint64_t z = (r->s += 0x9E3779B97F4A7C15ull); |
| 26 | z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ull; |
| 27 | z = (z ^ (z >> 27)) * 0x94D049BB133111EBull; |
| 28 | return z ^ (z >> 31); |
| 29 | } |
| 30 | static inline uint64_t pd0_fnv1a64(const char *tag) |
| 31 | { |
| 32 | uint64_t h = 0xcbf29ce484222325ull; size_t n = strlen(tag); |
| 33 | for (size_t i = 0; i < n; i++) { h ^= (uint8_t)tag[i]; h *= 0x100000001b3ull; } |
| 34 | return h; |
| 35 | } |
| 36 | static inline void pd0_rng_stream(pd0_rng *r, uint64_t seed, const char *tag) { r->s = seed ^ pd0_fnv1a64(tag); } |
| 37 | /* uniform micro in [0, 1_000_000] */ |
| 38 | static inline int64_t pd0_rng_unit(pd0_rng *r) { return (int64_t)(pd0_rng_next(r) % 1000001ull); } |
| 39 | /* Irwin-Hall noise draw, sd of the four-uniform sum 577_350 micro */ |
| 40 | static inline int64_t pd0_rng_noise(pd0_rng *r, int64_t sigma_micro) |
| 41 | { |
| 42 | int64_t s = pd0_rng_unit(r) + pd0_rng_unit(r) + pd0_rng_unit(r) + pd0_rng_unit(r) - 2000000; |
| 43 | return (s * sigma_micro) / 577350; |
| 44 | } |
| 45 | #endif |