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 / float-oracle.om

724 lines · 12315 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Build: cat seed/ng/src/ir.om seed/ng/tests/ir/float-oracle.om | seed/out/omc
3note Native seeded-omc computations produce the expected bits in a checked IR unit.
4note Neither ngrun nor any rn_ function is linked here. The only shared unit is the IR writer.
5note RTZ integer oracle uses native nearest conversion then native convert-back comparison,
6note independent of ngrun's leading-bit integer encoder. 1024 splitmix64 values per float width.
7global fo_s uint 4
8fn fo_const t uint x ulong to uint
9ret ir_res ir_op get fo_s 0 1 t x 0
10end
11fn fo_un op uint t uint im ulong a uint to uint
12o = ir_op get fo_s 0 op t im 1
13ir_set o 0 a
14ret ir_res o
15end
16fn fo_expect t uint v uint bits ulong to void
17it = uint 5
18if or eq t 13 eq t 4
19it = 4
20end
21a = v
22if or eq t 13 eq t 14
23a = fo_un 37 it 0 v
24end
25b = fo_const it bits
26o = ir_op get fo_s 0 30 1 0 2
27ir_set o 0 a
28ir_set o 1 b
29c = ir_res o
30q = ir_op get fo_s 0 82 0 14 2
31ir_set q 0 get fo_s 1
32ir_set q 1 c
33put fo_s 1 ir_otok q
34put fo_s 2 add get fo_s 2 1
35end
36fn fo_bcast t uint bits ulong to void
37it = uint 5
38if eq t 13
39it = 4
40end
41a = fo_const it bits
42b = fo_un 37 t 0 a
43c = fo_un 37 it 0 b
44expect = bitcast ulong bitcast f64 bits
45if eq t 13
46expect = ulong bitcast uint bitcast f32 uint bits
47end
48fo_expect it c expect
49end
50fn fo_special t uint j ulong to ulong
51if eq t 13
52if eq j 0
53ret 0x00000000
54end
55if eq j 1
56ret 0x80000000
57end
58if eq j 2
59ret 0x7f800000
60end
61if eq j 3
62ret 0xff800000
63end
64if eq j 4
65ret 0x7fc00000
66end
67if eq j 5
68ret 0xffc00000
69end
70if eq j 6
71ret 0x7f800001
72end
73if eq j 7
74ret 0xff800001
75end
76if eq j 8
77ret 0x7fffffff
78end
79if eq j 9
80ret 0xffffffff
81end
82if eq j 10
83ret 0x00000001
84end
85if eq j 11
86ret 0x80000001
87end
88if eq j 12
89ret 0x007fffff
90end
91if eq j 13
92ret 0x807fffff
93end
94if eq j 14
95ret 0x00800000
96end
97if eq j 15
98ret 0x80800000
99end
100if eq j 16
101ret 0x7f7fffff
102end
103ret 0xff7fffff
104end
105if eq j 0
106ret 0x0000000000000000
107end
108if eq j 1
109ret 0x8000000000000000
110end
111if eq j 2
112ret 0x7ff0000000000000
113end
114if eq j 3
115ret 0xfff0000000000000
116end
117if eq j 4
118ret 0x7ff8000000000000
119end
120if eq j 5
121ret 0xfff8000000000000
122end
123if eq j 6
124ret 0x7ff0000000000001
125end
126if eq j 7
127ret 0xfff0000000000001
128end
129if eq j 8
130ret 0x7fffffffffffffff
131end
132if eq j 9
133ret 0xffffffffffffffff
134end
135if eq j 10
136ret 0x0000000000000001
137end
138if eq j 11
139ret 0x8000000000000001
140end
141if eq j 12
142ret 0x000fffffffffffff
143end
144if eq j 13
145ret 0x800fffffffffffff
146end
147if eq j 14
148ret 0x0010000000000000
149end
150if eq j 15
151ret 0x8010000000000000
152end
153if eq j 16
154ret 0x7fefffffffffffff
155end
156ret 0xffefffffffffffff
157end
158fn fo_mix state ulong to ulong
159z = add state 0x9e3779b97f4a7c15
160z = mul xor z shr z 30 0xbf58476d1ce4e5b9
161z = mul xor z shr z 27 0x94d049bb133111eb
162ret xor z shr z 31
163end
164fn fo_f2i t uint x f64 to ulong
165if eq t 2
166ret ulong uchar x
167end
168if eq t 3
169ret ulong ushort x
170end
171if eq t 4
172ret ulong uint x
173end
174if eq t 5
175ret ulong x
176end
177if eq t 6
178ret bitcast ulong long byte x
179end
180if eq t 7
181ret bitcast ulong long short x
182end
183if eq t 8
184ret bitcast ulong long int x
185end
186ret bitcast ulong long x
187end
188fn fo_intcase t uint s uint bits ulong to void
189x = bitcast f64 bits
190if eq s 13
191x = f64 bitcast f32 uint bits
192end
193a = fo_const s bits
194v = fo_un 36 t 0 a
195expected = fo_f2i t x
196note Compare signed integers as u64 raw bits, with canonical sign extension.
197u = v
198if eq t 9
199u = fo_un 37 5 0 v
200end
201if or eq t 2 eq t 6
202u = fo_un 36 5 0 v
203end
204if or eq t 3 eq t 7
205u = fo_un 36 5 0 v
206end
207if or eq t 4 eq t 8
208u = fo_un 36 5 0 v
209end
210if eq t 5
211u = v
212end
213fo_expect 5 u expected
214end
215fn fo_round t uint mag ulong z bool to ulong
216limit = mul f64 shl ulong 1 63 2.0f64
217if eq t 14
218v = f64 mag
219b = bitcast ulong v
220if z
221if or ge v limit gt ulong v mag
222b = sub b 1
223end
224end
225ret b
226end
227v = f32 mag
228b = ulong bitcast uint v
229if z
230if or ge f64 v limit gt ulong v mag
231b = sub b 1
232end
233end
234ret b
235end
236fn fo_i2f t uint s uint bits ulong im ulong to void
237mag = bits
238sg = ulong 0
239if ge s 6
240if lt bitcast long bits 0
241mag = sub ulong 0 bits
242sg = 0x8000000000000000
243if eq t 13
244sg = 0x80000000
245end
246end
247end
248expected = or sg fo_round t mag eq and im 3 1
249a = fo_const s bits
250v = fo_un 36 t im a
251fo_expect t v expected
252end
253note Independent IEEE binary64 to binary32 toward-zero encoder (review F3).
254note Uses only sign/exponent/significand integers, not a nearest float or a predecessor correction.
255fn fo_narrowz bits ulong to ulong
256sg = and shr bits 32 0x80000000
257ex = and shr bits 52 2047
258m = and bits 0x000fffffffffffff
259if eq ex 2047
260if eq m 0
261ret or sg 0x7f800000
262end
263ret or sg 0x7fc00000
264end
265if eq ex 0
266ret sg
267end
268e = sub long ex 1023
269if gt e 127
270ret or sg 0x7f7fffff
271end
272if lt e -149
273ret sg
274end
275m = or m 0x0010000000000000
276if ge e -126
277frac = and shr m 29 0x7fffff
278expo = shl ulong add e 127 23
279ret or sg or expo frac
280end
281shift = ulong sub neg e 97
282ret or sg shr m shift
283end
284fn fo_narrow bits ulong im ulong to void
285x = bitcast f64 bits
286y = f32 x
287if eq and im 3 1
288y = bitcast f32 uint fo_narrowz bits
289end
290if ne and im 4 0
291if and eq fabs f64 y div 1.0f64 0.0f64 lt fabs x div 1.0f64 0.0f64
292m = bitcast f32 uint 0x7f7fffff
293y = m
294if lt x 0.0f64
295y = neg m
296end
297end
298end
299note NaN conversion payload is not specified. Compare the resulting classification only.
300a = fo_const 14 bits
301v = fo_un 36 13 im a
302if ne x x
303fo_nancheck v
304else
305fo_expect 13 v ulong bitcast uint y
306end
307end
308fn fo_width t uint to ulong
309if or eq t 2 eq t 6
310ret 8
311end
312if or eq t 3 eq t 7
313ret 16
314end
315if or eq t 4 eq t 8
316ret 32
317end
318ret 64
319end
320fn fo_boundary t uint j ulong to f64
321w = fo_width t
322hi = mul f64 shl ulong 1 sub w 1 2.0f64
323lo = f64 0.0
324if ge t 6
325hi = f64 shl ulong 1 sub w 1
326lo = neg hi
327end
328if eq j 0
329ret sub lo 1.0
330end
331if eq j 1
332ret sub lo 0.5
333end
334if eq j 2
335ret lo
336end
337if eq j 3
338ret add lo 0.5
339end
340if eq j 4
341ret add lo 1.0
342end
343if eq j 5
344ret sub hi 1.0
345end
346if eq j 6
347ret sub hi 0.5
348end
349if eq j 7
350ret hi
351end
352if eq j 8
353ret add hi 0.5
354end
355if eq j 9
356ret add hi 1.0
357end
358note Adjacent representable values below and above the high boundary, including 64-bit widths.
359u = bitcast ulong hi
360if eq j 10
361ret bitcast f64 sub u 1
362end
363if eq j 11
364ret bitcast f64 add u 1
365end
366u = bitcast ulong lo
367if eq j 12
368if eq lo 0.0f64
369ret bitcast f64 ulong 0x8000000000000001
370end
371ret bitcast f64 sub u 1
372end
373if eq j 13
374ret bitcast f64 add u 1
375end
376if eq j 14
377ret -0.5f64
378end
379ret 0.5f64
380end
381note IR min/max follow OMEGESE fmin/fmax (FMINNM/FMAXNM): native seeded omc is the reference, unguarded.
382fn fo_mmbits t uint op uint a ulong b ulong to ulong
383if eq t 13
384x = bitcast f32 uint a
385y = bitcast f32 uint b
386if eq op 24
387ret ulong bitcast uint fmin x y
388end
389ret ulong bitcast uint fmax x y
390end
391x = bitcast f64 a
392y = bitcast f64 b
393if eq op 24
394ret bitcast ulong fmin x y
395end
396ret bitcast ulong fmax x y
397end
398note the contract set {qNaN, sNaN, +0, -0, +inf, -inf, 1.0, smallest denormal}, every ordered pair
399fn fo_mmval t uint j ulong to ulong
400if eq t 13
401if eq j 0
402ret 0x7fc00000
403end
404if eq j 1
405ret 0x7f800001
406end
407if eq j 2
408ret 0
409end
410if eq j 3
411ret 0x80000000
412end
413if eq j 4
414ret 0x7f800000
415end
416if eq j 5
417ret 0xff800000
418end
419if eq j 6
420ret 0x3f800000
421end
422ret 1
423end
424if eq j 0
425ret 0x7ff8000000000000
426end
427if eq j 1
428ret 0x7ff0000000000001
429end
430if eq j 2
431ret 0
432end
433if eq j 3
434ret 0x8000000000000000
435end
436if eq j 4
437ret 0x7ff0000000000000
438end
439if eq j 5
440ret 0xfff0000000000000
441end
442if eq j 6
443ret 0x3ff0000000000000
444end
445ret 1
446end
447fn fo_mmset t uint to void
448for i ulong 8
449for j ulong 8
450a = fo_mmval t i
451b = fo_mmval t j
452fo_mmcase t 24 a b
453fo_mmcase t 25 a b
454end
455end
456end
457fn fo_nancheck v uint to void
458o = ir_op get fo_s 0 31 1 0 2
459ir_set o 0 v
460ir_set o 1 v
461q = ir_op get fo_s 0 82 0 14 2
462ir_set q 0 get fo_s 1
463ir_set q 1 ir_res o
464put fo_s 1 ir_otok q
465put fo_s 2 add get fo_s 2 1
466end
467fn fo_quietcheck t uint v uint to void
468it = uint 5
469quiet = ulong 0x0008000000000000
470if eq t 13
471it = 4
472quiet = 0x00400000
473end
474b = fo_un 37 it 0 v
475mask = fo_const it quiet
476o = ir_op get fo_s 0 16 it 0 2
477ir_set o 0 b
478ir_set o 1 mask
479fo_expect it ir_res o quiet
480end
481fn fo_mmexpect t uint v uint expected ulong to void
482x = bitcast f64 expected
483if eq t 13
484x = f64 bitcast f32 uint expected
485end
486if ne x x
487fo_nancheck v
488fo_quietcheck t v
489else
490fo_expect t v expected
491end
492end
493fn fo_mmcase t uint op uint a ulong b ulong to void
494expected = fo_mmbits t op a b
495va = fo_const t a
496vb = fo_const t b
497o = ir_op get fo_s 0 op t 0 2
498ir_set o 0 va
499ir_set o 1 vb
500fo_mmexpect t ir_res o expected
501end
502fn fo_mmmatrix t uint to void
503for i ulong 18
504for j ulong 18
505a = fo_special t i
506b = fo_special t j
507fo_mmcase t 24 a b
508fo_mmcase t 25 a b
509end
510end
511end
512fn fo_fmexp t uint a ulong b ulong c ulong fused bool to ulong
513if eq t 13
514x = bitcast f32 uint a
515y = bitcast f32 uint b
516z = bitcast f32 uint c
517if fused
518ret ulong bitcast uint fma x y z
519end
520p = mul x y
521ret ulong bitcast uint add p z
522end
523x = bitcast f64 a
524y = bitcast f64 b
525z = bitcast f64 c
526if fused
527ret bitcast ulong fma x y z
528end
529p = mul x y
530ret bitcast ulong add p z
531end
532fn fo_fmacase t uint a ulong b ulong c ulong to void
533expected = fo_fmexp t a b c true
534unfused = fo_fmexp t a b c false
535if ne expected unfused
536x = bitcast f64 expected
537y = bitcast f64 unfused
538if eq t 13
539x = f64 bitcast f32 uint expected
540y = f64 bitcast f32 uint unfused
541end
542if and eq x x eq y y
543put fo_s 3 add get fo_s 3 1
544end
545end
546va = fo_const t a
547vb = fo_const t b
548vc = fo_const t c
549o = ir_op get fo_s 0 46 t 0 3
550ir_set o 0 va
551ir_set o 1 vb
552ir_set o 2 vc
553fo_expect t ir_res o expected
554end
555global fo_num uchar 24
556text fo_msg0 "FLOATORACLE checks="
557text fo_msg1 " fused_diff="
558text fo_msg2 "\n"
559fn fo_print n ulong to void
560x = n
561i = ulong 24
562while true bound 24
563i = sub i 1
564put fo_num i uchar add 48 rem x 10
565x = div x 10
566if eq x 0
567k = write 2 fo_num i sub 24 i
568ret
569end
570end
571end
572fn main to int
573ir_init
574f = ir_fnew 2 0 8
575put fo_s 0 ir_blk f
576put fo_s 1 ir_ftok f
577for k ulong 2
578s = uint 13
579if eq k 1
580s = 14
581end
582state = ulong 0x6e6772756e
583for j ulong 1042
584bits = fo_mix state
585state = bits
586if eq s 13
587bits = and bits 0xffffffff
588end
589if lt j 18
590bits = fo_special s j
591end
592fo_bcast s bits
593fb = fo_mix bits
594fc = fo_mix fb
595if lt j 18
596fb = 0x3ff0000000000000
597fc = 0
598if eq s 13
599fb = 0x3f800000
600end
601end
602if eq s 13
603fb = and fb 0xffffffff
604fc = and fc 0xffffffff
605end
606fo_fmacase s bits fb fc
607fo_mmcase s 24 bits fb
608fo_mmcase s 25 bits fb
609for d uint 8
610fo_intcase add d 2 s bits
611end
612if eq s 14
613for m ulong 4
614im = m
615if ge m 2
616im = add m 2
617end
618fo_narrow bits im
619end
620end
621end
622end
623fo_mmmatrix 13
624fo_mmmatrix 14
625fo_mmset 13
626fo_mmset 14
627fo_fmacase 13 0x3f800001 0x3f7ffffe 0xbf800000
628fo_fmacase 14 0x3ff0000000000001 0x3feffffffffffffe 0xbff0000000000000
629note Exact witnesses (Wolfram, bay 7 review of 26f67fbb): (1+2^-23)(1-2^-23)-1 = -2^-46 is 0xa8800000 and
630note (1+2^-52)(1-2^-52)-1 = -2^-104 is 0xb970000000000000. 0x3f7ff001 * 0x33800800 + 1 = 1 + 2^-24 + 2^-60:
631note fused f32 gives 0x3f800001; unfused, and f64 fma then narrowing to f32 (double rounding), give 0x3f800000.
632note Native omc must reproduce the exact values, so its fma is not taken on trust; exit 3 otherwise.
633fo_fmacase 13 0x3f7ff001 0x33800800 0x3f800000
634if ne fo_fmexp 13 0x3f800001 0x3f7ffffe 0xbf800000 true 0xa8800000
635ret 3
636end
637if ne fo_fmexp 14 0x3ff0000000000001 0x3feffffffffffffe 0xbff0000000000000 true 0xb970000000000000
638ret 3
639end
640if ne fo_fmexp 13 0x3f7ff001 0x33800800 0x3f800000 true 0x3f800001
641ret 3
642end
643if ne fo_fmexp 13 0x3f7ff001 0x33800800 0x3f800000 false 0x3f800000
644ret 3
645end
646for d uint 8
647t = add d 2
648for j ulong 16
649x = fo_boundary t j
650fo_intcase t 14 bitcast ulong x
651fo_intcase t 13 ulong bitcast uint f32 x
652end
653w = fo_width t
654mx = sub ulong 0 1
655if lt w 64
656mx = sub shl ulong 1 w 1
657end
658if ge t 6
659mx = sub shl ulong 1 sub w 1 1
660end
661for j ulong 6
662v = mx
663if eq j 1
664v = sub mx 1
665end
666if eq j 2
667v = 0
668end
669if eq j 3
670v = 1
671end
672if ge j 4
673if ge t 6
674v = sub ulong 0 shl ulong 1 sub w 1
675if eq j 5
676v = add v 1
677end
678end
679end
680for k uint 2
681ft = add k 13
682for m ulong 4
683im = m
684if ge m 2
685im = add m 2
686end
687fo_i2f ft t v im
688end
689end
690end
691end
692note Precision boundaries across every possible u64 exponent, both float widths and modes.
693for e ulong 64
694p = shl ulong 1 e
695for j ulong 3
696v = p
697if eq j 1
698v = sub p 1
699end
700if eq j 2
701v = add p 1
702end
703for k uint 2
704for m ulong 2
705fo_i2f add k 13 5 v m
706end
707end
708end
709end
710zero = fo_const 8 0
711o = ir_op get fo_s 0 63 0 0 2
712ir_set o 0 get fo_s 1
713ir_set o 1 zero
714k = write 2 fo_msg0 0 len fo_msg0
715fo_print ulong get fo_s 2
716k = write 2 fo_msg1 0 len fo_msg1
717fo_print ulong get fo_s 3
718k = write 2 fo_msg2 0 len fo_msg2
719n = ir_wr
720if eq ar_out 1 1 0 n 0
721ret 1
722end
723ret 0
724end