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 / src / turing / ty_qcont_lib.om
| 1 | om2 |
| 2 | note SPDX-License-Identifier: Apache-2.0 |
| 3 | note Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 4 | note |
| 5 | note src/turing/ty_qcont_lib.om -- Pure seeded OM-2 implementation of TURING |
| 6 | note quantized continuous scoring (qint.v1), PRD1, and PRD2 mixture scoring. |
| 7 | |
| 8 | const TY_OK long 0 |
| 9 | const TY_E_OVERFLOW long -8 |
| 10 | |
| 11 | const TYQ_OK long 0 |
| 12 | const TYQ_FAIL_PROTOCOL long -101 |
| 13 | const TYQ_FAIL_FLOOR long -102 |
| 14 | const TYQ_FAIL_LEAK long -103 |
| 15 | const TYQ_FAIL_DIGEST long -104 |
| 16 | const TYQ_FAIL_PROFILE long -105 |
| 17 | const TYQ_FAIL_BASELINE long -106 |
| 18 | const TYQ_E_ARG long -107 |
| 19 | const TYQ_E_IO long -108 |
| 20 | |
| 21 | const TYQ_FAM_GAUSS ulong 0 |
| 22 | const TYQ_FAM_STUDENT ulong 1 |
| 23 | const TYQ_FAM_MIX ulong 2 |
| 24 | const TYQ_KMAX ulong 8 |
| 25 | const TYQ_K_LIMIT long 2251799813685248 |
| 26 | |
| 27 | const PRD2_R_NONE ulong 0 |
| 28 | const PRD2_R_MAGIC ulong 1 |
| 29 | const PRD2_R_VERSION ulong 2 |
| 30 | const PRD2_R_SIZE ulong 3 |
| 31 | const PRD2_R_COUNT ulong 4 |
| 32 | const PRD2_R_INDEX ulong 5 |
| 33 | const PRD2_R_FAMILY ulong 6 |
| 34 | const PRD2_R_K ulong 7 |
| 35 | const PRD2_R_RESERVED ulong 8 |
| 36 | const PRD2_R_WEIGHT_NONFINITE ulong 9 |
| 37 | const PRD2_R_WEIGHT_RANGE ulong 10 |
| 38 | const PRD2_R_WEIGHT_SUM ulong 11 |
| 39 | const PRD2_R_GAUSS_WEIGHT ulong 12 |
| 40 | const PRD2_R_LOC ulong 13 |
| 41 | const PRD2_R_SCALE ulong 14 |
| 42 | |
| 43 | const PRD_HDR ulong 8 |
| 44 | const PRD_REC ulong 24 |
| 45 | const P2_HDR ulong 16 |
| 46 | const P2_REC ulong 16 |
| 47 | const P2_CMP ulong 24 |
| 48 | |
| 49 | text t_qst_ok "OK" |
| 50 | text t_qst_fp "FAIL_PROTOCOL" |
| 51 | text t_qst_ff "FAIL_FLOOR" |
| 52 | text t_qst_fl "FAIL_LEAK" |
| 53 | text t_qst_fd "FAIL_DIGEST" |
| 54 | text t_qst_fpr "FAIL_PROFILE" |
| 55 | text t_qst_fb "FAIL_BASELINE" |
| 56 | text t_qst_ea "E_ARG" |
| 57 | text t_qst_ei "E_IO" |
| 58 | text t_qst_un "UNKNOWN" |
| 59 | |
| 60 | text t_p1_mag "PRD1" |
| 61 | text t_p2_mag "PRD2" |
| 62 | |
| 63 | note ---- Pools for predictions (up to 8192 preds, 8192*8 = 65536 comps) ---- |
| 64 | global g_qp_idx ulong 8192 |
| 65 | global g_qp_fam ulong 8192 |
| 66 | global g_qp_k ulong 8192 |
| 67 | global g_qp_loc f64 8192 |
| 68 | global g_qp_sc f64 8192 |
| 69 | global g_qc_pi f64 65536 |
| 70 | global g_qc_loc f64 65536 |
| 71 | global g_qc_sc f64 65536 |
| 72 | global g_qk_arr long 8192 |
| 73 | |
| 74 | note ---- Wire I/O & return scratch ---- |
| 75 | global g_qwire uchar 262144 |
| 76 | global g_qwire2 uchar 262144 |
| 77 | global g_qret_f f64 16 |
| 78 | global g_qret_i long 16 |
| 79 | global g_qret_u ulong 16 |
| 80 | global g_qmix_b f64 8 |
| 81 | |
| 82 | note ---- Exact IEEE-754 double helpers ---- |
| 83 | fn q_ln2 to f64 |
| 84 | ret bitcast f64 or shl ulong 0x3fe62e42 32 ulong 0xfefa39ef |
| 85 | end |
| 86 | |
| 87 | fn q_ln2_hi to f64 |
| 88 | ret bitcast f64 or shl ulong 0x3fe62e42 32 ulong 0xfee00000 |
| 89 | end |
| 90 | |
| 91 | fn q_ln2_lo to f64 |
| 92 | ret bitcast f64 or shl ulong 0x3dea39ef 32 ulong 0x35793c76 |
| 93 | end |
| 94 | |
| 95 | fn q_hln2pi to f64 |
| 96 | ret bitcast f64 or shl ulong 0x3fed67f1 32 ulong 0xc864beb4 |
| 97 | end |
| 98 | |
| 99 | fn q_delta to f64 |
| 100 | ret bitcast f64 shl ulong 0x3eb00000 32 |
| 101 | end |
| 102 | |
| 103 | fn q_sd_min to f64 |
| 104 | ret bitcast f64 shl ulong 0x3f500000 32 |
| 105 | end |
| 106 | |
| 107 | fn q_norm_tol to f64 |
| 108 | ret f64 0.000000001 |
| 109 | end |
| 110 | |
| 111 | fn q_nan to f64 |
| 112 | ret div f64 0.0 f64 0.0 |
| 113 | end |
| 114 | |
| 115 | fn q_inf to f64 |
| 116 | ret div f64 1.0 f64 0.0 |
| 117 | end |
| 118 | |
| 119 | fn q_isnan x f64 to bool |
| 120 | ret ne x x |
| 121 | end |
| 122 | |
| 123 | fn q_isfinite x f64 to bool |
| 124 | if ne x x |
| 125 | ret false |
| 126 | end |
| 127 | ret eq sub x x f64 0.0 |
| 128 | end |
| 129 | |
| 130 | fn q_fabs x f64 to f64 |
| 131 | u = and bitcast ulong x or shl ulong 0x7fffffff 32 ulong 0xffffffff |
| 132 | ret bitcast f64 u |
| 133 | end |
| 134 | |
| 135 | fn q_floor x f64 to f64 |
| 136 | if not q_isfinite x |
| 137 | ret x |
| 138 | end |
| 139 | ax = q_fabs x |
| 140 | if ge ax f64 4503599627370496.0 |
| 141 | ret x |
| 142 | end |
| 143 | iv = long x |
| 144 | fv = f64 iv |
| 145 | if gt fv x |
| 146 | ret sub fv f64 1.0 |
| 147 | end |
| 148 | ret fv |
| 149 | end |
| 150 | |
| 151 | fn q_llrint x f64 to long |
| 152 | fl = q_floor x |
| 153 | fr = sub x fl |
| 154 | if lt fr f64 0.5 |
| 155 | ret long fl |
| 156 | end |
| 157 | if gt fr f64 0.5 |
| 158 | ret add long fl 1 |
| 159 | end |
| 160 | iv = long fl |
| 161 | if eq and bitcast ulong iv 1 0 |
| 162 | ret iv |
| 163 | end |
| 164 | ret add iv 1 |
| 165 | end |
| 166 | |
| 167 | fn q_pow2i k long to f64 |
| 168 | if lt k -1022 |
| 169 | if lt k -1074 |
| 170 | ret f64 0.0 |
| 171 | end |
| 172 | sh = ulong sub k -1074 |
| 173 | ret bitcast f64 shl ulong 1 sh |
| 174 | end |
| 175 | if gt k 1023 |
| 176 | ret q_inf |
| 177 | end |
| 178 | be = ulong add k 1023 |
| 179 | ret bitcast f64 shl be 52 |
| 180 | end |
| 181 | |
| 182 | fn q_ln x f64 to f64 |
| 183 | if or q_isnan x lt x f64 0.0 |
| 184 | ret q_nan |
| 185 | end |
| 186 | if eq x f64 0.0 |
| 187 | ret sub f64 0.0 q_inf |
| 188 | end |
| 189 | if not q_isfinite x |
| 190 | ret q_inf |
| 191 | end |
| 192 | u = bitcast ulong x |
| 193 | exp = sub long shr u 52 1023 |
| 194 | if eq shr u 52 0 |
| 195 | x2 = mul x q_pow2i 54 |
| 196 | u = bitcast ulong x2 |
| 197 | exp = sub sub long shr u 52 1023 54 |
| 198 | end |
| 199 | mu = or and u or shl ulong 0x000fffff 32 ulong 0xffffffff shl ulong 0x3ff00000 32 |
| 200 | m = bitcast f64 mu |
| 201 | if gt m bitcast f64 or shl ulong 0x3ff6a09e 32 ulong 0x667f3bcd |
| 202 | m = mul m f64 0.5 |
| 203 | exp = add exp 1 |
| 204 | end |
| 205 | t = div sub m f64 1.0 add m f64 1.0 |
| 206 | t2 = mul t t |
| 207 | s = div f64 1.0 f64 19.0 |
| 208 | s = add div f64 1.0 f64 17.0 mul t2 s |
| 209 | s = add div f64 1.0 f64 15.0 mul t2 s |
| 210 | s = add div f64 1.0 f64 13.0 mul t2 s |
| 211 | s = add div f64 1.0 f64 11.0 mul t2 s |
| 212 | s = add div f64 1.0 f64 9.0 mul t2 s |
| 213 | s = add div f64 1.0 f64 7.0 mul t2 s |
| 214 | s = add div f64 1.0 f64 5.0 mul t2 s |
| 215 | s = add div f64 1.0 f64 3.0 mul t2 s |
| 216 | s = add f64 1.0 mul t2 s |
| 217 | lm = mul mul f64 2.0 t s |
| 218 | ef = f64 exp |
| 219 | ret add mul ef q_ln2_hi add mul ef q_ln2_lo lm |
| 220 | end |
| 221 | |
| 222 | fn q_log2 x f64 to f64 |
| 223 | ret div q_ln x q_ln2 |
| 224 | end |
| 225 | |
| 226 | fn q_log1p x f64 to f64 |
| 227 | if q_isnan x |
| 228 | ret q_nan |
| 229 | end |
| 230 | if lt q_fabs x f64 0.25 |
| 231 | t = div x add f64 2.0 x |
| 232 | t2 = mul t t |
| 233 | s = div f64 1.0 f64 17.0 |
| 234 | s = add div f64 1.0 f64 15.0 mul t2 s |
| 235 | s = add div f64 1.0 f64 13.0 mul t2 s |
| 236 | s = add div f64 1.0 f64 11.0 mul t2 s |
| 237 | s = add div f64 1.0 f64 9.0 mul t2 s |
| 238 | s = add div f64 1.0 f64 7.0 mul t2 s |
| 239 | s = add div f64 1.0 f64 5.0 mul t2 s |
| 240 | s = add div f64 1.0 f64 3.0 mul t2 s |
| 241 | s = add f64 1.0 mul t2 s |
| 242 | ret mul mul f64 2.0 t s |
| 243 | end |
| 244 | ret q_ln add f64 1.0 x |
| 245 | end |
| 246 | |
| 247 | fn q_exp x f64 to f64 |
| 248 | if q_isnan x |
| 249 | ret q_nan |
| 250 | end |
| 251 | if gt x f64 709.782712893384 |
| 252 | ret q_inf |
| 253 | end |
| 254 | if lt x f64 -745.1332191019412 |
| 255 | ret f64 0.0 |
| 256 | end |
| 257 | kf = q_floor add div x q_ln2 f64 0.5 |
| 258 | k = long kf |
| 259 | r = sub sub x mul kf q_ln2_hi mul kf q_ln2_lo |
| 260 | p = div f64 1.0 f64 14.0 |
| 261 | p = add f64 1.0 mul r div p f64 13.0 |
| 262 | p = add f64 1.0 mul r div p f64 12.0 |
| 263 | p = add f64 1.0 mul r div p f64 11.0 |
| 264 | p = add f64 1.0 mul r div p f64 10.0 |
| 265 | p = add f64 1.0 mul r div p f64 9.0 |
| 266 | p = add f64 1.0 mul r div p f64 8.0 |
| 267 | p = add f64 1.0 mul r div p f64 7.0 |
| 268 | p = add f64 1.0 mul r div p f64 6.0 |
| 269 | p = add f64 1.0 mul r div p f64 5.0 |
| 270 | p = add f64 1.0 mul r div p f64 4.0 |
| 271 | p = add f64 1.0 mul r div p f64 3.0 |
| 272 | p = add f64 1.0 mul r div p f64 2.0 |
| 273 | p = add f64 1.0 mul r p |
| 274 | if gt k 512 |
| 275 | ret mul mul p q_pow2i 512 q_pow2i sub k 512 |
| 276 | end |
| 277 | if lt k -512 |
| 278 | ret mul mul p q_pow2i -512 q_pow2i add k 512 |
| 279 | end |
| 280 | ret mul p q_pow2i k |
| 281 | end |
| 282 | |
| 283 | fn q_exp2 x f64 to f64 |
| 284 | ret q_exp mul x q_ln2 |
| 285 | end |
| 286 | |
| 287 | fn q_sinh x f64 to f64 |
| 288 | ax = q_fabs x |
| 289 | if lt ax f64 0.1 |
| 290 | x2 = mul x x |
| 291 | s = div f64 1.0 f64 39916800.0 |
| 292 | s = add div f64 1.0 f64 362880.0 mul x2 s |
| 293 | s = add div f64 1.0 f64 5040.0 mul x2 s |
| 294 | s = add div f64 1.0 f64 120.0 mul x2 s |
| 295 | s = add div f64 1.0 f64 6.0 mul x2 s |
| 296 | ret mul x add f64 1.0 mul x2 s |
| 297 | end |
| 298 | ep = q_exp x |
| 299 | em = q_exp sub f64 0.0 x |
| 300 | ret mul f64 0.5 sub ep em |
| 301 | end |
| 302 | |
| 303 | fn q_cos x f64 to f64 |
| 304 | tp = bitcast f64 or shl ulong 0x401921fb 32 ulong 0x54442d18 |
| 305 | pi = bitcast f64 or shl ulong 0x400921fb 32 ulong 0x54442d18 |
| 306 | hp = bitcast f64 or shl ulong 0x3ff921fb 32 ulong 0x54442d18 |
| 307 | r = q_fabs x |
| 308 | n = q_floor div r tp |
| 309 | r = sub r mul n tp |
| 310 | if gt r pi |
| 311 | r = sub tp r |
| 312 | end |
| 313 | sgn = f64 1.0 |
| 314 | if gt r hp |
| 315 | r = sub pi r |
| 316 | sgn = f64 -1.0 |
| 317 | end |
| 318 | r2 = mul r r |
| 319 | c = div f64 1.0 f64 87178291200.0 |
| 320 | c = sub div f64 1.0 f64 479001600.0 mul r2 c |
| 321 | c = sub div f64 1.0 f64 3628800.0 mul r2 c |
| 322 | c = sub div f64 1.0 f64 40320.0 mul r2 c |
| 323 | c = sub div f64 1.0 f64 720.0 mul r2 c |
| 324 | c = sub div f64 1.0 f64 24.0 mul r2 c |
| 325 | c = sub f64 0.5 mul r2 c |
| 326 | c = sub f64 1.0 mul r2 c |
| 327 | ret mul sgn c |
| 328 | end |
| 329 | |
| 330 | note ---- Checked i64 add (result in g_qret_i[0]) ---- |
| 331 | fn ty_add_chk a long b long to long |
| 332 | im = bitcast long or shl ulong 0x7fffffff 32 ulong 0xffffffff |
| 333 | inm = bitcast long shl ulong 0x80000000 32 |
| 334 | if gt b 0 |
| 335 | if gt a sub im b |
| 336 | ret TY_E_OVERFLOW |
| 337 | end |
| 338 | end |
| 339 | if lt b 0 |
| 340 | if lt a sub inm b |
| 341 | ret TY_E_OVERFLOW |
| 342 | end |
| 343 | end |
| 344 | put g_qret_i 0 add a b |
| 345 | ret TY_OK |
| 346 | end |
| 347 | |
| 348 | note ---- qint.v1 Gaussian scorer ---- |
| 349 | fn tyq_lnsinhc a f64 to f64 |
| 350 | if lt a f64 0.0001 |
| 351 | ret div mul a a f64 6.0 |
| 352 | end |
| 353 | if lt a f64 1.0 |
| 354 | ret q_ln div q_sinh a a |
| 355 | end |
| 356 | ret add sub a q_ln mul f64 2.0 a q_log1p sub f64 0.0 q_exp mul f64 -2.0 a |
| 357 | end |
| 358 | |
| 359 | note ty_qcont_bits: slot p in g_qp_*, k. Writes bits -> g_qret_f[0], hit -> g_qret_u[0]. |
| 360 | fn ty_qcont_bits p ulong k long to long |
| 361 | if ne get g_qp_fam p TYQ_FAM_GAUSS |
| 362 | ret TYQ_FAIL_PROTOCOL |
| 363 | end |
| 364 | lc = get g_qp_loc p |
| 365 | sc = get g_qp_sc p |
| 366 | if or not q_isfinite lc or not q_isfinite sc not gt sc f64 0.0 |
| 367 | ret TYQ_FAIL_PROTOCOL |
| 368 | end |
| 369 | if or ge k TYQ_K_LIMIT le k sub 0 TYQ_K_LIMIT |
| 370 | ret TYQ_FAIL_PROTOCOL |
| 371 | end |
| 372 | s = sc |
| 373 | hit = ulong 0 |
| 374 | if lt s q_sd_min |
| 375 | s = q_sd_min |
| 376 | hit = 1 |
| 377 | end |
| 378 | zc = div sub mul add f64 k f64 0.5 q_delta lc s |
| 379 | b = div q_delta s |
| 380 | if or not q_isfinite zc or not q_isfinite b not gt b f64 0.0 |
| 381 | ret TYQ_FAIL_PROTOCOL |
| 382 | end |
| 383 | a = mul mul q_fabs zc b f64 0.5 |
| 384 | t1 = add mul mul zc zc f64 0.5 q_hln2pi |
| 385 | t2 = add q_ln b tyq_lnsinhc a |
| 386 | v = div sub t1 t2 q_ln2 |
| 387 | if not q_isfinite v |
| 388 | ret TYQ_FAIL_PROTOCOL |
| 389 | end |
| 390 | put g_qret_f 0 v |
| 391 | put g_qret_u 0 hit |
| 392 | ret TYQ_OK |
| 393 | end |
| 394 | |
| 395 | note ty_qcont_point_ub: writes ub -> g_qret_i[0], hit -> g_qret_u[0]. |
| 396 | fn ty_qcont_point_ub p ulong k long to long |
| 397 | rc = ty_qcont_bits p k |
| 398 | if ne rc TYQ_OK |
| 399 | ret rc |
| 400 | end |
| 401 | sc = mul f64 1000000.0 get g_qret_f 0 |
| 402 | if not q_isfinite sc |
| 403 | ret TYQ_FAIL_PROTOCOL |
| 404 | end |
| 405 | if gt q_fabs sc bitcast f64 or shl ulong 0x43df399b 32 ulong 0x1438a100 |
| 406 | ret TYQ_FAIL_PROTOCOL |
| 407 | end |
| 408 | put g_qret_i 0 q_llrint sc |
| 409 | ret TYQ_OK |
| 410 | end |
| 411 | |
| 412 | note ty_qcont_sum_ub: slots base..base+n-1, k in g_qk_arr[kbase..]. Writes ld_ub -> g_qret_i[1], hits -> g_qret_u[1]. |
| 413 | fn ty_qcont_sum_ub base ulong kbase ulong n ulong to long |
| 414 | sum = long 0 |
| 415 | hits = ulong 0 |
| 416 | for i ulong n |
| 417 | rc = ty_qcont_point_ub add base i get g_qk_arr add kbase i |
| 418 | if ne rc TYQ_OK |
| 419 | ret rc |
| 420 | end |
| 421 | ub = get g_qret_i 0 |
| 422 | hit = get g_qret_u 0 |
| 423 | if ne ty_add_chk sum ub TY_OK |
| 424 | ret TYQ_FAIL_PROTOCOL |
| 425 | end |
| 426 | sum = get g_qret_i 0 |
| 427 | hits = add hits hit |
| 428 | end |
| 429 | put g_qret_i 1 sum |
| 430 | put g_qret_u 1 hits |
| 431 | ret TYQ_OK |
| 432 | end |
| 433 | |
| 434 | fn ty_qcont_bytes_ub pb ulong to long |
| 435 | put g_qret_i 0 mul long 8000000 long pb |
| 436 | ret TYQ_OK |
| 437 | end |
| 438 | |
| 439 | fn ty_qcont_rider_ub kp ulong npi ulong to long |
| 440 | if eq kp 0 |
| 441 | put g_qret_i 0 0 |
| 442 | ret TYQ_OK |
| 443 | end |
| 444 | if eq npi 0 |
| 445 | ret TYQ_FAIL_PROTOCOL |
| 446 | end |
| 447 | v = mul f64 1000000.0 mul mul f64 kp f64 0.5 q_log2 f64 npi |
| 448 | if or not q_isfinite v gt v bitcast f64 or shl ulong 0x43df399b 32 ulong 0x1438a100 |
| 449 | ret TYQ_FAIL_PROTOCOL |
| 450 | end |
| 451 | put g_qret_i 0 q_llrint v |
| 452 | ret TYQ_OK |
| 453 | end |
| 454 | |
| 455 | fn ty_qcont_floor_check fh ulong sp ulong to long |
| 456 | if gt fh 18446744073709551 |
| 457 | ret TYQ_FAIL_FLOOR |
| 458 | end |
| 459 | if gt mul fh 1000 sp |
| 460 | ret TYQ_FAIL_FLOOR |
| 461 | end |
| 462 | ret TYQ_OK |
| 463 | end |
| 464 | |
| 465 | note ---- Wire little-endian helpers on g_qwire / g_qwire2 ---- |
| 466 | fn qw_rd32 which ulong off ulong to ulong |
| 467 | v = ulong 0 |
| 468 | for i ulong 4 |
| 469 | b = ulong 0 |
| 470 | if eq which 0 |
| 471 | b = ulong get g_qwire add off i |
| 472 | else |
| 473 | b = ulong get g_qwire2 add off i |
| 474 | end |
| 475 | v = or v shl b mul i 8 |
| 476 | end |
| 477 | ret v |
| 478 | end |
| 479 | |
| 480 | fn qw_rdf which ulong off ulong to f64 |
| 481 | v = ulong 0 |
| 482 | for i ulong 8 |
| 483 | b = ulong 0 |
| 484 | if eq which 0 |
| 485 | b = ulong get g_qwire add off i |
| 486 | else |
| 487 | b = ulong get g_qwire2 add off i |
| 488 | end |
| 489 | v = or v shl b mul i 8 |
| 490 | end |
| 491 | ret bitcast f64 v |
| 492 | end |
| 493 | |
| 494 | fn qw_wr32 which ulong off ulong v ulong to void |
| 495 | for i ulong 4 |
| 496 | b = uchar and shr v mul i 8 255 |
| 497 | if eq which 0 |
| 498 | put g_qwire add off i b |
| 499 | else |
| 500 | put g_qwire2 add off i b |
| 501 | end |
| 502 | end |
| 503 | end |
| 504 | |
| 505 | fn qw_wrf which ulong off ulong d f64 to void |
| 506 | u = bitcast ulong d |
| 507 | for i ulong 8 |
| 508 | b = uchar and shr u mul i 8 255 |
| 509 | if eq which 0 |
| 510 | put g_qwire add off i b |
| 511 | else |
| 512 | put g_qwire2 add off i b |
| 513 | end |
| 514 | end |
| 515 | end |
| 516 | |
| 517 | note ---- PRD1 ---- |
| 518 | fn ty_prd_validate p ulong to long |
| 519 | if ne get g_qp_fam p TYQ_FAM_GAUSS |
| 520 | ret TYQ_FAIL_PROTOCOL |
| 521 | end |
| 522 | lc = get g_qp_loc p |
| 523 | sc = get g_qp_sc p |
| 524 | if or not q_isfinite lc or not q_isfinite sc not gt sc f64 0.0 |
| 525 | ret TYQ_FAIL_PROTOCOL |
| 526 | end |
| 527 | ret TYQ_OK |
| 528 | end |
| 529 | |
| 530 | fn ty_prd_parse_one which ulong off ulong p ulong exp_idx ulong to bool |
| 531 | idx = qw_rd32 which off |
| 532 | put g_qp_idx p idx |
| 533 | put g_qp_fam p qw_rd32 which add off 4 |
| 534 | put g_qp_k p 0 |
| 535 | put g_qp_loc p qw_rdf which add off 8 |
| 536 | put g_qp_sc p qw_rdf which add off 16 |
| 537 | if ne idx exp_idx |
| 538 | ret false |
| 539 | end |
| 540 | ret eq ty_prd_validate p TYQ_OK |
| 541 | end |
| 542 | |
| 543 | note Parse PRD1 from g_qwire[0..blen-1] into g_qp_*[dst..dst+cnt-1] |
| 544 | fn ty_prd_parse which ulong blen ulong first ulong cnt ulong dst ulong to long |
| 545 | if eq cnt 0 |
| 546 | ret TYQ_E_ARG |
| 547 | end |
| 548 | if gt add first cnt 4294967295 |
| 549 | ret TYQ_E_ARG |
| 550 | end |
| 551 | if lt blen PRD_HDR |
| 552 | ret TYQ_FAIL_PROTOCOL |
| 553 | end |
| 554 | for i ulong 4 |
| 555 | b = uchar 0 |
| 556 | if eq which 0 |
| 557 | b = get g_qwire i |
| 558 | else |
| 559 | b = get g_qwire2 i |
| 560 | end |
| 561 | if ne b get t_p1_mag i |
| 562 | ret TYQ_FAIL_PROTOCOL |
| 563 | end |
| 564 | end |
| 565 | if ne qw_rd32 which 4 1 |
| 566 | ret TYQ_FAIL_PROTOCOL |
| 567 | end |
| 568 | if ne sub blen PRD_HDR mul cnt PRD_REC |
| 569 | ret TYQ_FAIL_PROTOCOL |
| 570 | end |
| 571 | for i ulong cnt |
| 572 | if not ty_prd_parse_one which add PRD_HDR mul i PRD_REC add dst i add first i |
| 573 | ret TYQ_FAIL_PROTOCOL |
| 574 | end |
| 575 | end |
| 576 | ret TYQ_OK |
| 577 | end |
| 578 | |
| 579 | note Encode PRD1 from g_qp_*[src..src+cnt-1] into g_qwire2[0..]; len -> g_qret_u[0] |
| 580 | fn ty_prd_encode src ulong first ulong cnt ulong to long |
| 581 | for i ulong cnt |
| 582 | p = add src i |
| 583 | if or ne ty_prd_validate p TYQ_OK ne get g_qp_idx p add first i |
| 584 | ret TYQ_FAIL_PROTOCOL |
| 585 | end |
| 586 | end |
| 587 | for i ulong 4 |
| 588 | put g_qwire2 i get t_p1_mag i |
| 589 | end |
| 590 | qw_wr32 1 4 1 |
| 591 | for i ulong cnt |
| 592 | off = add PRD_HDR mul i PRD_REC |
| 593 | p = add src i |
| 594 | qw_wr32 1 off get g_qp_idx p |
| 595 | qw_wr32 1 add off 4 get g_qp_fam p |
| 596 | qw_wrf 1 add off 8 get g_qp_loc p |
| 597 | qw_wrf 1 add off 16 get g_qp_sc p |
| 598 | end |
| 599 | put g_qret_u 0 add PRD_HDR mul cnt PRD_REC |
| 600 | ret TYQ_OK |
| 601 | end |
| 602 | |
| 603 | note ---- PRD2 ---- |
| 604 | note Validates g_qp_*[p]; reason -> g_qret_u[2] |
| 605 | fn ty_prd2_validate p ulong to long |
| 606 | put g_qret_u 2 PRD2_R_NONE |
| 607 | fam = get g_qp_fam p |
| 608 | kv = get g_qp_k p |
| 609 | if and ne fam TYQ_FAM_GAUSS ne fam TYQ_FAM_MIX |
| 610 | put g_qret_u 2 PRD2_R_FAMILY |
| 611 | ret TYQ_FAIL_PROTOCOL |
| 612 | end |
| 613 | if and eq fam TYQ_FAM_GAUSS ne kv 1 |
| 614 | put g_qret_u 2 PRD2_R_K |
| 615 | ret TYQ_FAIL_PROTOCOL |
| 616 | end |
| 617 | if and eq fam TYQ_FAM_MIX or lt kv 1 gt kv TYQ_KMAX |
| 618 | put g_qret_u 2 PRD2_R_K |
| 619 | ret TYQ_FAIL_PROTOCOL |
| 620 | end |
| 621 | sum = f64 0.0 |
| 622 | bo = mul p 8 |
| 623 | for j ulong kv |
| 624 | pi = get g_qc_pi add bo j |
| 625 | lc = get g_qc_loc add bo j |
| 626 | sc = get g_qc_sc add bo j |
| 627 | if not q_isfinite pi |
| 628 | put g_qret_u 2 PRD2_R_WEIGHT_NONFINITE |
| 629 | ret TYQ_FAIL_PROTOCOL |
| 630 | end |
| 631 | if or not gt pi f64 0.0 gt pi f64 1.0 |
| 632 | put g_qret_u 2 PRD2_R_WEIGHT_RANGE |
| 633 | ret TYQ_FAIL_PROTOCOL |
| 634 | end |
| 635 | if not q_isfinite lc |
| 636 | put g_qret_u 2 PRD2_R_LOC |
| 637 | ret TYQ_FAIL_PROTOCOL |
| 638 | end |
| 639 | if or not q_isfinite sc not gt sc f64 0.0 |
| 640 | put g_qret_u 2 PRD2_R_SCALE |
| 641 | ret TYQ_FAIL_PROTOCOL |
| 642 | end |
| 643 | sum = add sum pi |
| 644 | end |
| 645 | if eq fam TYQ_FAM_GAUSS |
| 646 | if ne get g_qc_pi bo f64 1.0 |
| 647 | put g_qret_u 2 PRD2_R_GAUSS_WEIGHT |
| 648 | ret TYQ_FAIL_PROTOCOL |
| 649 | end |
| 650 | if ne get g_qp_loc p get g_qc_loc bo |
| 651 | put g_qret_u 2 PRD2_R_LOC |
| 652 | ret TYQ_FAIL_PROTOCOL |
| 653 | end |
| 654 | if ne get g_qp_sc p get g_qc_sc bo |
| 655 | put g_qret_u 2 PRD2_R_LOC |
| 656 | ret TYQ_FAIL_PROTOCOL |
| 657 | end |
| 658 | else |
| 659 | if gt q_fabs sub sum f64 1.0 q_norm_tol |
| 660 | put g_qret_u 2 PRD2_R_WEIGHT_SUM |
| 661 | ret TYQ_FAIL_PROTOCOL |
| 662 | end |
| 663 | end |
| 664 | ret TYQ_OK |
| 665 | end |
| 666 | |
| 667 | fn ty_prd2_read_cmps which ulong off2 ulong p ulong kv ulong to void |
| 668 | bo = mul p 8 |
| 669 | for j ulong kv |
| 670 | co = add off2 mul j P2_CMP |
| 671 | put g_qc_pi add bo j qw_rdf which co |
| 672 | put g_qc_loc add bo j qw_rdf which add co 8 |
| 673 | put g_qc_sc add bo j qw_rdf which add co 16 |
| 674 | end |
| 675 | if eq get g_qp_fam p TYQ_FAM_GAUSS |
| 676 | put g_qp_loc p get g_qc_loc bo |
| 677 | put g_qp_sc p get g_qc_sc bo |
| 678 | end |
| 679 | end |
| 680 | |
| 681 | fn ty_prd2_parse_one which ulong blen ulong off ulong p ulong exp_idx ulong to long |
| 682 | if lt sub blen off P2_REC |
| 683 | put g_qret_u 2 PRD2_R_SIZE |
| 684 | ret -1 |
| 685 | end |
| 686 | idx = qw_rd32 which off |
| 687 | fam = qw_rd32 which add off 4 |
| 688 | kv = qw_rd32 which add off 8 |
| 689 | rsv = qw_rd32 which add off 12 |
| 690 | off2 = add off P2_REC |
| 691 | put g_qp_idx p idx |
| 692 | put g_qp_fam p fam |
| 693 | put g_qp_k p kv |
| 694 | if ne idx exp_idx |
| 695 | put g_qret_u 2 PRD2_R_INDEX |
| 696 | ret -1 |
| 697 | end |
| 698 | if ne rsv 0 |
| 699 | put g_qret_u 2 PRD2_R_RESERVED |
| 700 | ret -1 |
| 701 | end |
| 702 | if or lt kv 1 gt kv TYQ_KMAX |
| 703 | put g_qret_u 2 PRD2_R_K |
| 704 | ret -1 |
| 705 | end |
| 706 | if lt sub blen off2 mul kv P2_CMP |
| 707 | put g_qret_u 2 PRD2_R_SIZE |
| 708 | ret -1 |
| 709 | end |
| 710 | ty_prd2_read_cmps which off2 p kv |
| 711 | if ne ty_prd2_validate p TYQ_OK |
| 712 | ret -1 |
| 713 | end |
| 714 | ret long add off2 mul kv P2_CMP |
| 715 | end |
| 716 | |
| 717 | fn ty_prd2_parse which ulong blen ulong first ulong cnt ulong dst ulong to long |
| 718 | put g_qret_u 2 PRD2_R_NONE |
| 719 | if eq cnt 0 |
| 720 | ret TYQ_E_ARG |
| 721 | end |
| 722 | if gt add first cnt 4294967295 |
| 723 | ret TYQ_E_ARG |
| 724 | end |
| 725 | if lt blen P2_HDR |
| 726 | put g_qret_u 2 PRD2_R_SIZE |
| 727 | ret TYQ_FAIL_PROTOCOL |
| 728 | end |
| 729 | for i ulong 4 |
| 730 | b = uchar 0 |
| 731 | if eq which 0 |
| 732 | b = get g_qwire i |
| 733 | else |
| 734 | b = get g_qwire2 i |
| 735 | end |
| 736 | if ne b get t_p2_mag i |
| 737 | put g_qret_u 2 PRD2_R_MAGIC |
| 738 | ret TYQ_FAIL_PROTOCOL |
| 739 | end |
| 740 | end |
| 741 | if ne qw_rd32 which 4 2 |
| 742 | put g_qret_u 2 PRD2_R_VERSION |
| 743 | ret TYQ_FAIL_PROTOCOL |
| 744 | end |
| 745 | if or ne qw_rd32 which 8 cnt ne qw_rd32 which 12 first |
| 746 | put g_qret_u 2 PRD2_R_COUNT |
| 747 | ret TYQ_FAIL_PROTOCOL |
| 748 | end |
| 749 | off = P2_HDR |
| 750 | for i ulong cnt |
| 751 | noff = ty_prd2_parse_one which blen off add dst i add first i |
| 752 | if lt noff 0 |
| 753 | ret TYQ_FAIL_PROTOCOL |
| 754 | end |
| 755 | off = ulong noff |
| 756 | end |
| 757 | if ne off blen |
| 758 | put g_qret_u 2 PRD2_R_SIZE |
| 759 | ret TYQ_FAIL_PROTOCOL |
| 760 | end |
| 761 | ret TYQ_OK |
| 762 | end |
| 763 | |
| 764 | fn ty_prd2_enc_one which ulong off ulong p ulong to ulong |
| 765 | kv = get g_qp_k p |
| 766 | qw_wr32 which off get g_qp_idx p |
| 767 | qw_wr32 which add off 4 get g_qp_fam p |
| 768 | qw_wr32 which add off 8 kv |
| 769 | qw_wr32 which add off 12 0 |
| 770 | o = add off P2_REC |
| 771 | bo = mul p 8 |
| 772 | for j ulong kv |
| 773 | qw_wrf which o get g_qc_pi add bo j |
| 774 | qw_wrf which add o 8 get g_qc_loc add bo j |
| 775 | qw_wrf which add o 16 get g_qc_sc add bo j |
| 776 | o = add o P2_CMP |
| 777 | end |
| 778 | ret o |
| 779 | end |
| 780 | |
| 781 | note Encode PRD2 from g_qp_*[src..src+cnt-1] into g_qwire[0..]; len -> g_qret_u[0] |
| 782 | fn ty_prd2_encode which ulong src ulong first ulong cnt ulong to long |
| 783 | for i ulong cnt |
| 784 | p = add src i |
| 785 | if or ne ty_prd2_validate p TYQ_OK ne get g_qp_idx p add first i |
| 786 | ret TYQ_FAIL_PROTOCOL |
| 787 | end |
| 788 | end |
| 789 | for i ulong 4 |
| 790 | if eq which 0 |
| 791 | put g_qwire i get t_p2_mag i |
| 792 | else |
| 793 | put g_qwire2 i get t_p2_mag i |
| 794 | end |
| 795 | end |
| 796 | qw_wr32 which 4 2 |
| 797 | qw_wr32 which 8 cnt |
| 798 | qw_wr32 which 12 first |
| 799 | off = P2_HDR |
| 800 | for i ulong cnt |
| 801 | off = ty_prd2_enc_one which off add src i |
| 802 | end |
| 803 | put g_qret_u 0 off |
| 804 | ret TYQ_OK |
| 805 | end |
| 806 | |
| 807 | note ty_qcont2_bits: uses scratch slot 8191 for component Gaussian calls |
| 808 | fn ty_qcont2_bits p ulong k long to long |
| 809 | if ne ty_prd2_validate p TYQ_OK |
| 810 | ret TYQ_FAIL_PROTOCOL |
| 811 | end |
| 812 | if eq get g_qp_fam p TYQ_FAM_GAUSS |
| 813 | ret ty_qcont_bits p k |
| 814 | end |
| 815 | kv = get g_qp_k p |
| 816 | bo = mul p 8 |
| 817 | anyhit = ulong 0 |
| 818 | m = q_inf |
| 819 | for j ulong kv |
| 820 | put g_qp_idx 8191 get g_qp_idx p |
| 821 | put g_qp_fam 8191 TYQ_FAM_GAUSS |
| 822 | put g_qp_loc 8191 get g_qc_loc add bo j |
| 823 | put g_qp_sc 8191 get g_qc_sc add bo j |
| 824 | rc = ty_qcont_bits 8191 k |
| 825 | if ne rc TYQ_OK |
| 826 | ret rc |
| 827 | end |
| 828 | bj = get g_qret_f 0 |
| 829 | put g_qmix_b j bj |
| 830 | anyhit = or anyhit get g_qret_u 0 |
| 831 | if lt bj m |
| 832 | m = bj |
| 833 | end |
| 834 | end |
| 835 | acc = f64 0.0 |
| 836 | for j ulong kv |
| 837 | pi = get g_qc_pi add bo j |
| 838 | bj = get g_qmix_b j |
| 839 | acc = add acc mul pi q_exp2 sub m bj |
| 840 | end |
| 841 | v = sub m q_log2 acc |
| 842 | if not q_isfinite v |
| 843 | ret TYQ_FAIL_PROTOCOL |
| 844 | end |
| 845 | put g_qret_f 0 v |
| 846 | put g_qret_u 0 anyhit |
| 847 | ret TYQ_OK |
| 848 | end |
| 849 | |
| 850 | fn ty_qcont2_point_ub p ulong k long to long |
| 851 | if eq get g_qp_fam p TYQ_FAM_GAUSS |
| 852 | if eq ty_prd2_validate p TYQ_OK |
| 853 | ret ty_qcont_point_ub p k |
| 854 | end |
| 855 | end |
| 856 | rc = ty_qcont2_bits p k |
| 857 | if ne rc TYQ_OK |
| 858 | ret rc |
| 859 | end |
| 860 | s = mul f64 1000000.0 get g_qret_f 0 |
| 861 | if not q_isfinite s |
| 862 | ret TYQ_FAIL_PROTOCOL |
| 863 | end |
| 864 | if gt q_fabs s bitcast f64 or shl ulong 0x43df399b 32 ulong 0x1438a100 |
| 865 | ret TYQ_FAIL_PROTOCOL |
| 866 | end |
| 867 | put g_qret_i 0 q_llrint s |
| 868 | ret TYQ_OK |
| 869 | end |
| 870 | |
| 871 | fn ty_qcont2_sum_ub base ulong kbase ulong n ulong to long |
| 872 | if eq n 0 |
| 873 | ret TYQ_E_ARG |
| 874 | end |
| 875 | sum = long 0 |
| 876 | hits = ulong 0 |
| 877 | for i ulong n |
| 878 | rc = ty_qcont2_point_ub add base i get g_qk_arr add kbase i |
| 879 | if ne rc TYQ_OK |
| 880 | ret rc |
| 881 | end |
| 882 | ub = get g_qret_i 0 |
| 883 | hit = get g_qret_u 0 |
| 884 | if ne ty_add_chk sum ub TY_OK |
| 885 | ret TYQ_FAIL_PROTOCOL |
| 886 | end |
| 887 | sum = get g_qret_i 0 |
| 888 | hits = add hits hit |
| 889 | end |
| 890 | put g_qret_i 1 sum |
| 891 | put g_qret_u 1 hits |
| 892 | ret TYQ_OK |
| 893 | end |