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 / fp.om

216 lines · 4455 bytes Raw Blame History
1om2
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3note gfx942 host executor. ADD is integer-only; bounded EXP/LOG/RCP/RSQ representatives use native binary64.
4note Binary32 addition: magnitude-order operands, align with guard/round/sticky, normalize,
5note round nearest ties even, gradual underflow. Exact cancellation is +0; -0 + -0 is -0.
6note NaNs and invalid infinity addition produce canonical quiet NaN 0x7fc00000.
7note NaN payload/sign propagation is not specified by IEEE arithmetic; this is the model policy.
8note tests/amd-exec/oracle.py checks bit patterns against independently rounded Python float/struct.
9fn jam x ulong n ulong to ulong
10if eq n 0
11ret x
12end
13if ge n 64
14if ne x 0
15ret 1
16end
17ret 0
18end
19r = shr x n
20if ne and x sub shl ulong 1 n 1 0
21r = or r 1
22end
23ret r
24end
25fn fpround am ulong ae ulong sg ulong to ulong
26lo = and am 7
27am = shr am 3
28if or gt lo 4 and eq lo 4 ne and am 1 0
29am = add am 1
30end
31if ge am 0x1000000
32am = shr am 1
33ae = add ae 1
34end
35if ge ae 255
36ret or sg 0x7f800000
37end
38if lt am 0x800000
39ae = 0
40end
41ret or or sg shl ae 23 and am 0x7fffff
42end
43fn fpadd a ulong b ulong to ulong
44ax = and a 0x7fffffff
45bx = and b 0x7fffffff
46if or gt ax 0x7f800000 gt bx 0x7f800000
47ret 0x7fc00000
48end
49if eq ax 0x7f800000
50if and eq bx ax ne shr a 31 shr b 31
51ret 0x7fc00000
52end
53ret a
54end
55if eq bx 0x7f800000
56ret b
57end
58if lt ax bx
59t = a
60a = b
61b = t
62t = ax
63ax = bx
64bx = t
65end
66if eq ax 0
67ret and and a b 0x80000000
68end
69sg = and a 0x80000000
70ae = shr ax 23
71be = shr bx 23
72am = and ax 0x7fffff
73bm = and bx 0x7fffff
74if ne ae 0
75am = or am 0x800000
76else
77ae = 1
78end
79if ne be 0
80bm = or bm 0x800000
81else
82be = 1
83end
84am = shl am 3
85bm = jam shl bm 3 sub ae be
86if eq and xor a b 0x80000000 0
87am = add am bm
88if ge am 0x8000000
89am = jam am 1
90ae = add ae 1
91end
92else
93am = sub am bm
94if eq am 0
95ret 0
96end
97while and lt am 0x4000000 gt ae 1 bound 999999
98am = shl am 1
99ae = sub ae 1
100end
101end
102ret fpround am ae sg
103end
104note CDNA3 PDF p172, IEEE mode: signaling NaNs quiet before numeric max; prefer +0.
105fn fpmax a ulong b ulong to ulong
106ax = and a 0x7fffffff
107bx = and b 0x7fffffff
108if and gt ax 0x7f800000 eq and ax 0x400000 0
109ret or a 0x400000
110end
111if and gt bx 0x7f800000 eq and bx 0x400000 0
112ret or b 0x400000
113end
114if gt ax 0x7f800000
115ret b
116end
117if gt bx 0x7f800000
118ret a
119end
120if and eq ax 0 eq bx 0
121ret and a b
122end
123if ge bitcast f32 uint a bitcast f32 uint b
124ret a
125end
126ret b
127end
128note Transcendental representative, not a claim of device bit identity. PDF pp195-196: 1ULP, FTZ.
129note Native binary64 arithmetic only here. Decimal oracle bounds each result independently.
130fn fpftz a ulong to ulong
131if lt and a 0x7fffffff 0x800000
132ret and a 0x80000000
133end
134ret a
135end
136fn fpexp a ulong to ulong
137if gt and a 0x7fffffff 0x7f800000
138ret 0x7fc00000
139end
140x = f64 bitcast f32 uint fpftz a
141if ge x 128.0f64
142ret 0x7f800000
143end
144if lt x -126.0f64
145ret 0
146end
147n = int x
148if lt x f64 n
149n = sub n 1
150end
151r = mul sub x f64 n 0.6931471805599453f64
152t = 1.0f64
153y = 1.0f64
154for j ulong 18
155t = div mul t r f64 add j 1
156y = add y t
157end
158scale = bitcast f64 shl ulong add n 1023 52
159ret fpftz ulong bitcast uint f32 mul y scale
160end
161note CDNA3 V_LOG_F32 opcode33: base2 log, 1ULP and input/output FTZ.
162note Reduce to mantissa [1,2); log(m)=2*(z+z^3/3+...), z=(m-1)/(m+1).
163note Thirty terms at |z|<=1/3 put truncation far below binary32 rounding.
164fn fplog a ulong to ulong
165a = fpftz a
166ax = and a 0x7fffffff
167if gt ax 0x7f800000
168ret or and a 0x80000000 0x7fc00000
169end
170if eq ax 0
171ret 0xff800000
172end
173if ne and a 0x80000000 0
174ret 0xffc00000
175end
176if eq ax 0x7f800000
177ret a
178end
179n = sub int shr ax 23 127
180m = f64 bitcast f32 uint or and ax 0x7fffff 0x3f800000
181z = div sub m 1.0f64 add m 1.0f64
182zz = mul z z
183t = z
184y = z
185for j ulong 29
186t = mul t zz
187y = add y div t f64 add mul add j 1 2 1
188end
189ret fpftz ulong bitcast uint f32 add f64 n div mul y 2.0f64 0.6931471805599453f64
190end
191fn fprcp a ulong to ulong
192a = fpftz a
193if gt and a 0x7fffffff 0x7f800000
194ret 0x7fc00000
195end
196if eq and a 0x7fffffff 0
197ret or and a 0x80000000 0x7f800000
198end
199x = f64 bitcast f32 uint a
200ret fpftz ulong bitcast uint f32 div 1.0f64 x
201end
202note CDNA3 PDF p197: reciprocal square root, 1ULP and FTZ; preserve the signed-zero examples.
203fn fprsq a ulong to ulong
204a = fpftz a
205if gt and a 0x7fffffff 0x7f800000
206ret or and a 0x80000000 0x7fc00000
207end
208if eq and a 0x7fffffff 0
209ret or and a 0x80000000 0x7f800000
210end
211if ne and a 0x80000000 0
212ret 0xffc00000
213end
214x = f64 bitcast f32 uint a
215ret fpftz ulong bitcast uint f32 div 1.0f64 fsqrt x
216end