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 / tests / numeric_oracle.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 | /* |
| 18 | * numeric_oracle.h -- independent answers for the Gate 5 CPU tier. |
| 19 | * |
| 20 | * Test-only. Included once, by tests/test_omega_numeric.c. Nothing here calls |
| 21 | * src/omega_numeric.c: the point is a second derivation that does not share |
| 22 | * code, instructions or rounding helpers with the reference or the CPU |
| 23 | * realization, so a bug in either one shows up as a mismatch. |
| 24 | * |
| 25 | * FADD FSUB FMUL FFMA I2FP DIV RCP RSQ |
| 26 | * Integer soft-float. Operands are unpacked to (sign, integer |
| 27 | * significand, power of two). The exact result is formed in 128-bit |
| 28 | * integers (a sum of widely separated operands keeps a sticky bit), |
| 29 | * then rounded once, to nearest even, with gradual underflow |
| 30 | * (subnormals kept, no flush to zero). No floating-point instruction |
| 31 | * is used. NaN results are compared as a class (any NaN matches). |
| 32 | * |
| 33 | * EXP LOG |
| 34 | * A high-precision value computed in IEEE binary128 (long double on |
| 35 | * AArch64 Linux: 113-bit significand, software arithmetic in the |
| 36 | * compiler runtime, no libm), from a Taylor series (exp) and the |
| 37 | * atanh series (log), then rounded once to FP32. The Omega sequences |
| 38 | * are polynomial approximations, not correctly rounded, so they are |
| 39 | * checked against an ulp bound measured on the corpus and stated at |
| 40 | * the check (NUM_ORACLE_EXP_ULP / NUM_ORACLE_LOG_ULP), never as |
| 41 | * bit-exact. |
| 42 | */ |
| 43 | #ifndef NUMERIC_ORACLE_H |
| 44 | #define NUMERIC_ORACLE_H |
| 45 | |
| 46 | #include <stdint.h> |
| 47 | #include <stdbool.h> |
| 48 | |
| 49 | typedef unsigned __int128 or_u128; |
| 50 | |
| 51 | #define OR_QNAN 0x7fc00000u |
| 52 | #define OR_INF 0x7f800000u |
| 53 | #define OR_SIGN 0x80000000u |
| 54 | |
| 55 | enum { OR_ZERO, OR_FINITE, OR_INFINITE, OR_NAN }; |
| 56 | |
| 57 | /* Unpack FP32 bits: finite value = m * 2^e (m has at most 24 bits). */ |
| 58 | static int or_decode(uint32_t u, uint32_t *sign, uint64_t *m, int *e) { |
| 59 | uint32_t ef = (u >> 23) & 0xffu, f = u & 0x007fffffu; |
| 60 | *sign = u & OR_SIGN; |
| 61 | if (ef == 0xffu) return f ? OR_NAN : OR_INFINITE; |
| 62 | if (ef == 0) { |
| 63 | if (!f) return OR_ZERO; |
| 64 | *m = f; *e = -149; |
| 65 | return OR_FINITE; |
| 66 | } |
| 67 | *m = f | 0x00800000u; *e = (int)ef - 150; |
| 68 | return OR_FINITE; |
| 69 | } |
| 70 | |
| 71 | static int or_msb(or_u128 m) { |
| 72 | uint64_t hi = (uint64_t)(m >> 64); |
| 73 | return hi ? 127 - __builtin_clzll(hi) : 63 - __builtin_clzll((uint64_t)m); |
| 74 | } |
| 75 | |
| 76 | /* Round m * 2^e (m > 0, below 2^126) to FP32, nearest even, subnormals kept. |
| 77 | * Callers that dropped low bits OR a 1 into bit 0 (sticky) and keep at least |
| 78 | * two bits below the rounding point, so the jammed value rounds like the |
| 79 | * exact one. */ |
| 80 | static uint32_t or_pack(uint32_t sign, or_u128 m, int e) { |
| 81 | int p = or_msb(m); |
| 82 | int E = p + e; |
| 83 | if (E > 127) return sign | OR_INF; |
| 84 | int shift = (E >= -126) ? p - 23 : -149 - e; |
| 85 | uint64_t q; |
| 86 | if (shift <= 0) { |
| 87 | q = (uint64_t)(m << -shift); |
| 88 | } else if (shift > p + 1) { |
| 89 | q = 0; /* below half of the smallest subnormal */ |
| 90 | } else { |
| 91 | q = (uint64_t)(m >> shift); |
| 92 | or_u128 rem = m & ((((or_u128)1) << shift) - 1); |
| 93 | or_u128 half = ((or_u128)1) << (shift - 1); |
| 94 | if (rem > half || (rem == half && (q & 1u))) q++; |
| 95 | } |
| 96 | uint32_t bits = (E >= -126) ? ((uint32_t)(E + 127) << 23) + (uint32_t)(q - 0x00800000u) |
| 97 | : (uint32_t)q; |
| 98 | if ((bits & 0x7fffffffu) >= OR_INF) return sign | OR_INF; |
| 99 | return sign | bits; |
| 100 | } |
| 101 | |
| 102 | /* Exact signed sum of sa*ma*2^ea and sb*mb*2^eb (each m below 2^49), rounded |
| 103 | * once. An exactly zero sum is +0 (round to nearest). */ |
| 104 | static uint32_t or_sum(uint32_t sa, or_u128 ma, int ea, uint32_t sb, or_u128 mb, int eb) { |
| 105 | if (ea < eb) { |
| 106 | uint32_t ts = sa; sa = sb; sb = ts; |
| 107 | or_u128 tm = ma; ma = mb; mb = tm; |
| 108 | int te = ea; ea = eb; eb = te; |
| 109 | } |
| 110 | int d = ea - eb, e; |
| 111 | or_u128 A, B; |
| 112 | if (d <= 64) { |
| 113 | A = ma << d; B = mb; e = eb; |
| 114 | } else { |
| 115 | /* B is below 2^-15 of A: keep 64 guard bits on A, fold B's lost bits |
| 116 | * into a sticky bit. A is even, so the jammed result is odd and |
| 117 | * never sits on a rounding boundary. */ |
| 118 | int k = d - 64; |
| 119 | A = ma << 64; e = ea - 64; |
| 120 | B = (k >= 127) ? (mb != 0) : ((mb >> k) | ((mb & ((((or_u128)1) << k) - 1)) != 0)); |
| 121 | } |
| 122 | or_u128 m; uint32_t s; |
| 123 | if (sa == sb) { m = A + B; s = sa; } |
| 124 | else if (A >= B) { m = A - B; s = sa; } |
| 125 | else { m = B - A; s = sb; } |
| 126 | if (m == 0) return 0; |
| 127 | return or_pack(s, m, e); |
| 128 | } |
| 129 | |
| 130 | static uint32_t or_add(uint32_t ua, uint32_t ub) { |
| 131 | uint32_t sa, sb; uint64_t ma = 0, mb = 0; int ea = 0, eb = 0; |
| 132 | int ca = or_decode(ua, &sa, &ma, &ea), cb = or_decode(ub, &sb, &mb, &eb); |
| 133 | if (ca == OR_NAN || cb == OR_NAN) return OR_QNAN; |
| 134 | if (ca == OR_INFINITE && cb == OR_INFINITE) return sa == sb ? ua : OR_QNAN; |
| 135 | if (ca == OR_INFINITE) return ua; |
| 136 | if (cb == OR_INFINITE) return ub; |
| 137 | if (ca == OR_ZERO && cb == OR_ZERO) return sa & sb; |
| 138 | if (ca == OR_ZERO) return ub; |
| 139 | if (cb == OR_ZERO) return ua; |
| 140 | return or_sum(sa, ma, ea, sb, mb, eb); |
| 141 | } |
| 142 | |
| 143 | static uint32_t or_sub(uint32_t ua, uint32_t ub) { return or_add(ua, ub ^ OR_SIGN); } |
| 144 | |
| 145 | static uint32_t or_mul(uint32_t ua, uint32_t ub) { |
| 146 | uint32_t sa, sb; uint64_t ma = 0, mb = 0; int ea = 0, eb = 0; |
| 147 | int ca = or_decode(ua, &sa, &ma, &ea), cb = or_decode(ub, &sb, &mb, &eb); |
| 148 | uint32_t s = sa ^ sb; |
| 149 | if (ca == OR_NAN || cb == OR_NAN) return OR_QNAN; |
| 150 | if ((ca == OR_INFINITE && cb == OR_ZERO) || (ca == OR_ZERO && cb == OR_INFINITE)) return OR_QNAN; |
| 151 | if (ca == OR_INFINITE || cb == OR_INFINITE) return s | OR_INF; |
| 152 | if (ca == OR_ZERO || cb == OR_ZERO) return s; |
| 153 | return or_pack(s, (or_u128)ma * mb, ea + eb); |
| 154 | } |
| 155 | |
| 156 | /* a * b + c with one rounding. */ |
| 157 | static uint32_t or_fma(uint32_t ua, uint32_t ub, uint32_t uc) { |
| 158 | uint32_t sa, sb, sc; uint64_t ma = 0, mb = 0, mc = 0; int ea = 0, eb = 0, ec = 0; |
| 159 | int ca = or_decode(ua, &sa, &ma, &ea), cb = or_decode(ub, &sb, &mb, &eb); |
| 160 | int cc = or_decode(uc, &sc, &mc, &ec); |
| 161 | uint32_t ps = sa ^ sb; |
| 162 | if (ca == OR_NAN || cb == OR_NAN || cc == OR_NAN) return OR_QNAN; |
| 163 | if ((ca == OR_INFINITE && cb == OR_ZERO) || (ca == OR_ZERO && cb == OR_INFINITE)) return OR_QNAN; |
| 164 | if (ca == OR_INFINITE || cb == OR_INFINITE) { |
| 165 | if (cc == OR_INFINITE && sc != ps) return OR_QNAN; |
| 166 | return ps | OR_INF; |
| 167 | } |
| 168 | if (cc == OR_INFINITE) return uc; |
| 169 | if (ca == OR_ZERO || cb == OR_ZERO) { |
| 170 | if (cc == OR_ZERO) return ps & sc; |
| 171 | return uc; |
| 172 | } |
| 173 | if (cc == OR_ZERO) return or_pack(ps, (or_u128)ma * mb, ea + eb); |
| 174 | return or_sum(ps, (or_u128)ma * mb, ea + eb, sc, mc, ec); |
| 175 | } |
| 176 | |
| 177 | static uint32_t or_i2f(int32_t v) { |
| 178 | if (v == 0) return 0; |
| 179 | uint32_t s = v < 0 ? OR_SIGN : 0; |
| 180 | uint64_t m = v < 0 ? (uint64_t)(-(int64_t)v) : (uint64_t)v; |
| 181 | return or_pack(s, m, 0); |
| 182 | } |
| 183 | |
| 184 | /* a / b, correctly rounded: 100-bit integer long division plus sticky. */ |
| 185 | static uint32_t or_div(uint32_t ua, uint32_t ub) { |
| 186 | uint32_t sa, sb; uint64_t ma = 0, mb = 0; int ea = 0, eb = 0; |
| 187 | int ca = or_decode(ua, &sa, &ma, &ea), cb = or_decode(ub, &sb, &mb, &eb); |
| 188 | uint32_t s = sa ^ sb; |
| 189 | if (ca == OR_NAN || cb == OR_NAN) return OR_QNAN; |
| 190 | if ((ca == OR_INFINITE && cb == OR_INFINITE) || (ca == OR_ZERO && cb == OR_ZERO)) return OR_QNAN; |
| 191 | if (ca == OR_INFINITE || cb == OR_ZERO) return s | OR_INF; |
| 192 | if (ca == OR_ZERO || cb == OR_INFINITE) return s; |
| 193 | or_u128 n = ((or_u128)ma) << 100; |
| 194 | or_u128 q = n / mb, r = n % mb; |
| 195 | return or_pack(s, (q << 1) | (r != 0), ea - eb - 101); |
| 196 | } |
| 197 | |
| 198 | /* sqrt, correctly rounded: integer square root of a >= 2^100 scaled value. */ |
| 199 | static uint32_t or_sqrt(uint32_t ua) { |
| 200 | uint32_t sa; uint64_t ma = 0; int ea = 0; |
| 201 | int ca = or_decode(ua, &sa, &ma, &ea); |
| 202 | if (ca == OR_NAN) return OR_QNAN; |
| 203 | if (ca == OR_ZERO) return ua; /* sqrt(-0) = -0 */ |
| 204 | if (sa) return OR_QNAN; |
| 205 | if (ca == OR_INFINITE) return ua; |
| 206 | int s = 100 - or_msb(ma); |
| 207 | if ((ea - s) & 1) s++; /* even exponent */ |
| 208 | or_u128 x = ((or_u128)ma) << s; |
| 209 | or_u128 root = 0, bit = ((or_u128)1) << 124; |
| 210 | while (bit > x) bit >>= 2; |
| 211 | while (bit) { |
| 212 | if (x >= root + bit) { x -= root + bit; root = (root >> 1) + bit; } |
| 213 | else root >>= 1; |
| 214 | bit >>= 2; |
| 215 | } |
| 216 | return or_pack(0, (root << 1) | (x != 0), (ea - s) / 2 - 1); |
| 217 | } |
| 218 | |
| 219 | static uint32_t or_rcp(uint32_t ua) { return or_div(0x3f800000u, ua); } |
| 220 | static uint32_t or_rsq(uint32_t ua) { return or_div(0x3f800000u, or_sqrt(ua)); } |
| 221 | |
| 222 | /* ---- binary128 references for EXP and LOG --------------------------------- */ |
| 223 | |
| 224 | static const long double OR_LN2 = 0.693147180559945309417232121458176568L; |
| 225 | |
| 226 | static long double or_pow2(int k) { |
| 227 | long double r = 1.0L; |
| 228 | long double f = k >= 0 ? 2.0L : 0.5L; |
| 229 | for (int n = k >= 0 ? k : -k; n; n--) r *= f; |
| 230 | return r; |
| 231 | } |
| 232 | |
| 233 | /* float bits from a binary128 value, rounded once (compiler runtime). */ |
| 234 | static uint32_t or_ld_to_bits(long double v) { |
| 235 | float f = (float)v; |
| 236 | uint32_t u; |
| 237 | __builtin_memcpy(&u, &f, sizeof(u)); |
| 238 | return u; |
| 239 | } |
| 240 | |
| 241 | static uint32_t or_exp(uint32_t ux) { |
| 242 | uint32_t sx; uint64_t mx = 0; int ex = 0; |
| 243 | int cx = or_decode(ux, &sx, &mx, &ex); |
| 244 | if (cx == OR_NAN) return OR_QNAN; |
| 245 | if (cx == OR_INFINITE) return sx ? 0 : OR_INF; |
| 246 | if (cx == OR_ZERO) return 0x3f800000u; |
| 247 | long double x = (long double)mx * or_pow2(ex); |
| 248 | if (sx) x = -x; |
| 249 | if (x > 89.0L) return OR_INF; |
| 250 | if (x < -110.0L) return 0; |
| 251 | long double kq = x / OR_LN2; |
| 252 | int k = (int)(kq >= 0 ? kq + 0.5L : kq - 0.5L); |
| 253 | long double r = x - (long double)k * OR_LN2; /* |r| <= ~0.35 */ |
| 254 | long double term = 1.0L, sum = 1.0L; |
| 255 | for (int n = 1; n < 40; n++) { term *= r / (long double)n; sum += term; } |
| 256 | return or_ld_to_bits(sum * or_pow2(k)); |
| 257 | } |
| 258 | |
| 259 | static uint32_t or_log(uint32_t ux) { |
| 260 | uint32_t sx; uint64_t mx = 0; int ex = 0; |
| 261 | int cx = or_decode(ux, &sx, &mx, &ex); |
| 262 | if (cx == OR_NAN) return OR_QNAN; |
| 263 | if (cx == OR_ZERO) return OR_SIGN | OR_INF; |
| 264 | if (sx) return OR_QNAN; |
| 265 | if (cx == OR_INFINITE) return OR_INF; |
| 266 | /* x = mx * 2^ex; write it as m * 2^k with m in [sqrt(1/2), sqrt(2)) */ |
| 267 | int k = ex + or_msb(mx); |
| 268 | long double m = (long double)mx * or_pow2(-or_msb(mx)); /* [1, 2) */ |
| 269 | if (m > 1.41421356237309504880168872420969808L) { m *= 0.5L; k++; } |
| 270 | long double z = (m - 1.0L) / (m + 1.0L), z2 = z * z; /* |z| < 0.172 */ |
| 271 | long double term = z, sum = 0.0L; |
| 272 | for (int n = 1; n < 80; n += 2) { sum += term / (long double)n; term *= z2; } |
| 273 | return or_ld_to_bits((long double)k * OR_LN2 + 2.0L * sum); |
| 274 | } |
| 275 | |
| 276 | /* Distance in units in the last place between two FP32 values, counting |
| 277 | * representable numbers between them (+0 and -0 are one point). */ |
| 278 | static uint32_t or_ulp_distance(uint32_t a, uint32_t b) { |
| 279 | int64_t oa = (a & OR_SIGN) ? -(int64_t)(a & 0x7fffffffu) : (int64_t)a; |
| 280 | int64_t ob = (b & OR_SIGN) ? -(int64_t)(b & 0x7fffffffu) : (int64_t)b; |
| 281 | int64_t d = oa - ob; |
| 282 | if (d < 0) d = -d; |
| 283 | return d > 0xffffffffLL ? 0xffffffffu : (uint32_t)d; |
| 284 | } |
| 285 | |
| 286 | static bool or_is_nan(uint32_t u) { return (u & 0x7fffffffu) > OR_INF; } |
| 287 | |
| 288 | /* NaN class match, else identical bits. */ |
| 289 | static bool or_same(uint32_t want, uint32_t got) { |
| 290 | if (or_is_nan(want) || or_is_nan(got)) return or_is_nan(want) && or_is_nan(got); |
| 291 | return want == got; |
| 292 | } |
| 293 | |
| 294 | /* ---- E1 scalar contract (docs/numeric/E1_SCALAR_CONTRACT.md) ------------------ |
| 295 | * Second derivations of the E1 ops, written differently from the reference |
| 296 | * in src/omega_numeric.c on purpose: |
| 297 | * integer conversions x scaled to a fixed point with 26 fraction bits in |
| 298 | * an int64; floor is the arithmetic right shift, ceil |
| 299 | * is -floor(-x), RNI adds one half and fixes the tie. |
| 300 | * U32 -> FP32 or_pack of the integer. |
| 301 | * FP32 -> F16 binary search over the 31744 finite F16 bit |
| 302 | * patterns, then an exact comparison with the |
| 303 | * midpoint (all values scaled to integers). |
| 304 | * FP32 -> BF16 the bias trick on the bit pattern: add 0x7fff plus |
| 305 | * the kept lsb, drop 16 bits. |
| 306 | * F16/BF16 -> FP32 or_pack of the narrow value's integer significand. |
| 307 | * compare predicates the total-order key (negative patterns inverted), |
| 308 | * -0 folded onto +0 first. |
| 309 | * FFMA_V or_fma above. |
| 310 | */ |
| 311 | enum { OR_RTZ, OR_FLOOR, OR_CEIL, OR_RNE }; |
| 312 | |
| 313 | /* FP32 bits -> integer in rounding direction mode, saturated to [lo, hi], |
| 314 | * NaN -> 0. */ |
| 315 | static int64_t or_to_int(uint32_t u, int mode, int64_t lo, int64_t hi) { |
| 316 | uint32_t s; uint64_t m = 0; int e = 0; |
| 317 | int cl = or_decode(u, &s, &m, &e); |
| 318 | if (cl == OR_NAN || cl == OR_ZERO) return 0; |
| 319 | if (cl == OR_INFINITE) return s ? lo : hi; |
| 320 | int lead = 63 - __builtin_clzll(m) + e; /* |x| in [2^lead, 2^(lead+1)) */ |
| 321 | if (lead >= 32) return s ? lo : hi; /* beyond every target range */ |
| 322 | if (lead < -2) { /* 0 < |x| < 1/4: no tie possible */ |
| 323 | if (mode == OR_FLOOR) return s ? -1 : 0; |
| 324 | if (mode == OR_CEIL) return s ? 0 : 1; |
| 325 | return 0; |
| 326 | } |
| 327 | /* lead >= -2 gives e >= -25: X = x * 2^26 is an exact integer below 2^58 */ |
| 328 | int64_t X = (int64_t)(m << (e + 26)); |
| 329 | if (s) X = -X; |
| 330 | const int64_t ONE = 1ll << 26, HALF = 1ll << 25; |
| 331 | int64_t q; |
| 332 | switch (mode) { |
| 333 | case OR_FLOOR: q = X >> 26; break; /* arithmetic shift = floor */ |
| 334 | case OR_CEIL: q = -((-X) >> 26); break; |
| 335 | case OR_RNE: { |
| 336 | int64_t t = X + HALF; /* floor(x + 1/2) */ |
| 337 | q = t >> 26; |
| 338 | if ((t & (ONE - 1)) == 0 && (q & 1)) q -= 1; /* exact tie: to even */ |
| 339 | break; |
| 340 | } |
| 341 | default: q = s ? -((-X) >> 26) : (X >> 26); break; /* toward zero */ |
| 342 | } |
| 343 | return q < lo ? lo : q > hi ? hi : q; |
| 344 | } |
| 345 | |
| 346 | static uint32_t or_u2f(uint32_t v) { return v ? or_pack(0, v, 0) : 0; } |
| 347 | |
| 348 | /* Exact value of a positive finite F16 pattern h (0..0x7bff) as hm * 2^he. */ |
| 349 | static void or_f16_val(uint32_t h, uint64_t *hm, int *he) { |
| 350 | uint32_t ef = (h >> 10) & 0x1fu, f = h & 0x3ffu; |
| 351 | if (ef == 0) { *hm = f; *he = -24; } |
| 352 | else { *hm = f | 0x400u; *he = (int)ef - 25; } |
| 353 | } |
| 354 | |
| 355 | /* v * 2^e scaled by 2^60 (e >= -60), as an integer. */ |
| 356 | static or_u128 or_scaled(uint64_t v, int e) { return ((or_u128)v) << (e + 60); } |
| 357 | |
| 358 | static uint32_t or_f32_to_f16(uint32_t u) { |
| 359 | uint32_t s; uint64_t m = 0; int e = 0; |
| 360 | int cl = or_decode(u, &s, &m, &e); |
| 361 | uint32_t sign = s >> 16; |
| 362 | if (cl == OR_NAN) return sign | 0x7e00u; |
| 363 | if (cl == OR_INFINITE) return sign | 0x7c00u; |
| 364 | if (cl == OR_ZERO) return sign; |
| 365 | int lead = 63 - __builtin_clzll(m) + e; /* |x| in [2^lead, 2^(lead+1)) */ |
| 366 | if (lead >= 17) return sign | 0x7c00u; /* |x| >= 2^17: far past 65520 */ |
| 367 | if (lead < -26) return sign; /* |x| < 2^-26 < half of 2^-24 */ |
| 368 | /* now e >= -26 - 23 = -49, so |x| * 2^60 is an integer below 2^78 */ |
| 369 | or_u128 X = or_scaled(m, e); |
| 370 | /* largest finite pattern lo with value(lo) <= |x| (0 if |x| < 2^-24) */ |
| 371 | uint32_t lo = 0, hi = 0x7bffu; |
| 372 | while (lo < hi) { |
| 373 | uint32_t mid = (lo + hi + 1) / 2; |
| 374 | uint64_t hm; int he; or_f16_val(mid, &hm, &he); |
| 375 | if (or_scaled(hm, he) <= X) lo = mid; else hi = mid - 1; |
| 376 | } |
| 377 | uint64_t lm; int le; or_f16_val(lo, &lm, &le); |
| 378 | or_u128 L = or_scaled(lm, le), H; |
| 379 | if (lo == 0x7bffu) H = or_scaled(1, 16); /* the next step past 65504 is 2^16 (inf) */ |
| 380 | else { uint64_t hm; int he; or_f16_val(lo + 1, &hm, &he); H = or_scaled(hm, he); } |
| 381 | if (L == X) return sign | lo; |
| 382 | or_u128 twice = X * 2, sum = L + H; |
| 383 | uint32_t pick = (twice < sum) ? lo : (twice > sum) ? lo + 1 : ((lo & 1u) ? lo + 1 : lo); |
| 384 | return sign | pick; /* lo + 1 == 0x7c00 is +inf */ |
| 385 | } |
| 386 | |
| 387 | static uint32_t or_f32_to_bf16(uint32_t u) { |
| 388 | if (or_is_nan(u)) return ((u >> 16) & 0x8000u) | 0x7fc0u; |
| 389 | uint32_t lsb = (u >> 16) & 1u; |
| 390 | return (uint32_t)(((uint64_t)u + 0x7fffu + lsb) >> 16) & 0xffffu; |
| 391 | } |
| 392 | |
| 393 | static uint32_t or_f16_to_f32(uint32_t h) { |
| 394 | uint32_t sign = (h & 0x8000u) << 16, ef = (h >> 10) & 0x1fu; |
| 395 | if (ef == 0x1fu) return (h & 0x3ffu) ? OR_QNAN : (sign | OR_INF); |
| 396 | uint64_t hm; int he; or_f16_val(h & 0x7fffu, &hm, &he); |
| 397 | return hm ? or_pack(sign, hm, he) : sign; |
| 398 | } |
| 399 | |
| 400 | static uint32_t or_bf16_to_f32(uint32_t h) { |
| 401 | uint32_t sign = (h & 0x8000u) << 16, ef = (h >> 7) & 0xffu, f = h & 0x7fu; |
| 402 | if (ef == 0xffu) return f ? OR_QNAN : (sign | OR_INF); |
| 403 | if (ef == 0) return f ? or_pack(sign, f, -133) : sign; /* f * 2^-126 / 2^7 */ |
| 404 | return or_pack(sign, f | 0x80u, (int)ef - 134); |
| 405 | } |
| 406 | |
| 407 | /* Total-order key: larger key, larger value. -0 is folded onto +0. */ |
| 408 | static uint32_t or_key(uint32_t u) { |
| 409 | if (u == OR_SIGN) u = 0; |
| 410 | return (u & OR_SIGN) ? ~u : (u | OR_SIGN); |
| 411 | } |
| 412 | |
| 413 | /* Predicate by name: "LT" "LE" "GT" "GE" "EQ" "NE" "NUM" "NAN" and the U |
| 414 | * forms (true also when unordered). */ |
| 415 | static bool or_pred(const char *p, uint32_t a, uint32_t b) { |
| 416 | bool un = or_is_nan(a) || or_is_nan(b); |
| 417 | size_t n = 0; while (p[n]) n++; |
| 418 | if (p[0] == 'N' && p[1] == 'U' && p[2] == 'M') return !un; |
| 419 | if (p[0] == 'N' && p[1] == 'A' && p[2] == 'N') return un; |
| 420 | bool u_form = n == 3 && p[2] == 'U'; |
| 421 | if (un) return u_form; |
| 422 | uint32_t ka = or_key(a), kb = or_key(b); |
| 423 | if (p[0] == 'L' && p[1] == 'T') return ka < kb; |
| 424 | if (p[0] == 'L' && p[1] == 'E') return ka <= kb; |
| 425 | if (p[0] == 'G' && p[1] == 'T') return ka > kb; |
| 426 | if (p[0] == 'G' && p[1] == 'E') return ka >= kb; |
| 427 | if (p[0] == 'E' && p[1] == 'Q') return ka == kb; |
| 428 | if (p[0] == 'N' && p[1] == 'E') return ka != kb; |
| 429 | return false; |
| 430 | } |
| 431 | |
| 432 | #endif |