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
omega-mv / bench / prime_race / cpu_base.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | note aien-cpu-c-base: conventional single-thread CPU sieve following the upstream "base" rules, |
| 4 | note implemented in pure seeded OM-2. |
| 5 | note Storage: 1 bit per odd number, bit k <-> number 2k+1, inverted (set = composite). |
| 6 | note Concatenated after bench/prime_race/prime_race_impl.om. |
| 7 | |
| 8 | const CPU_MAX_WORDS ulong 1048576 |
| 9 | |
| 10 | global cpu_comp ulong 1048576 |
| 11 | global cpu_exp ulong 1048576 |
| 12 | global cpu_limit ulong 1 |
| 13 | global cpu_nwords ulong 1 |
| 14 | global cpu_ready int 1 |
| 15 | |
| 16 | fn cpu_isqrt_u64 n ulong to ulong |
| 17 | r = ulong 0 |
| 18 | lo = ulong 0 |
| 19 | hi = ulong 2097152 |
| 20 | while le lo hi bound 64 |
| 21 | mid = add lo shr sub hi lo 1 |
| 22 | if le mul mid mid n |
| 23 | r = mid |
| 24 | lo = add mid 1 |
| 25 | else |
| 26 | if eq mid 0 |
| 27 | break |
| 28 | end |
| 29 | hi = sub mid 1 |
| 30 | end |
| 31 | end |
| 32 | ret r |
| 33 | end |
| 34 | |
| 35 | fn cpu_is_comp n ulong to ulong |
| 36 | k = shr sub n 1 1 |
| 37 | w = shr k 6 |
| 38 | b = and k 63 |
| 39 | ret and shr get cpu_comp w b 1 |
| 40 | end |
| 41 | |
| 42 | fn cpu_mark_comp n ulong to void |
| 43 | k = shr sub n 1 1 |
| 44 | w = shr k 6 |
| 45 | b = and k 63 |
| 46 | put cpu_comp w or get cpu_comp w shl ulong 1 b |
| 47 | end |
| 48 | |
| 49 | fn cpu_setup limit ulong to int |
| 50 | nw = pr_words64 limit |
| 51 | if gt nw CPU_MAX_WORDS |
| 52 | ret -1 |
| 53 | end |
| 54 | put cpu_limit 0 limit |
| 55 | put cpu_nwords 0 nw |
| 56 | put cpu_ready 0 1 |
| 57 | ret 0 |
| 58 | end |
| 59 | |
| 60 | fn cpu_pass limit ulong to int |
| 61 | nw = pr_words64 limit |
| 62 | if gt nw CPU_MAX_WORDS |
| 63 | ret -1 |
| 64 | end |
| 65 | put cpu_limit 0 limit |
| 66 | put cpu_nwords 0 nw |
| 67 | for w ulong nw bound 9223372036854775807 |
| 68 | put cpu_comp w 0 |
| 69 | end |
| 70 | q = cpu_isqrt_u64 limit |
| 71 | factor = ulong 3 |
| 72 | while le factor q bound 9223372036854775807 |
| 73 | n = factor |
| 74 | while le n limit bound 9223372036854775807 |
| 75 | if eq cpu_is_comp n 0 |
| 76 | factor = n |
| 77 | break |
| 78 | end |
| 79 | n = add n 2 |
| 80 | end |
| 81 | if gt factor q |
| 82 | break |
| 83 | end |
| 84 | num = mul factor factor |
| 85 | step = shl factor 1 |
| 86 | while le num limit bound 9223372036854775807 |
| 87 | cpu_mark_comp num |
| 88 | num = add num step |
| 89 | end |
| 90 | factor = add factor 2 |
| 91 | end |
| 92 | ret 0 |
| 93 | end |
| 94 | |
| 95 | fn cpu_export limit ulong to int |
| 96 | if or eq get cpu_ready 0 0 ne get cpu_limit 0 limit |
| 97 | ret -1 |
| 98 | end |
| 99 | nw = pr_words64 limit |
| 100 | odd = pr_odd_count limit |
| 101 | for w ulong nw bound 9223372036854775807 |
| 102 | put cpu_exp w xor get cpu_comp w 0xffffffffffffffff |
| 103 | end |
| 104 | if gt nw 0 |
| 105 | put cpu_exp 0 and get cpu_exp 0 0xfffffffffffffffe |
| 106 | rem64 = and odd 63 |
| 107 | if ne rem64 0 |
| 108 | mask = sub shl ulong 1 rem64 1 |
| 109 | last = sub nw 1 |
| 110 | put cpu_exp last and get cpu_exp last mask |
| 111 | end |
| 112 | end |
| 113 | ret 0 |
| 114 | end |
| 115 | |
| 116 | fn cpu_get_export_word idx ulong to ulong |
| 117 | ret get cpu_exp idx |
| 118 | end |
| 119 | |
| 120 | fn cpu_threads to ulong |
| 121 | ret 1 |
| 122 | end |