interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / gpu / amd / gfx942 / exec / fma.om

197 lines · 3259 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note Exact binary32 fused multiply-add, RN-even, gradual underflow, canonical NaN.
3note Finite x = mantissa * 2^(effective_exponent-150). Scale exact sum by 2^298.
4note Products and addends then are integers; their sum needs at most 554 bits, hence nine u64 limbs.
5note This is a format-derived bound, not an input cap. No intermediate floating-point rounding.
6global fm ulong 9
7global fc ulong 9
8fn fmbool value bool to ulong
9if value
10ret 1
11end
12ret 0
13end
14fn fmexp x ulong to ulong
15e = and shr x 23 255
16if eq e 0
17ret 1
18end
19ret e
20end
21fn fmmant x ulong to ulong
22v = and x 0x7fffff
23if ne and x 0x7f800000 0
24v = or v 0x800000
25end
26ret v
27end
28fn fmput other bool i ulong v ulong to void
29if other
30put fc i v
31else
32put fm i v
33end
34end
35fn fmplace other bool v ulong shift ulong to void
36i = div shift 64
37s = and shift 63
38fmput other i shl v s
39if and ne s 0 lt add i 1 len fm
40fmput other add i 1 shr v sub 64 s
41end
42end
43fn fmmag to int
44for j ulong 9
45i = sub 8 j
46if gt get fm i get fc i
47ret 1
48end
49if lt get fm i get fc i
50ret -1
51end
52end
53ret 0
54end
55fn fmadd to void
56cy = ulong 0
57for i ulong 9
58a = get fm i
59b = get fc i
60v = add a b
61c = lt v a
62w = add v cy
63cy = fmbool or c lt w v
64put fm i w
65end
66end
67fn fmsub swap bool to void
68borrow = ulong 0
69for i ulong 9
70a = get fm i
71b = get fc i
72if swap
73t = a
74a = b
75b = t
76end
77v = sub a b
78c = lt a b
79w = sub v borrow
80borrow = fmbool or c lt v borrow
81put fm i w
82end
83end
84fn fmget shift ulong to ulong
85i = div shift 64
86s = and shift 63
87v = shr get fm i s
88if and ne s 0 lt add i 1 len fm
89v = or v shl get fm add i 1 sub 64 s
90end
91ret v
92end
93fn fmsticky limit ulong to bool
94full = div limit 64
95for i ulong full
96if ne get fm i 0
97ret true
98end
99end
100bits = and limit 63
101if ne bits 0
102if ne and get fm full sub shl ulong 1 bits 1 0
103ret true
104end
105end
106ret false
107end
108fn fmround sg ulong to ulong
109n = ulong 0
110for j ulong 9
111i = sub 8 j
112v = get fm i
113if ne v 0
114n = mul i 64
115while gt v 1 bound 999999
116v = shr v 1
117n = add n 1
118end
119break
120end
121end
122shift = ulong 149
123if gt n 172
124shift = sub n 23
125end
126m = and fmget shift 0xffffff
127half = ne and fmget sub shift 1 1 0
128if and half or fmsticky sub shift 1 ne and m 1 0
129m = add m 1
130end
131if ge m 0x1000000
132m = shr m 1
133n = add n 1
134end
135if ge n 426
136ret or sg 0x7f800000
137end
138e = ulong 0
139if ge m 0x800000
140e = 1
141if gt n 172
142e = sub n 171
143end
144end
145ret or sg or shl e 23 and m 0x7fffff
146end
147fn fmfinite a ulong b ulong c ulong to ulong
148for i ulong 9
149put fm i 0
150put fc i 0
151end
152p = mul fmmant a fmmant b
153shift = sub add fmexp a fmexp b 2
154fmplace false p shift
155fmplace true fmmant c add fmexp c 148
156sp = and xor a b 0x80000000
157sc = and c 0x80000000
158sg = sp
159if eq sp sc
160fmadd
161else
162cmp = fmmag
163if eq cmp 0
164ret 0
165end
166if lt cmp 0
167sg = sc
168end
169fmsub lt cmp 0
170end
171ret fmround sg
172end
173fn fpfma a ulong b ulong c ulong to ulong
174ax = and a 0x7fffffff
175bx = and b 0x7fffffff
176cx = and c 0x7fffffff
177if or or gt ax 0x7f800000 gt bx 0x7f800000 gt cx 0x7f800000
178ret 0x7fc00000
179end
180sp = and xor a b 0x80000000
181if or eq ax 0x7f800000 eq bx 0x7f800000
182if or eq ax 0 eq bx 0
183ret 0x7fc00000
184end
185if and eq cx 0x7f800000 ne sp and c 0x80000000
186ret 0x7fc00000
187end
188ret or sp 0x7f800000
189end
190if eq cx 0x7f800000
191ret c
192end
193if and or eq ax 0 eq bx 0 eq cx 0
194ret and sp and c 0x80000000
195end
196ret fmfinite a b c
197end