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 / om / runtime / rx_capq_core.om
| 1 | om2 |
| 2 | note rx_capq_core.om -- Capability Graph key hashing, dimension evaluation, |
| 3 | note Pareto dominance, strict ordering, and 128-bit tolerance band in pure OM-2. |
| 4 | note Replaces pure ranking/hashing logic in src/runtime/rx_capq.c. |
| 5 | |
| 6 | const CQ_PPM ulong 1000000 |
| 7 | const MASK32 ulong 4294967295 |
| 8 | const MIX1 ulong 0x9e3779b97f4a7c15 |
| 9 | const MIX2 ulong 0xbf58476d1ce4e5b9 |
| 10 | const MIX3 ulong 0x94d049bb133111eb |
| 11 | |
| 12 | fn dim_val vals view ulong 7 dim ulong to ulong |
| 13 | if eq dim 0 |
| 14 | ret get vals 0 |
| 15 | end |
| 16 | if eq dim 1 |
| 17 | ret get vals 1 |
| 18 | end |
| 19 | if eq dim 2 |
| 20 | ret get vals 2 |
| 21 | end |
| 22 | if eq dim 3 |
| 23 | c = get vals 3 |
| 24 | if ge c CQ_PPM |
| 25 | ret 0 |
| 26 | end |
| 27 | ret sub CQ_PPM c |
| 28 | end |
| 29 | if eq dim 4 |
| 30 | r = get vals 4 |
| 31 | if ge r CQ_PPM |
| 32 | ret 0 |
| 33 | end |
| 34 | ret sub CQ_PPM r |
| 35 | end |
| 36 | if eq dim 5 |
| 37 | if ne get vals 5 0 |
| 38 | ret 0 |
| 39 | end |
| 40 | ret 1 |
| 41 | end |
| 42 | if eq dim 6 |
| 43 | if ne get vals 6 0 |
| 44 | ret 0 |
| 45 | end |
| 46 | ret 1 |
| 47 | end |
| 48 | ret 0 |
| 49 | end |
| 50 | |
| 51 | fn mul128_hi a ulong b ulong to ulong |
| 52 | a0 = and a MASK32 |
| 53 | a1 = shr a 32 |
| 54 | b0 = and b MASK32 |
| 55 | b1 = shr b 32 |
| 56 | p1 = mul a0 b1 |
| 57 | p2 = mul a1 b0 |
| 58 | mid = add shr mul a0 b0 32 and p1 MASK32 |
| 59 | mid = add mid and p2 MASK32 |
| 60 | hi = add mul a1 b1 shr p1 32 |
| 61 | hi = add hi shr p2 32 |
| 62 | ret add hi shr mid 32 |
| 63 | end |
| 64 | |
| 65 | fn mul128 a ulong b ulong dst edit ulong 2 to void |
| 66 | put dst 0 mul a b |
| 67 | put dst 1 mul128_hi a b |
| 68 | end |
| 69 | |
| 70 | export fn rx_cq_key_hash cap_id ulong real_id ulong mach_id ulong skill_id ulong to ulong |
| 71 | h = mul cap_id MIX1 |
| 72 | t1 = add add add real_id MIX2 shl h 6 shr h 2 |
| 73 | h = xor h t1 |
| 74 | t2 = add add add mach_id MIX3 shl h 6 shr h 2 |
| 75 | h = xor h t2 |
| 76 | t3 = add add add skill_id MIX1 shl h 6 shr h 2 |
| 77 | h = xor h t3 |
| 78 | ret and xor h shr h 32 MASK32 |
| 79 | end |
| 80 | |
| 81 | export fn rx_cq_dim_value vals view ulong 7 dim ulong to ulong |
| 82 | ret dim_val vals dim |
| 83 | end |
| 84 | |
| 85 | export fn rx_cq_dominates a view ulong 7 b view ulong 7 dims view uchar 7 nd ulong to ulong |
| 86 | n = nd |
| 87 | if gt n 7 |
| 88 | n = ulong 7 |
| 89 | end |
| 90 | strict = ulong 0 |
| 91 | for i ulong n bound 7 |
| 92 | d = ulong get dims i |
| 93 | x = dim_val a d |
| 94 | y = dim_val b d |
| 95 | if gt x y |
| 96 | ret 0 |
| 97 | end |
| 98 | if lt x y |
| 99 | strict = ulong 1 |
| 100 | end |
| 101 | end |
| 102 | ret strict |
| 103 | end |
| 104 | |
| 105 | note Strict lexicographic order over dims[0..nd-1], then 4 id tie-breakers in [7..10]. |
| 106 | export fn rx_cq_cmp_full a view ulong 11 b view ulong 11 dims view uchar 7 nd ulong to long |
| 107 | n = nd |
| 108 | if gt n 7 |
| 109 | n = ulong 7 |
| 110 | end |
| 111 | av = new ulong 7 0 |
| 112 | bv = new ulong 7 0 |
| 113 | for k ulong 7 |
| 114 | put av k get a k |
| 115 | put bv k get b k |
| 116 | end |
| 117 | for i ulong n bound 7 |
| 118 | d = ulong get dims i |
| 119 | x = dim_val av d |
| 120 | y = dim_val bv d |
| 121 | if lt x y |
| 122 | ret -1 |
| 123 | end |
| 124 | if gt x y |
| 125 | ret 1 |
| 126 | end |
| 127 | end |
| 128 | for j ulong 4 |
| 129 | idx = add j 7 |
| 130 | ka = get a idx |
| 131 | kb = get b idx |
| 132 | if lt ka kb |
| 133 | ret -1 |
| 134 | end |
| 135 | if gt ka kb |
| 136 | ret 1 |
| 137 | end |
| 138 | end |
| 139 | ret 0 |
| 140 | end |
| 141 | |
| 142 | note Exact 128-bit tolerance band: v * CQ_PPM <= best * (CQ_PPM + tol_ppm). |
| 143 | export fn rx_cq_within v ulong best ulong tol_ppm ulong to ulong |
| 144 | lhs = new ulong 2 0 |
| 145 | rhs = new ulong 2 0 |
| 146 | mul128 v CQ_PPM lhs |
| 147 | mul128 best add CQ_PPM tol_ppm rhs |
| 148 | lhi = get lhs 1 |
| 149 | rhi = get rhs 1 |
| 150 | if lt lhi rhi |
| 151 | ret 1 |
| 152 | end |
| 153 | if gt lhi rhi |
| 154 | ret 0 |
| 155 | end |
| 156 | if le get lhs 0 get rhs 0 |
| 157 | ret 1 |
| 158 | end |
| 159 | ret 0 |
| 160 | end |
| 161 | |
| 162 | note Compute allowed source mask for a compiled query plan. |
| 163 | export fn rx_cq_plan_sources domains ulong need_src ulong loc ulong pin ulong self_mach ulong to ulong |
| 164 | ns = need_src |
| 165 | if eq ns 0 |
| 166 | ns = ulong 31 |
| 167 | end |
| 168 | s = and domains ns |
| 169 | aloc = loc |
| 170 | if eq aloc 0 |
| 171 | aloc = ulong 3 |
| 172 | end |
| 173 | if eq and aloc 1 0 |
| 174 | s = and s 16 |
| 175 | end |
| 176 | if eq and aloc 2 0 |
| 177 | s = and s 15 |
| 178 | end |
| 179 | if ne pin 0 |
| 180 | if eq pin self_mach |
| 181 | s = and s 15 |
| 182 | else |
| 183 | s = and s 16 |
| 184 | end |
| 185 | end |
| 186 | ret s |
| 187 | end |
| 188 | |
| 189 | note Check feasibility of one catalog entry against need + tradeoffs. |
| 190 | note ent[14]: 0=mach, 1=in_t, 2=out_t, 3=fx, 4=auth_res, 5=auth_r, 6=lat, |
| 191 | note 7=eng, 8=rel, 9=ev, 10=cost, 11=conf, 12=local, 13=live |
| 192 | note need[14]: 0=loc, 1=pin, 2=acc_in, 3=req_out, 4=fx_cls, 5=res_lo, 6=res_hi, |
| 193 | note 7=max_r, 8=lat_b, 9=eng_b, 10=rel_req, 11=ev_req, 12=max_cost, 13=min_conf |
| 194 | export fn rx_cq_filter_entry ent view ulong 14 need view ulong 14 to ulong |
| 195 | loc = get need 0 |
| 196 | local_bit = ulong 2 |
| 197 | if ne get ent 12 0 |
| 198 | local_bit = ulong 1 |
| 199 | end |
| 200 | if eq and loc local_bit 0 |
| 201 | ret 0 |
| 202 | end |
| 203 | pin = get need 1 |
| 204 | if ne pin 0 |
| 205 | if ne get ent 0 pin |
| 206 | ret 0 |
| 207 | end |
| 208 | end |
| 209 | if ne and get ent 1 xor get need 2 0xffffffffffffffff 0 |
| 210 | ret 0 |
| 211 | end |
| 212 | if ne and get need 3 xor get ent 2 0xffffffffffffffff 0 |
| 213 | ret 0 |
| 214 | end |
| 215 | if ne and get ent 3 xor get need 4 0xffffffffffffffff 0 |
| 216 | ret 0 |
| 217 | end |
| 218 | ar = get ent 5 |
| 219 | if ne and ar xor get need 7 0xffffffffffffffff 0 |
| 220 | ret 0 |
| 221 | end |
| 222 | if ne ar 0 |
| 223 | res = get ent 4 |
| 224 | if or lt res get need 5 gt res get need 6 |
| 225 | ret 0 |
| 226 | end |
| 227 | end |
| 228 | lb = get need 8 |
| 229 | if ne lb 0 |
| 230 | if gt get ent 6 lb |
| 231 | ret 0 |
| 232 | end |
| 233 | end |
| 234 | eb = get need 9 |
| 235 | if ne eb 0 |
| 236 | if gt get ent 7 eb |
| 237 | ret 0 |
| 238 | end |
| 239 | end |
| 240 | if lt get ent 8 get need 10 |
| 241 | ret 0 |
| 242 | end |
| 243 | if lt get ent 9 get need 11 |
| 244 | ret 0 |
| 245 | end |
| 246 | mc = get need 12 |
| 247 | if ne mc 0 |
| 248 | if gt get ent 10 mc |
| 249 | ret 0 |
| 250 | end |
| 251 | end |
| 252 | if lt get ent 11 get need 13 |
| 253 | ret 0 |
| 254 | end |
| 255 | ret 1 |
| 256 | end |
| 257 | |
| 258 | fn cq_put_u16 dst edit uchar 188 off ulong v ulong to void |
| 259 | put dst off uchar and v 255 |
| 260 | put dst add off 1 uchar and shr v 8 255 |
| 261 | end |
| 262 | |
| 263 | fn cq_put_u32 dst edit uchar 188 off ulong v ulong to void |
| 264 | cq_put_u16 dst off v |
| 265 | cq_put_u16 dst add off 2 shr v 16 |
| 266 | end |
| 267 | |
| 268 | fn cq_put_u64 dst edit uchar 188 off ulong v ulong to void |
| 269 | cq_put_u32 dst off v |
| 270 | cq_put_u32 dst add off 4 shr v 32 |
| 271 | end |
| 272 | |
| 273 | fn cq_get_u16 src view uchar 188 off ulong to ulong |
| 274 | b0 = ulong get src off |
| 275 | b1 = ulong get src add off 1 |
| 276 | ret or b0 shl b1 8 |
| 277 | end |
| 278 | |
| 279 | fn cq_get_u32 src view uchar 188 off ulong to ulong |
| 280 | lo = cq_get_u16 src off |
| 281 | hi = cq_get_u16 src add off 2 |
| 282 | ret or lo shl hi 16 |
| 283 | end |
| 284 | |
| 285 | fn cq_get_u64 src view uchar 188 off ulong to ulong |
| 286 | lo = cq_get_u32 src off |
| 287 | hi = cq_get_u32 src add off 4 |
| 288 | ret or lo shl hi 32 |
| 289 | end |
| 290 | |
| 291 | note Encode 184-byte ACGR wire record body (caller appends 4-byte SHA-256 check). |
| 292 | note hdr[4]: 0=kind (1..3), 1=source, 2=evidence_level, 3=live |
| 293 | note fields[17]: cap_id, real_id, skill_id, skill_ver, op, effects, in_types, |
| 294 | note out_types, auth_res, auth_rights, conf, rel, cost, lat, eng, ev_ref, gen |
| 295 | export fn rx_cq_wire_encode_body hdr view ulong 4 fields view ulong 17 mid view uchar 44 sdig view uchar 32 dst edit uchar 188 to ulong |
| 296 | kind = get hdr 0 |
| 297 | src = get hdr 1 |
| 298 | ev = get hdr 2 |
| 299 | live = get hdr 3 |
| 300 | if or lt kind 1 gt kind 3 |
| 301 | ret 0 |
| 302 | end |
| 303 | if or ge src 5 eq src 4 |
| 304 | ret 0 |
| 305 | end |
| 306 | if gt live 2 |
| 307 | ret 0 |
| 308 | end |
| 309 | for i ulong 188 |
| 310 | put dst i uchar 0 |
| 311 | end |
| 312 | put dst 0 uchar 65 |
| 313 | put dst 1 uchar 67 |
| 314 | put dst 2 uchar 71 |
| 315 | put dst 3 uchar 82 |
| 316 | put dst 4 uchar 1 |
| 317 | put dst 5 uchar and kind 255 |
| 318 | put dst 6 uchar and src 255 |
| 319 | put dst 7 uchar and ev 255 |
| 320 | for i ulong 44 |
| 321 | put dst add 8 i get mid i |
| 322 | end |
| 323 | cq_put_u32 dst 52 get fields 0 |
| 324 | cq_put_u32 dst 56 get fields 1 |
| 325 | cq_put_u32 dst 60 get fields 2 |
| 326 | cq_put_u32 dst 64 get fields 3 |
| 327 | for i ulong 32 |
| 328 | put dst add 68 i get sdig i |
| 329 | end |
| 330 | cq_put_u32 dst 100 get fields 4 |
| 331 | cq_put_u16 dst 104 get fields 5 |
| 332 | wlive = live |
| 333 | if eq kind 3 |
| 334 | wlive = ulong 0 |
| 335 | end |
| 336 | cq_put_u16 dst 106 wlive |
| 337 | cq_put_u64 dst 108 get fields 6 |
| 338 | cq_put_u64 dst 116 get fields 7 |
| 339 | cq_put_u64 dst 124 get fields 8 |
| 340 | cq_put_u32 dst 132 get fields 9 |
| 341 | cq_put_u32 dst 136 get fields 10 |
| 342 | cq_put_u32 dst 140 get fields 11 |
| 343 | cq_put_u64 dst 144 get fields 12 |
| 344 | cq_put_u64 dst 152 get fields 13 |
| 345 | cq_put_u64 dst 160 get fields 14 |
| 346 | cq_put_u64 dst 168 get fields 15 |
| 347 | cq_put_u64 dst 176 get fields 16 |
| 348 | ret 1 |
| 349 | end |
| 350 | |
| 351 | note Decode and validate 188-byte ACGR wire record body (caller verifies 4-byte SHA-256 check). |
| 352 | export fn rx_cq_wire_decode_body src view uchar 188 hdr edit ulong 4 fields edit ulong 17 sdig edit uchar 32 to ulong |
| 353 | if or ne get src 0 uchar 65 ne get src 1 uchar 67 |
| 354 | ret 0 |
| 355 | end |
| 356 | if or ne get src 2 uchar 71 ne get src 3 uchar 82 |
| 357 | ret 0 |
| 358 | end |
| 359 | if ne get src 4 uchar 1 |
| 360 | ret 0 |
| 361 | end |
| 362 | kind = ulong get src 5 |
| 363 | source = ulong get src 6 |
| 364 | ev = ulong get src 7 |
| 365 | live = cq_get_u16 src 106 |
| 366 | if or lt kind 1 gt kind 3 |
| 367 | ret 0 |
| 368 | end |
| 369 | if or ge source 5 eq source 4 |
| 370 | ret 0 |
| 371 | end |
| 372 | if or gt ev 3 gt live 2 |
| 373 | ret 0 |
| 374 | end |
| 375 | is_w = eq kind 3 |
| 376 | is_lw = eq live 0 |
| 377 | if ne is_w is_lw |
| 378 | ret 0 |
| 379 | end |
| 380 | put hdr 0 kind |
| 381 | put hdr 1 source |
| 382 | put hdr 2 ev |
| 383 | put hdr 3 live |
| 384 | put fields 0 cq_get_u32 src 52 |
| 385 | put fields 1 cq_get_u32 src 56 |
| 386 | put fields 2 cq_get_u32 src 60 |
| 387 | put fields 3 cq_get_u32 src 64 |
| 388 | for i ulong 32 |
| 389 | put sdig i get src add 68 i |
| 390 | end |
| 391 | put fields 4 cq_get_u32 src 100 |
| 392 | put fields 5 cq_get_u16 src 104 |
| 393 | put fields 6 cq_get_u64 src 108 |
| 394 | put fields 7 cq_get_u64 src 116 |
| 395 | put fields 8 cq_get_u64 src 124 |
| 396 | put fields 9 cq_get_u32 src 132 |
| 397 | put fields 10 cq_get_u32 src 136 |
| 398 | put fields 11 cq_get_u32 src 140 |
| 399 | put fields 12 cq_get_u64 src 144 |
| 400 | put fields 13 cq_get_u64 src 152 |
| 401 | put fields 14 cq_get_u64 src 160 |
| 402 | put fields 15 cq_get_u64 src 168 |
| 403 | put fields 16 cq_get_u64 src 176 |
| 404 | ret 1 |
| 405 | end |
| 406 | |
| 407 | note Validate wire-apply generation and withdrawal state transitions. |
| 408 | note Returns 0 (CQ_OK), -11 (CQ_E_STALE), -7 (CQ_E_NOT_FOUND), -8 (CQ_E_WITHDRAWN). |
| 409 | export fn rx_cq_wire_apply_check has_old ulong old_gen ulong old_live ulong new_gen ulong kind ulong to long |
| 410 | if ne has_old 0 |
| 411 | if le new_gen old_gen |
| 412 | ret -11 |
| 413 | end |
| 414 | else |
| 415 | if ne kind 1 |
| 416 | ret -7 |
| 417 | end |
| 418 | end |
| 419 | if and eq kind 2 eq old_live 0 |
| 420 | ret -8 |
| 421 | end |
| 422 | ret 0 |
| 423 | end |