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/docs/numeric/E1_TRANSCENDENTAL_CONTRACT.md # 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 / docs / numeric / E1_TRANSCENDENTAL_CONTRACT.md` 200 lines ยท 12885 bytes Raw Blame History Rendered 1 # E1 WP-B: FP32 transcendental contract (CPU tier, bounded) 2 3 Status: CPU tier implemented and checked against all 2^32 inputs per op. 4 GPU (GB10) realization: NOT_RUN. These ops are not registered in the 5 OMEGA_NOP table yet. 6 7 Sources: `src/omega_numeric_transc.c`, `src/omega_numeric_transc.h`. 8 Test: `tests/test_omega_transc.c`. Make targets: `test-numeric-transc` 9 (fast, about 1.1M inputs per op plus 10 mutants), `test-numeric-transc-full` 10 (exhaustive), `test-numeric-transc-digest` (frozen full-domain digests). 11 12 ## 1. What the contract promises 13 14 For each op `f` and every FP32 input `x` (all 2^32 bit patterns): 15 16 1. **Fixed sequence.** The result is produced by the exact sequence of 17 binary32 operations written in `omega_numeric_transc.c`: FADD, FSUB, 18 FMUL, FMADD (fused, one rounding), FDIV and FSQRT (correctly rounded), 19 integer and bit operations, exact comparisons, and exact int-to-float 20 conversion of small integers. Every floating-point step is an explicit 21 AArch64 instruction, so the compiler cannot contract, reassociate, 22 constant-fold or widen it. No libm, no double, no long double. 23 2. **Environment.** FPCR: round to nearest even, no flush to zero, no 24 default-NaN, no FEAT_AFP controls (`OMEGA_NUMERIC_FPCR_REQUIRED_CLEAR`). 25 The test refuses to run otherwise. 26 3. **Determinism.** Same input, same output bits, every run, any thread 27 count and order (checked: two full passes, different thread counts, 28 reversed order, identical digests). 29 4. **Bounded error.** `d(f_seq(x), CR(f(x))) <= OMEGA_TRANSC_MAX_ULP_` 30 for every finite result (Section 3), where CR is the correctly rounded 31 FP32 value of the exact real function. 32 5. **Special values exact.** NaN in gives the canonical quiet NaN 33 `0x7fc00000`; results that are NaN, +-inf or a zero match the correctly 34 rounded result exactly, including the sign of zero. 35 6. **Parity target.** A realization on any other substrate must issue the 36 same operations in the same order and reproduce the full-domain digests 37 of Section 6 bit for bit. 38 39 ## 2. Ops, algorithms, domains 40 41 | Op | Algorithm (frozen) | Domain and special behaviour | 42 |---|---|---| 43 | SIGMOID | `e^-abs(x) = m 2^k` (shared exp core); x >= 0: `1/(1 + m 2^k)`; x < 0: `(m/(1 + m 2^k)) 2^k` so the only rounding into subnormals is the last multiply | all x; x >= 18 -> 1; x <= -104 -> +0; +-0 -> 0.5; +inf -> 1; -inf -> +0 | 44 | TANH | `abs(x) < 0.5625`: odd Taylor to x^21 (Horner in x^2, FMADD); else `1 - 2/(e^(2abs(x)) + 1)`; sign restored by bit | all x; `abs(x) >= 9.5` -> +-1; +-0 -> +-0; subnormals return x | 45 | RSQRT | `y = 1/sqrt(x)` by FSQRT and FDIV (within 1 ulp), then an exact integer rounding step: move y to the neighbour while the midpoint test `N^2 X 2^s < 1` (128-bit integer) says the true value lies past the midpoint. Midpoints are never exact roots, so there are no ties. | correctly rounded for all x > 0 incl. subnormals (scaled by 2^24, result by 2^12, both exact); +0 -> +inf; -0 -> -inf; x < 0 -> NaN; +inf -> +0 | 46 | EXP2 | `k = nearest(x)` (1.5 2^23 add trick), `f = x - k` exact, `abs(f) <= 1/2`; `2^f = 1 + f p(f)` degree-8 Taylor in `ln2^n/n!`; `m 2^k` by two exact power-of-two multiplies (the last one rounds once into subnormals) | x >= 128 -> +inf; x < -151 -> +0; -inf -> +0; +-0 -> 1; x = -150 -> +0 (tie to even) | 47 | LOG2 | `x = m 2^e`, `m` in (0.707, 1.414]; `z = (m-1)/(m+1)` with its exact residue (2Sum, FMADD remainder); `log2 m = (2/ln2)(z + z^3/3 + ... + z^11/11)` with 2/ln2 split in two parts and the leading product kept exact; `e + log2 m` by 2Sum | x > 0 incl. subnormals (scaled by 2^23 exactly); +-0 -> -inf; x < 0 -> NaN; +inf -> +inf; 1 -> +0 | 48 | ERF | `abs(x) < 0.5`: `(2/sqrt pi) x (1 + x^2 q(x^2))`, q degree 6, 2/sqrt(pi) split in two parts; `0.5 <= abs(x) < 4`: `1 - erfc(abs(x))`, `erfc(y) = e^(-y^2) erfcx(y)`, `y^2` exact as two floats into the exp core, `erfcx` from 17 degree-10 Taylor pieces on [0.5, 11); sign restored by bit | all x; `abs(x) >= 4` -> +-1; +-0 -> +-0 | 49 | SIN, COS | `abs(x) <= pi/4`: no reduction; else `k = nearest(x 2/pi)`, `r = x - k pi/2` with pi/2 split in three (24 + 24 + 24 bits), the first product exact, the second as an exact 2Prod, carried as `rh + rl`; quadrant `(k + [cos]) mod 4` picks `sin_poly` (to r^13) or `cos_poly` (to r^14, `1 - r^2/2` kept exact) and the sign | **admitted domain `abs(x) <= 2^22`** (`OMEGA_TRANSC_TRIG_MAX_ABS`). Outside it, and for +-inf and NaN, the result is NaN (`0x7fc00000`). sin(+-0) = +-0; cos(+-0) = 1 | 50 | GELU (erf form) | `x Phi(x) = (x/2)(1 + erf(x/sqrt 2))`. `abs(y) < 0.5`: `x/2 + (x/2) erf_small(y)`; else `x^2/2` exact, `erfc(abs(y)) = e^(-y^2) erfcx(abs(y))` with the rounding of `y = x/sqrt2` put back to first order through `erfcx' = 2y erfcx - 2/sqrt pi`; `e^(-y^2)` and `erfcx` each carried as a high and a low float (the last rounding recovered by FMADD, the low part of the erfcx constant term from a second table) and multiplied with an exact 2Prod; x >= 0: `x - (x/2) erfc(y)`; x < 0: `(x/2) erfc(abs(y))`, one rounding into subnormals | all x; x >= 8 -> x (incl. +inf); x < -15.5 -> -0 (incl. -inf); +-0 -> +-0; the result always has the sign of x | 51 52 Why the domain limit on SIN/COS: the three-part pi/2 reduction is exact 53 enough only while `k` fits 22 bits. A Payne-Hanek reduction for the full 54 range is deferred; the contract states NaN instead of an unchecked value. 55 56 Saturation thresholds (18, 104, 9.5, 128, 151, 4, 8, 15.5) are points past 57 which the exact function rounds to the stated constant; the reasons are 58 in comments next to each threshold and the exhaustive check confirms each. 59 60 ### Porting notes (GPU parity) 61 62 - Division and square root must be the IEEE correctly rounded forms 63 (PTX `div.rn.f32`, `sqrt.rn.f32`), never `rcp`/`rsqrt.approx` or fast-math 64 division; denormals preserved (`.ftz` off); FMADD as `fma.rn.f32`; no 65 contraction of separate multiply and add. 66 - The RSQRT rounding step uses a 128-bit integer product (`N^2 X < 2^75`); 67 a GPU port computes it with 64-bit multiply-high pairs. It is pure 68 integer arithmetic, so parity does not depend on the FP unit. 69 - `scale2` is only ever called with a normal `m`; with a subnormal `m` it 70 could round twice. 71 72 ## 3. Error metric (ulp) 73 74 `ord(b)` maps FP32 bits to integers: `ord(b) = b` for sign 0 and 75 `ord(b) = -(b & 0x7fffffff)` for sign 1. Both zeros map to 0, the smallest 76 subnormals to +-1, and consecutive representable values to consecutive 77 integers, across the subnormal/normal boundary and through zero. 78 79 `d(s, o) = |ord(s) - ord(o)|`, where `o` is the exact result rounded once 80 to FP32 (round to nearest even, with gradual underflow). So `d` counts the 81 representable FP32 values between the two results: `d = 0` is correctly 82 rounded; `d = n` is n units of the result's binade (2^-149 in the subnormal 83 range). Near zero the metric does not blow up: a result off by one 84 subnormal step is `d = 1`. Special cases are not measured by `d`: NaN 85 results must be `0x7fc00000` exactly when the exact result is NaN, infinities 86 must match exactly, and a zero result must match the correctly rounded 87 zero including its sign. Any special mismatch fails the test. 88 89 ## 4. Declared bounds 90 91 Declared maximum = observed maximum over all 2^32 inputs + a margin of 1 92 ulp (0 for RSQRT, which is correctly rounded by construction and observed 93 so). The test fails if any input exceeds the declared bound. 94 95 | Op | Observed max ulp (exhaustive) | Declared max ulp | Inputs with d > 0 | 96 |---|---|---|---| 97 | SIGMOID | 2 | 3 | 125,485,046 (196,747 at 2) | 98 | TANH | 2 | 3 | 6,116,832 (14 at 2) | 99 | RSQRT | 0 | 0 (correctly rounded) | 0 | 100 | EXP2 | 1 | 2 | 8,754,037 | 101 | LOG2 | 1 | 2 | 197,064 | 102 | ERF | 2 | 3 | 22,917,730 (2,290 at 2) | 103 | SIN (`abs(x) <= 2^22`) | 1 | 2 | 6,324,452 | 104 | COS (`abs(x) <= 2^22`) | 1 | 2 | 5,681,590 | 105 | GELU | 2 | 3 | 9,208,815 (1,993 at 2) | 106 107 The fast target (`make test-numeric-transc`) checks about 1.1M inputs per 108 op: 2^20 multiplicative-hash samples of the full bit space, +-64 ulp around 109 56 thresholds and special points (both signs), and NaN/inf/zero/subnormal 110 edges. The exhaustive target checks every input. 111 112 ## 5. The oracle 113 114 The reference is an independent IEEE binary128 evaluation (`long double` 115 on AArch64 Linux: 113-bit significand, software arithmetic from the 116 compiler runtime, no libm) in `tests/test_omega_transc.c`. It shares no 117 code, constants or algorithms with the sequences: 118 119 - exp: `k = nearest(a/ln2)`, Taylor of `e^r - 1` on `r/256` run to 120 2^-120, then 8 squarings in the form `e(2 + e)`; 2^k built from bits. 121 - log2: a table of `log2(1 + j 2^-23)` for all 2^23 mantissas from the 122 atanh series run to 2^-120 (no sqrt(2) split). 123 - 1/sqrt: a table of the FP32-rounded `1/sqrt(v)` for all 2^24 (mantissa, 124 exponent parity) pairs, from a double seed and three binary128 Newton 125 steps; exact power-of-two rescale. 126 - erf: alternating Maclaurin series below 0.5, the all-positive Kummer 127 series `e^(-y^2) sum y (2y^2)^n / (2n+1)!!` up to 4.5; erfc beyond 4.5 by 128 the continued fraction evaluated backwards from depth 240. 129 - sin/cos: `x - k PA - k PB - k PC` with pi/2 = PA (80 bits, so `k PA` is 130 exact for `k < 2^33`) + PB + PC, then Taylor series run to 2^-120. 131 - GELU: `(x/2) erfc(-x/sqrt 2)`; tanh: `expm1(2x)/(expm1(2x) + 2)` below 132 0.5, else `1 - 2/(e^(2x) + 1)`. 133 134 Each value is rounded once to FP32 by the runtime conversion. Shortcuts 135 with their justification (the omitted terms are far below half an ulp): 136 `|x| < 2^-40` uses the first two or three series terms (next term below 137 2^-80 relative); sigmoid `x >= 40` -> 1, `x <= -110` -> 0; tanh `|x| >= 12` 138 -> +-1; erf `|x| >= 4.5` -> +-1 (erfc(4.5) < 2^-32); GELU `x >= 16` -> x, 139 `x <= -16` -> -0; EXP2 integer x -> exact `2^x`. 140 141 Self-checks run before every oracle mode (identities `e^ln2 = 2`, 142 `e^1 e^-1 = 1`, exp/log inversion, `sin^2 + cos^2 = 1`, `sin(pi/6) = 1/2`, 143 large-argument reduction, `rsqrt(2)^2 2 = 1`, erf(1) and erfc(5) known 144 values, continued fraction depth 240 vs 480, continued fraction vs 145 `1 - erf` at 4.5, series seam at 0.5). Any failure fails the run. 146 147 Hard-to-round inputs: a binary128 value within 2^-113 relative of an FP32 148 midpoint could round the wrong way. The sequences' errors are reported 149 against the oracle; a mis-rounded oracle could at most shift a measured 150 distance by 1, which the margin covers. 151 152 ## 6. Determinism and parity digests 153 154 Full-domain digest per op: `D = sha256(C_0 .. C_255)`, 155 `C_c = sha256(B_c,0 .. B_c,1023)`, `B_c,b = sha256` of the 2^14 outputs 156 (FP32 bits, little-endian) for inputs `c 2^24 + b 2^14 ..` in increasing 157 order. The exhaustive run computes them twice (8 threads forward, 5 threads 158 reversed) and requires equality; `make test-numeric-transc-digest` 159 recomputes them from the sequences alone and compares them with the frozen 160 values in the test. 161 162 | Op | Full-domain digest (sha256) | 163 |---|---| 164 | SIGMOID | ``b4e43cb75fb625314aa464a4b4cffd7bc398d75cb7a50ff23af786025af1886d`` | 165 | TANH | ``932c09ffe7f02125dc302bfe9c3666a4866b109627fd89cfca0827cf3b1e5daa`` | 166 | RSQRT | ``454fc9d5fb2e640acc8ba48422c3ae5e61d17fcee8cec20091cfb4a044ec51e4`` | 167 | EXP2 | ``227cb65782077d8224121eca527f8beba5d9fc80648ee57c38895466a8345bd8`` | 168 | LOG2 | ``fdf21ee2f13fa4ffec0c5c5ea1af4e1650ff9dbd91b138a608a68b43670a07f3`` | 169 | ERF | ``aef63f62db823726c45f88b6605263d614854595679edcb17c3a4492abc74aba`` | 170 | SIN | ``9e4debc75f832121cea47e9fad2e008329ccc62b291c3b68e6f60bb3f908cddc`` | 171 | COS | ``28d580ee2efe1256397cc8a3b2dcd4bfe397100a0f9663e1c010baa136dcceb3`` | 172 | GELU | ``bda4da73a44f4b06cd1ebebe13dec56b91c7e87b826253380e922b3ba38689db`` (re-frozen after the subnormal tie fix; was ``5663383b`...`) | 173 174 ## 7. Mutation evidence 175 176 `-DOMEGA_TRANSC_MUTATE=` flips mantissa bit 12 (relative 2^-11) of one 177 coefficient per op (EXP2, LOG2, shared exp core, TANH, ERF small, one erfcx 178 piece, SIN, COS, GELU's 1/sqrt2) or, for RSQRT, removes the exact rounding 179 step. `make test-numeric-transc` builds all 10 mutants and requires each to 180 FAIL the bound check; it does. 181 182 ## 8. Coefficients 183 184 All coefficients are frozen FP32 values written as exact hex literals 185 (24-bit integer times a power of two). They were computed with 60-digit 186 `bc` arithmetic and rounded once to FP32: Taylor coefficients of `e^r`, 187 `ln2^n/n!` for 2^f, odd Taylor coefficients of tanh (Bernoulli form), of 188 erf, sin and cos, the atanh series `2/((2n+1) ln2)` for log2, and the 189 erfcx Taylor coefficients about 17 centres from the exact recurrence 190 `(n+1) a_(n+1) = 2c a_n + 2 a_(n-1)` (with `-2/sqrt(pi)` in the first 191 step). Split constants (ln2, 2/ln2, 2/sqrt(pi), pi/2, 1/sqrt2) are given as 192 a high FP32 part and the FP32-rounded remainder. No coefficient was tuned 193 against the test. 194 195 ## 9. Not done 196 197 - GB10 realization and parity run: NOT_RUN (this work package is the CPU 198 tier only). 199 - SIN/COS beyond `|x| <= 2^22`: not admitted (NaN by contract). 200 - OMEGA_NOP registration: not done (out of scope for WP-B).