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 / seed / ng / tests / ir / intcv-oracle.om

306 lines · 4026 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Build with src/ir.om. Usage: intcv SOURCE-ID DEST-ID CASE, ids u8..u64=2..5, i8..i64=6..9.
3note Emits a minimal IR unit, then does the equivalent native seeded-omc conversion.
4note Exit 0 or trap 5 is the independent expected result for the emitted unit.
5note 4 unsigned widths x 4 signed widths x 8 boundary cases = 128 runs.
6global ic_arg uchar 24
7fn ic_num arg ulong to uint
8n = argread arg ic_arg 0 len ic_arg
9if or le n 0 gt n 3
10exit 2
11end
12v = uint 0
13for j ulong ulong n bound 3
14c = uint get ic_arg j
15if or lt c 48 gt c 57
16exit 2
17end
18v = add mul v 10 sub c 48
19end
20ret v
21end
22fn ic_width t uint to ulong
23if or eq t 2 eq t 6
24ret 8
25end
26if or eq t 3 eq t 7
27ret 16
28end
29if or eq t 4 eq t 8
30ret 32
31end
32ret 64
33end
34fn ic8 x uchar t uint to void
35if eq t 6
36y = byte x
37ret
38end
39if eq t 7
40y = short x
41ret
42end
43if eq t 8
44y = int x
45ret
46end
47y = long x
48end
49fn ic16 x ushort t uint to void
50if eq t 6
51y = byte x
52ret
53end
54if eq t 7
55y = short x
56ret
57end
58if eq t 8
59y = int x
60ret
61end
62y = long x
63end
64fn ic32 x uint t uint to void
65if eq t 6
66y = byte x
67ret
68end
69if eq t 7
70y = short x
71ret
72end
73if eq t 8
74y = int x
75ret
76end
77y = long x
78end
79fn ic64 x ulong t uint to void
80if eq t 6
81y = byte x
82ret
83end
84if eq t 7
85y = short x
86ret
87end
88if eq t 8
89y = int x
90ret
91end
92y = long x
93end
94fn ic_emit s uint t uint v ulong to void
95ir_init
96f = ir_fnew 2 0 8
97b = ir_blk f
98a = ir_res ir_op b 1 s v 0
99o = ir_op b 78 t 5 2
100ir_set o 0 ir_ftok f
101ir_set o 1 a
102tok = ir_otok o
103cv = ir_op b 36 t 0 1
104ir_set cv 0 a
105z = ir_res ir_op b 1 8 0 0
106o = ir_op b 63 0 0 2
107ir_set o 0 tok
108ir_set o 1 z
109n = ir_wr
110if eq ar_out 1 1 0 n 0
111exit 2
112end
113end
114fn ic_umain to int
115if ne argcnt 4
116ret 2
117end
118s = ic_num 1
119t = ic_num 2
120j = ic_num 3
121if or lt s 2 gt s 5
122ret 2
123end
124if or lt t 6 gt t 9
125ret 2
126end
127if gt j 7
128ret 2
129end
130ws = ic_width s
131mx = not ulong 0
132if lt ws 64
133mx = sub shl ulong 1 ws 1
134end
135hi = shl ulong 1 sub ic_width t 1
136v = ulong 0
137if eq j 1
138v = sub hi 1
139end
140if eq j 2
141v = hi
142end
143if eq j 3
144v = add hi 1
145end
146if eq j 4
147v = mx
148end
149if eq j 5
150v = sub mx 1
151end
152if eq j 6
153v = 1
154end
155if eq j 7
156v = shl ulong 1 sub ws 1
157end
158if gt v mx
159v = mx
160end
161ic_emit s t v
162if eq s 2
163ic8 uchar v t
164end
165if eq s 3
166ic16 ushort v t
167end
168if eq s 4
169ic32 uint v t
170end
171if eq s 5
172ic64 v t
173end
174ret 0
175end
176
177fn ic_boundary t uint j ulong to f64
178w = ic_width t
179hi = mul f64 shl ulong 1 sub w 1 2.0f64
180lo = f64 0.0
181if ge t 6
182hi = f64 shl ulong 1 sub w 1
183lo = neg hi
184end
185if eq j 0
186ret sub lo 1.0
187end
188if eq j 1
189ret sub lo 0.5
190end
191if eq j 2
192ret lo
193end
194if eq j 3
195ret add lo 0.5
196end
197if eq j 4
198ret add lo 1.0
199end
200if eq j 5
201ret sub hi 1.0
202end
203if eq j 6
204ret sub hi 0.5
205end
206if eq j 7
207ret hi
208end
209if eq j 8
210ret add hi 0.5
211end
212if eq j 9
213ret add hi 1.0
214end
215note Adjacent representable values below and above the high boundary, including 64-bit widths.
216u = bitcast ulong hi
217if eq j 10
218ret bitcast f64 sub u 1
219end
220if eq j 11
221ret bitcast f64 add u 1
222end
223u = bitcast ulong lo
224if eq j 12
225if eq lo 0.0f64
226ret bitcast f64 ulong 0x8000000000000001
227end
228ret bitcast f64 sub u 1
229end
230if eq j 13
231ret bitcast f64 add u 1
232end
233if eq j 14
234ret -0.5f64
235end
236ret 0.5f64
237end
238note Independent integer decoding of truncation range, not ngrun's float comparisons.
239fn ic_floatbad s uint t uint bits ulong to bool
240p = ulong 52
241bias = ulong 1023
242es = ulong 2047
243sg = ne shr bits 63 0
244if eq s 13
245p = 23
246bias = 127
247es = 255
248sg = ne shr bits 31 0
249end
250e = and shr bits p es
251m = and bits sub shl ulong 1 p 1
252if eq e es
253ret true
254end
255if lt e bias
256ret false
257end
258e = sub e bias
259w = ic_width t
260if lt t 6
261ret or sg ge e w
262end
263if not sg
264ret ge e sub w 1
265end
266if gt e sub w 1
267ret true
268end
269if lt e sub w 1
270ret false
271end
272if ge e p
273ret ne m 0
274end
275ret ne shr m sub p e 0
276end
277fn ic_fmain s uint to int
278t = ic_num 2
279j = ic_num 3
280if or lt t 2 gt t 9
281ret 2
282end
283if gt j 15
284ret 2
285end
286x = ic_boundary t ulong j
287bits = bitcast ulong x
288if eq s 13
289bits = ulong bitcast uint f32 x
290end
291ic_emit s t bits
292if ic_floatbad s t bits
293ret 105
294end
295ret 0
296end
297fn main to int
298if ne argcnt 4
299ret 2
300end
301s = ic_num 1
302if or eq s 13 eq s 14
303ret ic_fmain s
304end
305ret ic_umain
306end