interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / om / tools / fmt.om

392 lines · 8180 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note fmt: exact printf("%.Pf") and printf("%.Pg") of an f64, as glibc prints them (the exact binary value
3note rounded once, ties to even; inf, -inf, nan, -nan; the sign of -0). Shared by the om/tools units that
4note format floats; a unit spec concatenates this file before the unit (om/units.sh name=fmt.om,unit.om).
5note fm_dig writes the exact decimal digits of |x|: integer digits d 0..ip-1 then fraction digits
6note d ip..ip+fl-1, and returns ip * 65536 + fl. Limbs are base 1e9, little-endian, 130 of them; the
7note longest value is 2^53 * 5^1074 (767 digits) and the longest digit string 16 + 1074.
8const FMDN ulong 1200
9const FMON ulong 1600
10const FMLN ulong 130
11const FMB ulong 1000000000
12fn fm_mul l edit ulong 130 n ulong f ulong to ulong
13c = ulong 0
14for i ulong n
15v = add mul get l i f c
16put l i rem v FMB
17c = div v FMB
18end
19k = n
20while gt c 0 bound 130
21put l k rem c FMB
22c = div c FMB
23k = add k 1
24end
25ret k
26end
27fn fm_ndig v ulong to ulong
28n = ulong 1
29x = div v 10
30while gt x 0 bound 20
31n = add n 1
32x = div x 10
33end
34ret n
35end
36note fm_tod writes the limbs l 0..n-1 as decimal digits (the top limb without leading zeros) at d p.
37note fm_wd writes the w low decimal digits of x at d p..p+w-1, most significant first.
38fn fm_wd d edit uchar 1200 p ulong w ulong x ulong to ulong
39k = add p w
40v = x
41for i ulong w
42k = sub k 1
43put d k uchar add 48 rem v 10
44v = div v 10
45end
46ret add p w
47end
48fn fm_tod l edit ulong 130 n ulong d edit uchar 1200 p ulong to ulong
49x = get l sub n 1
50q = fm_wd d p fm_ndig x x
51j = sub n 1
52while gt j 0 bound 130
53j = sub j 1
54q = fm_wd d q 9 get l j
55end
56ret q
57end
58note fm_big: |x| = m 2^k as the integer m 2^k (k >= 0) or m 5^s with s fraction digits (k = -s);
59note the limbs go to l 0..n-1, s to l 129; returns n with no zero top limb above limb 0.
60fn fm_big l edit ulong 130 b ulong to ulong
61e = and shr b 52 2047
62m = and b 4503599627370495
63s = ulong 1074
64if ne e 0
65m = or m 4503599627370496
66s = sub ulong 1075 e
67end
68put l 0 rem m FMB
69put l 1 rem div m FMB FMB
70n = ulong 2
71if ge e 1075
72k = sub e 1075
73s = ulong 0
74while ge k 29 bound 40
75n = fm_mul l n 536870912
76k = sub k 29
77end
78n = fm_mul l n shl ulong 1 k
79end
80put l 129 s
81k = s
82while ge k 13 bound 90
83n = fm_mul l n 1220703125
84k = sub k 13
85end
86f = ulong 1
87for i ulong k
88f = mul f 5
89end
90n = fm_mul l n f
91while gt n 1 bound 130
92if ne get l sub n 1 0
93break
94end
95n = sub n 1
96end
97ret n
98end
99fn fm_dig x f64 d edit uchar 1200 to ulong
100l = new ulong 130 0
101n = fm_big l bitcast ulong x
102s = get l 129
103t = add fm_ndig get l sub n 1 mul 9 sub n 1
104if eq s 0
105q = fm_tod l n d 0
106ret shl q 16
107end
108if gt t s
109q = fm_tod l n d 0
110ret or shl sub q s 16 s
111end
112put d 0 uchar 48
113z = sub s t
114for i ulong z
115put d add i 1 uchar 48
116end
117q = fm_tod l n d add z 1
118ret or shl sub q s 16 s
119end
120note fm_at: digit i of a string of n digits, '0' past its end.
121fn fm_at d edit uchar 1200 n ulong i ulong to ulong
122if lt i n
123ret ulong get d i
124end
125ret ulong 48
126end
127note fm_rup: 1 when keeping digits 0..q-1 of the n digits rounds up (ties to even).
128fn fm_rup d edit uchar 1200 n ulong q ulong to ulong
129if ge q n
130ret ulong 0
131end
132c = ulong get d q
133if gt c 53
134ret ulong 1
135end
136if lt c 53
137ret ulong 0
138end
139j = add q 1
140while lt j n bound 1200
141if ne get d j uchar 48
142ret ulong 1
143end
144j = add j 1
145end
146if eq q 0
147ret ulong 0
148end
149ret and sub ulong get d sub q 1 48 1
150end
151note fm_inc adds one at digit q-1; 1 when the carry leaves digit 0.
152fn fm_inc d edit uchar 1200 q ulong to ulong
153j = q
154while gt j 0 bound 1200
155j = sub j 1
156c = get d j
157if eq c uchar 57
158put d j uchar 48
159else
160put d j uchar add c 1
161ret ulong 0
162end
163end
164ret ulong 1
165end
166note fm_sp: inf/nan text; returns the new length, or 0 when x is finite. Sign printed for -inf and -nan.
167fn fm_sp x f64 o edit uchar 1600 to ulong
168b = bitcast ulong x
169if ne and shr b 52 2047 2047
170ret ulong 0
171end
172p = ulong 0
173if ne shr b 63 0
174put o 0 uchar 45
175p = ulong 1
176end
177if eq and b 4503599627370495 0
178put o p uchar 105
179put o add p 1 uchar 110
180put o add p 2 uchar 102
181ret add p 3
182end
183put o p uchar 110
184put o add p 1 uchar 97
185put o add p 2 uchar 110
186ret add p 3
187end
188note fm_f: printf("%.Pf", x) into o; returns the length.
189fn fm_f x f64 pr ulong o edit uchar 1600 to ulong
190r = fm_sp x o
191if ne r 0
192ret r
193end
194d = new uchar 1200 0
195r = fm_dig x d
196ip = shr r 16
197n = add ip and r 65535
198q = add ip pr
199p = ulong 0
200if ne shr bitcast ulong x 63 0
201put o 0 uchar 45
202p = ulong 1
203end
204if ne fm_rup d n q 0
205if ne fm_inc d q 0
206put o p uchar 49
207p = add p 1
208end
209end
210for i ulong ip
211put o p get d i
212p = add p 1
213end
214if eq pr 0
215ret p
216end
217put o p uchar 46
218p = add p 1
219for i ulong pr
220put o p uchar fm_at d n add ip i
221p = add p 1
222end
223ret p
224end
225note fm_frst: index of the first nonzero digit of 0..n-1, n when there is none.
226fn fm_frst d edit uchar 1200 n ulong to ulong
227z = ulong 0
228while lt z n bound 1200
229if ne get d z uchar 48
230ret z
231end
232z = add z 1
233end
234ret n
235end
236note fm_e2: the exponent of the e style: sign and at least two digits of |X|. xn is 1 when X < 0.
237fn fm_e2 o edit uchar 1600 p ulong xn ulong xa ulong to ulong
238put o p uchar 101
239q = add p 1
240if ne xn 0
241put o q uchar 45
242else
243put o q uchar 43
244end
245q = add q 1
246if lt xa 10
247put o q uchar 48
248q = add q 1
249end
250w = fm_ndig xa
251k = add q w
252x = xa
253for i ulong w
254k = sub k 1
255put o k uchar add 48 rem x 10
256x = div x 10
257end
258ret add q w
259end
260note fm_gd: R digit i of %g (digits d z.. of n); after a carry out of digit 0, 1 then zeros.
261fn fm_gd d edit uchar 1200 n ulong z ulong c ulong i ulong to ulong
262if ne c 0
263if eq i 0
264ret ulong 49
265end
266ret ulong 48
267end
268ret fm_at d n add z i
269end
270note fm_rd moves the pr digits R of %g to d 0..pr-1 (reads d z+i at or after the write at i).
271fn fm_rd d edit uchar 1200 n ulong z ulong c ulong pr ulong to ulong
272for i ulong pr
273put d i uchar fm_gd d n z c i
274end
275ret pr
276end
277note fm_ges: e style of the k kept digits d 0..k-1 with exponent xs (see fm_ge) at o p.
278fn fm_ges o edit uchar 1600 p ulong d edit uchar 1200 k ulong xs ulong to ulong
279put o p get d 0
280q = add p 1
281if gt k 1
282put o q uchar 46
283q = add q 1
284for i ulong sub k 1
285put o q get d add i 1
286q = add q 1
287end
288end
289ret fm_e2 o q and xs 1 shr xs 1
290end
291note fm_gfs: f style of the k kept digits d 0..k-1 with exponent xs (see fm_ge) at o p.
292fn fm_gfs o edit uchar 1600 p ulong d edit uchar 1200 k ulong xs ulong to ulong
293xa = shr xs 1
294q = p
295if ne and xs 1 0
296put o q uchar 48
297put o add q 1 uchar 46
298q = add q 2
299for i ulong sub xa 1
300put o q uchar 48
301q = add q 1
302end
303for i ulong k
304put o q get d i
305q = add q 1
306end
307ret q
308end
309for i ulong add xa 1
310put o q get d i
311q = add q 1
312end
313if gt k add xa 1
314put o q uchar 46
315q = add q 1
316for i ulong sub sub k xa 1
317put o q get d add add xa 1 i
318q = add q 1
319end
320end
321ret q
322end
323note fm_ge emits R = d 0..pr-1 with exponent X (xs = 2 |X| + 1 when X < 0, else 2 X) at o p.
324note e style when X < -4 or X >= pr; trailing fraction zeros removed; returns the length.
325fn fm_ge o edit uchar 1600 p ulong d edit uchar 1200 pr ulong xs ulong to ulong
326xa = shr xs 1
327el = ulong 0
328if ne and xs 1 0
329if gt xa 4
330el = ulong 1
331end
332else
333if ge xa pr
334el = ulong 1
335end
336end
337k = pr
338while gt k 1 bound 1200
339if ne get d sub k 1 uchar 48
340break
341end
342k = sub k 1
343end
344note k is the count of R digits that survive trailing-zero removal (at least 1).
345if ne el 0
346ret fm_ges o p d k xs
347end
348ret fm_gfs o p d k xs
349end
350note fm_g: printf("%.Pg", x) into o (P 0 counts as 1); returns the length. The P significant digits are
351note rounded at the first nonzero digit z; the exponent X = ip - 1 - z after rounding picks the style.
352fn fm_g x f64 pr ulong o edit uchar 1600 to ulong
353r = fm_sp x o
354if ne r 0
355ret r
356end
357if eq pr 0
358pr = ulong 1
359end
360d = new uchar 1200 0
361r = fm_dig x d
362ip = shr r 16
363n = add ip and r 65535
364p = ulong 0
365if ne shr bitcast ulong x 63 0
366put o 0 uchar 45
367p = ulong 1
368end
369z = fm_frst d n
370if eq z n
371put o p uchar 48
372ret add p 1
373end
374q = add z pr
375c = ulong 0
376if ne fm_rup d n q 0
377c = fm_inc d q
378end
379note after a carry out of digit 0 the digits are 1 0..0 one place up: the leading 1 sits at index -1,
380note so X = ip. Otherwise X = ip - 1 - z for the first nonzero rounded digit z.
381xs = shl ip 1
382if eq c 0
383z = fm_frst d q
384if lt z ip
385xs = shl sub sub ip 1 z 1
386else
387xs = or shl add sub z ip 1 1 1
388end
389end
390r = fm_rd d n z c pr
391ret fm_ge o p d r xs
392end