interplane.Get Omega
Read in your language

yang

This project has not described itself yet.

Brought by drakestapleton

branch main Branches Tags History

yang / src / ufbi_math.om

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