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 / tests / turing / test_ty_prd2.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | note |
| 4 | note tests/turing/test_ty_prd2.om -- PRD2 reader + mixture scorer tests (R28-v2 |
| 5 | note and negative controls N1-N4) in pure seeded OM-2. |
| 6 | note Composed after src/turing/ty_qcont_lib.om. |
| 7 | |
| 8 | const P2_NPTS ulong 2000 |
| 9 | |
| 10 | text t_p2_fail "FAIL test_ty_prd2 check=" |
| 11 | text t_p2_nl "\n" |
| 12 | text t_p2_sum1 "test_ty_prd2: " |
| 13 | text t_p2_sum2 " checks, " |
| 14 | text t_p2_sum3 " failed\n" |
| 15 | |
| 16 | global g_p2_chk ulong 1 |
| 17 | global g_p2_fl ulong 1 |
| 18 | global g_p2o uchar 256 |
| 19 | global g_p2_st ulong 1 |
| 20 | global g_p2_y f64 2001 |
| 21 | |
| 22 | fn p2_puts s view uchar * to void |
| 23 | n = len s |
| 24 | for i ulong n |
| 25 | put g_p2o i get s i |
| 26 | end |
| 27 | _w = write 1 g_p2o 0 n |
| 28 | end |
| 29 | |
| 30 | fn p2_put_u64 v ulong to void |
| 31 | if eq v 0 |
| 32 | put g_p2o 0 uchar 48 |
| 33 | _w = write 1 g_p2o 0 1 |
| 34 | ret |
| 35 | end |
| 36 | n = ulong 0 |
| 37 | x = v |
| 38 | while gt x 0 |
| 39 | put g_p2o n uchar add 48 rem x 10 |
| 40 | x = div x 10 |
| 41 | n = add n 1 |
| 42 | end |
| 43 | i = ulong 0 |
| 44 | while lt i div n 2 |
| 45 | j = sub sub n 1 i |
| 46 | tmp = get g_p2o i |
| 47 | put g_p2o i get g_p2o j |
| 48 | put g_p2o j tmp |
| 49 | i = add i 1 |
| 50 | end |
| 51 | _w = write 1 g_p2o 0 n |
| 52 | end |
| 53 | |
| 54 | fn chk ok bool id ulong to void |
| 55 | put g_p2_chk 0 add get g_p2_chk 0 1 |
| 56 | if not ok |
| 57 | put g_p2_fl 0 add get g_p2_fl 0 1 |
| 58 | p2_puts t_p2_fail |
| 59 | p2_put_u64 id |
| 60 | p2_puts t_p2_nl |
| 61 | end |
| 62 | end |
| 63 | |
| 64 | fn set_g2 p ulong idx ulong lc f64 sc f64 to void |
| 65 | put g_qp_idx p idx |
| 66 | put g_qp_fam p TYQ_FAM_GAUSS |
| 67 | put g_qp_k p 1 |
| 68 | put g_qp_loc p lc |
| 69 | put g_qp_sc p sc |
| 70 | bo = mul p 8 |
| 71 | put g_qc_pi bo f64 1.0 |
| 72 | put g_qc_loc bo lc |
| 73 | put g_qc_sc bo sc |
| 74 | end |
| 75 | |
| 76 | fn set_m2 p ulong idx ulong pi0 f64 l0 f64 s0 f64 to void |
| 77 | put g_qp_idx p idx |
| 78 | put g_qp_fam p TYQ_FAM_MIX |
| 79 | put g_qp_k p 2 |
| 80 | bo = mul p 8 |
| 81 | put g_qc_pi bo pi0 |
| 82 | put g_qc_loc bo l0 |
| 83 | put g_qc_sc bo s0 |
| 84 | end |
| 85 | |
| 86 | fn set_m2_c1 p ulong pi1 f64 l1 f64 s1 f64 to void |
| 87 | bo = add mul p 8 1 |
| 88 | put g_qc_pi bo pi1 |
| 89 | put g_qc_loc bo l1 |
| 90 | put g_qc_sc bo s1 |
| 91 | end |
| 92 | |
| 93 | fn kof y f64 to long |
| 94 | ret long q_floor div y q_delta |
| 95 | end |
| 96 | |
| 97 | fn test_row28 to void |
| 98 | set_m2 0 0 f64 0.5 f64 0.0 f64 1.0 |
| 99 | set_m2_c1 0 f64 0.4 f64 0.0 f64 1.0 |
| 100 | chk and eq ty_prd2_validate 0 TYQ_FAIL_PROTOCOL eq get g_qret_u 2 PRD2_R_WEIGHT_SUM 1 |
| 101 | chk eq ty_qcont2_bits 0 0 TYQ_FAIL_PROTOCOL 2 |
| 102 | put g_qc_pi 1 f64 0.6 |
| 103 | chk and eq ty_prd2_validate 0 TYQ_FAIL_PROTOCOL eq get g_qret_u 2 PRD2_R_WEIGHT_SUM 3 |
| 104 | put g_qc_pi 0 f64 1.5 |
| 105 | put g_qc_pi 1 f64 -0.5 |
| 106 | chk and eq ty_prd2_validate 0 TYQ_FAIL_PROTOCOL eq get g_qret_u 2 PRD2_R_WEIGHT_RANGE 4 |
| 107 | put g_qc_pi 0 f64 0.5 |
| 108 | put g_qc_pi 1 f64 0.5 |
| 109 | chk and eq ty_prd2_validate 0 TYQ_OK eq get g_qret_u 2 PRD2_R_NONE 5 |
| 110 | chk and eq ty_qcont2_bits 0 123 TYQ_OK q_isfinite get g_qret_f 0 6 |
| 111 | chk ne get g_qret_u 2 PRD2_R_FAMILY 7 |
| 112 | exact = true |
| 113 | for ui ulong 81 |
| 114 | iv = sub long ui 40 |
| 115 | lc = mul f64 0.37 f64 iv |
| 116 | sc = add f64 0.001 mul f64 0.05 f64 and bitcast ulong iv 7 |
| 117 | kv = kof add lc mul f64 0.013 f64 iv |
| 118 | set_g2 0 0 lc sc |
| 119 | put g_qp_idx 1 0 |
| 120 | put g_qp_fam 1 TYQ_FAM_MIX |
| 121 | put g_qp_k 1 1 |
| 122 | put g_qc_pi 8 f64 1.0 |
| 123 | put g_qc_loc 8 lc |
| 124 | put g_qc_sc 8 sc |
| 125 | r1 = ty_qcont_bits 0 kv |
| 126 | bg = get g_qret_f 0 |
| 127 | hg = get g_qret_u 0 |
| 128 | r2 = ty_qcont2_bits 1 kv |
| 129 | bm = get g_qret_f 0 |
| 130 | hm = get g_qret_u 0 |
| 131 | if or ne r1 TYQ_OK ne r2 TYQ_OK |
| 132 | exact = false |
| 133 | end |
| 134 | if or ne bitcast ulong bg bitcast ulong bm ne hg hm |
| 135 | exact = false |
| 136 | end |
| 137 | end |
| 138 | chk exact 8 |
| 139 | worst = f64 0.0 |
| 140 | for ui ulong 41 |
| 141 | iv = sub long ui 20 |
| 142 | lc = mul f64 0.1 f64 iv |
| 143 | sc = f64 0.2 |
| 144 | kv = kof add lc f64 0.05 |
| 145 | set_g2 0 0 lc sc |
| 146 | put g_qp_idx 1 0 |
| 147 | put g_qp_fam 1 TYQ_FAM_MIX |
| 148 | put g_qp_k 1 8 |
| 149 | for j ulong 8 |
| 150 | put g_qc_pi add 8 j f64 0.125 |
| 151 | put g_qc_loc add 8 j lc |
| 152 | put g_qc_sc add 8 j sc |
| 153 | end |
| 154 | chk eq ty_qcont_bits 0 kv TYQ_OK 9 |
| 155 | bg = get g_qret_f 0 |
| 156 | chk eq ty_qcont2_bits 1 kv TYQ_OK 10 |
| 157 | df = q_fabs sub bg get g_qret_f 0 |
| 158 | if gt df worst |
| 159 | worst = df |
| 160 | end |
| 161 | end |
| 162 | chk le worst f64 0.000000000001 11 |
| 163 | end |
| 164 | |
| 165 | fn cp_w0_to_w1 blen ulong to void |
| 166 | for i ulong blen |
| 167 | put g_qwire2 i get g_qwire i |
| 168 | end |
| 169 | end |
| 170 | |
| 171 | fn test_wire to void |
| 172 | set_g2 0 128 f64 0.5 f64 0.25 |
| 173 | set_m2 1 129 f64 0.25 f64 -1.0 f64 0.5 |
| 174 | set_m2_c1 1 f64 0.75 f64 2.0 f64 0.125 |
| 175 | set_g2 2 130 f64 0.0 f64 1.0 |
| 176 | chk eq ty_prd2_encode 0 0 128 3 TYQ_OK 20 |
| 177 | blen = get g_qret_u 0 |
| 178 | chk eq blen 160 21 |
| 179 | chk eq ty_prd2_parse 0 blen 128 3 10 TYQ_OK 22 |
| 180 | chk and eq get g_qp_k 11 2 and eq get g_qc_loc 89 f64 2.0 eq get g_qp_loc 12 f64 0.0 23 |
| 181 | chk eq ty_prd2_encode 1 10 128 3 TYQ_OK 24 |
| 182 | chk eq get g_qret_u 0 blen 25 |
| 183 | same = true |
| 184 | for i ulong blen |
| 185 | if ne get g_qwire i get g_qwire2 i |
| 186 | same = false |
| 187 | end |
| 188 | end |
| 189 | chk same 26 |
| 190 | chk and eq ty_prd2_parse 0 blen 128 4 10 TYQ_FAIL_PROTOCOL eq get g_qret_u 2 PRD2_R_COUNT 27 |
| 191 | chk and eq ty_prd2_parse 0 blen 129 3 10 TYQ_FAIL_PROTOCOL eq get g_qret_u 2 PRD2_R_COUNT 28 |
| 192 | chk and eq ty_prd2_parse 0 sub blen 1 128 3 10 TYQ_FAIL_PROTOCOL eq get g_qret_u 2 PRD2_R_SIZE 29 |
| 193 | cp_w0_to_w1 blen |
| 194 | put g_qwire2 blen uchar 0 |
| 195 | chk and eq ty_prd2_parse 1 add blen 1 128 3 10 TYQ_FAIL_PROTOCOL eq get g_qret_u 2 PRD2_R_SIZE 30 |
| 196 | cp_w0_to_w1 blen |
| 197 | put g_qwire2 5 uchar 3 |
| 198 | chk and eq ty_prd2_parse 1 blen 128 3 10 TYQ_FAIL_PROTOCOL eq get g_qret_u 2 PRD2_R_VERSION 31 |
| 199 | cp_w0_to_w1 blen |
| 200 | put g_qwire2 28 uchar 1 |
| 201 | chk and eq ty_prd2_parse 1 blen 128 3 10 TYQ_FAIL_PROTOCOL eq get g_qret_u 2 PRD2_R_RESERVED 32 |
| 202 | cp_w0_to_w1 blen |
| 203 | put g_qwire2 20 uchar 1 |
| 204 | chk and eq ty_prd2_parse 1 blen 128 3 10 TYQ_FAIL_PROTOCOL eq get g_qret_u 2 PRD2_R_FAMILY 33 |
| 205 | cp_w0_to_w1 blen |
| 206 | put g_qwire2 24 uchar 9 |
| 207 | chk and eq ty_prd2_parse 1 blen 128 3 10 TYQ_FAIL_PROTOCOL eq get g_qret_u 2 PRD2_R_K 34 |
| 208 | set_g2 5 7 f64 1.0 f64 1.0 |
| 209 | chk eq ty_prd_encode 5 7 1 TYQ_OK 35 |
| 210 | vlen = get g_qret_u 0 |
| 211 | chk eq vlen 32 36 |
| 212 | chk and eq ty_prd2_parse 1 vlen 7 1 10 TYQ_FAIL_PROTOCOL eq get g_qret_u 2 PRD2_R_MAGIC 37 |
| 213 | chk eq ty_prd_parse 0 blen 128 3 10 TYQ_FAIL_PROTOCOL 38 |
| 214 | set_m2 5 0 f64 0.5 f64 0.0 f64 1.0 |
| 215 | set_m2_c1 5 f64 0.4 f64 0.0 f64 1.0 |
| 216 | chk eq ty_prd2_encode 1 5 0 1 TYQ_FAIL_PROTOCOL 39 |
| 217 | end |
| 218 | |
| 219 | fn u01 to f64 |
| 220 | st = get g_p2_st 0 |
| 221 | st = xor st shr st 12 |
| 222 | st = xor st shl st 25 |
| 223 | st = xor st shr st 27 |
| 224 | put g_p2_st 0 st |
| 225 | v = mul st ulong 2685821657736338717 |
| 226 | ret div add f64 shr v 11 f64 0.5 f64 9007199254740992.0 |
| 227 | end |
| 228 | |
| 229 | fn gauss to f64 |
| 230 | a = u01 |
| 231 | b = u01 |
| 232 | ret mul fsqrt mul f64 -2.0 q_ln a q_cos mul f64 6.283185307179586 b |
| 233 | end |
| 234 | |
| 235 | fn test_controls to void |
| 236 | put g_p2_st 0 or shl ulong 0x9e3779b9 32 ulong 0x7f4a7c15 |
| 237 | sd = f64 0.05 |
| 238 | put g_p2_y 0 f64 0.0 |
| 239 | for i ulong P2_NPTS |
| 240 | yi = get g_p2_y i |
| 241 | yn = add yi mul sd gauss |
| 242 | put g_p2_y add i 1 yn |
| 243 | put g_qk_arr i kof yn |
| 244 | set_g2 i i yi sd |
| 245 | set_m2 add 2000 i i f64 0.5 f64 0.0 sd |
| 246 | set_m2_c1 add 2000 i f64 0.5 f64 0.0 sd |
| 247 | set_m2 add 4000 i i f64 0.5 yi sd |
| 248 | set_m2_c1 add 4000 i f64 0.5 yi f64 1000000.0 |
| 249 | end |
| 250 | for i ulong P2_NPTS |
| 251 | set_g2 add 6000 i i get g_p2_y rem mul i 7919 P2_NPTS sd |
| 252 | end |
| 253 | chk eq ty_qcont2_sum_ub 0 0 P2_NPTS TYQ_OK 40 |
| 254 | h = get g_qret_i 1 |
| 255 | chk eq get g_qret_u 1 0 41 |
| 256 | chk eq ty_qcont2_sum_ub 2000 0 P2_NPTS TYQ_OK 42 |
| 257 | c = get g_qret_i 1 |
| 258 | chk eq ty_qcont2_sum_ub 6000 0 P2_NPTS TYQ_OK 43 |
| 259 | s = get g_qret_i 1 |
| 260 | chk eq ty_qcont2_sum_ub 4000 0 P2_NPTS TYQ_OK 44 |
| 261 | p = get g_qret_i 1 |
| 262 | chk eq ty_qcont_sum_ub 0 0 P2_NPTS TYQ_OK 45 |
| 263 | chk eq get g_qret_i 1 h 46 |
| 264 | chk gt c h 47 |
| 265 | chk gt s h 48 |
| 266 | per_h = div div f64 h f64 1000000.0 f64 P2_NPTS |
| 267 | per_p = div div f64 p f64 1000000.0 f64 P2_NPTS |
| 268 | chk ge sub per_p per_h f64 0.9 49 |
| 269 | end |
| 270 | |
| 271 | fn main to int |
| 272 | test_row28 |
| 273 | test_wire |
| 274 | test_controls |
| 275 | p2_puts t_p2_sum1 |
| 276 | p2_put_u64 get g_p2_chk 0 |
| 277 | p2_puts t_p2_sum2 |
| 278 | p2_put_u64 get g_p2_fl 0 |
| 279 | p2_puts t_p2_sum3 |
| 280 | if ne get g_p2_fl 0 0 |
| 281 | ret 1 |
| 282 | end |
| 283 | ret 0 |
| 284 | end |