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/tests/test_omega_transc.c # 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 / tests / test_omega_transc.c` 760 lines ยท 33827 bytes Raw Blame History 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 /* 18 * test_omega_transc.c -- E1 WP-B: bounded-contract checks for the frozen FP32 19 * transcendental sequences in src/omega_numeric_transc.c. 20 * 21 * Oracle: an independent IEEE binary128 evaluation (long double on AArch64 22 * Linux: 113-bit significand, software arithmetic from the compiler runtime, 23 * no libm). It shares no code with the sequences: different algorithms 24 * (series run to 2^-118 relative, squaring of expm1, a 2^23-entry log2 25 * table, Newton for 1/sqrt, the Kummer series and a continued fraction for 26 * erf/erfc, 80+113+113-bit pi/2 reduction), then one rounding to FP32 by the 27 * compiler runtime. Where an input range is skipped by a short closed form 28 * (tiny |x|, saturation) the bound that justifies it is written next to it. 29 * 30 * Error metric: d(s, o) = | ord(s) - ord(o) |, where o is the oracle value 31 * rounded once to FP32 and ord() maps FP32 bits to consecutive integers 32 * (+0 and -0 both map to 0, the smallest subnormal to +-1). So d counts the 33 * representable FP32 values between the sequence result and the correctly 34 * rounded result: d = 0 means correctly rounded, d <= n means within n ulp 35 * of the CR result (ulp of the result's binade, 2^-149 in the subnormal 36 * range). Special results must match exactly: NaN (canonical 0x7fc00000), 37 * +-inf, and the sign of a zero result. 38 * 39 * Modes: 40 * (none) | fast special values, oracle self-checks, about 1.1M inputs per 41 * op (2^20 multiplicative-hash samples of all 2^32 bit 42 * patterns plus +-64 ulp around every threshold), 43 * determinism (two evaluations, different thread counts). 44 * full all 2^32 inputs of every op against the oracle, then a 45 * second sequence-only pass with another thread count and 46 * reversed chunk order; prints the full-domain digests. 47 * digest sequence-only full-domain digests, compared with the 48 * frozen digests below (cross-run, cross-build parity). 49 * Optional second argument: an op name to restrict the run. 50 * Env OMEGA_TRANSC_THREADS (default 8). 51 * Exit 0 only if every check passes. 52 */ 53 #include "omega_numeric_transc.h" 54 #include "omega_numeric.h" 55 #include "sha256.h" 56 57 #include 58 #include 59 #include 60 #include 61 #include 62 #include 63 #include 64 65 #if LDBL_MANT_DIG != 113 66 #error "oracle needs IEEE binary128 long double (AArch64 Linux)" 67 #endif 68 69 typedef long double Q; 70 71 static int g_fail = 0; 72 static int g_verbose = 0; /* env OMEGA_TRANSC_VERBOSE: print up to 40 over-bound inputs */ 73 static atomic_int g_printed; 74 #define FAIL(...) do { printf("FAIL: " __VA_ARGS__); printf("\n"); g_fail = 1; } while (0) 75 76 static inline uint32_t bits_of(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } 77 static inline float float_of(uint32_t u) { float f; memcpy(&f, &u, 4); return f; } 78 static inline int nan_bits(uint32_t u) { return (u & 0x7fffffffu) > 0x7f800000u; } 79 80 /* ---- binary128 oracle ------------------------------------------------------ */ 81 82 static const Q QLN2 = 0.6931471805599453094172321214581765680755001343L; 83 static const Q QINVLN2 = 1.4426950408889634073599246810018921374266459541L; 84 static const Q QTWO_OVER_SQRTPI = 1.1283791670955125738961589031215451716881012586L; 85 static const Q QINV_SQRTPI = 0.5641895835477562869480794515607725858440506293L; 86 static const Q QINV_SQRT2 = 0.7071067811865475244008443621048490392848359376L; 87 static const Q QINV_SQRT2PI = 0.3989422804014326779399460599343818684758586311L; 88 static const Q QTWO_OVER_PI = 0.6366197723675813430755350534900574481378385829L; 89 static const Q QPIO4 = 0.78539816339744830961566084581987572104929234985L; 90 /* pi/2 = PA + PB + PC: PA has 80 bits (k PA exact for k < 2^33). */ 91 static const Q QPA = 1.570796326794896619231321054808035091003570737910877141985110938549041748046875L; 92 static const Q QPB = 6.`36831716351095013961776675768502358811299070339e`-25L; 93 static const Q QPC = 2.`54630579611570017288406302157224126338250084121e`-61L; 94 95 static Q qabs(Q a) { return a < 0 ? -a : a; } 96 97 /* 2^k exactly, |k| <= 16000. */ 98 static Q q_pow2(int k) { 99 union { Q q; uint64_t w[2]; } u; 100 u.w[0] = 0; 101 u.w[1] = (uint64_t)(k + 16383) << 48; 102 return u.q; 103 } 104 105 static uint32_t q_round(Q v) { return bits_of((float)v); } 106 107 /* e^u - 1 for |u| <= 0.75: Taylor on u/256 to 2^-120, then 8 squarings in 108 * the form (1+e)^2 - 1 = e (2 + e), which keeps relative accuracy. */ 109 static Q q_expm1_small(Q u) { 110 Q v = u * q_pow2(-8); 111 Q s = v, t = v; 112 for (int n = 2; n < 40; n++) { 113 t = t * v / (Q)n; 114 s += t; 115 if (qabs(t) < qabs(s) * q_pow2(-120)) break; 116 } 117 for (int i = 0; i < 8; i++) s = s * (2.0L + s); 118 return s; 119 } 120 121 static Q q_exp(Q a) { 122 Q kq = a * QINVLN2; 123 long long k = (long long)(kq >= 0 ? kq + 0.5L : kq - 0.5L); 124 Q r = a - (Q)k * QLN2; 125 return (1.0L + q_expm1_small(r)) * q_pow2((int)k); 126 } 127 128 static Q q_expm1(Q u) { 129 if (qabs(u) <= 0.75L) return q_expm1_small(u); 130 return q_exp(u) - 1.0L; 131 } 132 133 /* ln(m), m in [1, 2): 2 atanh(z), z = (m-1)/(m+1) <= 1/3, run to 2^-120. */ 134 static Q q_ln_mant(Q m) { 135 Q z = (m - 1.0L) / (m + 1.0L), z2 = z * z; 136 Q s = z, t = z; 137 for (int n = 3; n < 400; n += 2) { 138 t *= z2; 139 Q term = t / (Q)n; 140 s += term; 141 if (term < s * q_pow2(-120)) break; 142 } 143 return 2.0L * s; 144 } 145 146 /* erf(y), y >= 0, y < 4.5. */ 147 static Q q_erf(Q y) { 148 if (y < 0.5L) { /* alternating Maclaurin series */ 149 Q y2 = y * y, t = y, s = y; 150 for (int n = 1; n < 200; n++) { 151 t = -t * y2 / (Q)n; 152 Q term = t / (Q)(2 * n + 1); 153 s += term; 154 if (qabs(term) < qabs(s) * q_pow2(-120)) break; 155 } 156 return QTWO_OVER_SQRTPI * s; 157 } 158 /* Kummer form, all terms positive: e^-y^2 sum y (2y^2)^n / (2n+1)!! */ 159 Q y2 = y * y, t = y, s = y; 160 for (int n = 1; n < 2000; n++) { 161 t = t * 2.0L * y2 / (Q)(2 * n + 1); 162 s += t; 163 if (t < s * q_pow2(-120)) break; 164 } 165 return QTWO_OVER_SQRTPI * q_exp(-y2) * s; 166 } 167 168 /* erfc(y), y >= 4.5: continued fraction 169 * erfc(y) = e^-y^2 / sqrt(pi) / (y + (1/2)/(y + 1/(y + (3/2)/(y + ...)))), 170 * evaluated backwards from depth n. Self-checked against depth 2n and 171 * against 1 - erf at y = 4.5 (oracle_selfcheck). */ 172 static Q q_erfc_cf(Q y, int depth) { 173 Q f = y; 174 for (int n = depth; n >= 1; n--) f = y + ((Q)n * 0.5L) / f; 175 return q_exp(-y * y) * QINV_SQRTPI / f; 176 } 177 #define ERFC_CF_DEPTH 240 178 179 static Q q_erfc(Q y) { /* any sign */ 180 if (y < 0) return -y < 4.5L ? 1.0L + q_erf(-y) : 2.0L - q_erfc_cf(-y, ERFC_CF_DEPTH); 181 if (y < 4.5L) return 1.0L - q_erf(y); 182 return q_erfc_cf(y, ERFC_CF_DEPTH); 183 } 184 185 /* sin / cos of a reduced |r| <= ~pi/4 + tiny, series to 2^-120. */ 186 static Q q_sin_r(Q r) { 187 Q r2 = r * r, t = r, s = r; 188 for (int n = 1; n < 60; n++) { 189 t = -t * r2 / (Q)((2 * n) * (2 * n + 1)); 190 s += t; 191 if (qabs(t) < qabs(s) * q_pow2(-120)) break; 192 } 193 return s; 194 } 195 static Q q_cos_r(Q r) { 196 Q r2 = r * r, t = 1.0L, s = 1.0L; 197 for (int n = 1; n < 60; n++) { 198 t = -t * r2 / (Q)((2 * n - 1) * (2 * n)); 199 s += t; 200 if (qabs(t) < qabs(s) * q_pow2(-120)) break; 201 } 202 return s; 203 } 204 /* x = k pi/2 + r, |x| <= 2^22; x - k PA exact (see QPA). */ 205 static Q q_sincos(Q x, int want_cos) { 206 long long k = 0; 207 Q r = x; 208 if (qabs(x) > QPIO4) { 209 Q kq = x * QTWO_OVER_PI; 210 k = (long long)(kq >= 0 ? kq + 0.5L : kq - 0.5L); 211 r = ((x - (Q)k * QPA) - (Q)k * QPB) - (Q)k * QPC; 212 } 213 unsigned q = (unsigned)((k + want_cos) & 3); 214 Q v = (q & 1) ? q_cos_r(r) : q_sin_r(r); 215 return (q & 2) ? -v : v; 216 } 217 218 /* 1/sqrt(v), v in [1, 4): double seed (FSQRT/FDIV instructions), two Newton 219 * steps in binary128 (53 -> 106 -> >113 bits). */ 220 static Q q_rsqrt(Q v) { 221 double d = (double)v, s; 222 __asm__("fsqrt %d0, %d1" : "=w"(s) : "w"(d)); 223 Q y = (Q)(1.0 / s); 224 for (int i = 0; i < 3; i++) y = y * (3.0L - v * y * y) * 0.5L; 225 return y; 226 } 227 228 /* Tables (built once, in parallel): log2 of every mantissa 1 + j 2^-23 and 229 * the FP32-rounded 1/sqrt of every (mantissa, exponent parity) pair. */ 230 static Q *g_log2_tab; /* 2^23 entries */ 231 static uint32_t *g_rsqrt_tab; /* 2^24 entries */ 232 233 /* positive finite nonzero x = X 2^E with X in [2^23, 2^24) */ 234 static void norm_pos(uint32_t u, uint32_t *X, int *E) { 235 uint32_t e = (u >> 23) & 0xffu, m = u & 0x7fffffu; 236 if (e == 0) { 237 int sh = 0; 238 while (!(m & 0x800000u)) { m <<= 1; sh++; } 239 *X = m; 240 *E = 1 - 150 - sh; 241 } else { 242 *X = m | 0x800000u; 243 *E = (int)e - 150; 244 } 245 } 246 247 /* ---- per-op oracles: FP32 bits of the correctly rounded result -------------- */ 248 249 #define QNANB 0x7fc00000u 250 #define PINFB 0x7f800000u 251 #define NINFB 0xff800000u 252 253 static Q xq(uint32_t u) { return (Q)float_of(u); } 254 255 static uint32_t or_sigmoid(uint32_t u) { 256 if (nan_bits(u)) return QNANB; 257 Q x = xq(u); 258 if (x >= 40.0L) return 0x3f800000u; /* 1 - s < e^-40 << 2^-25 */ 259 if (x <= -110.0L) return 0; /* s < e^-110 < 2^-158 */ 260 if (qabs(x) < q_pow2(-40)) return q_round(0.5L + x * 0.25L - x * x * x / 48.0L); /* next term x^5/480 */ 261 return q_round(1.0L / (1.0L + q_exp(-x))); 262 } 263 264 static uint32_t or_tanh(uint32_t u) { 265 if (nan_bits(u)) return QNANB; 266 Q x = xq(u), ax = qabs(x), r; 267 if (ax >= 12.0L) r = 1.0L; /* 1 - tanh < 2e^-24 << 2^-25 */ 268 else if (ax < q_pow2(-40)) r = ax - ax * ax * ax / 3.0L + 2.0L * ax * ax * ax * ax * ax / 15.0L; 269 else if (ax < 0.5L) { Q em = q_expm1(2.0L * ax); r = em / (em + 2.0L); } 270 else r = 1.0L - 2.0L / (q_exp(2.0L * ax) + 1.0L); 271 uint32_t b = q_round(r); 272 return b | (u & 0x80000000u); 273 } 274 275 static uint32_t or_rsqrt(uint32_t u) { 276 if (nan_bits(u)) return QNANB; 277 if (u == 0) return PINFB; 278 if (u == 0x80000000u) return NINFB; 279 if (u & 0x80000000u) return QNANB; 280 if (u == PINFB) return 0; 281 uint32_t X; int E; 282 norm_pos(u, &X, &E); 283 /* x = (X/2^23) 2^(E+23); p = parity of that exponent */ 284 int ee = E + 23, p = ee & 1; 285 uint32_t r = g_rsqrt_tab[(X - 0x800000u) | ((uint32_t)p << 23)]; 286 int adj = -(ee - p) / 2; /* result normal: exact scaling */ 287 return r + (uint32_t)(adj * (1 << 23)); 288 } 289 290 static uint32_t or_exp2(uint32_t u) { 291 if (nan_bits(u)) return QNANB; 292 Q x = xq(u); 293 if (x >= 128.0L) return PINFB; 294 if (x < -152.0L) return 0; 295 if (x == (Q)(long long)x) return q_round(q_pow2((int)(long long)x)); /* exact (2^-150 ties to +0) */ 296 if (qabs(x) < q_pow2(-40)) { Q t = x * QLN2; return q_round(1.0L + t + t * t / 2.0L + t * t * t / 6.0L); } 297 return q_round(q_exp(x * QLN2)); 298 } 299 300 static uint32_t or_log2(uint32_t u) { 301 if (nan_bits(u)) return QNANB; 302 if ((u & 0x7fffffffu) == 0) return NINFB; 303 if (u & 0x80000000u) return QNANB; 304 if (u == PINFB) return PINFB; 305 uint32_t X; int E; 306 norm_pos(u, &X, &E); 307 return q_round((Q)(E + 23) + g_log2_tab[X - 0x800000u]); 308 } 309 310 static uint32_t or_erf(uint32_t u) { 311 if (nan_bits(u)) return QNANB; 312 Q ax = qabs(xq(u)); 313 uint32_t b = ax >= 4.5L ? 0x3f800000u : q_round(q_erf(ax)); /* erfc(4.5) < 2^-32 */ 314 return b | (u & 0x80000000u); 315 } 316 317 static uint32_t or_trig(uint32_t u, int want_cos) { 318 if (nan_bits(u)) return QNANB; 319 Q x = xq(u); 320 if (!(qabs(x) <= `4194304`.0L)) return QNANB; /* contract domain |x| <= 2^22 */ 321 if (qabs(x) < q_pow2(-40)) { /* next terms < 2^-160 relative */ 322 if (want_cos) return q_round(1.0L - x * x / 2.0L); 323 if (x == 0) return u; 324 return q_round(x - x * x * x / 6.0L); 325 } 326 return q_round(q_sincos(x, want_cos)); 327 } 328 static uint32_t or_sin(uint32_t u) { return or_trig(u, 0); } 329 static uint32_t or_cos(uint32_t u) { return or_trig(u, 1); } 330 331 static uint32_t or_gelu(uint32_t u) { 332 if (nan_bits(u)) return QNANB; 333 Q x = xq(u); 334 if (x >= 16.0L) return u; /* x erfc(11.3)/2 < 2^-180 x; +inf */ 335 if (x <= -16.0L) return 0x80000000u; /* |gelu| < 8 erfc(11.3) < 2^-180 */ 336 if (x == 0) return u; 337 if (qabs(x) < q_pow2(-100)) { 338 /* x^2/sqrt(2 pi) is below 2^-100 of x/2 and would vanish from the 339 * binary128 sum; any value in (x/2, x/2 + |x/2| 2^-100] rounds to float 340 * like the true one (only x/2 itself can be a float boundary). */ 341 Q t = 0.5L * x; 342 return q_round(t + qabs(t) * q_pow2(-100)); 343 } 344 if (qabs(x) < q_pow2(-40)) return q_round(0.5L * x + x * x * QINV_SQRT2PI); /* next x^4/(6 sqrt(2pi)) */ 345 return q_round(x * 0.5L * q_erfc(-x * QINV_SQRT2)); 346 } 347 348 /* ---- op table ---------------------------------------------------------------- */ 349 350 typedef struct { 351 const char *name; 352 float (*seq)(float); 353 uint32_t (*oracle)(uint32_t); 354 uint32_t bound; 355 const char *frozen; /* full-domain digest hex, "" until recorded */ 356 } op_t; 357 358 /* Frozen full-domain digests (hex): D = sha256(C_0 .. C_255), C_c = sha256(B_c,0 .. B_c,1023), 359 * B_c,b = sha256 of the outputs (FP32 bits, little-endian) for the 2^14 inputs 360 * whose bits are (c 2^24 + b 2^14) .. +2^14-1, in increasing order. 361 * Recorded from the first exhaustive run (make test-numeric-transc-full); 362 * An empty string would mean "not recorded" and makes digest mode FAIL. */ 363 #define DIGEST_SIGMOID "`b4e43cb75fb625314aa464a4b4cffd7bc398d75cb7a50ff23af786025af1886d`" 364 #define DIGEST_TANH "`932c09ffe7f02125dc302bfe9c3666a4866b109627fd89cfca0827cf3b1e5daa`" 365 #define DIGEST_RSQRT "`454fc9d5fb2e640acc8ba48422c3ae5e61d17fcee8cec20091cfb4a044ec51e4`" 366 #define DIGEST_EXP2 "`227cb65782077d8224121eca527f8beba5d9fc80648ee57c38895466a8345bd8`" 367 #define DIGEST_LOG2 "`fdf21ee2f13fa4ffec0c5c5ea1af4e1650ff9dbd91b138a608a68b43670a07f3`" 368 #define DIGEST_ERF "`aef63f62db823726c45f88b6605263d614854595679edcb17c3a4492abc74aba`" 369 #define DIGEST_SIN "`9e4debc75f832121cea47e9fad2e008329ccc62b291c3b68e6f60bb3f908cddc`" 370 #define DIGEST_COS "`28d580ee2efe1256397cc8a3b2dcd4bfe397100a0f9663e1c010baa136dcceb3`" 371 #define DIGEST_GELU "`bda4da73a44f4b06cd1ebebe13dec56b91c7e87b826253380e922b3ba38689db`" 372 373 static op_t g_ops[] = { 374 { "SIGMOID", omega_math_sigmoid, or_sigmoid, OMEGA_TRANSC_MAX_ULP_SIGMOID, DIGEST_SIGMOID }, 375 { "TANH", omega_math_tanh, or_tanh, OMEGA_TRANSC_MAX_ULP_TANH, DIGEST_TANH }, 376 { "RSQRT", omega_math_rsqrt, or_rsqrt, OMEGA_TRANSC_MAX_ULP_RSQRT, DIGEST_RSQRT }, 377 { "EXP2", omega_math_exp2, or_exp2, OMEGA_TRANSC_MAX_ULP_EXP2, DIGEST_EXP2 }, 378 { "LOG2", omega_math_log2, or_log2, OMEGA_TRANSC_MAX_ULP_LOG2, DIGEST_LOG2 }, 379 { "ERF", omega_math_erf, or_erf, OMEGA_TRANSC_MAX_ULP_ERF, DIGEST_ERF }, 380 { "SIN", omega_math_sin, or_sin, OMEGA_TRANSC_MAX_ULP_SIN, DIGEST_SIN }, 381 { "COS", omega_math_cos, or_cos, OMEGA_TRANSC_MAX_ULP_COS, DIGEST_COS }, 382 { "GELU", omega_math_gelu, or_gelu, OMEGA_TRANSC_MAX_ULP_GELU, DIGEST_GELU }, 383 }; 384 #define NOPS ((int)(sizeof(g_ops) / sizeof(g_ops[0]))) 385 386 /* ---- comparison ---------------------------------------------------------------- */ 387 388 static int64_t ord(uint32_t u) { 389 return (u & 0x80000000u) ? -(int64_t)(u & 0x7fffffffu) : (int64_t)u; 390 } 391 392 /* Returns the distance, or UINT32_MAX for a special-value mismatch. */ 393 static uint32_t dist(uint32_t s, uint32_t o) { 394 if (nan_bits(o) || nan_bits(s)) return (s == QNANB && nan_bits(o)) ? 0 : UINT32_MAX; 395 if ((o & 0x7fffffffu) == PINFB || (s & 0x7fffffffu) == PINFB) return s == o ? 0 : UINT32_MAX; 396 if ((o & 0x7fffffffu) == 0 || (s & 0x7fffffffu) == 0) return s == o ? 0 : UINT32_MAX; /* zero is exact: either side zero demands identical bits */ 397 int64_t d = ord(s) - ord(o); 398 if (d < 0) d = -d; 399 return d >= UINT32_MAX ? UINT32_MAX - 1 : (uint32_t)d; 400 } 401 402 typedef struct { 403 uint64_t n, over, hist[4]; /* hist: d = 0, 1, 2, >= 3 */ 404 uint32_t maxd, worst_in, worst_seq, worst_or; 405 } stats_t; 406 407 static void stats_add(stats_t *st, uint32_t in, uint32_t s, uint32_t o, uint32_t bound) { 408 uint32_t d = dist(s, o); 409 if (st->n == 0 || d > st->maxd) { st->maxd = d; st->worst_in = in; st->worst_seq = s; st->worst_or = o; } 410 st->n++; 411 st->hist[d < 3 ? d : 3]++; 412 if (d > bound) { 413 st->over++; 414 if (g_verbose && atomic_fetch_add(&g_printed, 1) < 40) 415 printf(" over: x=0x%08x seq=0x%08x cr=0x%08x d=%u\n", in, s, o, d); 416 } 417 } 418 419 static void stats_merge(stats_t *a, const stats_t *b) { 420 if (b->n == 0) return; 421 if (a->n == 0 || b->maxd > a->maxd) { 422 a->maxd = b->maxd; a->worst_in = b->worst_in; a->worst_seq = b->worst_seq; a->worst_or = b->worst_or; 423 } 424 a->n += b->n; 425 a->over += b->over; 426 for (int i = 0; i < 4; i++) a->hist[i] += b->hist[i]; 427 } 428 429 static void stats_print(const op_t *op, const stats_t *st, const char *what) { 430 printf("%-8s %-10s n=%llu max_ulp=%s%u (bound %u) at x=0x%08x seq=0x%08x cr=0x%08x d0=%llu d1=%llu d2=%llu d3+=%llu over=%llu %s\n", 431 op->name, what, (unsigned long long)st->n, 432 st->maxd == UINT32_MAX ? "SPECIAL_MISMATCH:" : "", st->maxd == UINT32_MAX ? 0 : st->maxd, 433 op->bound, st->worst_in, st->worst_seq, st->worst_or, 434 (unsigned long long)st->hist[0], (unsigned long long)st->hist[1], 435 (unsigned long long)st->hist[2], (unsigned long long)st->hist[3], 436 (unsigned long long)st->over, st->over ? "FAIL" : "PASS"); 437 if (st->over) FAIL("%s %s: %llu inputs exceed the declared bound", op->name, what, (unsigned long long)st->over); 438 } 439 440 /* ---- threads -------------------------------------------------------------- */ 441 442 static int g_threads = 8; 443 444 typedef struct { 445 void (*fn)(void *ctx, uint32_t item, stats_t *st); 446 void *ctx; 447 uint32_t nitems; 448 int reverse; 449 atomic_uint next; 450 pthread_mutex_t mu; 451 stats_t total; 452 } pool_t; 453 454 static void *pool_worker(void *arg) { 455 pool_t *p = arg; 456 stats_t local; 457 memset(&local, 0, sizeof(local)); 458 for (;;) { 459 uint32_t i = atomic_fetch_add(&p->next, 1u); 460 if (i >= p->nitems) break; 461 uint32_t item = p->reverse ? p->nitems - 1u - i : i; 462 p->fn(p->ctx, item, &local); 463 } 464 pthread_mutex_lock(&p->mu); 465 stats_merge(&p->total, &local); 466 pthread_mutex_unlock(&p->mu); 467 return NULL; 468 } 469 470 static stats_t pool_run(void (*fn)(void *, uint32_t, stats_t *), void *ctx, uint32_t nitems, int threads, int reverse) { 471 pool_t p; 472 memset(&p, 0, sizeof(p)); 473 p.fn = fn; p.ctx = ctx; p.nitems = nitems; p.reverse = reverse; 474 atomic_init(&p.next, 0u); 475 pthread_mutex_init(&p.mu, NULL); 476 pthread_t th[64]; 477 if (threads > 64) threads = 64; 478 for (int t = 0; t < threads; t++) pthread_create(&th[t], NULL, pool_worker, &p); 479 for (int t = 0; t < threads; t++) pthread_join(th[t], NULL); 480 pthread_mutex_destroy(&p.mu); 481 return p.total; 482 } 483 484 /* ---- tables ----------------------------------------------------------------- */ 485 486 static void build_log2_item(void *ctx, uint32_t item, stats_t *st) { 487 (void)ctx; (void)st; 488 for (uint32_t j = item << 15; j < ((item + 1u) << 15); j++) { 489 Q m = 1.0L + (Q)j * q_pow2(-23); 490 g_log2_tab[j] = q_ln_mant(m) * QINVLN2; 491 } 492 } 493 static void build_rsqrt_item(void *ctx, uint32_t item, stats_t *st) { 494 (void)ctx; (void)st; 495 for (uint32_t j = item << 16; j < ((item + 1u) << 16); j++) { 496 Q v = (1.0L + (Q)(j & 0x7fffffu) * q_pow2(-23)) * ((j >> 23) ? 2.0L : 1.0L); 497 g_rsqrt_tab[j] = q_round(q_rsqrt(v)); 498 } 499 } 500 static void build_tables(void) { 501 if (g_log2_tab) return; 502 g_log2_tab = malloc(sizeof(Q) << 23); 503 g_rsqrt_tab = malloc(sizeof(uint32_t) << 24); 504 if (!g_log2_tab || !g_rsqrt_tab) { printf("FAIL: oracle table allocation\n"); exit(2); } 505 pool_run(build_log2_item, NULL, 1u << 8, g_threads, 0); 506 pool_run(build_rsqrt_item, NULL, 1u << 8, g_threads, 0); 507 } 508 509 /* ---- oracle self-checks ------------------------------------------------------ */ 510 511 static void check_rel(const char *what, Q got, Q want, int bits) { 512 Q e = qabs(got - want); 513 if (!(e <= qabs(want) * q_pow2(-bits))) FAIL("oracle self-check %s", what); 514 } 515 516 static void oracle_selfcheck(void) { 517 check_rel("exp(ln2) = 2", q_exp(QLN2), 2.0L, 100); 518 check_rel("exp(1) exp(-1) = 1", q_exp(1.0L) * q_exp(-1.0L), 1.0L, 100); 519 check_rel("exp(-100) e^100", q_exp(-100.0L) * q_exp(100.0L), 1.0L, 98); 520 check_rel("ln(1.5) inverts exp", q_exp(q_ln_mant(1.5L)), 1.5L, 100); 521 check_rel("ln(1.9999) inverts exp", q_exp(q_ln_mant(1.9999L)), 1.9999L, 100); 522 check_rel("log2 table entry 2^22 is log2(1.5)", g_log2_tab[1u << 22] * QLN2, q_ln_mant(1.5L), 100); 523 check_rel("expm1 small", q_expm1(1e-20L), 1e-20L + 0.5e-40L, 100); 524 check_rel("erfc CF depth", q_erfc_cf(4.5L, ERFC_CF_DEPTH), q_erfc_cf(4.5L, 2 * ERFC_CF_DEPTH), 105); 525 check_rel("erfc CF vs 1-erf at 4.5", q_erfc_cf(4.5L, ERFC_CF_DEPTH), 1.0L - q_erf(4.5L), 70); 526 check_rel("erf small/Kummer seam at 0.5", q_erf(0.4999999999999999999999L), q_erf(0.5L), 60); 527 check_rel("sin^2 + cos^2", q_sin_r(0.7L) * q_sin_r(0.7L) + q_cos_r(0.7L) * q_cos_r(0.7L), 1.0L, 108); 528 check_rel("sin(pi/6) = 1/2", q_sincos(QPA / 3.0L + QPB / 3.0L, 0), 0.5L, 100); 529 check_rel("cos(2^20 pi/2 + 1) reduction", q_sincos(`1048576`.0L * QPA + `1048576`.0L * QPB + 1.0L, 1), q_cos_r(1.0L), 70); 530 check_rel("rsqrt(2)^2 * 2", q_rsqrt(2.0L) * q_rsqrt(2.0L) * 2.0L, 1.0L, 110); 531 check_rel("erf(1) known value", q_erf(1.0L), 0.84270079294971486934122063508260925929606699796630L, 108); 532 check_rel("erfc(5) known value", q_erfc(5.0L), 1.`5374597944280348501883434e`-12L, 80); 533 check_rel("gelu constant 1/sqrt(2pi)", QINV_SQRT2PI * QINV_SQRT2PI * 2.0L / QINV_SQRTPI / QINV_SQRTPI, 1.0L, 108); 534 printf("oracle self-checks: %s\n", g_fail ? "FAIL" : "PASS"); 535 } 536 537 /* ---- special values (the contract table) ------------------------------------ */ 538 539 static void sv(const char *op, float (*fn)(float), uint32_t in, uint32_t want) { 540 uint32_t got = bits_of(fn(float_of(in))); 541 if (got != want) FAIL("special %s(0x%08x) = 0x%08x, contract says 0x%08x", op, in, got, want); 542 } 543 544 static void special_values(void) { 545 const uint32_t PZ = 0, NZ = 0x80000000u, ONE = 0x3f800000u, MONE = 0xbf800000u, SNAN = 0x7f800001u, NNAN = 0xffc00001u; 546 sv("sigmoid", omega_math_sigmoid, SNAN, QNANB); sv("sigmoid", omega_math_sigmoid, NNAN, QNANB); 547 sv("sigmoid", omega_math_sigmoid, PINFB, ONE); sv("sigmoid", omega_math_sigmoid, NINFB, PZ); 548 sv("sigmoid", omega_math_sigmoid, PZ, 0x3f000000u); sv("sigmoid", omega_math_sigmoid, NZ, 0x3f000000u); 549 sv("tanh", omega_math_tanh, SNAN, QNANB); sv("tanh", omega_math_tanh, PINFB, ONE); sv("tanh", omega_math_tanh, NINFB, MONE); 550 sv("tanh", omega_math_tanh, PZ, PZ); sv("tanh", omega_math_tanh, NZ, NZ); sv("tanh", omega_math_tanh, 1u, 1u); 551 sv("rsqrt", omega_math_rsqrt, PZ, PINFB); sv("rsqrt", omega_math_rsqrt, NZ, NINFB); sv("rsqrt", omega_math_rsqrt, PINFB, PZ); 552 sv("rsqrt", omega_math_rsqrt, NINFB, QNANB); sv("rsqrt", omega_math_rsqrt, MONE, QNANB); sv("rsqrt", omega_math_rsqrt, SNAN, QNANB); 553 sv("rsqrt", omega_math_rsqrt, 0x40800000u, 0x3f000000u); sv("rsqrt", omega_math_rsqrt, ONE, ONE); 554 sv("rsqrt", omega_math_rsqrt, 0x00000001u, 0x64b504f3u); /* 2^74.5, correctly rounded */ 555 sv("exp2", omega_math_exp2, SNAN, QNANB); sv("exp2", omega_math_exp2, PINFB, PINFB); sv("exp2", omega_math_exp2, NINFB, PZ); 556 sv("exp2", omega_math_exp2, PZ, ONE); sv("exp2", omega_math_exp2, NZ, ONE); sv("exp2", omega_math_exp2, 0x43000000u, PINFB); 557 sv("exp2", omega_math_exp2, 0xc3160000u, PZ); /* 2^-150: tie, rounds to +0 */ 558 sv("exp2", omega_math_exp2, 0xc3150000u, 1u); /* 2^-149 */ 559 sv("exp2", omega_math_exp2, 0x42fe0000u, 0x7f000000u); /* 2^127 */ 560 sv("exp2", omega_math_exp2, 0x41200000u, 0x44800000u); /* 2^10 */ 561 sv("log2", omega_math_log2, PZ, NINFB); sv("log2", omega_math_log2, NZ, NINFB); sv("log2", omega_math_log2, MONE, QNANB); 562 sv("log2", omega_math_log2, NINFB, QNANB); sv("log2", omega_math_log2, PINFB, PINFB); sv("log2", omega_math_log2, ONE, PZ); 563 sv("log2", omega_math_log2, SNAN, QNANB); 564 sv("log2", omega_math_log2, 1u, 0xc3150000u); /* -149 */ 565 sv("log2", omega_math_log2, 0x7f000000u, 0x42fe0000u); /* 127 */ 566 sv("log2", omega_math_log2, 0x3f000000u, MONE); 567 sv("erf", omega_math_erf, SNAN, QNANB); sv("erf", omega_math_erf, PINFB, ONE); sv("erf", omega_math_erf, NINFB, MONE); 568 sv("erf", omega_math_erf, PZ, PZ); sv("erf", omega_math_erf, NZ, NZ); 569 sv("sin", omega_math_sin, PZ, PZ); sv("sin", omega_math_sin, NZ, NZ); sv("sin", omega_math_sin, PINFB, QNANB); 570 sv("sin", omega_math_sin, NINFB, QNANB); sv("sin", omega_math_sin, SNAN, QNANB); 571 sv("sin", omega_math_sin, 0x4a800001u, QNANB); /* 2^22 + 0.5: outside the domain */ 572 sv("cos", omega_math_cos, PZ, ONE); sv("cos", omega_math_cos, NZ, ONE); sv("cos", omega_math_cos, PINFB, QNANB); 573 sv("cos", omega_math_cos, NINFB, QNANB); sv("cos", omega_math_cos, 0xca800001u, QNANB); 574 sv("gelu", omega_math_gelu, SNAN, QNANB); sv("gelu", omega_math_gelu, PINFB, PINFB); sv("gelu", omega_math_gelu, NINFB, NZ); 575 sv("gelu", omega_math_gelu, PZ, PZ); sv("gelu", omega_math_gelu, NZ, NZ); 576 /* tiny subnormal ties (Codex): x/2 + x^2/sqrt(2pi) breaks toward +inf */ 577 sv("gelu", omega_math_gelu, 0x00000001u, 0x00000001u); sv("gelu", omega_math_gelu, 0x80000001u, NZ); 578 sv("gelu", omega_math_gelu, 0x00000003u, 0x00000002u); sv("gelu", omega_math_gelu, 0x80000003u, 0x80000001u); 579 sv("gelu", omega_math_gelu, 0x00800001u, 0x00400001u); sv("gelu", omega_math_gelu, 0x80800001u, 0x80400000u); 580 sv("gelu", omega_math_gelu, 0x00000002u, 0x00000001u); sv("gelu", omega_math_gelu, 0x80000002u, 0x80000001u); 581 printf("special values: %s\n", g_fail ? "FAIL" : "PASS"); 582 } 583 584 /* ---- fast mode: sampled inputs ------------------------------------------------- */ 585 586 static uint32_t *g_sample; 587 static uint32_t g_nsample; 588 589 static void build_sample(void) { 590 static const float th[] = { 591 0.0f, 1.0f, 0.5f, 0.5625f, 0.75f, 1.25f, 1.5f, 1.75f, 2.0f, 2.5f, 3.0f, 3.5f, 4.0f, 5.0f, 6.0f, 7.0f, 592 8.0f, 9.0f, 9.5f, 10.0f, 11.0f, 12.0f, 15.5f, 16.0f, 17.5f, 18.0f, 20.0f, 40.0f, 87.0f, 88.7f, 100.0f, 593 103.9f, 104.0f, 110.0f, 126.0f, 127.0f, 128.0f, 149.0f, 150.0f, 151.0f, 152.0f, 594 0.`78539816f`, 1.`5707963f`, 3.`1415927f`, 4.`712389f`, 6.`2831855f`, 1.`41421356f`, 0.`70710678f`, 595 `4194304`.0f, `1048576`.0f, 0x1p-12f, 0x1p-24f, 0x1p-40f, 0x1p-126f, 0x1p-149f, 0x1.fffffep127f, 596 }; 597 uint32_t nth = sizeof(th) / sizeof(th[0]); 598 uint32_t nhash = 1u << 20; 599 g_nsample = nhash + nth * 2u * 129u + 8u; 600 g_sample = malloc(sizeof(uint32_t) * g_nsample); 601 uint32_t n = 0; 602 for (uint32_t i = 0; i < nhash; i++) g_sample[n++] = i * 0x9E3779B1u + 0x7f4a7c15u; 603 for (uint32_t i = 0; i < nth; i++) { 604 for (uint32_t s = 0; s < 2; s++) { 605 uint32_t b = bits_of(th[i]) | (s << 31); 606 for (int d = -64; d <= 64; d++) g_sample[n++] = (uint32_t)((int64_t)b + d); 607 } 608 } 609 static const uint32_t extra[] = { 0x7f800000u, 0xff800000u, 0x7fc00000u, 0xffc00000u, 0x7f800001u, 0x00000000u, 0x80000000u, 0x00000001u }; 610 for (uint32_t i = 0; i < 8; i++) g_sample[n++] = extra[i]; 611 g_nsample = n; 612 } 613 614 typedef struct { const op_t *op; int use_oracle; uint8_t (*dig)[32]; } job_t; 615 616 #define SAMPLE_BLOCK 4096u 617 static void sample_item(void *ctx, uint32_t item, stats_t *st) { 618 job_t *j = ctx; 619 uint32_t lo = item * SAMPLE_BLOCK, hi = lo + SAMPLE_BLOCK; 620 if (hi > g_nsample) hi = g_nsample; 621 uint8_t buf[SAMPLE_BLOCK * 4]; 622 uint32_t nb = 0; 623 for (uint32_t i = lo; i < hi; i++) { 624 uint32_t in = g_sample[i]; 625 uint32_t s = bits_of(j->op->seq(float_of(in))); 626 memcpy(buf + 4 * nb++, &s, 4); 627 if (j->use_oracle) stats_add(st, in, s, j->op->oracle(in), j->op->bound); 628 } 629 sha256_hash(buf, nb * 4u, j->dig[item]); 630 } 631 632 static void digest_of(uint8_t (*dig)[32], uint32_t n, uint8_t out[32]) { 633 sha256_ctx c; 634 sha256_init(&c); 635 for (uint32_t i = 0; i < n; i++) sha256_update(&c, dig[i], 32); 636 sha256_final(&c, out); 637 } 638 639 static void hex32(const uint8_t d[32], char out[65]) { 640 for (int i = 0; i < 32; i++) sprintf(out + 2 * i, "%02x", d[i]); 641 } 642 643 static void run_fast(const op_t *op) { 644 uint32_t nitems = (g_nsample + SAMPLE_BLOCK - 1u) / SAMPLE_BLOCK; 645 uint8_t (*d1)[32] = calloc(nitems, 32), (*d2)[32] = calloc(nitems, 32); 646 job_t j1 = { op, 1, d1 }, j2 = { op, 0, d2 }; 647 stats_t st = pool_run(sample_item, &j1, nitems, g_threads, 0); 648 stats_print(op, &st, "sampled"); 649 pool_run(sample_item, &j2, nitems, g_threads > 3 ? 3 : 1, 1); 650 uint8_t a[32], b[32]; 651 digest_of(d1, nitems, a); 652 digest_of(d2, nitems, b); 653 char h[65]; 654 hex32(a, h); 655 if (memcmp(a, b, 32)) FAIL("%s: sampled outputs differ between two runs", op->name); 656 else printf("%-8s determinism: two runs bit-identical, sample digest %.16s\n", op->name, h); 657 free(d1); free(d2); 658 } 659 660 /* ---- full mode: all 2^32 inputs ------------------------------------------------ */ 661 662 #define CHUNK_BITS 24u 663 #define NCHUNK (1u << (32u - CHUNK_BITS)) 664 #define BLOCK_BITS 14u /* work item = 2^14 inputs; 2^10 items per chunk */ 665 666 typedef struct { const op_t *op; int use_oracle; uint8_t (*blk)[32]; } fjob_t; 667 668 static void full_item(void *ctx, uint32_t item, stats_t *st) { 669 fjob_t *j = ctx; 670 uint32_t base = item << BLOCK_BITS; 671 static __thread uint8_t buf[4u << BLOCK_BITS]; 672 for (uint32_t i = 0; i < (1u << BLOCK_BITS); i++) { 673 uint32_t in = base + i; 674 uint32_t s = bits_of(j->op->seq(float_of(in))); 675 memcpy(buf + 4 * i, &s, 4); 676 if (j->use_oracle) stats_add(st, in, s, j->op->oracle(in), j->op->bound); 677 } 678 sha256_hash(buf, sizeof(buf), j->blk[item]); 679 } 680 681 /* chunk digest = sha256 of its 2^10 block digests; final = sha256 of 256 chunk digests */ 682 static void full_digest(uint8_t (*blk)[32], uint8_t out[32]) { 683 uint32_t per = 1u << (CHUNK_BITS - BLOCK_BITS); 684 uint8_t (*ch)[32] = calloc(NCHUNK, 32); 685 for (uint32_t c = 0; c < NCHUNK; c++) digest_of(blk + c * per, per, ch[c]); 686 digest_of(ch, NCHUNK, out); 687 free(ch); 688 } 689 690 static void run_full(const op_t *op, int use_oracle) { 691 uint32_t nitems = 1u << (32u - BLOCK_BITS); 692 uint8_t (*b1)[32] = calloc(nitems, 32); 693 fjob_t j1 = { op, use_oracle, b1 }; 694 stats_t st = pool_run(full_item, &j1, nitems, g_threads, 0); 695 uint8_t a[32]; 696 full_digest(b1, a); 697 char h[65]; 698 hex32(a, h); 699 if (use_oracle) { 700 stats_print(op, &st, "exhaustive"); 701 /* determinism: second pass, other thread count, reversed order */ 702 uint8_t (*b2)[32] = calloc(nitems, 32); 703 fjob_t j2 = { op, 0, b2 }; 704 pool_run(full_item, &j2, nitems, g_threads > 3 ? g_threads - 3 : 1, 1); 705 uint8_t b[32]; 706 full_digest(b2, b); 707 if (memcmp(a, b, 32)) FAIL("%s: full-domain outputs differ between two runs", op->name); 708 else printf("%-8s determinism: two full passes bit-identical\n", op->name); 709 free(b2); 710 } 711 printf("%-8s full-domain digest %s\n", op->name, h); 712 if (op->frozen[0]) { 713 if (strcmp(h, op->frozen)) FAIL("%s: full-domain digest differs from the frozen digest", op->name); 714 else printf("%-8s matches frozen digest\n", op->name); 715 } else if (!use_oracle) { 716 FAIL("%s: no frozen digest recorded", op->name); 717 } 718 free(b1); 719 } 720 721 /* ---- main ------------------------------------------------------------------------ */ 722 723 int main(int argc, char **argv) { 724 setvbuf(stdout, NULL, _IOLBF, 0); 725 const char *mode = argc > 1 ? argv[1] : "fast"; 726 const char *only = argc > 2 ? argv[2] : NULL; 727 const char *t = getenv("OMEGA_TRANSC_THREADS"); 728 if (t) g_threads = atoi(t); 729 if (g_threads < 1) g_threads = 1; 730 g_verbose = getenv("OMEGA_TRANSC_VERBOSE") != NULL; 731 732 uint64_t fpcr; 733 __asm__ volatile("mrs %0, fpcr" : "=r"(fpcr)); 734 if (fpcr & OMEGA_NUMERIC_FPCR_REQUIRED_CLEAR) { 735 printf("FAIL: FPCR 0x%llx has required-clear bits set (need RNE, no FZ/DN/AH)\n", (unsigned long long)fpcr); 736 return 1; 737 } 738 printf("E1 WP-B transcendental CPU tier: mode %s, %d threads, FPCR 0x%llx\n", mode, g_threads, (unsigned long long)fpcr); 739 740 int is_full = !strcmp(mode, "full"), is_digest = !strcmp(mode, "digest"); 741 if (!is_full && !is_digest && strcmp(mode, "fast")) { printf("usage: %s [fast|full|digest] [OP]\n", argv[0]); return 2; } 742 743 if (!is_digest) { 744 build_tables(); 745 oracle_selfcheck(); 746 special_values(); 747 } 748 if (!is_full && !is_digest) build_sample(); 749 int ran = 0; 750 for (int i = 0; i < NOPS; i++) { 751 if (only && strcmp(only, g_ops[i].name)) continue; 752 ran++; 753 if (is_full) run_full(&g_ops[i], 1); 754 else if (is_digest) run_full(&g_ops[i], 0); 755 else run_fast(&g_ops[i]); 756 } 757 if (!ran) { printf("FAIL: unknown op %s\n", only); return 2; } 758 printf("E1 WP-B verdict: %s\n", g_fail ? "FAIL" : "PASS"); 759 return g_fail ? 1 : 0; 760 }