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 / dual_experiment / applic_core.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Pure seeded OM-2 core logic backing src/dual_experiment/ |
| 3 | note (applic_sim.c, applic_predict.c, applic_metrics.c): xorshift64* RNG, |
| 4 | note seed/sweep/regime/ledger helpers, flow-balance-v2 aggregate predictor, |
| 5 | note error finalization, deterministic load heapsort, and Pearson correlation. |
| 6 | om2 |
| 7 | |
| 8 | fn ap_rng_step st edit ulong 1 to ulong |
| 9 | s = get st 0 |
| 10 | s = xor s shr s 12 |
| 11 | s = xor s shl s 25 |
| 12 | s = xor s shr s 27 |
| 13 | put st 0 s |
| 14 | ret mul s 0x2545f4914f6cdd1d |
| 15 | end |
| 16 | |
| 17 | fn ap_clamp01 x f64 to f64 |
| 18 | z0 = bitcast f64 ulong 0 |
| 19 | one = bitcast f64 ulong 0x3ff0000000000000 |
| 20 | if lt x z0 |
| 21 | ret z0 |
| 22 | end |
| 23 | if gt x one |
| 24 | ret one |
| 25 | end |
| 26 | ret x |
| 27 | end |
| 28 | |
| 29 | fn ap_get_u64 src view uchar * pos ulong to ulong |
| 30 | v = ulong 0 |
| 31 | for i ulong 8 |
| 32 | b = ulong get src add pos i |
| 33 | v = or v shl b mul i 8 |
| 34 | end |
| 35 | ret v |
| 36 | end |
| 37 | |
| 38 | fn ap_get_u32 src view uchar * pos ulong to ulong |
| 39 | v = ulong 0 |
| 40 | for i ulong 4 |
| 41 | b = ulong get src add pos i |
| 42 | v = or v shl b mul i 8 |
| 43 | end |
| 44 | ret v |
| 45 | end |
| 46 | |
| 47 | fn ap_item_load items view uchar * idx ulong to ulong |
| 48 | base = mul idx 16 |
| 49 | d = ap_get_u32 items add base 4 |
| 50 | s = ulong get items add base 14 |
| 51 | ret mul d s |
| 52 | end |
| 53 | |
| 54 | fn ap_load_before items view uchar * a ulong b ulong to bool |
| 55 | la = ap_item_load items a |
| 56 | lb = ap_item_load items b |
| 57 | if ne la lb |
| 58 | ret gt la lb |
| 59 | end |
| 60 | ret lt a b |
| 61 | end |
| 62 | |
| 63 | fn ap_sift items view uchar * idx edit uint * root ulong n ulong to void |
| 64 | r = root |
| 65 | while true |
| 66 | child = add mul r 2 1 |
| 67 | if ge child n |
| 68 | ret |
| 69 | end |
| 70 | pick = r |
| 71 | ci = ulong get idx child |
| 72 | pi = ulong get idx pick |
| 73 | if not ap_load_before items ci pi |
| 74 | pick = child |
| 75 | pi = ci |
| 76 | end |
| 77 | c2 = add child 1 |
| 78 | if lt c2 n |
| 79 | c2i = ulong get idx c2 |
| 80 | if not ap_load_before items c2i pi |
| 81 | pick = c2 |
| 82 | end |
| 83 | end |
| 84 | if eq pick r |
| 85 | ret |
| 86 | end |
| 87 | tmp = get idx r |
| 88 | put idx r get idx pick |
| 89 | put idx pick tmp |
| 90 | r = pick |
| 91 | end |
| 92 | end |
| 93 | |
| 94 | export fn applic_om_rng_next st edit ulong 1 to ulong |
| 95 | ret ap_rng_step st |
| 96 | end |
| 97 | |
| 98 | export fn applic_om_rng_unit st edit ulong 1 to ulong |
| 99 | v = ap_rng_step st |
| 100 | q = shr v 11 |
| 101 | scale = bitcast f64 ulong 0x4340000000000000 |
| 102 | ret bitcast ulong div f64 q scale |
| 103 | end |
| 104 | |
| 105 | export fn applic_om_rng_range st edit ulong 1 lo ulong hi ulong to ulong |
| 106 | v = ap_rng_step st |
| 107 | span = add sub hi lo 1 |
| 108 | ret add lo rem v span |
| 109 | end |
| 110 | |
| 111 | export fn applic_om_seed wl ulong seed_index ulong to ulong |
| 112 | base = ulong 0x4455414c2d415031 |
| 113 | a = mul wl 0x9e3779b97f4a7c15 |
| 114 | b = mul add seed_index 1 0xd1b54a32d192ed03 |
| 115 | s = xor xor base a b |
| 116 | if eq s 0 |
| 117 | ret 1 |
| 118 | end |
| 119 | ret s |
| 120 | end |
| 121 | |
| 122 | export fn applic_om_sweep_sigma seed_index ulong to ulong |
| 123 | f = div f64 seed_index f64 ulong 23 |
| 124 | smax = bitcast f64 ulong 0x3ff8000000000000 |
| 125 | ret bitcast ulong mul smax f |
| 126 | end |
| 127 | |
| 128 | export fn applic_om_family_is_het wl ulong step ulong to ulong |
| 129 | if or eq wl 1 eq wl 7 |
| 130 | ret 1 |
| 131 | end |
| 132 | if and eq wl 6 ge step 1000 |
| 133 | ret 1 |
| 134 | end |
| 135 | ret 0 |
| 136 | end |
| 137 | |
| 138 | export fn applic_om_capacity_this_step wl ulong step ulong to ulong |
| 139 | if and eq wl 5 ge step 1000 |
| 140 | ret 20 |
| 141 | end |
| 142 | ret 32 |
| 143 | end |
| 144 | |
| 145 | export fn applic_om_ledger_ok counts view ulong 5 to ulong |
| 146 | sum = add add add get counts 1 get counts 2 get counts 3 get counts 4 |
| 147 | if eq get counts 0 sum |
| 148 | ret 1 |
| 149 | end |
| 150 | ret 0 |
| 151 | end |
| 152 | |
| 153 | fn ap_pred_flow inp view ulong 8 mean_d f64 to f64 |
| 154 | w = bitcast f64 ulong 0x4034000000000000 |
| 155 | one = bitcast f64 ulong 0x3ff0000000000000 |
| 156 | z0 = bitcast f64 ulong 0 |
| 157 | frac = div w mean_d |
| 158 | if gt frac one |
| 159 | frac = one |
| 160 | end |
| 161 | late = z0 |
| 162 | if gt sub w mean_d z0 |
| 163 | late = div sub w mean_d w |
| 164 | end |
| 165 | arr = bitcast f64 get inp 7 |
| 166 | r_term = mul f64 get inp 1 frac |
| 167 | q_term = mul f64 get inp 2 frac |
| 168 | ret add add r_term q_term mul arr late |
| 169 | end |
| 170 | |
| 171 | fn ap_pred_rcap inp view ulong 8 to f64 |
| 172 | z0 = bitcast f64 ulong 0 |
| 173 | one = bitcast f64 ulong 0x3ff0000000000000 |
| 174 | w = bitcast f64 ulong 0x4034000000000000 |
| 175 | mem = bitcast f64 ulong 0x4090000000000000 |
| 176 | mean_s = bitcast f64 get inp 5 |
| 177 | if le mean_s z0 |
| 178 | mean_s = one |
| 179 | end |
| 180 | mean_m = bitcast f64 get inp 6 |
| 181 | if le mean_m z0 |
| 182 | mean_m = w |
| 183 | end |
| 184 | by_slots = div f64 get inp 0 mean_s |
| 185 | by_mem = div mem mean_m |
| 186 | if lt by_mem by_slots |
| 187 | ret by_mem |
| 188 | end |
| 189 | ret by_slots |
| 190 | end |
| 191 | |
| 192 | fn ap_pred_finish in_next f64 rcap f64 comp f64 out_p edit ulong 3 to void |
| 193 | z0 = bitcast f64 ulong 0 |
| 194 | one = bitcast f64 ulong 0x3ff0000000000000 |
| 195 | maxq = bitcast f64 ulong 0x4070000000000000 |
| 196 | qdens = ap_clamp01 div sub in_next rcap maxq |
| 197 | occ = one |
| 198 | if gt rcap z0 |
| 199 | occ = div in_next rcap |
| 200 | end |
| 201 | ramp = bitcast f64 ulong 0x3fe8000000000000 |
| 202 | rspan = bitcast f64 ulong 0x3fd0000000000000 |
| 203 | blk = ap_clamp01 div sub occ ramp rspan |
| 204 | put out_p 0 bitcast ulong comp |
| 205 | put out_p 1 bitcast ulong qdens |
| 206 | put out_p 2 bitcast ulong blk |
| 207 | end |
| 208 | |
| 209 | export fn applic_om_predict_with_arrivals inp view ulong 8 out_p edit ulong 3 to void |
| 210 | z0 = bitcast f64 ulong 0 |
| 211 | w = bitcast f64 ulong 0x4034000000000000 |
| 212 | mean_d = bitcast f64 get inp 4 |
| 213 | if le mean_d z0 |
| 214 | mean_d = bitcast f64 ulong 0x4028000000000000 |
| 215 | end |
| 216 | rcap = ap_pred_rcap inp |
| 217 | cap_w = div mul rcap w mean_d |
| 218 | comp = ap_pred_flow inp mean_d |
| 219 | if lt cap_w comp |
| 220 | comp = cap_w |
| 221 | end |
| 222 | in_next = sub add f64 get inp 3 bitcast f64 get inp 7 comp |
| 223 | if lt in_next z0 |
| 224 | in_next = z0 |
| 225 | end |
| 226 | ap_pred_finish in_next rcap comp out_p |
| 227 | end |
| 228 | |
| 229 | export fn applic_om_error_finalize se view ulong 4 scored ulong out_e edit ulong 4 to void |
| 230 | z0 = bitcast f64 ulong 0 |
| 231 | if eq scored 0 |
| 232 | for i ulong 4 |
| 233 | put out_e i 0 |
| 234 | end |
| 235 | ret |
| 236 | end |
| 237 | n = f64 scored |
| 238 | mean_c = div bitcast f64 get se 3 n |
| 239 | rc = fsqrt div bitcast f64 get se 0 n |
| 240 | if gt mean_c z0 |
| 241 | rc = div rc mean_c |
| 242 | end |
| 243 | rq = fsqrt div bitcast f64 get se 1 n |
| 244 | rb = fsqrt div bitcast f64 get se 2 n |
| 245 | three = bitcast f64 ulong 0x4008000000000000 |
| 246 | comp = div add add rc rq rb three |
| 247 | put out_e 0 bitcast ulong rc |
| 248 | put out_e 1 bitcast ulong rq |
| 249 | put out_e 2 bitcast ulong rb |
| 250 | put out_e 3 bitcast ulong comp |
| 251 | end |
| 252 | |
| 253 | export fn applic_om_sort_desc items view uchar * idx edit uint * n ulong to void |
| 254 | if lt n 2 |
| 255 | ret |
| 256 | end |
| 257 | i = div n 2 |
| 258 | while gt i 0 |
| 259 | i = sub i 1 |
| 260 | ap_sift items idx i n |
| 261 | end |
| 262 | i = sub n 1 |
| 263 | while gt i 0 |
| 264 | t = get idx 0 |
| 265 | put idx 0 get idx i |
| 266 | put idx i t |
| 267 | ap_sift items idx 0 i |
| 268 | i = sub i 1 |
| 269 | end |
| 270 | end |
| 271 | |
| 272 | fn ap_mean_f64 bytes view uchar * n ulong to f64 |
| 273 | s = bitcast f64 ulong 0 |
| 274 | for i ulong n |
| 275 | s = add s bitcast f64 ap_get_u64 bytes mul i 8 |
| 276 | end |
| 277 | ret div s f64 n |
| 278 | end |
| 279 | |
| 280 | fn ap_sum_sq_dev bytes view uchar * n ulong m f64 to f64 |
| 281 | s = bitcast f64 ulong 0 |
| 282 | for i ulong n |
| 283 | d = sub bitcast f64 ap_get_u64 bytes mul i 8 m |
| 284 | s = add s mul d d |
| 285 | end |
| 286 | ret s |
| 287 | end |
| 288 | |
| 289 | fn ap_sum_xy_dev xbytes view uchar * ybytes view uchar * mx f64 my f64 to f64 |
| 290 | n = div len xbytes 8 |
| 291 | s = bitcast f64 ulong 0 |
| 292 | for i ulong n |
| 293 | dx = sub bitcast f64 ap_get_u64 xbytes mul i 8 mx |
| 294 | dy = sub bitcast f64 ap_get_u64 ybytes mul i 8 my |
| 295 | s = add s mul dx dy |
| 296 | end |
| 297 | ret s |
| 298 | end |
| 299 | |
| 300 | export fn applic_om_pearson xbytes view uchar * ybytes view uchar * n ulong out_r edit ulong 2 to void |
| 301 | z0 = bitcast f64 ulong 0 |
| 302 | put out_r 0 0 |
| 303 | put out_r 1 0 |
| 304 | if lt n 2 |
| 305 | ret |
| 306 | end |
| 307 | mx = ap_mean_f64 xbytes n |
| 308 | my = ap_mean_f64 ybytes n |
| 309 | sxx = ap_sum_sq_dev xbytes n mx |
| 310 | syy = ap_sum_sq_dev ybytes n my |
| 311 | sxy = ap_sum_xy_dev xbytes ybytes mx my |
| 312 | put out_r 1 bitcast ulong div sxy f64 sub n 1 |
| 313 | if or le sxx z0 le syy z0 |
| 314 | ret |
| 315 | end |
| 316 | put out_r 0 bitcast ulong div sxy fsqrt mul sxx syy |
| 317 | end |