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 / tests / algebra / test_phase_twin.om

620 lines · 13393 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Pure seeded OM-2 phase-domain Z3 digital twin test suite (gates 1-3 + self-checks).
3note Replaces tests/algebra/test_phase_twin.c.
4note Concatenated after src/algebra/oma_lib.om + src/algebra/phase_twin.om.
5
6const Z999 f64 3.2905267314919255
7const Z95 f64 1.959963984540054
8const CONTRACT_P f64 0.000001
9const CONTRACT_ALPHA f64 0.05
10const CONTRACT_RHO_DB f64 25.0
11
12global pt_quick int 1
13global pt_rfd int 1
14global pt_gfail int 1
15global pt_rpath uchar 256
16global pt_rpath_len ulong 1
17
18note tally: [0]=trials, [1]=mismatches, [2]=flagged, [3]=failures
19global pt_tal_u ulong 4
20global pt_tal_maxd f64 1
21
22text t_def_path "phase_twin_receipt.json"
23text t_qflag "--quick"
24text t_pt_pass "PHASE_TWIN PASS (receipt "
25text t_pt_fail "PHASE_TWIN FAIL (receipt "
26text t_pt_end ")\n"
27text t_sc_p "self-checks PASS\n"
28text t_sc_f "self-checks FAIL\n"
29text t_g1_p "gate1 PASS\n"
30text t_g1_f "gate1 FAIL\n"
31text t_g1b_p "gate1b PASS\n"
32text t_g1b_f "gate1b FAIL\n"
33text t_g2_p "gate2 PASS\n"
34text t_g2_f "gate2 FAIL\n"
35text t_g3_p "gate3 PASS\n"
36text t_g3_f "gate3 FAIL\n"
37
38text j_open "{\n \"schema\": \"OMEGA_MIXED_ALGEBRA_PHASE_TWIN_V1\",\n \"provenance\": \"SIMULATED_DEVELOPMENT\",\n \"language\": \"OM-2\",\n \"run_mode\": \""
39text j_mode_full "full"
40text j_mode_quick "quick"
41text j_hdr_end "\",\n"
42text j_sc_k " \"self_checks\": {\"pass\": "
43text j_g1_k "},\n \"gate1_noiseless\": {\"pass\": "
44text j_g1b_k "},\n \"gate1b_reference_operating_point\": {\"pass\": "
45text j_g2_k "},\n \"gate2_snr_curve\": {\"pass\": "
46text j_g3_k "},\n \"gate3_calibration\": {\"pass\": "
47text j_ver_k "},\n \"verified\": "
48text j_ov_p ",\n \"overall\": \"PASS\"\n}\n"
49text j_ov_f ",\n \"overall\": \"FAIL\"\n}\n"
50text j_true "true"
51text j_false "false"
52text j_one "1"
53text j_zero "0"
54text j_comma_chk ", \"checks\": "
55text j_comma_fail ", \"failures\": "
56text j_comma_cal ", \"calibrations\": "
57
58fn rput s view uchar * to void
59 oma_w_all get pt_rfd 0 s len s
60end
61
62fn rbool ok bool to void
63 if ok
64 rput j_true
65 else
66 rput j_false
67 end
68end
69
70fn pt_scale n ulong to ulong
71 if ne get pt_quick 0 0
72 ret div n 50
73 end
74 ret n
75end
76
77fn pt_design_a to void
78 put pt_cf 0 125000000.0f64
79 put pt_ci 0 64
80 put pt_ci 1 4096
81 put pt_cf 1 0.5f64
82 put pt_ci 2 14
83 put pt_ci 3 14
84 put pt_cf 2 0.0f64
85 put pt_cf 3 0.0f64
86 put pt_cf 4 0.0f64
87 put pt_cf 5 0.0f64
88 put pt_cf 6 0.0f64
89 put pt_cf 7 0.0f64
90 put pt_cf 8 div 8192.0f64 125000000.0f64
91 put pt_ci 4 PT_ADD_CORR_PRODUCT
92 put pt_ci 5 0
93 put pt_ci 6 0
94 put pt_ci 7 0
95 put pt_cf 9 0.0f64
96 put pt_cf 10 0.3f64
97 put pt_cf 11 30.0f64
98end
99
100fn pt_design_b to void
101 pt_design_a
102 put pt_cf 0 1000000.0f64
103 put pt_ci 1 1024
104 put pt_ci 2 12
105 put pt_ci 3 12
106 put pt_cf 8 div 2048.0f64 1000000.0f64
107end
108
109fn run_pair_one op int a int b int to void
110 want = new uchar 1 0
111 dist = new f64 1 0.0f64
112 rc = int 0
113 orc = int 0
114 if ne op 0
115 rc = pt_twin_mul_const a b
116 orc = oma_z3_mul a b want
117 else
118 rc = pt_twin_add a b
119 orc = oma_z3_add a b want
120 end
121 if or ne rc PT_OK ne orc OMA_OK
122 put pt_gfail 0 add get pt_gfail 0 1
123 ret
124 end
125 dmy = pt_decode pt_deg2rad get pt_res_ph 0 dist
126 if gt get dist 0 get pt_tal_maxd 0
127 put pt_tal_maxd 0 get dist 0
128 end
129 put pt_tal_u 0 add get pt_tal_u 0 1
130 bad = ulong 0
131 if ne get pt_res_i 0 int get want 0
132 put pt_tal_u 1 add get pt_tal_u 1 1
133 bad = 1
134 end
135 if ne get pt_res_i 3 0
136 put pt_tal_u 2 add get pt_tal_u 2 1
137 bad = 1
138 end
139 put pt_tal_u 3 add get pt_tal_u 3 bad
140end
141
142fn run_pairs seed ulong op int reps ulong to void
143 for i ulong 4
144 put pt_tal_u i 0
145 end
146 put pt_tal_maxd 0 0.0f64
147 if ne pt_twin_init seed PT_OK
148 put pt_gfail 0 add get pt_gfail 0 1
149 ret
150 end
151 for r ulong reps bound 2000
152 for a int 3
153 for b int 3
154 run_pair_one op a b
155 end
156 end
157 end
158end
159
160fn test_sc_rng to bool
161 ra = new ulong 5 0
162 rb = new ulong 5 0
163 spa = new f64 1 0.0f64
164 spb = new f64 1 0.0f64
165 pt_rng_seed ra spa 42
166 pt_rng_seed rb spb 42
167 ok = true
168 s1 = 0.0f64
169 s2 = 0.0f64
170 for i ulong 50000
171 ga = pt_rng_gauss ra spa
172 gb = pt_rng_gauss rb spb
173 if ne ga gb
174 ok = false
175 end
176 s1 = add s1 ga
177 s2 = add s2 mul ga ga
178 end
179 if or ge fabs div s1 50000.0f64 0.02f64 ge fabs sub div s2 50000.0f64 1.0f64 0.03f64
180 ok = false
181 end
182 ret ok
183end
184
185fn test_self_checks to bool
186 ok = true
187 d = new f64 1 0.0f64
188 if or ne pt_decode 0.0f64 d 0 ne get d 0 0.0f64
189 ok = false
190 end
191 if ne pt_decode div PT_TWO_PI 3.0f64 d 1
192 ok = false
193 end
194 if ne pt_decode neg div PT_TWO_PI 3.0f64 d 2
195 ok = false
196 end
197 if eq pt_decode PT_PI d 0
198 ok = false
199 end
200 if or ne pt_decode pt_deg2rad 59.9f64 d 0 ne pt_decode pt_deg2rad 60.1f64 d 1
201 ok = false
202 end
203 w22 = add mul uint 2 PT_NCO_THIRD mul uint 2 PT_NCO_THIRD
204 if ne pt_decode mul PT_TWO_PI div f64 w22 4294967296.0f64 d 1
205 ok = false
206 end
207
208 pt_design_b
209 if ne pt_twin_init 1 PT_OK
210 ok = false
211 end
212 if or ne pt_twin_add 3 0 PT_E_ARG ne pt_twin_add 0 -1 PT_E_ARG
213 ok = false
214 end
215 if ne pt_twin_mul_const 0 3 PT_E_ARG
216 ok = false
217 end
218 put pt_ci 1 100
219 if ne pt_twin_init 1 PT_E_ARG
220 ok = false
221 end
222
223 ok = and ok test_sc_rng
224
225 e1 = pt_ref_add_exact 10.0f64 360
226 e2 = pt_ref_add_exact 10.0f64 720
227 if ge div fabs sub e1 e2 e2 0.005f64
228 ok = false
229 end
230 if ge fabs sub pt_ref_single_craig 1.0f64 0.1834f64 0.0005f64
231 ok = false
232 end
233 wi = new f64 2 0.0f64
234 pt_wilson 0 4000000 Z95 wi
235 if or ne get wi 0 0.0f64 ge get wi 1 0.000001f64
236 ok = false
237 end
238
239 if ok
240 oma_put_str t_sc_p
241 else
242 oma_put_str t_sc_f
243 end
244 rput j_sc_k
245 rbool ok
246 ret ok
247end
248
249fn test_gate1 to bool
250 ok = true
251 reps = ulong 200
252 if ne get pt_quick 0 0
253 reps = 20
254 end
255 tot_chk = ulong 0
256 tot_fail = ulong 0
257 seed = ulong 0x5a31
258 for d ulong 3
259 for m ulong 3
260 if eq d 0
261 pt_design_a
262 else
263 pt_design_b
264 if eq d 2
265 put pt_ci 2 0
266 put pt_ci 3 0
267 end
268 end
269 put pt_ci 4 add int m 1
270 run_pairs seed 0 reps
271 seed = add seed 1
272 tot_chk = add tot_chk get pt_tal_u 0
273 tot_fail = add tot_fail get pt_tal_u 3
274 if or ne get pt_tal_u 0 mul 9 reps ne get pt_tal_u 3 0
275 ok = false
276 end
277 end
278 end
279
280 pt_design_b
281 put pt_ci 5 1
282 put pt_ci 6 1000
283 put pt_ci 7 1
284 put pt_cf 9 5.0f64
285 run_pairs 0x5a40 1 reps
286 tot_chk = add tot_chk get pt_tal_u 0
287 tot_fail = add tot_fail get pt_tal_u 3
288 if or ne get pt_tal_u 0 mul 9 reps ne get pt_tal_u 3 0
289 ok = false
290 end
291
292 if ok
293 oma_put_str t_g1_p
294 else
295 oma_put_str t_g1_f
296 end
297 rput j_g1_k
298 rbool ok
299 rput j_comma_chk
300 oma_put_u64 get pt_rfd 0 tot_chk
301 rput j_comma_fail
302 oma_put_u64 get pt_rfd 0 tot_fail
303 ret ok
304end
305
306fn test_gate1b to bool
307 pt_design_a
308 put pt_cf 2 div get pt_cf 1 fsqrt 200000000.0f64
309 put pt_cf 3 mul mul 360.0f64 div get pt_cf 0 64.0f64 0.000000000001f64
310 put pt_cf 5 3.35f64
311 put pt_ci 5 1
312 put pt_ci 6 1000
313 put pt_ci 7 8
314 put pt_cf 9 5.0f64
315 reps = ulong 200
316 if ne get pt_quick 0 0
317 reps = 20
318 end
319 run_pairs 0x1b00 0 reps
320 ok = and eq get pt_tal_u 0 mul 9 reps eq get pt_tal_u 3 0
321 if ok
322 oma_put_str t_g1b_p
323 else
324 oma_put_str t_g1b_f
325 end
326 rput j_g1b_k
327 rbool ok
328 rput j_comma_chk
329 oma_put_u64 get pt_rfd 0 get pt_tal_u 0
330 rput j_comma_fail
331 oma_put_u64 get pt_rfd 0 get pt_tal_u 3
332 ret ok
333end
334
335fn g2_run_trials tr ulong errs edit ulong 2 to void
336 err = ulong 0
337 err_s = ulong 0
338 want = new uchar 1 0
339 for t ulong tr bound 500000
340 a = int rem t 3
341 b = int rem div t 3 3
342 rc = pt_twin_add a b
343 orc = oma_z3_add a b want
344 if ne get pt_res_i 0 int get want 0
345 err = add err 1
346 end
347 if ne get pt_res_i 1 a
348 err_s = add err_s 1
349 end
350 end
351 put errs 0 err
352 put errs 1 err_s
353end
354
355fn g2_point idx ulong rho_db f64 tr ulong note_add f64 note_s f64 to bool
356 rho = pt_pow10 div rho_db 10.0f64
357 pt_design_b
358 put pt_ci 1 64
359 put pt_ci 2 0
360 put pt_ci 3 0
361 put pt_cf 10 0.0f64
362 put pt_cf 11 0.0f64
363 put pt_cf 8 0.000128f64
364 put pt_cf 2 pt_sigma_for_rho get pt_cf 1 64 rho
365 if ne pt_twin_init add 0x2000 idx PT_OK
366 ret false
367 end
368 errs = new ulong 2 0
369 g2_run_trials tr errs
370 exact = pt_ref_add_exact rho 720
371 craig = pt_ref_single_craig rho
372 w = new f64 2 0.0f64
373 pt_wilson get errs 0 tr Z999 w
374 if or lt exact get w 0 gt exact get w 1
375 ret false
376 end
377 if ge rho_db 9.0f64
378 if gt div fabs sub note_add exact exact 0.05f64
379 ret false
380 end
381 end
382 if ge mul craig f64 tr 100.0f64
383 pt_wilson get errs 1 tr Z999 w
384 if or lt craig get w 0 gt craig get w 1
385 ret false
386 end
387 end
388 if gt div fabs sub craig note_s note_s 0.01f64
389 ret false
390 end
391 ret true
392end
393
394fn test_gate2 to bool
395 ok = true
396 ok = and ok g2_point 0 0.0f64 pt_scale 50000 0.352f64 0.1834f64
397 ok = and ok g2_point 1 6.0f64 pt_scale 100000 0.0576f64 0.01371f64
398 ok = and ok g2_point 2 8.0f64 pt_scale 150000 0.0152f64 0.002037f64
399 ok = and ok g2_point 3 10.0f64 pt_scale 200000 0.00196f64 0.0001065f64
400 ok = and ok g2_point 4 12.0f64 pt_scale 400000 0.0000846f64 0.000001082f64
401
402 pt_design_b
403 put pt_ci 1 64
404 put pt_ci 2 0
405 put pt_ci 3 0
406 put pt_cf 10 0.0f64
407 put pt_cf 11 0.0f64
408 put pt_cf 8 0.000128f64
409 put pt_cf 3 15.0f64
410 put pt_cf 2 0.0f64
411 jt = pt_scale 200000
412 jerr = ulong 0
413 want = new uchar 1 0
414 jw = new f64 2 0.0f64
415 if ne pt_twin_init 0x2100 PT_OK
416 ok = false
417 else
418 for t ulong jt bound 300000
419 a = int rem t 3
420 b = int rem div t 3 3
421 rc = pt_twin_add a b
422 orc = oma_z3_add a b want
423 if ne get pt_res_i 0 int get want 0
424 jerr = add jerr 1
425 end
426 end
427 jref = pt_ref_add_jitter 15.0f64
428 pt_wilson jerr jt Z999 jw
429 if or lt jref get jw 0 gt jref get jw 1
430 ok = false
431 end
432 end
433
434 if ok
435 oma_put_str t_g2_p
436 else
437 oma_put_str t_g2_f
438 end
439 rput j_g2_k
440 rbool ok
441 ret ok
442end
443
444fn pt_contract_point to void
445 pt_design_b
446 put pt_ci 1 64
447 put pt_cf 8 0.000128f64
448 put pt_cf 2 pt_sigma_for_rho get pt_cf 1 64 pt_pow10 div CONTRACT_RHO_DB 10.0f64
449 put pt_cf 3 1.0f64
450 put pt_ci 6 1000
451 put pt_ci 7 32
452 put pt_cf 9 5.0f64
453end
454
455fn g3_step a int b int to ulong
456 want = new uchar 1 0
457 rc = pt_twin_add a b
458 orc = oma_z3_add a b want
459 if ne get pt_res_i 0 int get want 0
460 ret 1
461 end
462 if ne get pt_res_i 3 0
463 ret 1
464 end
465 ret 0
466end
467
468fn g3_run_case seed ulong tr ulong to ulong
469 if ne pt_twin_init seed PT_OK
470 put pt_gfail 0 add get pt_gfail 0 1
471 ret 1
472 end
473 in_st = new ulong 5 0
474 in_sp = new f64 1 0.0f64
475 pt_rng_seed in_st in_sp xor seed 0xc0ffee
476 fails = ulong 0
477 for i ulong tr bound 600000
478 x = pt_rng_next in_st
479 a = int rem x 3
480 b = int rem shr x 32 3
481 fails = add fails g3_step a b
482 end
483 ret fails
484end
485
486fn g3_check_pair cid ulong seed_base ulong uncal_n ulong cal_n ulong to bool
487 off0 = 0.0f64
488 off1 = 0.0f64
489 dr0 = 0.0f64
490 dr1 = 0.0f64
491 if ne cid 1
492 off0 = 50.0f64
493 off1 = 20.0f64
494 end
495 if ne cid 0
496 dr0 = 2.0f64
497 dr1 = 1.5f64
498 end
499 w = new f64 2 0.0f64
500 pt_contract_point
501 put pt_cf 4 off0
502 put pt_cf 5 off1
503 put pt_cf 6 dr0
504 put pt_cf 7 dr1
505 put pt_ci 5 0
506 fu = g3_run_case seed_base uncal_n
507 pt_wilson fu uncal_n Z95 w
508 ok_u = gt get w 0 CONTRACT_P
509
510 pt_contract_point
511 put pt_cf 4 off0
512 put pt_cf 5 off1
513 put pt_cf 6 dr0
514 put pt_cf 7 dr1
515 put pt_ci 5 1
516 fc = g3_run_case add seed_base 1 cal_n
517 ok_c = eq fc 0
518 ret and ok_u ok_c
519end
520
521fn test_gate3 to bool
522 ok = true
523 ok = and ok g3_check_pair 0 0x3000 pt_scale 50000 pt_scale 50000
524 ok = and ok g3_check_pair 1 0x3002 300000 pt_scale 100000
525 ok = and ok g3_check_pair 2 0x3004 pt_scale 50000 pt_scale 200000
526 if ok
527 oma_put_str t_g3_p
528 else
529 oma_put_str t_g3_f
530 end
531 rput j_g3_k
532 rbool ok
533 rput j_comma_cal
534 oma_put_u64 get pt_rfd 0 get pt_st_u 5
535 ret ok
536end
537
538fn parse_args to void
539 put pt_quick 0 0
540 for i ulong len t_def_path bound 64
541 put pt_rpath i get t_def_path i
542 end
543 put pt_rpath_len 0 len t_def_path
544 ac = int argcnt
545 buf = new uchar 256 0
546 i = int 1
547 while lt i ac bound 32
548 k = argread ulong i buf 0 256
549 if gt k 0
550 uk = ulong k
551 is_q = eq uk len t_qflag
552 if is_q
553 for j ulong uk bound 16
554 if ne get buf j get t_qflag j
555 is_q = false
556 end
557 end
558 end
559 if is_q
560 put pt_quick 0 1
561 else
562 for j ulong uk bound 256
563 put pt_rpath j get buf j
564 end
565 put pt_rpath_len 0 uk
566 end
567 end
568 i = add i 1
569 end
570end
571
572fn main to int
573 parse_args
574 pbuf = new uchar 256 0
575 plen = get pt_rpath_len 0
576 for i ulong plen bound 256
577 put pbuf i get pt_rpath i
578 end
579 fd = fopen pbuf 0 plen 577 420
580 if lt fd 0
581 ret 2
582 end
583 put pt_rfd 0 int fd
584 rput j_open
585 if ne get pt_quick 0 0
586 rput j_mode_quick
587 else
588 rput j_mode_full
589 end
590 rput j_hdr_end
591
592 s = test_self_checks
593 g1 = test_gate1
594 g1b = test_gate1b
595 g2 = test_gate2
596 g3 = test_gate3
597 all_ok = and and and s g1 and g1b g2 and g3 eq get pt_gfail 0 0
598
599 rput j_ver_k
600 if all_ok
601 rput j_one
602 rput j_ov_p
603 else
604 rput j_zero
605 rput j_ov_f
606 end
607 rc_c = fclose int fd
608
609 if all_ok
610 oma_put_str t_pt_pass
611 else
612 oma_put_str t_pt_fail
613 end
614 oma_w_all 1 pbuf plen
615 oma_put_str t_pt_end
616 if all_ok
617 ret 0
618 end
619 ret 1
620end