yang
This project has not described itself yet.
Brought by drakestapleton
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note src/ufbi_math.om: Pure Seeded-OM-2 f64/f32 math, PRNG, and formatting helpers. |
| 3 | note Zero libc, zero libm, zero external runtime. |
| 4 | |
| 5 | text umMinus "-" |
| 6 | text umDot "." |
| 7 | text umInf "INF" |
| 8 | |
| 9 | fn uPrint s view uchar * to void |
| 10 | r = write 1 s 0 len s |
| 11 | end |
| 12 | |
| 13 | fn uPrintU x ulong to void |
| 14 | b = new uchar 24 0 |
| 15 | pos = ulong 24 |
| 16 | v = x |
| 17 | while true bound 24 |
| 18 | pos = sub pos 1 |
| 19 | put b pos uchar add 48 rem v 10 |
| 20 | v = div v 10 |
| 21 | if eq v 0 |
| 22 | break |
| 23 | end |
| 24 | end |
| 25 | r = write 1 b pos sub 24 pos |
| 26 | end |
| 27 | |
| 28 | fn uPrintI x long to void |
| 29 | if lt x 0 |
| 30 | uPrint umMinus |
| 31 | uPrintU ulong sub 0 x |
| 32 | ret |
| 33 | end |
| 34 | uPrintU ulong x |
| 35 | end |
| 36 | |
| 37 | note Print f64 with 6 decimal digits (e.g. -0.123456) |
| 38 | fn uPrintF64 v f64 to void |
| 39 | buf = new uchar 6 48 |
| 40 | x = v |
| 41 | if lt x 0.0f64 |
| 42 | uPrint umMinus |
| 43 | x = sub 0.0f64 x |
| 44 | end |
| 45 | if gt x 1000000000000.0f64 |
| 46 | uPrint umInf |
| 47 | ret |
| 48 | end |
| 49 | scaled = ulong add mul x 1000000.0f64 0.5f64 |
| 50 | ip = div scaled 1000000 |
| 51 | fp = rem scaled 1000000 |
| 52 | uPrintU ip |
| 53 | uPrint umDot |
| 54 | p = ulong 6 |
| 55 | f = fp |
| 56 | while gt p 0 bound 8 |
| 57 | p = sub p 1 |
| 58 | put buf p uchar add 48 rem f 10 |
| 59 | f = div f 10 |
| 60 | end |
| 61 | r = write 1 buf 0 6 |
| 62 | end |
| 63 | |
| 64 | fn uAbs64 x f64 to f64 |
| 65 | if lt x 0.0f64 |
| 66 | ret sub 0.0f64 x |
| 67 | end |
| 68 | ret x |
| 69 | end |
| 70 | |
| 71 | fn uSqrt64 x f64 to f64 |
| 72 | if le x 0.0f64 |
| 73 | ret 0.0f64 |
| 74 | end |
| 75 | ret fsqrt x |
| 76 | end |
| 77 | |
| 78 | fn uPi to f64 |
| 79 | ret bitcast f64 ulong 0x400921fb54442d18 |
| 80 | end |
| 81 | |
| 82 | fn uTwoPi to f64 |
| 83 | ret bitcast f64 ulong 0x401921fb54442d18 |
| 84 | end |
| 85 | |
| 86 | fn uHalfPi to f64 |
| 87 | ret bitcast f64 ulong 0x3ff921fb54442d18 |
| 88 | end |
| 89 | |
| 90 | fn uLn2 to f64 |
| 91 | ret bitcast f64 ulong 0x3fe62e42fefa39ef |
| 92 | end |
| 93 | |
| 94 | fn uInvLn2 to f64 |
| 95 | ret bitcast f64 ulong 0x3ff71547652b82fe |
| 96 | end |
| 97 | |
| 98 | fn uExp64 x f64 to f64 |
| 99 | v = x |
| 100 | if gt v 700.0f64 |
| 101 | ret bitcast f64 ulong 0x7e37e43c8800759c |
| 102 | end |
| 103 | if lt v neg 700.0f64 |
| 104 | ret 0.0f64 |
| 105 | end |
| 106 | kf = mul v uInvLn2 |
| 107 | ki = long 0 |
| 108 | if ge kf 0.0f64 |
| 109 | ki = long add kf 0.5f64 |
| 110 | else |
| 111 | ki = long sub kf 0.5f64 |
| 112 | end |
| 113 | r = sub v mul f64 ki uLn2 |
| 114 | p = f64 1.0f64 |
| 115 | p = add 1.0f64 mul r div p 12.0f64 |
| 116 | p = add 1.0f64 mul r div p 11.0f64 |
| 117 | p = add 1.0f64 mul r div p 10.0f64 |
| 118 | p = add 1.0f64 mul r div p 9.0f64 |
| 119 | p = add 1.0f64 mul r div p 8.0f64 |
| 120 | p = add 1.0f64 mul r div p 7.0f64 |
| 121 | p = add 1.0f64 mul r div p 6.0f64 |
| 122 | p = add 1.0f64 mul r div p 5.0f64 |
| 123 | p = add 1.0f64 mul r div p 4.0f64 |
| 124 | p = add 1.0f64 mul r div p 3.0f64 |
| 125 | p = add 1.0f64 mul r div p 2.0f64 |
| 126 | p = add 1.0f64 mul r p |
| 127 | eb = shl ulong add ki 1023 52 |
| 128 | ret mul p bitcast f64 eb |
| 129 | end |
| 130 | |
| 131 | note Abramowitz-Stegun 7.1.26 erf(x), max error < 1.5e-7 |
| 132 | fn uErf64 x f64 to f64 |
| 133 | sgn = f64 1.0f64 |
| 134 | ax = x |
| 135 | if lt ax 0.0f64 |
| 136 | sgn = neg 1.0f64 |
| 137 | ax = sub 0.0f64 ax |
| 138 | end |
| 139 | if gt ax 6.0f64 |
| 140 | ret sgn |
| 141 | end |
| 142 | t = div 1.0f64 add 1.0f64 mul 0.3275911f64 ax |
| 143 | y = mul t 1.061405429f64 |
| 144 | y = mul t sub y 1.453152027f64 |
| 145 | y = mul t add y 1.421413741f64 |
| 146 | y = mul t sub y 0.284496736f64 |
| 147 | y = mul t add y 0.254829592f64 |
| 148 | res = sub 1.0f64 mul y uExp64 neg mul ax ax |
| 149 | ret mul sgn res |
| 150 | end |
| 151 | |
| 152 | note Gaussian Total Variation distance: erf(delta / (2 * sqrt(2))) |
| 153 | fn uGaussTv delta f64 to f64 |
| 154 | ret uErf64 mul delta 0.3535533905932738f64 |
| 155 | end |
| 156 | |
| 157 | note Pure OM-2 sin(x) in f64 via range reduction to [-pi/2, pi/2] + 15th-order Taylor |
| 158 | fn uSin64 theta f64 to f64 |
| 159 | pi = uPi |
| 160 | twopi = uTwoPi |
| 161 | halfpi = uHalfPi |
| 162 | k = long div theta twopi |
| 163 | x = sub theta mul f64 k twopi |
| 164 | while gt x pi bound 8 |
| 165 | x = sub x twopi |
| 166 | end |
| 167 | while lt x neg pi bound 8 |
| 168 | x = add x twopi |
| 169 | end |
| 170 | if gt x halfpi |
| 171 | x = sub pi x |
| 172 | end |
| 173 | if lt x neg halfpi |
| 174 | x = sub neg pi x |
| 175 | end |
| 176 | x2 = mul x x |
| 177 | s = f64 1.0f64 |
| 178 | s = sub 1.0f64 div mul x2 s 210.0f64 |
| 179 | s = sub 1.0f64 div mul x2 s 156.0f64 |
| 180 | s = sub 1.0f64 div mul x2 s 110.0f64 |
| 181 | s = sub 1.0f64 div mul x2 s 72.0f64 |
| 182 | s = sub 1.0f64 div mul x2 s 42.0f64 |
| 183 | s = sub 1.0f64 div mul x2 s 20.0f64 |
| 184 | s = sub 1.0f64 div mul x2 s 6.0f64 |
| 185 | ret mul x s |
| 186 | end |
| 187 | |
| 188 | fn uCos64 theta f64 to f64 |
| 189 | ret uSin64 add theta uHalfPi |
| 190 | end |
| 191 | |
| 192 | fn uLog64 x f64 to f64 |
| 193 | if le x 0.0f64 |
| 194 | ret neg 700.0f64 |
| 195 | end |
| 196 | bits = bitcast ulong x |
| 197 | e = sub long and shr bits 52 2047 1023 |
| 198 | mbits = or and bits 0x000fffffffffffff 0x3ff0000000000000 |
| 199 | m = bitcast f64 mbits |
| 200 | if gt m 1.4142135623730951f64 |
| 201 | m = mul m 0.5f64 |
| 202 | e = add e 1 |
| 203 | end |
| 204 | t = div sub m 1.0f64 add m 1.0f64 |
| 205 | t2 = mul t t |
| 206 | s = div 1.0f64 15.0f64 |
| 207 | s = add div 1.0f64 13.0f64 mul t2 s |
| 208 | s = add div 1.0f64 11.0f64 mul t2 s |
| 209 | s = add div 1.0f64 9.0f64 mul t2 s |
| 210 | s = add div 1.0f64 7.0f64 mul t2 s |
| 211 | s = add div 1.0f64 5.0f64 mul t2 s |
| 212 | s = add div 1.0f64 3.0f64 mul t2 s |
| 213 | s = add 1.0f64 mul t2 s |
| 214 | ret add mul f64 e uLn2 mul 2.0f64 mul t s |
| 215 | end |
| 216 | |
| 217 | note Deterministic splitmix64 PRNG for host thermal collar / Box-Muller normal draws |
| 218 | global uRngState ulong 1 |
| 219 | |
| 220 | fn uRngSeed s ulong to void |
| 221 | put uRngState 0 s |
| 222 | end |
| 223 | |
| 224 | fn uRngNext to ulong |
| 225 | z = add get uRngState 0 0x9e3779b97f4a7c15 |
| 226 | put uRngState 0 z |
| 227 | z = mul xor z shr z 30 0xbf58476d1ce4e5b9 |
| 228 | z = mul xor z shr z 27 0x94d049bb133111eb |
| 229 | ret xor z shr z 31 |
| 230 | end |
| 231 | |
| 232 | fn uRngUnit64 to f64 |
| 233 | r = shr uRngNext 11 |
| 234 | inv53 = bitcast f64 ulong 0x3ca0000000000000 |
| 235 | ret mul f64 r inv53 |
| 236 | end |
| 237 | |
| 238 | note Box-Muller standard normal N(0,1) |
| 239 | fn uRngNorm64 to f64 |
| 240 | u1 = uRngUnit64 |
| 241 | eps = bitcast f64 ulong 0x3cd203af9ee75616 |
| 242 | if lt u1 eps |
| 243 | u1 = eps |
| 244 | end |
| 245 | u2 = uRngUnit64 |
| 246 | rad = uSqrt64 mul neg 2.0f64 uLog64 u1 |
| 247 | ret mul rad uCos64 mul uTwoPi u2 |
| 248 | end |