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 / tests / physics0 / osc / pd0_helpers.osc

62 lines · 2279 bytes Raw Blame History
1// entry: mul const_draw noise_map l0_s0 l0_s1 l1_s0 l1_s1 l2_s1 cube description_bits
2// PD-0 integer helpers written in the Omega Systems Core language (OSC-3
3// subset: integer scalars, no structs, no I/O). Each function recomputes a
4// piece of spec PD0_HIDDEN_EQUATION_BENCHMARK.md (physics main 2f881b1):
5// mul section 2.1 product rescale (truncating toward zero)
6// const_draw section 3 constant draw lo + ((hi - lo) * u) / 1e6
7// noise_map section 3 Irwin-Hall map from four uniforms to the noise
8// l0_s0, l0_s1 section 4.1 L0 kinematics one tick
9// l1_s0, l1_s1 section 4.1 L1 spring one tick
10// l2_s1 section 4.1 L2 damped spring velocity tick
11// cube the L4 monomial s0*s0*s0 in micro-units
12// description_bits section 6.2 rule for one relation
13// Limits: OSC has no 128-bit integer, so `mul` traps OVERFLOW instead of
14// rescaling when |a*b| >= 2^63; the C side (pd0_mul) uses 128-bit. The
15// differential test in tests/physics0/test_pd0_osc.c only feeds values inside
16// the world box (|v| <= 10.0 units, constants <= 9.0), where both agree.
17// expect-run: mul 2000000 3000000 -> 6000000
18// expect-run: l0_s0 1500000 -500000 -> 1000000
19// expect-run: description_bits 2 1 3 0 -> 144
20
21fn mul(a: i64, b: i64) -> i64 {
22 return (a * b) / 1000000;
23}
24
25fn const_draw(lo: i64, hi: i64, u: i64) -> i64 {
26 return lo + ((hi - lo) * u) / 1000000;
27}
28
29fn noise_map(u1: i64, u2: i64, u3: i64, u4: i64, sigma: i64) -> i64 {
30 let s: i64 = (u1 + u2 + u3 + u4) - 2000000;
31 return (s * sigma) / 577350;
32}
33
34fn l0_s0(s0: i64, s1: i64) -> i64 {
35 return s0 + s1;
36}
37
38fn l0_s1(s1: i64, u: i64) -> i64 {
39 return s1 + u;
40}
41
42fn l1_s0(s0: i64, s1: i64, dt: i64) -> i64 {
43 return s0 + mul(s1, dt);
44}
45
46fn l1_s1(s0: i64, s1: i64, u: i64, k: i64, dt: i64) -> i64 {
47 let spring: i64 = 0 - mul(k, s0);
48 return s1 + mul(spring + u, dt);
49}
50
51fn l2_s1(s0: i64, s1: i64, u: i64, k: i64, c: i64, dt: i64) -> i64 {
52 let f: i64 = (0 - mul(k, s0)) - mul(c, s1) + u;
53 return s1 + mul(f, dt);
54}
55
56fn cube(s0: i64) -> i64 {
57 return mul(mul(s0, s0), s0);
58}
59
60fn description_bits(n_vars: u32, n_channels: u32, n_terms: u32, n_latent: u32) -> u32 {
61 return n_terms * (8 * (n_vars + n_channels) + 24) + 8 * n_latent;
62}