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 / omega_numeric_transc.c
| 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 | * E1 WP-B: frozen FP32 transcendental sequences, CPU tier (bounded contract). |
| 19 | * |
| 20 | * Every arithmetic step below is one explicit AArch64 binary32 instruction |
| 21 | * (FADD, FSUB, FMUL, FMADD, FDIV, FSQRT) issued through inline asm, so the |
| 22 | * compiler cannot fuse, reassociate, constant-fold or widen it. Sign flips, |
| 23 | * classification, exponent extraction and powers of two are integer bit |
| 24 | * operations. Comparisons are exact. Nothing here uses libm, long double or |
| 25 | * double. The written order of operations IS the specification: a GB10 (or |
| 26 | * any other) realization must issue the same operations in the same order |
| 27 | * (FMADD = single-rounding fused multiply-add, FDIV/FSQRT = correctly rounded) |
| 28 | * to be bit-identical. |
| 29 | * |
| 30 | * Coefficients are frozen FP32 values written as exact hex literals |
| 31 | * (mantissa integer times a power of two). Derivation (50+ digit arithmetic, |
| 32 | * each rounded once to FP32): Taylor coefficients of e^r, 2^f (ln2^n/n!), |
| 33 | * tanh (Bernoulli form), erf, sin, cos, the atanh series of log2, and |
| 34 | * Taylor coefficients of erfcx(y) = e^(y^2) erfc(y) about 17 centres via the |
| 35 | * exact recurrence (n+1) a_(n+1) = 2c a_n + 2 a_(n-1) - (2/sqrt(pi)) [n=0]. |
| 36 | * See docs/numeric/E1_TRANSCENDENTAL_CONTRACT.md. |
| 37 | * |
| 38 | * OMEGA_TRANSC_MUTATE=<id> (test builds only) perturbs one coefficient or |
| 39 | * step so the test can prove its bound check catches a wrong sequence. |
| 40 | */ |
| 41 | #include "omega_numeric_transc.h" |
| 42 | |
| 43 | #include <stdint.h> |
| 44 | #include <string.h> |
| 45 | |
| 46 | #if !defined(__aarch64__) |
| 47 | #error "omega_numeric_transc.c issues AArch64 binary32 instructions (fadd, fmul, fmadd, fdiv, fsqrt)" |
| 48 | #endif |
| 49 | |
| 50 | #ifndef OMEGA_TRANSC_MUTATE |
| 51 | #define OMEGA_TRANSC_MUTATE 0 |
| 52 | #endif |
| 53 | |
| 54 | /* ---- Instruction primitives ------------------------------------------------ */ |
| 55 | |
| 56 | static inline uint32_t tb(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } |
| 57 | static inline float tf(uint32_t u) { float f; memcpy(&f, &u, 4); return f; } |
| 58 | |
| 59 | static inline float fadd(float a, float b) { float r; __asm__("fadd %s0, %s1, %s2" : "=w"(r) : "w"(a), "w"(b)); return r; } |
| 60 | static inline float fsub(float a, float b) { float r; __asm__("fsub %s0, %s1, %s2" : "=w"(r) : "w"(a), "w"(b)); return r; } |
| 61 | static inline float fmul(float a, float b) { float r; __asm__("fmul %s0, %s1, %s2" : "=w"(r) : "w"(a), "w"(b)); return r; } |
| 62 | static inline float fdiv(float a, float b) { float r; __asm__("fdiv %s0, %s1, %s2" : "=w"(r) : "w"(a), "w"(b)); return r; } |
| 63 | static inline float fsqrt(float a) { float r; __asm__("fsqrt %s0, %s1" : "=w"(r) : "w"(a)); return r; } |
| 64 | /* a*b + c, one rounding (FMADD). */ |
| 65 | static inline float ffma(float a, float b, float c) { |
| 66 | float r; |
| 67 | __asm__("fmadd %s0, %s1, %s2, %s3" : "=w"(r) : "w"(a), "w"(b), "w"(c)); |
| 68 | return r; |
| 69 | } |
| 70 | static inline float fneg(float a) { return tf(tb(a) ^ 0x80000000u); } |
| 71 | static inline float fabs_(float a) { return tf(tb(a) & 0x7fffffffu); } |
| 72 | static inline int is_nan(float a) { return (tb(a) & 0x7fffffffu) > 0x7f800000u; } |
| 73 | /* 2^n as an FP32 normal, n in [-126, 127]. */ |
| 74 | static inline float pow2i(int32_t n) { return tf((uint32_t)(n + 127) << 23); } |
| 75 | |
| 76 | #define QNAN tf(0x7fc00000u) |
| 77 | #define PINF tf(0x7f800000u) |
| 78 | #define NINF tf(0xff800000u) |
| 79 | |
| 80 | /* Mutation hook: flips mantissa bit 12 of one coefficient (relative 2^-11). */ |
| 81 | static inline float mut(float v) { return tf(tb(v) ^ (1u << 12)); } |
| 82 | #define K(id, v) ((OMEGA_TRANSC_MUTATE == (id)) ? mut(v) : (v)) |
| 83 | enum { |
| 84 | MUT_EXP2 = 1, MUT_LOG2 = 2, MUT_EXPCORE = 3, MUT_TANH = 4, MUT_RSQRT = 5, |
| 85 | MUT_ERF = 6, MUT_ERFCX = 7, MUT_SIN = 8, MUT_COS = 9, MUT_GELU = 10, |
| 86 | }; |
| 87 | |
| 88 | /* 1.5 * 2^23: x + MAGIC rounds x to an integer (ties to even) for |x| < 2^22, |
| 89 | * and the low mantissa bits of the sum hold that integer. */ |
| 90 | #define MAGIC 0xC00000p0f |
| 91 | |
| 92 | /* m * 2^k with k in [-252, 254] and m normal (every caller passes a normal m); |
| 93 | * one rounding (the last multiply), exact when the result is normal. */ |
| 94 | static float scale2(float m, int32_t k) { |
| 95 | if (k > 127) return fmul(fmul(m, pow2i(127)), pow2i(k - 127)); |
| 96 | if (k < -126) return fmul(fmul(m, pow2i(k + 126)), pow2i(-126)); |
| 97 | return fmul(m, pow2i(k)); |
| 98 | } |
| 99 | |
| 100 | /* 2Sum: s + e == a + b exactly. */ |
| 101 | static inline void two_sum(float a, float b, float *s, float *e) { |
| 102 | float x = fadd(a, b); |
| 103 | float bv = fsub(x, a); |
| 104 | float av = fsub(x, bv); |
| 105 | *s = x; |
| 106 | *e = fadd(fsub(a, av), fsub(b, bv)); |
| 107 | } |
| 108 | |
| 109 | /* ---- Shared exp core ------------------------------------------------------- */ |
| 110 | |
| 111 | /* |
| 112 | * e^(ah + al) = m * 2^k, |al| tiny next to |ah|, |ah| <= 190. |
| 113 | * k = nearest(ah / ln2); r = ah - k ln2 (Cody-Waite: the first FMADD is exact: |
| 114 | * when k != 0, |ah| > 0.34, so ah and k LN2_HI (21 significant bits) are both |
| 115 | * multiples of g = min(ulp(ah), 2^-24) >= 2^-25, and |r| < 0.35 < 2^24 g), |
| 116 | * then r += al and |
| 117 | * m = 1 + r + r^2/2! + ... + r^8/8! (Horner, FMADD), |r| <= 0.35. |
| 118 | */ |
| 119 | static float exp_core2(float ah, float al, int32_t *k_out, float *ml) { |
| 120 | const float INVLN2 = 0xB8AA3Bp-23f; |
| 121 | const float LN2_HI = 0xB17218p-24f; |
| 122 | const float LN2_LO = -0x82E308p-52f; |
| 123 | float t = fadd(fmul(ah, INVLN2), MAGIC); |
| 124 | float kf = fsub(t, MAGIC); |
| 125 | int32_t k = (int32_t)(tb(t) - tb(MAGIC)); |
| 126 | float r = ffma(fneg(kf), LN2_HI, ah); |
| 127 | r = ffma(fneg(kf), LN2_LO, r); |
| 128 | r = fadd(r, al); |
| 129 | float p = 0xD00D01p-39f; /* 1/8! */ |
| 130 | p = ffma(p, r, 0xD00D01p-36f); /* 1/7! */ |
| 131 | p = ffma(p, r, 0xB60B61p-33f); /* 1/6! */ |
| 132 | p = ffma(p, r, 0x888889p-30f); /* 1/5! */ |
| 133 | p = ffma(p, r, 0xAAAAABp-28f); /* 1/4! */ |
| 134 | p = ffma(p, r, K(MUT_EXPCORE, 0xAAAAABp-26f)); /* 1/3! */ |
| 135 | p = ffma(p, r, 0.5f); |
| 136 | p = ffma(p, r, 1.0f); |
| 137 | *k_out = k; |
| 138 | float m = ffma(p, r, 1.0f); |
| 139 | *ml = ffma(p, r, fsub(1.0f, m)); /* rounding error of m: 1 - m exact (Sterbenz) */ |
| 140 | return m; |
| 141 | } |
| 142 | |
| 143 | static float exp_core(float ah, float al, int32_t *k_out) { |
| 144 | float ml; |
| 145 | return exp_core2(ah, al, k_out, &ml); |
| 146 | } |
| 147 | |
| 148 | /* ---- EXP2 ------------------------------------------------------------------ */ |
| 149 | |
| 150 | float omega_math_exp2(float x) { |
| 151 | if (is_nan(x)) return QNAN; |
| 152 | if (x >= 128.0f) return PINF; /* includes +inf */ |
| 153 | if (x < -151.0f) return 0.0f; /* includes -inf; 2^x < 2^-151 rounds to +0 */ |
| 154 | float t = fadd(x, MAGIC); |
| 155 | float kf = fsub(t, MAGIC); |
| 156 | int32_t k = (int32_t)(tb(t) - tb(MAGIC)); |
| 157 | float f = fsub(x, kf); /* exact, |f| <= 1/2 */ |
| 158 | float p = 0xB16011p-43f; /* ln2^8/8! */ |
| 159 | p = ffma(p, f, 0xFFE5FEp-40f); |
| 160 | p = ffma(p, f, 0xA18489p-36f); |
| 161 | p = ffma(p, f, 0xAEC3FFp-33f); |
| 162 | p = ffma(p, f, 0x9D955Bp-30f); |
| 163 | p = ffma(p, f, 0xE35847p-28f); |
| 164 | p = ffma(p, f, K(MUT_EXP2, 0xF5FDF0p-26f)); |
| 165 | p = ffma(p, f, 0xB17218p-24f); /* ln2 */ |
| 166 | float m = ffma(p, f, 1.0f); |
| 167 | return scale2(m, k); |
| 168 | } |
| 169 | |
| 170 | /* ---- LOG2 ------------------------------------------------------------------ */ |
| 171 | |
| 172 | float omega_math_log2(float x) { |
| 173 | uint32_t u = tb(x); |
| 174 | if (is_nan(x)) return QNAN; |
| 175 | if ((u & 0x7fffffffu) == 0) return NINF; /* +-0 */ |
| 176 | if (u & 0x80000000u) return QNAN; /* x < 0, incl. -inf */ |
| 177 | if (u == 0x7f800000u) return PINF; |
| 178 | int32_t eadj = 0; |
| 179 | if ((u & 0x7f800000u) == 0) { /* subnormal: exact scale by 2^23 */ |
| 180 | x = fmul(x, 0x800000p0f); |
| 181 | u = tb(x); |
| 182 | eadj = -23; |
| 183 | } |
| 184 | int32_t e = (int32_t)((u >> 23) & 0xffu) - 127 + eadj; |
| 185 | float m = tf((u & 0x007fffffu) | 0x3f800000u); /* [1, 2) */ |
| 186 | if (m > 0xB504F3p-23f) { /* > sqrt(2) rounded: m in (0.707, 1.414] */ |
| 187 | m = fmul(m, 0.5f); |
| 188 | e += 1; |
| 189 | } |
| 190 | /* z = (m - 1)/(m + 1) with its rounding residue: num exact (Sterbenz), |
| 191 | * den = m + 1 rounded with exact error dl (2Sum), zl ~ (num/(den+dl)) - z. */ |
| 192 | float num = fsub(m, 1.0f); |
| 193 | float den, dl; |
| 194 | two_sum(m, 1.0f, &den, &dl); |
| 195 | float z = fdiv(num, den); |
| 196 | float rz = ffma(fneg(z), den, num); /* exact remainder */ |
| 197 | rz = ffma(fneg(z), dl, rz); |
| 198 | float zl = fdiv(rz, den); |
| 199 | /* log2(m) = (2/ln2) (z + z^3/3 + z^5/5 + ... + z^11/11) */ |
| 200 | const float C_HI = 0xB8AA3Bp-22f; /* 2/ln2 */ |
| 201 | const float C_LO = 0xA57060p-48f; |
| 202 | float z2 = fmul(z, z); |
| 203 | float p = 0x864D42p-25f; /* 2/(11 ln2) */ |
| 204 | p = ffma(p, z2, 0xA4258Ap-25f); /* 2/(9 ln2) */ |
| 205 | p = ffma(p, z2, 0xD30BB1p-25f); /* 2/(7 ln2) */ |
| 206 | p = ffma(p, z2, 0x93BB63p-24f); /* 2/(5 ln2) */ |
| 207 | p = ffma(p, z2, K(MUT_LOG2, 0xF6384Fp-24f)); /* 2/(3 ln2) */ |
| 208 | float z3 = fmul(z, z2); |
| 209 | float ph = fmul(z, C_HI); |
| 210 | float pl = ffma(z, C_HI, fneg(ph)); /* exact */ |
| 211 | float lo = ffma(z3, p, ffma(z, C_LO, fmul(zl, C_HI))); |
| 212 | float s, se; |
| 213 | two_sum((float)e, ph, &s, &se); /* e is an exact small integer */ |
| 214 | return fadd(s, fadd(se, fadd(pl, lo))); |
| 215 | } |
| 216 | |
| 217 | /* ---- SIGMOID --------------------------------------------------------------- */ |
| 218 | |
| 219 | float omega_math_sigmoid(float x) { |
| 220 | if (is_nan(x)) return QNAN; |
| 221 | if (x >= 18.0f) return 1.0f; /* 1 - s(x) < 2^-25: rounds to 1 */ |
| 222 | if (x <= -104.0f) return 0.0f; /* s(x) < e^-104 < 2^-150: rounds to +0 */ |
| 223 | int32_t k; |
| 224 | float m = exp_core(fneg(fabs_(x)), 0.0f, &k); /* e^-|x| = m 2^k */ |
| 225 | float t = scale2(m, k); |
| 226 | float d = fadd(1.0f, t); |
| 227 | if (!(tb(x) & 0x80000000u)) return fdiv(1.0f, d); /* x >= +0 */ |
| 228 | return scale2(fdiv(m, d), k); /* x < 0: e^x/(1+e^x), one rounding into subnormal */ |
| 229 | } |
| 230 | |
| 231 | /* ---- TANH ------------------------------------------------------------------ */ |
| 232 | |
| 233 | float omega_math_tanh(float x) { |
| 234 | if (is_nan(x)) return QNAN; |
| 235 | uint32_t sign = tb(x) & 0x80000000u; |
| 236 | float ax = fabs_(x); |
| 237 | float r; |
| 238 | if (ax >= 9.5f) { |
| 239 | r = 1.0f; /* 1 - tanh < 2e^-19 < 2^-25 */ |
| 240 | } else if (ax < 0.5625f) { |
| 241 | /* odd Taylor series to x^21: x + x^3 (c2 + x^2 (c3 + ...)) */ |
| 242 | float x2 = fmul(ax, ax); |
| 243 | float p = 0xCB3F0Cp-37f; /* coefficient of x^21 */ |
| 244 | p = ffma(p, x2, -0xFABEBCp-36f); |
| 245 | p = ffma(p, x2, 0x9AAC12p-34f); |
| 246 | p = ffma(p, x2, -0xBED1B2p-33f); |
| 247 | p = ffma(p, x2, 0xEB69E8p-32f); |
| 248 | p = ffma(p, x2, -0x91371Bp-30f); |
| 249 | p = ffma(p, x2, 0xB327A4p-29f); |
| 250 | p = ffma(p, x2, -0xDD0DD1p-28f); |
| 251 | p = ffma(p, x2, K(MUT_TANH, 0x888889p-26f)); |
| 252 | p = ffma(p, x2, -0xAAAAABp-25f); /* -1/3 */ |
| 253 | r = ffma(fmul(ax, x2), p, ax); |
| 254 | } else { |
| 255 | /* 1 - 2/(e^(2|x|) + 1) */ |
| 256 | int32_t k; |
| 257 | float m = exp_core(fadd(ax, ax), 0.0f, &k); |
| 258 | float e2 = scale2(m, k); |
| 259 | r = fsub(1.0f, fdiv(2.0f, fadd(e2, 1.0f))); |
| 260 | } |
| 261 | return tf(tb(r) | sign); |
| 262 | } |
| 263 | |
| 264 | /* ---- RSQRT (correctly rounded) ----------------------------------------------- */ |
| 265 | |
| 266 | typedef unsigned __int128 u128; |
| 267 | |
| 268 | /* Positive normal FP32 bits w -> integer significand M (24 bits) and E with |
| 269 | * value = M * 2^E. */ |
| 270 | static inline void unpack_pos(uint32_t w, uint64_t *M, int32_t *E) { |
| 271 | *M = (uint64_t)((w & 0x7fffffu) | 0x800000u); |
| 272 | *E = (int32_t)(w >> 23) - 150; |
| 273 | } |
| 274 | |
| 275 | /* Is the midpoint between adjacent positive normal floats w1 < w2 below |
| 276 | * 1/sqrt(X 2^ex)? Midpoint = N 2^(Elow - 1) with N odd (no ties exist). |
| 277 | * mid < 1/sqrt(x) <=> N^2 X 2^(2 Elow - 2 + ex) < 1. */ |
| 278 | static int mid_below(uint32_t w1, uint32_t w2, uint64_t X, int32_t ex) { |
| 279 | uint64_t M1, M2; |
| 280 | int32_t E1, E2; |
| 281 | unpack_pos(w1, &M1, &E1); |
| 282 | unpack_pos(w2, &M2, &E2); |
| 283 | int32_t el = E1 < E2 ? E1 : E2; |
| 284 | uint64_t N = (M1 << (E1 - el)) + (M2 << (E2 - el)); |
| 285 | u128 L = (u128)N * N * X; /* < 2^78 */ |
| 286 | int32_t s = 2 * el - 2 + ex; |
| 287 | if (s >= 0) return 0; /* L 2^s >= 1 */ |
| 288 | if (-s >= 127) return 1; |
| 289 | return L < ((u128)1 << (-s)); |
| 290 | } |
| 291 | |
| 292 | float omega_math_rsqrt(float x) { |
| 293 | uint32_t u = tb(x); |
| 294 | if (is_nan(x)) return QNAN; |
| 295 | if (u == 0x00000000u) return PINF; |
| 296 | if (u == 0x80000000u) return NINF; |
| 297 | if (u & 0x80000000u) return QNAN; /* x < 0, incl. -inf */ |
| 298 | if (u == 0x7f800000u) return 0.0f; |
| 299 | int32_t post = 0; |
| 300 | if ((u & 0x7f800000u) == 0) { /* subnormal: x 2^24 exact */ |
| 301 | x = fmul(x, 0x1000000p0f); |
| 302 | u = tb(x); |
| 303 | post = 12; |
| 304 | } |
| 305 | float y = fdiv(1.0f, fsqrt(x)); /* within one ulp of 1/sqrt(x) */ |
| 306 | uint64_t X; |
| 307 | int32_t ex; |
| 308 | unpack_pos(u, &X, &ex); |
| 309 | uint32_t w = tb(y); |
| 310 | if (OMEGA_TRANSC_MUTATE != MUT_RSQRT) { |
| 311 | /* Exact rounding step: move to the neighbour while 1/sqrt(x) lies past |
| 312 | * the midpoint. Results are normal in [2^-64, 2^63], so w +- 1 stays |
| 313 | * a positive normal. */ |
| 314 | while (mid_below(w, w + 1u, X, ex)) w += 1u; |
| 315 | while (!mid_below(w - 1u, w, X, ex)) w -= 1u; |
| 316 | } |
| 317 | return fmul(tf(w), pow2i(post)); |
| 318 | } |
| 319 | |
| 320 | /* ---- ERF / ERFC core ------------------------------------------------------- */ |
| 321 | |
| 322 | /* erf(y) for 0 <= y < 0.5 (also tiny/subnormal y): |
| 323 | * (2/sqrt(pi)) y (1 + y^2 q(y^2)), q = sum_(n=1..7) (-1)^n y^(2n-2)/(n!(2n+1)). */ |
| 324 | static float erf_small(float y) { |
| 325 | const float TSP_HI = 0x906EBBp-23f; /* 2/sqrt(pi) */ |
| 326 | const float TSP_LO = -0xFBD649p-48f; |
| 327 | float y2 = fmul(y, y); |
| 328 | float q = -0xDDEBBDp-40f; /* n=7 */ |
| 329 | q = ffma(q, y2, 0xE00E01p-37f); |
| 330 | q = ffma(q, y2, -0xC6980Cp-34f); |
| 331 | q = ffma(q, y2, 0x97B426p-31f); |
| 332 | q = ffma(q, y2, -0xC30C31p-29f); |
| 333 | q = ffma(q, y2, K(MUT_ERF, 0xCCCCCDp-27f)); |
| 334 | q = ffma(q, y2, -0xAAAAABp-25f); /* n=1: -1/3 */ |
| 335 | float w = fmul(y2, q); |
| 336 | float A = fmul(y, TSP_HI); |
| 337 | float Al = ffma(y, TSP_HI, fneg(A)); |
| 338 | float B = ffma(y, TSP_LO, Al); |
| 339 | return fadd(A, ffma(A, w, B)); |
| 340 | } |
| 341 | |
| 342 | #include "omega_numeric_transc_tables.h" |
| 343 | |
| 344 | /* erfcx(y) = c + cl: c is the plain Horner result, cl collects the FP32 |
| 345 | * rounding of the last step (exact: a0 - c is exact by Sterbenz, then one |
| 346 | * FMADD) and the low part of a0. */ |
| 347 | static float erfcx_eval2(float y, float *cl) { |
| 348 | int i = 0; |
| 349 | while (i + 1 < ERFCX_N && y >= ERFCX_LO[i + 1]) i++; |
| 350 | float t = fsub(y, ERFCX_C[i]); /* exact (Sterbenz) */ |
| 351 | const float *a = ERFCX_A[i]; |
| 352 | float p = a[10]; |
| 353 | for (int n = 9; n >= 1; n--) p = ffma(p, t, a[n]); |
| 354 | float a0 = (OMEGA_TRANSC_MUTATE == MUT_ERFCX && i == 2) ? mut(a[0]) : a[0]; |
| 355 | float c = ffma(p, t, a0); |
| 356 | *cl = fadd(ffma(p, t, fsub(a0, c)), ERFCX_A0LO[i]); |
| 357 | return c; |
| 358 | } |
| 359 | |
| 360 | static float erfcx_eval(float y) { |
| 361 | float cl; |
| 362 | return erfcx_eval2(y, &cl); |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * erfc(y) = e^-(yy) * erfcx(y) for 0.5 <= y < 11, returned unscaled as |
| 367 | * m * 2^k (m normal) so a caller can fold further factors in before the |
| 368 | * single rounding into the subnormal range. yy = hh + hl exactly. |
| 369 | */ |
| 370 | static float erfc_core(float y, float hh, float hl, int32_t *k) { |
| 371 | float m = exp_core(fneg(hh), fneg(hl), k); |
| 372 | return fmul(m, erfcx_eval(y)); |
| 373 | } |
| 374 | |
| 375 | /* ---- ERF ------------------------------------------------------------------- */ |
| 376 | |
| 377 | float omega_math_erf(float x) { |
| 378 | if (is_nan(x)) return QNAN; |
| 379 | uint32_t sign = tb(x) & 0x80000000u; |
| 380 | float ax = fabs_(x); |
| 381 | float r; |
| 382 | if (ax >= 4.0f) { |
| 383 | r = 1.0f; /* erfc(4) < 2^-25: rounds to 1 */ |
| 384 | } else if (ax < 0.5f) { |
| 385 | r = erf_small(ax); /* +0 -> +0 */ |
| 386 | } else { |
| 387 | float h = fmul(ax, ax); |
| 388 | float hl = ffma(ax, ax, fneg(h)); /* exact */ |
| 389 | int32_t k; |
| 390 | float m = erfc_core(ax, h, hl, &k); |
| 391 | r = fsub(1.0f, scale2(m, k)); /* erfc >= 1.5e-8: normal */ |
| 392 | } |
| 393 | return tf(tb(r) | sign); |
| 394 | } |
| 395 | |
| 396 | /* ---- GELU (erf form) ------------------------------------------------------- */ |
| 397 | |
| 398 | float omega_math_gelu(float x) { |
| 399 | if (is_nan(x)) return QNAN; |
| 400 | if (x >= 8.0f) return x; /* x erfc(x/sqrt2)/2 < 2^-25 x: rounds to x; +inf -> +inf */ |
| 401 | if (x < -15.5f) return tf(0x80000000u); /* |gelu| < 2^-150: rounds to -0; -inf -> -0 */ |
| 402 | if ((tb(x) & 0x7fffffffu) == 0) return x; /* gelu(+-0) = +-0 */ |
| 403 | if ((tb(x) & 0x7fffffffu) < 0x01000000u) { |
| 404 | /* |x| < 2^-125: x/2 is exact or an exact midpoint between subnormals, |
| 405 | * and the correction x^2/sqrt(2 pi) > 0 is below 2^-124 of it, so it |
| 406 | * only breaks the tie, always toward +inf (Codex finding: rounding x/2 |
| 407 | * first lost it, e.g. gelu(2^-149) = 2^-149, not 0). Integer only. */ |
| 408 | uint32_t m = tb(x) & 0x7fffffffu; |
| 409 | uint32_t s = tb(x) & 0x80000000u; |
| 410 | return tf(s | (s ? (m >> 1) : ((m + 1u) >> 1))); |
| 411 | } |
| 412 | const float INV_SQRT2 = K(MUT_GELU, 0xB504F3p-24f); |
| 413 | float y = fmul(x, INV_SQRT2); |
| 414 | float ay = fabs_(y); |
| 415 | float hx = fmul(x, 0.5f); |
| 416 | if (ay < 0.5f) { |
| 417 | /* x/2 + x/2 * erf(y) */ |
| 418 | float e = erf_small(ay); |
| 419 | e = tf(tb(e) | (tb(y) & 0x80000000u)); |
| 420 | float r = ffma(hx, e, hx); |
| 421 | return tf(tb(r) | (tb(x) & 0x80000000u)); /* gelu has the sign of x: -0 when x < 0 underflows */ |
| 422 | } |
| 423 | /* y^2 = x^2/2 exactly: x^2 = P + Pl (FMADD residue), halving is exact */ |
| 424 | float P = fmul(x, x); |
| 425 | float Pl = ffma(x, x, fneg(P)); |
| 426 | float h = fmul(P, 0.5f); |
| 427 | float hl = fmul(Pl, 0.5f); |
| 428 | /* the rounding of y is put back to first order: yl = x/sqrt2 - y */ |
| 429 | const float INV_SQRT2_LO = 0xCFE77Ap-50f; /* 1/sqrt2 - INV_SQRT2 (unmutated) */ |
| 430 | float yl = ffma(x, INV_SQRT2, fneg(y)); /* exact */ |
| 431 | yl = ffma(x, INV_SQRT2_LO, yl); |
| 432 | float ayl = (tb(x) & 0x80000000u) ? fneg(yl) : yl; |
| 433 | int32_t k; |
| 434 | float mel, cl; |
| 435 | float me = exp_core2(fneg(h), fneg(hl), &k, &mel); /* e^(-y^2) ~ (me + mel) 2^k */ |
| 436 | float c = erfcx_eval2(ay, &cl); /* erfcx(|y|) ~ c + cl */ |
| 437 | /* erfcx'(y) = 2y erfcx(y) - 2/sqrt(pi) */ |
| 438 | float dc = ffma(fmul(ay, 2.0f), c, -0x906EBBp-23f); |
| 439 | cl = ffma(ayl, dc, cl); |
| 440 | /* erfc(|y|) ~ (p + pl) 2^k: p + (exact 2Prod residue) = me c, plus the cross terms */ |
| 441 | float p = fmul(me, c); |
| 442 | float pl = ffma(me, c, fneg(p)); |
| 443 | pl = ffma(me, cl, pl); |
| 444 | pl = ffma(mel, c, pl); |
| 445 | if (!(tb(x) & 0x80000000u)) { |
| 446 | /* x (1 - erfc(y)/2) = x - (x/2) erfc(y); y < 5.66 keeps erfc normal */ |
| 447 | return ffma(fneg(hx), scale2(p, k), x); |
| 448 | } |
| 449 | /* x < 0: (x/2) erfc(|y|), folded before the one rounding into subnormals */ |
| 450 | return scale2(ffma(hx, p, fmul(hx, pl)), k); |
| 451 | } |
| 452 | |
| 453 | /* ---- SIN / COS ------------------------------------------------------------- */ |
| 454 | |
| 455 | /* sin(rh + rl), |rh| <= ~pi/4, |rl| <= ulp(rh) */ |
| 456 | static float sin_poly(float rh, float rl) { |
| 457 | float r2 = fmul(rh, rh); |
| 458 | float S = 0xB09231p-56f; /* 1/13! */ |
| 459 | S = ffma(S, r2, -0xD7322Bp-49f); /* -1/11! */ |
| 460 | S = ffma(S, r2, 0xB8EF1Dp-42f); /* 1/9! */ |
| 461 | S = ffma(S, r2, -0xD00D01p-36f); /* -1/7! */ |
| 462 | S = ffma(S, r2, K(MUT_SIN, 0x888889p-30f)); /* 1/5! */ |
| 463 | S = ffma(S, r2, -0xAAAAABp-26f); /* -1/3! */ |
| 464 | float t = fmul(rh, r2); |
| 465 | float rlc = ffma(fmul(r2, -0.5f), rl, rl); /* rl cos(rh) to second order */ |
| 466 | return fadd(rh, ffma(t, S, rlc)); |
| 467 | } |
| 468 | |
| 469 | /* cos(rh + rl) */ |
| 470 | static float cos_poly(float rh, float rl) { |
| 471 | float r2 = fmul(rh, rh); |
| 472 | float r2l = ffma(rh, rh, fneg(r2)); /* exact */ |
| 473 | float C = -0xC9CBA5p-60f; /* -1/14! */ |
| 474 | C = ffma(C, r2, 0x8F76C7p-52f); /* 1/12! */ |
| 475 | C = ffma(C, r2, -0x93F27Ep-45f); /* -1/10! */ |
| 476 | C = ffma(C, r2, 0xD00D01p-39f); /* 1/8! */ |
| 477 | C = ffma(C, r2, -0xB60B61p-33f); /* -1/6! */ |
| 478 | C = ffma(C, r2, K(MUT_COS, 0xAAAAABp-28f)); /* 1/4! */ |
| 479 | float hr = fmul(r2, 0.5f); |
| 480 | float h = fsub(1.0f, hr); |
| 481 | float eh = fsub(fsub(1.0f, h), hr); /* exact: 1 - hr = h + eh */ |
| 482 | float c1 = ffma(r2l, -0.5f, eh); |
| 483 | float c2 = ffma(fneg(rl), rh, c1); |
| 484 | float r4 = fmul(r2, r2); |
| 485 | return fadd(h, ffma(r4, C, c2)); |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * x = k pi/2 + (rh + rl). pi/2 = P1 + P2 + P3 (24 bits each). For |
| 490 | * |x| <= 2^22: r1 = x - k P1 is exact (one FMADD, |r1| < 1); k P2 is split |
| 491 | * exactly (FMUL + FMADD residue) and subtracted with two 2Sums; k P3 and |
| 492 | * the 2Sum errors form the low word. |
| 493 | */ |
| 494 | static float sincos_core(float x, int32_t qoff) { |
| 495 | if (is_nan(x)) return QNAN; |
| 496 | float ax = fabs_(x); |
| 497 | if (!(ax <= OMEGA_TRANSC_TRIG_MAX_ABS)) return QNAN; /* +-inf and out of domain */ |
| 498 | float rh, rl; |
| 499 | int32_t k; |
| 500 | if (ax <= 0xC90FDBp-24f) { /* |x| <= pi/4 (rounded) */ |
| 501 | k = 0; |
| 502 | rh = x; |
| 503 | rl = 0.0f; |
| 504 | } else { |
| 505 | const float P1 = 0xC90FDBp-23f; |
| 506 | const float P2 = -0xBBBD2Ep-48f; |
| 507 | const float P3 = -0xF72CEDp-73f; |
| 508 | float t = fadd(fmul(x, 0xA2F983p-24f), MAGIC); /* 2/pi */ |
| 509 | float kf = fsub(t, MAGIC); |
| 510 | k = (int32_t)(tb(t) - tb(MAGIC)); |
| 511 | float r1 = ffma(fneg(kf), P1, x); |
| 512 | float th = fmul(kf, P2); |
| 513 | float tl = ffma(kf, P2, fneg(th)); |
| 514 | float s1, e1, s2, e2; |
| 515 | two_sum(r1, fneg(th), &s1, &e1); |
| 516 | two_sum(s1, fneg(tl), &s2, &e2); |
| 517 | float lo = fsub(fadd(e1, e2), fmul(kf, P3)); |
| 518 | rh = fadd(s2, lo); |
| 519 | rl = fsub(lo, fsub(rh, s2)); |
| 520 | } |
| 521 | uint32_t q = (uint32_t)(k + qoff) & 3u; |
| 522 | float r = (q & 1u) ? cos_poly(rh, rl) : sin_poly(rh, rl); |
| 523 | return (q & 2u) ? fneg(r) : r; |
| 524 | } |
| 525 | |
| 526 | float omega_math_sin(float x) { |
| 527 | if ((tb(x) & 0x7fffffffu) == 0) return x; /* +-0 */ |
| 528 | return sincos_core(x, 0); |
| 529 | } |
| 530 | |
| 531 | float omega_math_cos(float x) { |
| 532 | return sincos_core(x, 1); |
| 533 | } |