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 / tools / exp445 / examples_expected.om

620 lines · 10623 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3note
4note tools/exp445/examples_expected.om -- pure seeded OM-2 computation of expected results
5note for exp445 worked examples. Replaces tools/exp445/examples_expected.c.
6om2
7
8text t_usage "usage: examples_expected <entry> < inputs\n"
9text t_badline "examples_expected: bad input line\n"
10text t_unk "examples_expected: unknown example "
11text t_nl "\n"
12text t_0 "0\n"
13text t_1 "1\n"
14text t_trap1 "TRAP 1\n"
15text t_trap2 "TRAP 2\n"
16text t_trap3 "TRAP 3\n"
17text t_trap5 "TRAP 5\n"
18
19text t_clamp_sub "clamp_sub"
20text t_hash_step "hash_step"
21text t_sum_squares3 "sum_squares3"
22text t_nibble_mirror "nibble_mirror"
23text t_halve_or_dec_steps "halve_or_dec_steps"
24text t_count_digits "count_digits"
25text t_sum_range "sum_range"
26text t_cube_xor_pair "cube_xor_pair"
27text t_count_above "count_above"
28text t_dot3 "dot3"
29text t_power_mod "power_mod"
30text t_narrow_sum "narrow_sum"
31text t_rotl8 "rotl8"
32text t_has_digit "has_digit"
33text t_tri_kind "tri_kind"
34
35global g_in uchar *
36global g_out uchar *
37note g_p: [0]=inlen [1]=inpos [2]=outlen [3]=arglen
38global g_p ulong 4
39global g_arg uchar 256
40global g_s long 6
41global g_u ulong 6
42
43fn out_push c uchar to void
44 n = get g_p 2
45 if ge n len g_out
46 nc = mul n 2
47 if lt nc 4096
48 nc = 4096
49 end
50 r = enlarge g_out nc
51 if lt r 0
52 exit 2
53 end
54 end
55 put g_out n c
56 put g_p 2 add n 1
57end
58
59fn out_str s view uchar * to void
60 i = ulong 0
61 n = len s
62 while lt i n
63 out_push get s i
64 i = add i 1
65 end
66end
67
68fn out_u64 v ulong to void
69 tmp = new uchar 24 0
70 if eq v 0
71 out_push 48
72 ret
73 end
74 k = ulong 0
75 while gt v 0
76 d = rem v 10
77 put tmp k uchar add 48 d
78 v = div v 10
79 k = add k 1
80 end
81 while gt k 0
82 k = sub k 1
83 out_push get tmp k
84 end
85end
86
87fn i64_to_u64 v long to ulong
88 if ge v 0
89 ret ulong v
90 end
91 if eq v sub neg long 9223372036854775807 1
92 ret 9223372036854775808
93 end
94 ret sub ulong 0 ulong neg v
95end
96
97fn u64_to_i64 u ulong to long
98 if le u 9223372036854775807
99 ret long u
100 end
101 if eq u 9223372036854775808
102 ret sub neg long 9223372036854775807 1
103 end
104 ret neg long sub ulong 0 u
105end
106
107fn out_i64 v long to void
108 if lt v 0
109 out_push 45
110 if eq v sub neg long 9223372036854775807 1
111 out_u64 9223372036854775808
112 else
113 out_u64 ulong neg v
114 end
115 ret
116 end
117 out_u64 ulong v
118end
119
120fn out_flush to void
121 n = get g_p 2
122 off = ulong 0
123 while lt off n
124 w = write 1 g_out off sub n off
125 if le w 0
126 exit 2
127 end
128 off = add off ulong w
129 end
130 put g_p 2 0
131end
132
133fn err_str s view uchar * to void
134 w = write 2 s 0 len s
135 if lt w 0
136 ret
137 end
138end
139
140fn read_all_stdin to void
141 r = enlarge g_in 4096
142 if lt r 0
143 exit 2
144 end
145 n = ulong 0
146 while true
147 cap = len g_in
148 if eq n cap
149 r2 = enlarge g_in mul cap 2
150 if lt r2 0
151 exit 2
152 end
153 cap = len g_in
154 end
155 got = read 0 g_in n sub cap n
156 if lt got 0
157 exit 2
158 end
159 if eq got 0
160 break
161 end
162 n = add n ulong got
163 end
164 put g_p 0 n
165 put g_p 1 0
166end
167
168fn arg_eq s view uchar * to bool
169 n = get g_p 3
170 if ne n len s
171 ret false
172 end
173 i = ulong 0
174 while lt i n
175 if ne get g_arg i get s i
176 ret false
177 end
178 i = add i 1
179 end
180 ret true
181end
182
183fn parse_one_int idx ulong pos ulong lend ulong to ulong
184 p = pos
185 while lt p lend
186 if ne get g_in p 32
187 break
188 end
189 p = add p 1
190 end
191 neg_ = false
192 if lt p lend
193 if eq get g_in p 45
194 neg_ = true
195 p = add p 1
196 end
197 end
198 acc = ulong 0
199 ndig = ulong 0
200 while lt p lend
201 c = get g_in p
202 if or lt c 48 gt c 57
203 break
204 end
205 d = ulong sub c 48
206 acc = add mul acc 10 d
207 ndig = add ndig 1
208 p = add p 1
209 end
210 if eq ndig 0
211 err_str t_badline
212 exit 2
213 end
214 if neg_
215 uv = sub ulong 0 acc
216 put g_u idx uv
217 put g_s idx u64_to_i64 uv
218 else
219 put g_u idx acc
220 put g_s idx u64_to_i64 acc
221 end
222 ret p
223end
224
225fn next_row n ulong to bool
226 pos = get g_p 1
227 inlen = get g_p 0
228 if ge pos inlen
229 ret false
230 end
231 lend = pos
232 while lt lend inlen
233 if eq get g_in lend 10
234 break
235 end
236 lend = add lend 1
237 end
238 nextp = lend
239 if lt lend inlen
240 nextp = add lend 1
241 end
242 p = pos
243 i = ulong 0
244 while lt i n
245 p = parse_one_int i p lend
246 i = add i 1
247 end
248 put g_p 1 nextp
249 ret true
250end
251
252fn fits16 v long to bool
253 ret and ge v neg long 32768 lt v 32768
254end
255
256fn fits8 v long to bool
257 ret and ge v neg long 128 lt v 128
258end
259
260fn addmod x ulong y ulong m ulong to ulong
261 if ge x sub m y
262 ret sub x sub m y
263 end
264 ret add x y
265end
266
267fn mulmod a ulong b ulong m ulong to ulong
268 aa = rem a m
269 bb = rem b m
270 res = ulong 0
271 while gt bb 0
272 if eq and bb 1 1
273 res = addmod res aa m
274 end
275 aa = addmod aa aa m
276 bb = shr bb 1
277 end
278 ret res
279end
280
281fn run_clamp_sub to void
282 while next_row 2
283 u0 = get g_u 0
284 u1 = get g_u 1
285 if ge u0 u1
286 out_u64 sub u0 u1
287 else
288 out_u64 0
289 end
290 out_push 10
291 end
292end
293
294fn run_hash_step to void
295 while next_row 2
296 u0 = and get g_u 0 4294967295
297 u1 = and get g_u 1 4294967295
298 v = and add mul u0 31 u1 4294967295
299 out_u64 v
300 out_push 10
301 end
302end
303
304fn run_sum_squares3 to void
305 while next_row 3
306 s0 = i64_to_u64 get g_s 0
307 s1 = i64_to_u64 get g_s 1
308 s2 = i64_to_u64 get g_s 2
309 a = u64_to_i64 mul s0 s0
310 b = u64_to_i64 mul s1 s1
311 c = u64_to_i64 mul s2 s2
312 ab = u64_to_i64 add i64_to_u64 a i64_to_u64 b
313 abc = u64_to_i64 add i64_to_u64 ab i64_to_u64 c
314 ok = and fits16 a fits16 b
315 ok = and ok fits16 c
316 ok = and ok fits16 ab
317 ok = and ok fits16 abc
318 if ok
319 out_i64 abc
320 out_push 10
321 else
322 out_str t_trap1
323 end
324 end
325end
326
327fn run_nibble_mirror to void
328 while next_row 1
329 x = and get g_u 0 65535
330 ok = and eq and x 15 shr x 12 eq and shr x 4 15 and shr x 8 15
331 if ok
332 out_str t_1
333 else
334 out_str t_0
335 end
336 end
337end
338
339fn run_halve_or_dec_steps to void
340 while next_row 1
341 x = and get g_u 0 4294967295
342 k = ulong 0
343 while gt x 1
344 if eq and x 1 1
345 x = sub x 1
346 else
347 x = shr x 1
348 end
349 k = add k 1
350 end
351 out_u64 k
352 out_push 10
353 end
354end
355
356fn run_count_digits to void
357 while next_row 1
358 v = get g_u 0
359 k = ulong 1
360 while ge v 10
361 v = div v 10
362 k = add k 1
363 end
364 out_u64 k
365 out_push 10
366 end
367end
368
369fn run_sum_range to void
370 while next_row 2
371 lo = get g_s 0
372 hi = get g_s 1
373 sum = long 0
374 if le lo hi
375 ulo = i64_to_u64 lo
376 uhi = i64_to_u64 hi
377 lh = add ulo uhi
378 cnt = add sub uhi ulo 1
379 prod = u64_to_i64 mul lh cnt
380 sum = div prod 2
381 end
382 out_i64 sum
383 out_push 10
384 end
385end
386
387fn run_cube_xor_pair to void
388 while next_row 1
389 u0 = get g_u 0
390 if ge u0 15
391 out_str t_trap3
392 else
393 a = mul mul u0 u0 u0
394 u1 = add u0 1
395 b = mul mul u1 u1 u1
396 out_u64 and xor a b 65535
397 out_push 10
398 end
399 end
400end
401
402fn run_count_above to void
403 while next_row 5
404 thr = get g_s 4
405 k = ulong 0
406 i = ulong 0
407 while lt i 4
408 if gt get g_s i thr
409 k = add k 1
410 end
411 i = add i 1
412 end
413 out_u64 k
414 out_push 10
415 end
416end
417
418fn run_dot3 to void
419 while next_row 6
420 p0 = mul i64_to_u64 get g_s 0 i64_to_u64 get g_s 3
421 p1 = mul i64_to_u64 get g_s 1 i64_to_u64 get g_s 4
422 p2 = mul i64_to_u64 get g_s 2 i64_to_u64 get g_s 5
423 res = u64_to_i64 add add p0 p1 p2
424 out_i64 res
425 out_push 10
426 end
427end
428
429fn run_power_mod to void
430 while next_row 3
431 b = get g_u 0
432 e = get g_u 1
433 m = get g_u 2
434 if eq m 0
435 out_str t_trap2
436 else
437 acc = rem ulong 1 m
438 x = rem b m
439 bit = ulong 64
440 while gt bit 0
441 bit = sub bit 1
442 acc = mulmod acc acc m
443 if eq and shr e bit 1 1
444 acc = mulmod acc x m
445 end
446 end
447 out_u64 acc
448 out_push 10
449 end
450 end
451end
452
453fn run_narrow_sum to void
454 while next_row 2
455 v = u64_to_i64 add i64_to_u64 get g_s 0 i64_to_u64 get g_s 1
456 if not fits16 v
457 out_str t_trap1
458 else
459 if not fits8 v
460 out_str t_trap5
461 else
462 out_i64 v
463 out_push 10
464 end
465 end
466 end
467end
468
469fn run_rotl8 to void
470 while next_row 2
471 r = and get g_u 0 255
472 k = rem get g_u 1 8
473 i = ulong 0
474 while lt i k
475 r = and or shl r 1 shr r 7 255
476 i = add i 1
477 end
478 out_u64 r
479 out_push 10
480 end
481end
482
483fn run_has_digit to void
484 while next_row 2
485 v = get g_u 0
486 d = get g_u 1
487 found = false
488 if le d 9
489 if eq v 0
490 if eq d 0
491 found = true
492 end
493 else
494 while gt v 0
495 if eq rem v 10 d
496 found = true
497 break
498 end
499 v = div v 10
500 end
501 end
502 end
503 if found
504 out_str t_1
505 else
506 out_str t_0
507 end
508 end
509end
510
511fn run_tri_kind to void
512 while next_row 3
513 a = get g_u 0
514 b = get g_u 1
515 c = get g_u 2
516 k = ulong 3
517 if or or le add a b c le add a c b le add b c a
518 k = 0
519 else
520 if and eq a b eq b c
521 k = 1
522 else
523 if or or eq a b eq b c eq a c
524 k = 2
525 end
526 end
527 end
528 out_u64 k
529 out_push 10
530 end
531end
532
533fn dispatch to bool
534 if arg_eq t_clamp_sub
535 run_clamp_sub
536 ret true
537 end
538 if arg_eq t_hash_step
539 run_hash_step
540 ret true
541 end
542 if arg_eq t_sum_squares3
543 run_sum_squares3
544 ret true
545 end
546 if arg_eq t_nibble_mirror
547 run_nibble_mirror
548 ret true
549 end
550 if arg_eq t_halve_or_dec_steps
551 run_halve_or_dec_steps
552 ret true
553 end
554 if arg_eq t_count_digits
555 run_count_digits
556 ret true
557 end
558 if arg_eq t_sum_range
559 run_sum_range
560 ret true
561 end
562 if arg_eq t_cube_xor_pair
563 run_cube_xor_pair
564 ret true
565 end
566 if arg_eq t_count_above
567 run_count_above
568 ret true
569 end
570 if arg_eq t_dot3
571 run_dot3
572 ret true
573 end
574 if arg_eq t_power_mod
575 run_power_mod
576 ret true
577 end
578 if arg_eq t_narrow_sum
579 run_narrow_sum
580 ret true
581 end
582 if arg_eq t_rotl8
583 run_rotl8
584 ret true
585 end
586 if arg_eq t_has_digit
587 run_has_digit
588 ret true
589 end
590 if arg_eq t_tri_kind
591 run_tri_kind
592 ret true
593 end
594 ret false
595end
596
597fn main to int
598 if ne argcnt 2
599 err_str t_usage
600 ret 2
601 end
602 al = argread 1 g_arg 0 255
603 if lt al 0
604 err_str t_usage
605 ret 2
606 end
607 put g_p 3 ulong al
608 read_all_stdin
609 if not dispatch
610 err_str t_unk
611 w = write 2 g_arg 0 get g_p 3
612 err_str t_nl
613 if lt w 0
614 ret 2
615 end
616 ret 2
617 end
618 out_flush
619 ret 0
620end