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 / reservoir / reservoir.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | note |
| 4 | note Pure seeded OM-2 ionic-droplet reservoir CPU reference and binary64 ridge |
| 5 | note readout (omega#440, Gate 1). Replaces src/reservoir/omega_reservoir.{c,h} |
| 6 | note and src/reservoir/omega_reservoir_readout.{c,h}. |
| 7 | note Zero host I/O, zero allocation, zero libm, zero device operations. |
| 8 | om2 |
| 9 | |
| 10 | note ---- status codes (OmegaResStatus) ---- |
| 11 | const RES_OK int 0 |
| 12 | const RES_ERR_NULL int -1 |
| 13 | const RES_ERR_TOPOLOGY int -2 |
| 14 | const RES_ERR_PARAM int -3 |
| 15 | const RES_ERR_NONFINITE int -4 |
| 16 | const RES_ERR_RANGE int -5 |
| 17 | const RES_ERR_STIFF int -6 |
| 18 | const RES_ERR_INPUT int -7 |
| 19 | const RES_ERR_MISMATCH int -8 |
| 20 | const RES_ERR_FPENV int -9 |
| 21 | const RES_ERR_SINGULAR int -10 |
| 22 | const RES_ERR_ALIAS int -11 |
| 23 | |
| 24 | note ---- model bounds and enum tags ---- |
| 25 | const RES_MAX_NODES ulong 64 |
| 26 | const RES_MAX_DEGREE ulong 6 |
| 27 | const RES_MAX_EDGES ulong 192 |
| 28 | const RES_MAX_SIDES ulong 384 |
| 29 | const RES_DIGEST_SIZE ulong 32 |
| 30 | |
| 31 | const RES_REF_NARMA uint 1 |
| 32 | const RES_REF_LORENZ uint 2 |
| 33 | const RES_INIT_N0 uint 1 |
| 34 | const RES_INIT_CONST uint 2 |
| 35 | |
| 36 | note ---- flat array layout sizes ---- |
| 37 | note ResTopology: [0]=node_count [1]=edge_count [2..385]=edges(a,b) [386..449]=degree [450..513]=is_ground |
| 38 | const RES_TOPO_WORDS ulong 514 |
| 39 | const RES_TOPO_EDGES ulong 2 |
| 40 | const RES_TOPO_DEG ulong 386 |
| 41 | const RES_TOPO_GND ulong 450 |
| 42 | |
| 43 | note ResParams: [0]=node_count [1..8]=globals [9..72]=conc [73..136]=tau0 [137..200]=v_tau [201..264]=v_e |
| 44 | const RES_PAR_WORDS ulong 265 |
| 45 | const RES_PAR_CONC ulong 9 |
| 46 | const RES_PAR_TAU0 ulong 73 |
| 47 | const RES_PAR_VTAU ulong 137 |
| 48 | const RES_PAR_VE ulong 201 |
| 49 | |
| 50 | note ResConfig: [0..513]=topo [514..778]=par [779]=step_s [780]=steps_per_knot [781]=reduced_count |
| 51 | note [782..845]=reduced_index [846..909]=reduced_node [910..5005]=chol [5006..5069]=chol_inv_diag |
| 52 | note [5070..5133]=ln_n0 [5134..5197]=ln_alpha0 [5198..5261]=inv_v_tau [5262..5325]=inv_v_e |
| 53 | note [5326]=inv_cap [5327]=g_unit [5328]=lambda_unit [5329]=half_step [5330]=sixth_step [5331..5362]=id(32) |
| 54 | const RES_CFG_WORDS ulong 5363 |
| 55 | const RES_CFG_PAR ulong 514 |
| 56 | const RES_CFG_STEP_S ulong 779 |
| 57 | const RES_CFG_K ulong 780 |
| 58 | const RES_CFG_RED_CNT ulong 781 |
| 59 | const RES_CFG_RED_IDX ulong 782 |
| 60 | const RES_CFG_RED_NODE ulong 846 |
| 61 | const RES_CFG_CHOL ulong 910 |
| 62 | const RES_CFG_CHOL_INV ulong 5006 |
| 63 | const RES_CFG_LN_N0 ulong 5070 |
| 64 | const RES_CFG_LN_A0 ulong 5134 |
| 65 | const RES_CFG_INV_VTAU ulong 5198 |
| 66 | const RES_CFG_INV_VE ulong 5262 |
| 67 | const RES_CFG_INV_CAP ulong 5326 |
| 68 | const RES_CFG_G_UNIT ulong 5327 |
| 69 | const RES_CFG_LAM_UNIT ulong 5328 |
| 70 | const RES_CFG_HALF_H ulong 5329 |
| 71 | const RES_CFG_SIXTH_H ulong 5330 |
| 72 | const RES_CFG_ID ulong 5331 |
| 73 | |
| 74 | note ResState: [0..31]=config_id [32]=step_lo [33]=step_hi [34]=node_count [35]=side_count |
| 75 | note [36..99]=v [100..483]=y [484..547]=vc [548..931]=yc |
| 76 | const RES_STATE_WORDS ulong 932 |
| 77 | const RES_ST_STEP_LO ulong 32 |
| 78 | const RES_ST_STEP_HI ulong 33 |
| 79 | const RES_ST_NODES ulong 34 |
| 80 | const RES_ST_SIDES ulong 35 |
| 81 | const RES_ST_V ulong 36 |
| 82 | const RES_ST_Y ulong 100 |
| 83 | const RES_ST_VC ulong 484 |
| 84 | const RES_ST_YC ulong 548 |
| 85 | |
| 86 | note ResMembraneStats: [0..1]=obs [2..3]=imp [4]=mem_imp [5]=max_mv [6]=max_nv [7..198]=edge_flag |
| 87 | const RES_MSTATS_WORDS ulong 200 |
| 88 | const RES_MS_OBS_LO ulong 0 |
| 89 | const RES_MS_OBS_HI ulong 1 |
| 90 | const RES_MS_IMP_LO ulong 2 |
| 91 | const RES_MS_IMP_HI ulong 3 |
| 92 | const RES_MS_MEM_IMP ulong 4 |
| 93 | const RES_MS_MAX_MV ulong 5 |
| 94 | const RES_MS_MAX_NV ulong 6 |
| 95 | const RES_MS_EFLAG ulong 7 |
| 96 | |
| 97 | note ResWorkspace: [0..63]=b [64..127]=r [128..383]=kv(4*64) [384..1919]=ky(4*384) |
| 98 | note [1920..1983]=sv [1984..2367]=sy [2368..2431]=cur [2432..3363]=spare(932) [3364..3563]=mstats(200) |
| 99 | const RES_WS_WORDS ulong 3564 |
| 100 | const RES_WS_B ulong 0 |
| 101 | const RES_WS_R ulong 64 |
| 102 | const RES_WS_KV ulong 128 |
| 103 | const RES_WS_KY ulong 384 |
| 104 | const RES_WS_SV ulong 1920 |
| 105 | const RES_WS_SY ulong 1984 |
| 106 | const RES_WS_CUR ulong 2368 |
| 107 | const RES_WS_SPARE ulong 2432 |
| 108 | const RES_WS_MSTATS ulong 3364 |
| 109 | |
| 110 | note ResRunReceipt: [0..31]=config_id [32..63]=input_digest [64..95]=final_state_digest |
| 111 | note [96..97]=steps [98..129]=run_id |
| 112 | const RES_RECEIPT_WORDS ulong 130 |
| 113 | |
| 114 | note Readout constants |
| 115 | const RES_RO_MAX_FEAT ulong 256 |
| 116 | const RES_RO_MAX_OUT ulong 4 |
| 117 | const RES_RO_DIM ulong 257 |
| 118 | const RES_RO_WORDS ulong 1031 |
| 119 | const RES_RWS_WORDS ulong 67334 |
| 120 | const RES_RWS_D_OFF ulong 66049 |
| 121 | const RES_RWS_R_OFF ulong 66306 |
| 122 | |
| 123 | note Domain strings and numeric policy |
| 124 | text res_dom_config "omega.reservoir.config.v1" |
| 125 | text res_dom_input "omega.reservoir.input.v1" |
| 126 | text res_dom_state "omega.reservoir.state.v1" |
| 127 | text res_dom_run "omega.reservoir.run.v1" |
| 128 | text res_num_policy "omega.reservoir.numeric.v2: binary32 RNE no-FTZ; ffp-contract=off; omega_math_exp/log/div/sqrt; state y=lnN; rk4 fixed step, TwoSum-compensated update; 1-e^w Taylor9 |w|<=0.5; dense Cholesky of reduced Laplacian; refuse |Va-Vb|>1 V per membrane, node V finite only" |
| 129 | |
| 130 | note ================================================================ |
| 131 | note SHA-256 (FIPS 180-4, adapted from om/sha256/sha256-om2.om) |
| 132 | note ================================================================ |
| 133 | |
| 134 | fn res_sha_kinit k edit uint 64 to void |
| 135 | put k 0 0x428a2f98 |
| 136 | put k 1 0x71374491 |
| 137 | put k 2 0xb5c0fbcf |
| 138 | put k 3 0xe9b5dba5 |
| 139 | put k 4 0x3956c25b |
| 140 | put k 5 0x59f111f1 |
| 141 | put k 6 0x923f82a4 |
| 142 | put k 7 0xab1c5ed5 |
| 143 | put k 8 0xd807aa98 |
| 144 | put k 9 0x12835b01 |
| 145 | put k 10 0x243185be |
| 146 | put k 11 0x550c7dc3 |
| 147 | put k 12 0x72be5d74 |
| 148 | put k 13 0x80deb1fe |
| 149 | put k 14 0x9bdc06a7 |
| 150 | put k 15 0xc19bf174 |
| 151 | put k 16 0xe49b69c1 |
| 152 | put k 17 0xefbe4786 |
| 153 | put k 18 0x0fc19dc6 |
| 154 | put k 19 0x240ca1cc |
| 155 | put k 20 0x2de92c6f |
| 156 | put k 21 0x4a7484aa |
| 157 | put k 22 0x5cb0a9dc |
| 158 | put k 23 0x76f988da |
| 159 | put k 24 0x983e5152 |
| 160 | put k 25 0xa831c66d |
| 161 | put k 26 0xb00327c8 |
| 162 | put k 27 0xbf597fc7 |
| 163 | put k 28 0xc6e00bf3 |
| 164 | put k 29 0xd5a79147 |
| 165 | put k 30 0x06ca6351 |
| 166 | put k 31 0x14292967 |
| 167 | put k 32 0x27b70a85 |
| 168 | put k 33 0x2e1b2138 |
| 169 | put k 34 0x4d2c6dfc |
| 170 | put k 35 0x53380d13 |
| 171 | put k 36 0x650a7354 |
| 172 | put k 37 0x766a0abb |
| 173 | put k 38 0x81c2c92e |
| 174 | put k 39 0x92722c85 |
| 175 | put k 40 0xa2bfe8a1 |
| 176 | put k 41 0xa81a664b |
| 177 | put k 42 0xc24b8b70 |
| 178 | put k 43 0xc76c51a3 |
| 179 | put k 44 0xd192e819 |
| 180 | put k 45 0xd6990624 |
| 181 | put k 46 0xf40e3585 |
| 182 | put k 47 0x106aa070 |
| 183 | put k 48 0x19a4c116 |
| 184 | put k 49 0x1e376c08 |
| 185 | put k 50 0x2748774c |
| 186 | put k 51 0x34b0bcb5 |
| 187 | put k 52 0x391c0cb3 |
| 188 | put k 53 0x4ed8aa4a |
| 189 | put k 54 0x5b9cca4f |
| 190 | put k 55 0x682e6ff3 |
| 191 | put k 56 0x748f82ee |
| 192 | put k 57 0x78a5636f |
| 193 | put k 58 0x84c87814 |
| 194 | put k 59 0x8cc70208 |
| 195 | put k 60 0x90befffa |
| 196 | put k 61 0xa4506ceb |
| 197 | put k 62 0xbef9a3f7 |
| 198 | put k 63 0xc67178f2 |
| 199 | end |
| 200 | |
| 201 | fn res_sha_iv s edit uint 8 to void |
| 202 | put s 0 0x6a09e667 |
| 203 | put s 1 0xbb67ae85 |
| 204 | put s 2 0x3c6ef372 |
| 205 | put s 3 0xa54ff53a |
| 206 | put s 4 0x510e527f |
| 207 | put s 5 0x9b05688c |
| 208 | put s 6 0x1f83d9ab |
| 209 | put s 7 0x5be0cd19 |
| 210 | end |
| 211 | |
| 212 | fn res_sha_sched w edit uint 64 fresh m view uchar * o ulong to void |
| 213 | q = ulong 0 |
| 214 | x = uint 0 |
| 215 | y = uint 0 |
| 216 | for i ulong 16 unroll |
| 217 | q = add o mul i 4 |
| 218 | x = uint get m q |
| 219 | x = or shl x 8 uint get m add q 1 |
| 220 | x = or shl x 8 uint get m add q 2 |
| 221 | put w i or shl x 8 uint get m add q 3 |
| 222 | end |
| 223 | for i ulong 48 unroll |
| 224 | x = get w add i 1 |
| 225 | y = get w add i 14 |
| 226 | x = add add get w i get w add i 9 xor xor rotr x 7 rotr x 18 shr x 3 |
| 227 | put w add i 16 add x xor xor rotr y 17 rotr y 19 shr y 10 |
| 228 | end |
| 229 | end |
| 230 | |
| 231 | fn res_sha_rounds s edit uint 8 w view uint 64 kt view uint 64 to void |
| 232 | a = get s 0 |
| 233 | b = get s 1 |
| 234 | c = get s 2 |
| 235 | d = get s 3 |
| 236 | e = get s 4 |
| 237 | f = get s 5 |
| 238 | g = get s 6 |
| 239 | h = get s 7 |
| 240 | t = uint 0 |
| 241 | for i ulong 64 unroll |
| 242 | h = add h get kt i |
| 243 | h = add h get w i |
| 244 | t = xor rotr e 6 rotr e 11 |
| 245 | t = xor t rotr e 25 |
| 246 | h = add h t |
| 247 | h = add h xor g and e xor f g |
| 248 | d = add d h |
| 249 | t = xor rotr a 2 rotr a 13 |
| 250 | t = xor t rotr a 22 |
| 251 | h = add h t |
| 252 | t = and c or a b |
| 253 | h = add h or and a b t |
| 254 | t = h |
| 255 | h = g |
| 256 | g = f |
| 257 | f = e |
| 258 | e = d |
| 259 | d = c |
| 260 | c = b |
| 261 | b = a |
| 262 | a = t |
| 263 | end |
| 264 | put s 0 add get s 0 a |
| 265 | put s 1 add get s 1 b |
| 266 | put s 2 add get s 2 c |
| 267 | put s 3 add get s 3 d |
| 268 | put s 4 add get s 4 e |
| 269 | put s 5 add get s 5 f |
| 270 | put s 6 add get s 6 g |
| 271 | put s 7 add get s 7 h |
| 272 | end |
| 273 | |
| 274 | fn res_sha_comp s edit uint 8 w edit uint 64 kt view uint 64 m view uchar * o ulong to void |
| 275 | res_sha_sched w m o |
| 276 | res_sha_rounds s w kt |
| 277 | end |
| 278 | |
| 279 | fn res_sha_run s edit uint 8 w edit uint 64 kt view uint 64 m view uchar * to void |
| 280 | for j ulong shr len m 6 bound 600000 |
| 281 | res_sha_comp s w kt m shl j 6 |
| 282 | end |
| 283 | end |
| 284 | |
| 285 | fn res_sha_blocks s edit uint 8 w edit uint 64 kt view uint 64 m view uchar * to void |
| 286 | q = ulong 0 |
| 287 | for b ulong shr len m 25 bound 100000 |
| 288 | res_sha_run s w kt part m q 0x2000000 |
| 289 | q = add q 0x2000000 |
| 290 | end |
| 291 | res_sha_run s w kt part m q sub len m q |
| 292 | end |
| 293 | |
| 294 | fn res_sha_finish s edit uint 8 w edit uint 64 kt view uint 64 p edit uchar 128 k ulong n ulong to void |
| 295 | put p k 0x80 |
| 296 | e = ulong 64 |
| 297 | if ge k 56 |
| 298 | e = 128 |
| 299 | end |
| 300 | bits = shl n 3 |
| 301 | for i ulong 8 unroll |
| 302 | put p sub e add i 1 uchar and shr bits mul i 8 0xff |
| 303 | end |
| 304 | res_sha_comp s w kt view p 0 |
| 305 | if eq e 128 |
| 306 | res_sha_comp s w kt view p 64 |
| 307 | end |
| 308 | end |
| 309 | |
| 310 | note shactx as flat uint[74]: [0..7]=state [8]=count_lo [9]=count_hi [10..73]=buf[0..63] |
| 311 | fn res_sha_init c edit uint 74 to void |
| 312 | s = new uint 8 0 |
| 313 | res_sha_iv s |
| 314 | for i ulong 8 unroll |
| 315 | put c i get s i |
| 316 | end |
| 317 | put c 8 0 |
| 318 | put c 9 0 |
| 319 | end |
| 320 | |
| 321 | fn res_sha_tobuf c edit uint 74 bi ulong d view uchar * o ulong k ulong to void |
| 322 | for i ulong k bound 64 |
| 323 | put c add 10 add bi i uint get d add o i |
| 324 | end |
| 325 | end |
| 326 | |
| 327 | fn res_sha_fill_blk c view uint 74 d view uchar * blk edit uchar 64 bi ulong to void |
| 328 | for i ulong bi bound 64 |
| 329 | put blk i uchar and get c add 10 i 0xff |
| 330 | end |
| 331 | remb = sub 64 bi |
| 332 | for i ulong remb bound 64 |
| 333 | put blk add bi i get d i |
| 334 | end |
| 335 | end |
| 336 | |
| 337 | fn res_sha_absorb c edit uint 74 d view uchar * bi ulong to void |
| 338 | n = len d |
| 339 | s = new uint 8 0 |
| 340 | w = new uint 64 0 |
| 341 | kt = new uint 64 0 |
| 342 | blk = new uchar 64 0 |
| 343 | res_sha_kinit kt |
| 344 | for i ulong 8 unroll |
| 345 | put s i get c i |
| 346 | end |
| 347 | off = ulong 0 |
| 348 | if gt bi 0 |
| 349 | off = sub 64 bi |
| 350 | res_sha_fill_blk view c d blk bi |
| 351 | res_sha_comp s w kt view blk 0 |
| 352 | end |
| 353 | nb = shr sub n off 6 |
| 354 | res_sha_blocks s w kt part d off shl nb 6 |
| 355 | off = add off shl nb 6 |
| 356 | res_sha_tobuf c 0 d off sub n off |
| 357 | for i ulong 8 unroll |
| 358 | put c i get s i |
| 359 | end |
| 360 | end |
| 361 | |
| 362 | fn res_sha_update c edit uint 74 d view uchar * to void |
| 363 | n = len d |
| 364 | cnt = or shl ulong get c 9 32 ulong get c 8 |
| 365 | bi = and cnt 63 |
| 366 | ncnt = add cnt n |
| 367 | put c 8 uint and ncnt 0xffffffff |
| 368 | put c 9 uint shr ncnt 32 |
| 369 | if lt n sub 64 bi |
| 370 | res_sha_tobuf c bi d 0 n |
| 371 | ret |
| 372 | end |
| 373 | res_sha_absorb c d bi |
| 374 | end |
| 375 | |
| 376 | fn res_sha_copy_pending c view uint 74 p edit uchar 128 k ulong to void |
| 377 | for i ulong k bound 64 |
| 378 | put p i uchar and get c add 10 i 0xff |
| 379 | end |
| 380 | end |
| 381 | |
| 382 | fn res_sha_final c edit uint 74 o edit uchar 32 to void |
| 383 | s = new uint 8 0 |
| 384 | w = new uint 64 0 |
| 385 | kt = new uint 64 0 |
| 386 | p = new uchar 128 0 |
| 387 | v = ulong 0 |
| 388 | res_sha_kinit kt |
| 389 | for i ulong 8 unroll |
| 390 | put s i get c i |
| 391 | end |
| 392 | cnt = or shl ulong get c 9 32 ulong get c 8 |
| 393 | k = and cnt 63 |
| 394 | res_sha_copy_pending view c p k |
| 395 | res_sha_finish s w kt p k cnt |
| 396 | for i ulong 8 unroll |
| 397 | v = ulong get s i |
| 398 | for j ulong 4 unroll |
| 399 | put o add mul i 4 j uchar and shr v sub 24 mul j 8 0xff |
| 400 | end |
| 401 | end |
| 402 | end |
| 403 | |
| 404 | fn res_sha_hash d view uchar * o edit uchar 32 to void |
| 405 | s = new uint 8 0 |
| 406 | w = new uint 64 0 |
| 407 | kt = new uint 64 0 |
| 408 | p = new uchar 128 0 |
| 409 | v = ulong 0 |
| 410 | res_sha_kinit kt |
| 411 | res_sha_iv s |
| 412 | n = len d |
| 413 | nb = shr n 6 |
| 414 | res_sha_blocks s w kt part d 0 shl nb 6 |
| 415 | off = shl nb 6 |
| 416 | k = sub n off |
| 417 | for i ulong k bound 64 |
| 418 | put p i get d add off i |
| 419 | end |
| 420 | res_sha_finish s w kt p k n |
| 421 | for i ulong 8 unroll |
| 422 | v = ulong get s i |
| 423 | for j ulong 4 unroll |
| 424 | put o add mul i 4 j uchar and shr v sub 24 mul j 8 0xff |
| 425 | end |
| 426 | end |
| 427 | end |
| 428 | |
| 429 | note ================================================================ |
| 430 | note Deterministic binary32 math (exact match to omega_math_exp / log) |
| 431 | note ================================================================ |
| 432 | |
| 433 | fn res_f32_fin x f32 to bool |
| 434 | u = bitcast uint x |
| 435 | ret lt and u 0x7f800000 0x7f800000 |
| 436 | end |
| 437 | |
| 438 | fn res_f32_isnan x f32 to bool |
| 439 | u = bitcast uint x |
| 440 | ret gt and u 0x7fffffff 0x7f800000 |
| 441 | end |
| 442 | |
| 443 | fn res_f32_iszero x f32 to bool |
| 444 | u = bitcast uint x |
| 445 | ret eq and u 0x7fffffff 0 |
| 446 | end |
| 447 | |
| 448 | fn res_f32_isinf x f32 to bool |
| 449 | u = bitcast uint x |
| 450 | ret eq and u 0x7fffffff 0x7f800000 |
| 451 | end |
| 452 | |
| 453 | fn res_f32_pos x f32 to bool |
| 454 | if not res_f32_fin x |
| 455 | ret false |
| 456 | end |
| 457 | ret gt x 0.0f32 |
| 458 | end |
| 459 | |
| 460 | fn res_f32_abs x f32 to f32 |
| 461 | if lt x 0.0f32 |
| 462 | ret neg x |
| 463 | end |
| 464 | ret x |
| 465 | end |
| 466 | |
| 467 | fn res_f32_bits_canon x f32 to uint |
| 468 | u = bitcast uint x |
| 469 | if eq u 0x80000000 |
| 470 | ret uint 0 |
| 471 | end |
| 472 | ret u |
| 473 | end |
| 474 | |
| 475 | fn res_math_exp x f32 to f32 |
| 476 | if res_f32_isnan x |
| 477 | ret bitcast f32 uint 0x7fc00000 |
| 478 | end |
| 479 | if lt x -104.0f32 |
| 480 | ret 0.0f32 |
| 481 | end |
| 482 | if gt x bitcast f32 uint 0x42b17218 |
| 483 | ret bitcast f32 uint 0x7f800000 |
| 484 | end |
| 485 | prod = mul x bitcast f32 uint 0x3fb8aa3b |
| 486 | k = int 0 |
| 487 | if ge prod 0.0f32 |
| 488 | k = int add prod 0.5f32 |
| 489 | else |
| 490 | k = int sub prod 0.5f32 |
| 491 | end |
| 492 | fk = f32 k |
| 493 | r = sub sub x mul fk bitcast f32 uint 0x3f317200 mul fk bitcast f32 uint 0x35bfbe8e |
| 494 | p = mul r add bitcast f32 uint 0x3d2aaaab mul r bitcast f32 uint 0x3c088889 |
| 495 | p = mul r add bitcast f32 uint 0x3e2aaaab p |
| 496 | p = mul r add 0.5f32 p |
| 497 | poly = add 1.0f32 mul r add 1.0f32 p |
| 498 | if gt k 127 |
| 499 | s1 = bitcast f32 shl uint 254 23 |
| 500 | s2 = bitcast f32 shl uint k 23 |
| 501 | ret mul mul poly s1 s2 |
| 502 | end |
| 503 | if lt k -126 |
| 504 | s1 = bitcast f32 shl uint add k 227 23 |
| 505 | s2 = bitcast f32 shl uint 27 23 |
| 506 | ret mul mul poly s1 s2 |
| 507 | end |
| 508 | ret mul poly bitcast f32 shl uint add k 127 23 |
| 509 | end |
| 510 | |
| 511 | fn res_math_log x f32 to f32 |
| 512 | if res_f32_isnan x |
| 513 | ret bitcast f32 uint 0x7fc00000 |
| 514 | end |
| 515 | if lt x 0.0f32 |
| 516 | ret bitcast f32 uint 0x7fc00000 |
| 517 | end |
| 518 | if res_f32_iszero x |
| 519 | ret bitcast f32 uint 0xff800000 |
| 520 | end |
| 521 | if res_f32_isinf x |
| 522 | ret bitcast f32 uint 0x7f800000 |
| 523 | end |
| 524 | eadj = int 0 |
| 525 | xv = x |
| 526 | ux = bitcast uint xv |
| 527 | if eq and ux 0x7f800000 0 |
| 528 | xv = mul xv 8388608.0f32 |
| 529 | eadj = -23 |
| 530 | end |
| 531 | u = bitcast uint xv |
| 532 | e = add sub int and shr u 23 0xff 127 eadj |
| 533 | m = bitcast f32 or and u 0x007fffff 0x3f800000 |
| 534 | if gt m bitcast f32 uint 0x3fb504f3 |
| 535 | m = mul m 0.5f32 |
| 536 | e = add e 1 |
| 537 | end |
| 538 | z = div sub m 1.0f32 add m 1.0f32 |
| 539 | z2 = mul z z |
| 540 | poly = add bitcast f32 uint 0x3e4ccccd mul z2 bitcast f32 uint 0x3e124925 |
| 541 | poly = add bitcast f32 uint 0x3eaaaaab mul z2 poly |
| 542 | poly = add 1.0f32 mul z2 poly |
| 543 | lnm = mul mul 2.0f32 z poly |
| 544 | ret add mul f32 e bitcast f32 uint 0x3f317218 lnm |
| 545 | end |
| 546 | |
| 547 | fn res_one_minus_exp w f32 to f32 |
| 548 | if le res_f32_abs w 0.5f32 |
| 549 | p = 1.0f32 |
| 550 | p = add 1.0f32 mul mul w div 1.0f32 10.0f32 p |
| 551 | p = add 1.0f32 mul mul w div 1.0f32 9.0f32 p |
| 552 | p = add 1.0f32 mul mul w div 1.0f32 8.0f32 p |
| 553 | p = add 1.0f32 mul mul w div 1.0f32 7.0f32 p |
| 554 | p = add 1.0f32 mul mul w div 1.0f32 6.0f32 p |
| 555 | p = add 1.0f32 mul mul w div 1.0f32 5.0f32 p |
| 556 | p = add 1.0f32 mul mul w div 1.0f32 4.0f32 p |
| 557 | p = add 1.0f32 mul mul w div 1.0f32 3.0f32 p |
| 558 | p = add 1.0f32 mul mul w 0.5f32 p |
| 559 | ret neg mul w p |
| 560 | end |
| 561 | ret sub 1.0f32 res_math_exp w |
| 562 | end |
| 563 | |
| 564 | fn res_carry_ok val f32 carry f32 to bool |
| 565 | ret le res_f32_abs carry mul res_f32_abs val bitcast f32 uint 0x33800000 |
| 566 | end |
| 567 | |
| 568 | note ================================================================ |
| 569 | note SHA-256 encoding helpers |
| 570 | note ================================================================ |
| 571 | |
| 572 | fn res_h_u16 c edit uint 74 v uint to void |
| 573 | b = new uchar 2 0 |
| 574 | put b 0 uchar and v 0xff |
| 575 | put b 1 uchar and shr v 8 0xff |
| 576 | res_sha_update c view b |
| 577 | end |
| 578 | |
| 579 | fn res_h_u32 c edit uint 74 v uint to void |
| 580 | b = new uchar 4 0 |
| 581 | put b 0 uchar and v 0xff |
| 582 | put b 1 uchar and shr v 8 0xff |
| 583 | put b 2 uchar and shr v 16 0xff |
| 584 | put b 3 uchar and shr v 24 0xff |
| 585 | res_sha_update c view b |
| 586 | end |
| 587 | |
| 588 | fn res_h_u64 c edit uint 74 v ulong to void |
| 589 | res_h_u32 c uint and v 0xffffffff |
| 590 | res_h_u32 c uint shr v 32 |
| 591 | end |
| 592 | |
| 593 | fn res_h_f32 c edit uint 74 v f32 to void |
| 594 | res_h_u32 c res_f32_bits_canon v |
| 595 | end |
| 596 | |
| 597 | fn res_h_domain c edit uint 74 dom view uchar * to void |
| 598 | z = new uchar 1 0 |
| 599 | res_sha_init c |
| 600 | res_sha_update c dom |
| 601 | res_sha_update c view z |
| 602 | end |
| 603 | |
| 604 | note ================================================================ |
| 605 | note Topology |
| 606 | note ================================================================ |
| 607 | |
| 608 | fn res_edge_less a1 uint b1 uint a2 uint b2 uint to bool |
| 609 | if lt a1 a2 |
| 610 | ret true |
| 611 | end |
| 612 | if eq a1 a2 |
| 613 | ret lt b1 b2 |
| 614 | end |
| 615 | ret false |
| 616 | end |
| 617 | |
| 618 | fn res_topo_insert_edge t edit uint 514 e ulong lo uint hi uint to void |
| 619 | i = e |
| 620 | while gt i 0 bound 192 |
| 621 | prev_off = add RES_TOPO_EDGES mul sub i 1 2 |
| 622 | pa = get t prev_off |
| 623 | pb = get t add prev_off 1 |
| 624 | if res_edge_less lo hi pa pb |
| 625 | cur_off = add RES_TOPO_EDGES mul i 2 |
| 626 | put t cur_off pa |
| 627 | put t add cur_off 1 pb |
| 628 | i = sub i 1 |
| 629 | else |
| 630 | break |
| 631 | end |
| 632 | end |
| 633 | dst_off = add RES_TOPO_EDGES mul i 2 |
| 634 | put t dst_off lo |
| 635 | put t add dst_off 1 hi |
| 636 | end |
| 637 | |
| 638 | fn res_topo_sort_edges t edit uint 514 edges view uint * nc uint ec uint to int |
| 639 | for e ulong ulong ec bound 192 |
| 640 | a = get edges mul e 2 |
| 641 | b = get edges add mul e 2 1 |
| 642 | if or ge a nc or ge b nc eq a b |
| 643 | ret RES_ERR_TOPOLOGY |
| 644 | end |
| 645 | lo = a |
| 646 | hi = b |
| 647 | if gt a b |
| 648 | lo = b |
| 649 | hi = a |
| 650 | end |
| 651 | res_topo_insert_edge t e lo hi |
| 652 | end |
| 653 | ret RES_OK |
| 654 | end |
| 655 | |
| 656 | fn res_topo_relax_pass t view uint 514 comp edit uint 64 ec ulong to bool |
| 657 | changed = false |
| 658 | for e ulong ec bound 192 |
| 659 | off = add RES_TOPO_EDGES mul e 2 |
| 660 | ea = ulong get t off |
| 661 | eb = ulong get t add off 1 |
| 662 | ca = get comp ea |
| 663 | cb = get comp eb |
| 664 | if lt ca cb |
| 665 | put comp eb ca |
| 666 | changed = true |
| 667 | else |
| 668 | if lt cb ca |
| 669 | put comp ea cb |
| 670 | changed = true |
| 671 | end |
| 672 | end |
| 673 | end |
| 674 | ret changed |
| 675 | end |
| 676 | |
| 677 | fn res_topo_comp_grounded t view uint 514 comp view uint 64 nc ulong ci uint to bool |
| 678 | for j ulong nc bound 64 |
| 679 | if eq get comp j ci |
| 680 | if ne get t add RES_TOPO_GND j 0 |
| 681 | ret true |
| 682 | end |
| 683 | end |
| 684 | end |
| 685 | ret false |
| 686 | end |
| 687 | |
| 688 | fn res_topo_check_conn t view uint 514 nc ulong ec ulong to int |
| 689 | comp = new uint 64 0 |
| 690 | for i ulong nc bound 64 |
| 691 | put comp i uint i |
| 692 | end |
| 693 | for pass ulong nc bound 64 |
| 694 | if not res_topo_relax_pass t comp ec |
| 695 | break |
| 696 | end |
| 697 | end |
| 698 | for i ulong nc bound 64 |
| 699 | if not res_topo_comp_grounded t view comp nc get comp i |
| 700 | ret RES_ERR_TOPOLOGY |
| 701 | end |
| 702 | end |
| 703 | ret RES_OK |
| 704 | end |
| 705 | |
| 706 | fn res_topo_check_edges t edit uint 514 nc ulong ec ulong to int |
| 707 | for e ulong ec bound 192 |
| 708 | off = add RES_TOPO_EDGES mul e 2 |
| 709 | ea = get t off |
| 710 | eb = get t add off 1 |
| 711 | if gt e 0 |
| 712 | poff = sub off 2 |
| 713 | if and eq ea get t poff eq eb get t add poff 1 |
| 714 | ret RES_ERR_TOPOLOGY |
| 715 | end |
| 716 | end |
| 717 | da = add get t add RES_TOPO_DEG ulong ea 1 |
| 718 | if gt da 6 |
| 719 | ret RES_ERR_TOPOLOGY |
| 720 | end |
| 721 | put t add RES_TOPO_DEG ulong ea da |
| 722 | db = add get t add RES_TOPO_DEG ulong eb 1 |
| 723 | if gt db 6 |
| 724 | ret RES_ERR_TOPOLOGY |
| 725 | end |
| 726 | put t add RES_TOPO_DEG ulong eb db |
| 727 | end |
| 728 | for i ulong nc bound 64 |
| 729 | if eq get t add RES_TOPO_DEG i 0 |
| 730 | ret RES_ERR_TOPOLOGY |
| 731 | end |
| 732 | end |
| 733 | ret RES_OK |
| 734 | end |
| 735 | |
| 736 | fn res_topo_set_grounds t edit uint 514 grounds view uint * nc uint gc uint to int |
| 737 | for g ulong ulong gc bound 64 |
| 738 | gn = get grounds g |
| 739 | if ge gn nc |
| 740 | ret RES_ERR_TOPOLOGY |
| 741 | end |
| 742 | if ne get t add RES_TOPO_GND ulong gn 0 |
| 743 | ret RES_ERR_TOPOLOGY |
| 744 | end |
| 745 | put t add RES_TOPO_GND ulong gn 1 |
| 746 | end |
| 747 | ret RES_OK |
| 748 | end |
| 749 | |
| 750 | fn res_topology_build meta view uint 3 edges view uint * grounds view uint * out_t edit uint 514 to int |
| 751 | nc = get meta 0 |
| 752 | ec = get meta 1 |
| 753 | gc = get meta 2 |
| 754 | if or lt nc 2 gt nc 64 |
| 755 | ret RES_ERR_TOPOLOGY |
| 756 | end |
| 757 | if or lt ec 1 gt ec 192 |
| 758 | ret RES_ERR_TOPOLOGY |
| 759 | end |
| 760 | if or lt gc 1 ge gc nc |
| 761 | ret RES_ERR_TOPOLOGY |
| 762 | end |
| 763 | if or lt len edges mul ulong ec 2 lt len grounds ulong gc |
| 764 | ret RES_ERR_TOPOLOGY |
| 765 | end |
| 766 | t = new uint 514 0 |
| 767 | put t 0 nc |
| 768 | put t 1 ec |
| 769 | rc = res_topo_sort_edges t edges nc ec |
| 770 | if ne rc RES_OK |
| 771 | ret rc |
| 772 | end |
| 773 | rc = res_topo_check_edges t ulong nc ulong ec |
| 774 | if ne rc RES_OK |
| 775 | ret rc |
| 776 | end |
| 777 | rc = res_topo_set_grounds t grounds nc gc |
| 778 | if ne rc RES_OK |
| 779 | ret rc |
| 780 | end |
| 781 | rc = res_topo_check_conn view t ulong nc ulong ec |
| 782 | if ne rc RES_OK |
| 783 | ret rc |
| 784 | end |
| 785 | for i ulong RES_TOPO_WORDS bound 514 |
| 786 | put out_t i get t i |
| 787 | end |
| 788 | ret RES_OK |
| 789 | end |
| 790 | |
| 791 | fn res_topology_chain nc uint ground uint out_t edit uint 514 to int |
| 792 | if or lt nc 2 gt nc 64 |
| 793 | ret RES_ERR_TOPOLOGY |
| 794 | end |
| 795 | edges = new uint 128 0 |
| 796 | garr = new uint 1 0 |
| 797 | put garr 0 ground |
| 798 | ec = sub nc 1 |
| 799 | for i ulong ulong ec bound 64 |
| 800 | put edges mul i 2 uint i |
| 801 | put edges add mul i 2 1 uint add i 1 |
| 802 | end |
| 803 | tmeta = new uint 3 0 |
| 804 | put tmeta 0 nc |
| 805 | put tmeta 1 ec |
| 806 | put tmeta 2 1 |
| 807 | ret res_topology_build view tmeta view edges view garr out_t |
| 808 | end |
| 809 | |
| 810 | note ================================================================ |
| 811 | note Parameters |
| 812 | note ================================================================ |
| 813 | |
| 814 | fn res_params_reference which uint nc uint out_p edit uint 265 to int |
| 815 | if or lt nc 1 gt nc 64 |
| 816 | ret RES_ERR_PARAM |
| 817 | end |
| 818 | if and ne which RES_REF_NARMA ne which RES_REF_LORENZ |
| 819 | ret RES_ERR_PARAM |
| 820 | end |
| 821 | for i ulong RES_PAR_WORDS bound 265 |
| 822 | put out_p i 0 |
| 823 | end |
| 824 | put out_p 0 nc |
| 825 | eps = 8.85e-12f32 |
| 826 | vtau = 0.045f32 |
| 827 | if eq which RES_REF_LORENZ |
| 828 | eps = f32 mul 2.1f64 8.85e-12f64 |
| 829 | vtau = 0.0454f32 |
| 830 | end |
| 831 | put out_p 1 bitcast uint eps |
| 832 | put out_p 2 bitcast uint 4.0e-9f32 |
| 833 | put out_p 3 bitcast uint 5.0e-8f32 |
| 834 | put out_p 4 bitcast uint 5.0e-8f32 |
| 835 | put out_p 5 bitcast uint 0.00015f32 |
| 836 | put out_p 6 bitcast uint 1.0e4f32 |
| 837 | put out_p 7 bitcast uint 0.002f32 |
| 838 | put out_p 8 bitcast uint 3.7919f32 |
| 839 | for i ulong ulong nc bound 64 |
| 840 | cval = f32 add 1 rem i 3 |
| 841 | put out_p add RES_PAR_CONC i bitcast uint cval |
| 842 | put out_p add RES_PAR_TAU0 i bitcast uint 0.001f32 |
| 843 | put out_p add RES_PAR_VTAU i bitcast uint vtau |
| 844 | put out_p add RES_PAR_VE i bitcast uint 0.0055f32 |
| 845 | end |
| 846 | ret RES_OK |
| 847 | end |
| 848 | |
| 849 | fn res_params_check p view uint 265 nc uint to int |
| 850 | if ne get p 0 nc |
| 851 | ret RES_ERR_PARAM |
| 852 | end |
| 853 | if not res_f32_pos bitcast f32 get p 1 |
| 854 | ret RES_ERR_PARAM |
| 855 | end |
| 856 | if not res_f32_pos bitcast f32 get p 2 |
| 857 | ret RES_ERR_PARAM |
| 858 | end |
| 859 | if not res_f32_pos bitcast f32 get p 3 |
| 860 | ret RES_ERR_PARAM |
| 861 | end |
| 862 | if not res_f32_pos bitcast f32 get p 4 |
| 863 | ret RES_ERR_PARAM |
| 864 | end |
| 865 | zv = bitcast f32 get p 5 |
| 866 | if or not res_f32_pos zv gt zv 1.0f32 |
| 867 | ret RES_ERR_PARAM |
| 868 | end |
| 869 | if not res_f32_pos bitcast f32 get p 6 |
| 870 | ret RES_ERR_PARAM |
| 871 | end |
| 872 | if not res_f32_pos bitcast f32 get p 7 |
| 873 | ret RES_ERR_PARAM |
| 874 | end |
| 875 | if not res_f32_fin bitcast f32 get p 8 |
| 876 | ret RES_ERR_PARAM |
| 877 | end |
| 878 | for i ulong ulong nc bound 64 |
| 879 | cv = bitcast f32 get p add RES_PAR_CONC i |
| 880 | if or not res_f32_fin cv lt cv 0.0f32 |
| 881 | ret RES_ERR_PARAM |
| 882 | end |
| 883 | if not res_f32_pos bitcast f32 get p add RES_PAR_TAU0 i |
| 884 | ret RES_ERR_PARAM |
| 885 | end |
| 886 | if not res_f32_pos bitcast f32 get p add RES_PAR_VTAU i |
| 887 | ret RES_ERR_PARAM |
| 888 | end |
| 889 | if not res_f32_pos bitcast f32 get p add RES_PAR_VE i |
| 890 | ret RES_ERR_PARAM |
| 891 | end |
| 892 | end |
| 893 | ret RES_OK |
| 894 | end |
| 895 | |
| 896 | note ================================================================ |
| 897 | note Configuration & Cholesky & Config Digest |
| 898 | note ================================================================ |
| 899 | |
| 900 | fn res_config_digest cfg view uint 5363 out_d edit uchar 32 to void |
| 901 | ctx = new uint 74 0 |
| 902 | res_h_domain ctx res_dom_config |
| 903 | nc = get cfg 0 |
| 904 | ec = get cfg 1 |
| 905 | res_h_u32 ctx nc |
| 906 | res_h_u32 ctx ec |
| 907 | for e ulong ulong ec bound 192 |
| 908 | res_h_u16 ctx get cfg add RES_TOPO_EDGES mul e 2 |
| 909 | res_h_u16 ctx get cfg add RES_TOPO_EDGES add mul e 2 1 |
| 910 | end |
| 911 | gbuf = new uchar 64 0 |
| 912 | for i ulong ulong nc bound 64 |
| 913 | put gbuf i uchar and get cfg add RES_TOPO_GND i 0xff |
| 914 | end |
| 915 | res_sha_update ctx part gbuf 0 ulong nc |
| 916 | for k ulong 8 bound 8 |
| 917 | res_h_f32 ctx bitcast f32 get cfg add RES_CFG_PAR add k 1 |
| 918 | end |
| 919 | for i ulong ulong nc bound 64 |
| 920 | res_h_f32 ctx bitcast f32 get cfg add RES_CFG_PAR add RES_PAR_CONC i |
| 921 | res_h_f32 ctx bitcast f32 get cfg add RES_CFG_PAR add RES_PAR_TAU0 i |
| 922 | res_h_f32 ctx bitcast f32 get cfg add RES_CFG_PAR add RES_PAR_VTAU i |
| 923 | res_h_f32 ctx bitcast f32 get cfg add RES_CFG_PAR add RES_PAR_VE i |
| 924 | end |
| 925 | res_h_f32 ctx bitcast f32 get cfg RES_CFG_STEP_S |
| 926 | res_h_u32 ctx get cfg RES_CFG_K |
| 927 | res_sha_update ctx res_num_policy |
| 928 | res_sha_final ctx out_d |
| 929 | end |
| 930 | |
| 931 | fn res_cfg_chol_cell c edit uint 5363 i ulong j ulong to int |
| 932 | s = bitcast f32 get c add RES_CFG_CHOL add mul i 64 j |
| 933 | for k ulong j bound 64 |
| 934 | rik = bitcast f32 get c add RES_CFG_CHOL add mul i 64 k |
| 935 | rjk = bitcast f32 get c add RES_CFG_CHOL add mul j 64 k |
| 936 | s = sub s mul rik rjk |
| 937 | end |
| 938 | if eq i j |
| 939 | if not gt s 0.0f32 |
| 940 | ret RES_ERR_SINGULAR |
| 941 | end |
| 942 | d = fsqrt s |
| 943 | put c add RES_CFG_CHOL add mul i 64 i bitcast uint d |
| 944 | put c add RES_CFG_CHOL_INV i bitcast uint div 1.0f32 d |
| 945 | else |
| 946 | invj = bitcast f32 get c add RES_CFG_CHOL_INV j |
| 947 | put c add RES_CFG_CHOL add mul i 64 j bitcast uint mul s invj |
| 948 | end |
| 949 | ret RES_OK |
| 950 | end |
| 951 | |
| 952 | fn res_cfg_chol c edit uint 5363 m ulong to int |
| 953 | for i ulong m bound 64 |
| 954 | for j ulong add i 1 bound 64 |
| 955 | rc = res_cfg_chol_cell c i j |
| 956 | if ne rc RES_OK |
| 957 | ret rc |
| 958 | end |
| 959 | end |
| 960 | end |
| 961 | ret RES_OK |
| 962 | end |
| 963 | |
| 964 | fn res_cfg_lap_edges c edit uint 5363 ec ulong to void |
| 965 | for e ulong ec bound 192 |
| 966 | off = add RES_TOPO_EDGES mul e 2 |
| 967 | ra = get c add RES_CFG_RED_IDX ulong get c off |
| 968 | rb = get c add RES_CFG_RED_IDX ulong get c add off 1 |
| 969 | if and ne ra 0xffffffff ne rb 0xffffffff |
| 970 | hi = ulong ra |
| 971 | lo = ulong rb |
| 972 | if lt hi lo |
| 973 | hi = ulong rb |
| 974 | lo = ulong ra |
| 975 | end |
| 976 | put c add RES_CFG_CHOL add mul hi 64 lo bitcast uint -1.0f32 |
| 977 | end |
| 978 | end |
| 979 | end |
| 980 | |
| 981 | fn res_cfg_fill_laplacian c edit uint 5363 nc ulong ec ulong to int |
| 982 | m = ulong 0 |
| 983 | for i ulong nc bound 64 |
| 984 | put c add RES_CFG_RED_IDX i 0xffffffff |
| 985 | if eq get c add RES_TOPO_GND i 0 |
| 986 | put c add RES_CFG_RED_IDX i uint m |
| 987 | put c add RES_CFG_RED_NODE m uint i |
| 988 | m = add m 1 |
| 989 | end |
| 990 | end |
| 991 | put c RES_CFG_RED_CNT uint m |
| 992 | for u ulong m bound 64 |
| 993 | rn = ulong get c add RES_CFG_RED_NODE u |
| 994 | deg = f32 get c add RES_TOPO_DEG rn |
| 995 | put c add RES_CFG_CHOL add mul u 64 u bitcast uint deg |
| 996 | end |
| 997 | res_cfg_lap_edges c ec |
| 998 | ret res_cfg_chol c m |
| 999 | end |
| 1000 | |
| 1001 | fn res_cfg_derived_one c edit uint 5363 par view uint 265 ln_n0s f32 i ulong to int |
| 1002 | ln0 = add ln_n0s mul bitcast f32 get par 8 bitcast f32 get par add RES_PAR_CONC i |
| 1003 | if or not res_f32_fin ln0 or lt ln0 -80.0f32 gt ln0 80.0f32 |
| 1004 | ret RES_ERR_PARAM |
| 1005 | end |
| 1006 | put c add RES_CFG_LN_N0 i bitcast uint ln0 |
| 1007 | lna = res_math_log div bitcast f32 get par 6 bitcast f32 get par add RES_PAR_TAU0 i |
| 1008 | if not res_f32_fin lna |
| 1009 | ret RES_ERR_PARAM |
| 1010 | end |
| 1011 | put c add RES_CFG_LN_A0 i bitcast uint lna |
| 1012 | ivt = div 1.0f32 bitcast f32 get par add RES_PAR_VTAU i |
| 1013 | ive = div 1.0f32 bitcast f32 get par add RES_PAR_VE i |
| 1014 | if or not res_f32_pos ivt not res_f32_pos ive |
| 1015 | ret RES_ERR_PARAM |
| 1016 | end |
| 1017 | put c add RES_CFG_INV_VTAU i bitcast uint ivt |
| 1018 | put c add RES_CFG_INV_VE i bitcast uint ive |
| 1019 | ret RES_OK |
| 1020 | end |
| 1021 | |
| 1022 | fn res_cfg_derived_nodes c edit uint 5363 par view uint 265 to int |
| 1023 | ln_n0s = res_math_log bitcast f32 get par 7 |
| 1024 | nc = ulong get par 0 |
| 1025 | for i ulong nc bound 64 |
| 1026 | rc = res_cfg_derived_one c par ln_n0s i |
| 1027 | if ne rc RES_OK |
| 1028 | ret rc |
| 1029 | end |
| 1030 | end |
| 1031 | ret RES_OK |
| 1032 | end |
| 1033 | |
| 1034 | fn res_cfg_derived c edit uint 5363 par view uint 265 step_s f32 to int |
| 1035 | v = div mul bitcast f32 get par 1 bitcast f32 get par 3 bitcast f32 get par 2 |
| 1036 | if not res_f32_pos v |
| 1037 | ret RES_ERR_PARAM |
| 1038 | end |
| 1039 | v = div 1.0f32 v |
| 1040 | if not res_f32_pos v |
| 1041 | ret RES_ERR_PARAM |
| 1042 | end |
| 1043 | put c RES_CFG_INV_CAP bitcast uint v |
| 1044 | v = mul bitcast f32 get par 4 bitcast f32 get par 3 |
| 1045 | if not res_f32_pos v |
| 1046 | ret RES_ERR_PARAM |
| 1047 | end |
| 1048 | put c RES_CFG_G_UNIT bitcast uint v |
| 1049 | v = div mul bitcast f32 get par 4 bitcast f32 get par 2 bitcast f32 get par 1 |
| 1050 | if not res_f32_pos v |
| 1051 | ret RES_ERR_PARAM |
| 1052 | end |
| 1053 | put c RES_CFG_LAM_UNIT bitcast uint v |
| 1054 | put c RES_CFG_HALF_H bitcast uint mul step_s 0.5f32 |
| 1055 | v = div step_s 6.0f32 |
| 1056 | if not res_f32_pos v |
| 1057 | ret RES_ERR_PARAM |
| 1058 | end |
| 1059 | put c RES_CFG_SIXTH_H bitcast uint v |
| 1060 | ret res_cfg_derived_nodes c par |
| 1061 | end |
| 1062 | |
| 1063 | fn res_topo_verify_canon topo view uint 514 to int |
| 1064 | nc = get topo 0 |
| 1065 | if or lt nc 2 gt nc 64 |
| 1066 | ret RES_ERR_TOPOLOGY |
| 1067 | end |
| 1068 | grounds = new uint 64 0 |
| 1069 | ng = uint 0 |
| 1070 | for i ulong ulong nc bound 64 |
| 1071 | if ne get topo add RES_TOPO_GND i 0 |
| 1072 | put grounds ulong ng uint i |
| 1073 | ng = add ng 1 |
| 1074 | end |
| 1075 | end |
| 1076 | edges = new uint 384 0 |
| 1077 | for k ulong 384 bound 384 |
| 1078 | put edges k get topo add RES_TOPO_EDGES k |
| 1079 | end |
| 1080 | canon = new uint 514 0 |
| 1081 | tmeta = new uint 3 0 |
| 1082 | put tmeta 0 nc |
| 1083 | put tmeta 1 get topo 1 |
| 1084 | put tmeta 2 ng |
| 1085 | st = res_topology_build view tmeta view edges view grounds canon |
| 1086 | if ne st RES_OK |
| 1087 | ret st |
| 1088 | end |
| 1089 | for i ulong RES_TOPO_WORDS bound 514 |
| 1090 | if ne get canon i get topo i |
| 1091 | ret RES_ERR_TOPOLOGY |
| 1092 | end |
| 1093 | end |
| 1094 | ret RES_OK |
| 1095 | end |
| 1096 | |
| 1097 | fn res_config_build topo view uint 514 par view uint 265 step_s f32 k_steps uint out_c edit uint 5363 to int |
| 1098 | st = res_topo_verify_canon topo |
| 1099 | if ne st RES_OK |
| 1100 | ret st |
| 1101 | end |
| 1102 | st = res_params_check par get topo 0 |
| 1103 | if ne st RES_OK |
| 1104 | ret st |
| 1105 | end |
| 1106 | if or not res_f32_pos step_s gt step_s 1.0e-3f32 |
| 1107 | ret RES_ERR_PARAM |
| 1108 | end |
| 1109 | if or lt k_steps 1 gt k_steps 1048576 |
| 1110 | ret RES_ERR_PARAM |
| 1111 | end |
| 1112 | for i ulong RES_CFG_WORDS bound 5363 |
| 1113 | put out_c i 0 |
| 1114 | end |
| 1115 | for i ulong RES_TOPO_WORDS bound 514 |
| 1116 | put out_c i get topo i |
| 1117 | end |
| 1118 | for i ulong RES_PAR_WORDS bound 265 |
| 1119 | put out_c add RES_CFG_PAR i get par i |
| 1120 | end |
| 1121 | put out_c RES_CFG_STEP_S bitcast uint step_s |
| 1122 | put out_c RES_CFG_K k_steps |
| 1123 | st = res_cfg_derived out_c par step_s |
| 1124 | if ne st RES_OK |
| 1125 | ret st |
| 1126 | end |
| 1127 | st = res_cfg_fill_laplacian out_c ulong get topo 0 ulong get topo 1 |
| 1128 | if ne st RES_OK |
| 1129 | ret st |
| 1130 | end |
| 1131 | dig = new uchar 32 0 |
| 1132 | res_config_digest view out_c dig |
| 1133 | for i ulong 32 bound 32 |
| 1134 | put out_c add RES_CFG_ID i uint get dig i |
| 1135 | end |
| 1136 | ret RES_OK |
| 1137 | end |
| 1138 | |
| 1139 | note ================================================================ |
| 1140 | note State initialization, pore density, state check |
| 1141 | note ================================================================ |
| 1142 | |
| 1143 | fn res_state_init_y cfg view uint 5363 kind uint yc uint out_s edit uint 932 to void |
| 1144 | for e ulong ulong get cfg 1 bound 192 |
| 1145 | dst = add RES_ST_Y mul e 2 |
| 1146 | if eq kind RES_INIT_N0 |
| 1147 | off = add RES_TOPO_EDGES mul e 2 |
| 1148 | na = ulong get cfg off |
| 1149 | va = get cfg add RES_CFG_LN_N0 na |
| 1150 | put out_s dst va |
| 1151 | nb = ulong get cfg add off 1 |
| 1152 | vb = get cfg add RES_CFG_LN_N0 nb |
| 1153 | put out_s add dst 1 vb |
| 1154 | else |
| 1155 | put out_s dst yc |
| 1156 | put out_s add dst 1 yc |
| 1157 | end |
| 1158 | end |
| 1159 | end |
| 1160 | |
| 1161 | fn res_state_init cfg view uint 5363 kind uint val f32 out_s edit uint 932 to int |
| 1162 | yconst = 0.0f32 |
| 1163 | if eq kind RES_INIT_CONST |
| 1164 | if not res_f32_pos val |
| 1165 | ret RES_ERR_PARAM |
| 1166 | end |
| 1167 | yconst = res_math_log val |
| 1168 | if or not res_f32_fin yconst or lt yconst -80.0f32 gt yconst 80.0f32 |
| 1169 | ret RES_ERR_RANGE |
| 1170 | end |
| 1171 | else |
| 1172 | if ne kind RES_INIT_N0 |
| 1173 | ret RES_ERR_PARAM |
| 1174 | end |
| 1175 | end |
| 1176 | for i ulong RES_STATE_WORDS bound 932 |
| 1177 | put out_s i 0 |
| 1178 | end |
| 1179 | for i ulong 32 bound 32 |
| 1180 | put out_s i get cfg add RES_CFG_ID i |
| 1181 | end |
| 1182 | put out_s RES_ST_NODES get cfg 0 |
| 1183 | put out_s RES_ST_SIDES mul get cfg 1 2 |
| 1184 | res_state_init_y cfg kind bitcast uint yconst out_s |
| 1185 | ret RES_OK |
| 1186 | end |
| 1187 | |
| 1188 | fn res_state_pore s view uint 932 side uint to f32 |
| 1189 | sc = get s RES_ST_SIDES |
| 1190 | if or ge side sc ge side 384 |
| 1191 | ret bitcast f32 uint 0x7fc00000 |
| 1192 | end |
| 1193 | ret res_math_exp bitcast f32 get s add RES_ST_Y ulong side |
| 1194 | end |
| 1195 | |
| 1196 | fn res_state_get_step s view uint 932 to ulong |
| 1197 | lo = ulong get s RES_ST_STEP_LO |
| 1198 | hi = ulong get s RES_ST_STEP_HI |
| 1199 | ret or shl hi 32 lo |
| 1200 | end |
| 1201 | |
| 1202 | fn res_state_set_step s edit uint 932 step ulong to void |
| 1203 | put s RES_ST_STEP_LO uint and step 0xffffffff |
| 1204 | put s RES_ST_STEP_HI uint shr step 32 |
| 1205 | end |
| 1206 | |
| 1207 | fn res_state_check cfg view uint 5363 s view uint 932 to int |
| 1208 | for i ulong 32 bound 32 |
| 1209 | if ne get s i get cfg add RES_CFG_ID i |
| 1210 | ret RES_ERR_MISMATCH |
| 1211 | end |
| 1212 | end |
| 1213 | nc = get cfg 0 |
| 1214 | ec = get cfg 1 |
| 1215 | if or ne get s RES_ST_NODES nc ne get s RES_ST_SIDES mul ec 2 |
| 1216 | ret RES_ERR_MISMATCH |
| 1217 | end |
| 1218 | for i ulong ulong nc bound 64 |
| 1219 | vi = bitcast f32 get s add RES_ST_V i |
| 1220 | vci = bitcast f32 get s add RES_ST_VC i |
| 1221 | if or not res_f32_fin vi not res_f32_fin vci |
| 1222 | ret RES_ERR_NONFINITE |
| 1223 | end |
| 1224 | if ne get cfg add RES_TOPO_GND i 0 |
| 1225 | if ne vi 0.0f32 |
| 1226 | ret RES_ERR_RANGE |
| 1227 | end |
| 1228 | end |
| 1229 | if not res_carry_ok vi vci |
| 1230 | ret RES_ERR_RANGE |
| 1231 | end |
| 1232 | end |
| 1233 | for e ulong ulong ec bound 192 |
| 1234 | na = ulong get cfg add RES_TOPO_EDGES mul e 2 |
| 1235 | nb = ulong get cfg add RES_TOPO_EDGES add mul e 2 1 |
| 1236 | dv = sub bitcast f32 get s add RES_ST_V na bitcast f32 get s add RES_ST_V nb |
| 1237 | if not le res_f32_abs dv 1.0f32 |
| 1238 | ret RES_ERR_RANGE |
| 1239 | end |
| 1240 | end |
| 1241 | ns = ulong mul ec 2 |
| 1242 | for k ulong ns bound 384 |
| 1243 | yk = bitcast f32 get s add RES_ST_Y k |
| 1244 | yck = bitcast f32 get s add RES_ST_YC k |
| 1245 | if or not res_f32_fin yk not res_f32_fin yck |
| 1246 | ret RES_ERR_NONFINITE |
| 1247 | end |
| 1248 | if or lt yk -80.0f32 or gt yk 80.0f32 not res_carry_ok yk yck |
| 1249 | ret RES_ERR_RANGE |
| 1250 | end |
| 1251 | end |
| 1252 | ret RES_OK |
| 1253 | end |
| 1254 | |
| 1255 | note ================================================================ |
| 1256 | note Dynamics: side_rate, derivative, stiffness |
| 1257 | note ================================================================ |
| 1258 | |
| 1259 | fn res_side_rate cfg view uint 5363 node ulong dv f32 y f32 lam_out edit f32 1 to f32 |
| 1260 | alpha = res_math_exp sub bitcast f32 get cfg add RES_CFG_LN_A0 node mul dv bitcast f32 get cfg add RES_CFG_INV_VTAU node |
| 1261 | ln_nss = add bitcast f32 get cfg add RES_CFG_LN_N0 node mul dv bitcast f32 get cfg add RES_CFG_INV_VE node |
| 1262 | zv = bitcast f32 get cfg add RES_CFG_PAR 5 |
| 1263 | r = res_one_minus_exp mul zv sub y ln_nss |
| 1264 | put lam_out 0 mul mul alpha zv sub 1.0f32 r |
| 1265 | ret mul alpha r |
| 1266 | end |
| 1267 | |
| 1268 | fn res_deriv_edge_b cfg view uint 5363 ws edit uint 3564 e ulong dv f32 to f32 |
| 1269 | pores = add res_math_exp bitcast f32 get ws add RES_WS_SY mul e 2 res_math_exp bitcast f32 get ws add RES_WS_SY add mul e 2 1 |
| 1270 | q = mul mul bitcast f32 get cfg RES_CFG_G_UNIT pores dv |
| 1271 | n = ulong get cfg add RES_TOPO_EDGES mul e 2 |
| 1272 | b = sub bitcast f32 get ws add RES_WS_B n q |
| 1273 | put ws add RES_WS_B n bitcast uint b |
| 1274 | n = ulong get cfg add RES_TOPO_EDGES add mul e 2 1 |
| 1275 | b = add bitcast f32 get ws add RES_WS_B n q |
| 1276 | put ws add RES_WS_B n bitcast uint b |
| 1277 | ret mul bitcast f32 get cfg RES_CFG_LAM_UNIT pores |
| 1278 | end |
| 1279 | |
| 1280 | fn res_deriv_side cfg view uint 5363 ws edit uint 3564 e ulong stage ulong side ulong to f32 |
| 1281 | na = ulong get cfg add RES_TOPO_EDGES mul e 2 |
| 1282 | nb = ulong get cfg add RES_TOPO_EDGES add mul e 2 1 |
| 1283 | dv = sub bitcast f32 get ws add RES_WS_SV na bitcast f32 get ws add RES_WS_SV nb |
| 1284 | if ne side 0 |
| 1285 | na = nb |
| 1286 | dv = neg dv |
| 1287 | end |
| 1288 | tmp = new f32 1 0.0f32 |
| 1289 | nb = add mul e 2 side |
| 1290 | dv = res_side_rate cfg na dv bitcast f32 get ws add RES_WS_SY nb tmp |
| 1291 | put ws add add RES_WS_KY mul stage 384 nb bitcast uint dv |
| 1292 | ret get tmp 0 |
| 1293 | end |
| 1294 | |
| 1295 | fn res_deriv_one_edge cfg view uint 5363 ws edit uint 3564 e ulong stage ulong want_lam bool to f32 |
| 1296 | lmax = res_deriv_side cfg ws e stage 0 |
| 1297 | lb = res_deriv_side cfg ws e stage 1 |
| 1298 | if not le lb lmax |
| 1299 | lmax = lb |
| 1300 | end |
| 1301 | na = ulong get cfg add RES_TOPO_EDGES mul e 2 |
| 1302 | nb = ulong get cfg add RES_TOPO_EDGES add mul e 2 1 |
| 1303 | dv = sub bitcast f32 get ws add RES_WS_SV na bitcast f32 get ws add RES_WS_SV nb |
| 1304 | lv = res_deriv_edge_b cfg ws e dv |
| 1305 | if not want_lam |
| 1306 | ret 0.0f32 |
| 1307 | end |
| 1308 | if not le lv lmax |
| 1309 | lmax = lv |
| 1310 | end |
| 1311 | ret lmax |
| 1312 | end |
| 1313 | |
| 1314 | fn res_deriv_edges cfg view uint 5363 ws edit uint 3564 stage ulong want_lam bool to f32 |
| 1315 | ec = ulong get cfg 1 |
| 1316 | lmax = 0.0f32 |
| 1317 | for e ulong ec bound 192 |
| 1318 | le_val = res_deriv_one_edge cfg ws e stage want_lam |
| 1319 | if want_lam |
| 1320 | if not le le_val lmax |
| 1321 | lmax = le_val |
| 1322 | end |
| 1323 | end |
| 1324 | end |
| 1325 | ret lmax |
| 1326 | end |
| 1327 | |
| 1328 | fn res_deriv_fwd_row cfg view uint 5363 ws edit uint 3564 i ulong to void |
| 1329 | s = bitcast f32 get ws add RES_WS_R i |
| 1330 | for k ulong i bound 64 |
| 1331 | rik = bitcast f32 get cfg add RES_CFG_CHOL add mul i 64 k |
| 1332 | rk = bitcast f32 get ws add RES_WS_R k |
| 1333 | s = sub s mul rik rk |
| 1334 | end |
| 1335 | invi = bitcast f32 get cfg add RES_CFG_CHOL_INV i |
| 1336 | put ws add RES_WS_R i bitcast uint mul s invi |
| 1337 | end |
| 1338 | |
| 1339 | fn res_deriv_bwd_row cfg view uint 5363 ws edit uint 3564 m ulong ii ulong to void |
| 1340 | s = bitcast f32 get ws add RES_WS_R ii |
| 1341 | k = add ii 1 |
| 1342 | while lt k m bound 64 |
| 1343 | rki = bitcast f32 get cfg add RES_CFG_CHOL add mul k 64 ii |
| 1344 | rk = bitcast f32 get ws add RES_WS_R k |
| 1345 | s = sub s mul rki rk |
| 1346 | k = add k 1 |
| 1347 | end |
| 1348 | invi = bitcast f32 get cfg add RES_CFG_CHOL_INV ii |
| 1349 | put ws add RES_WS_R ii bitcast uint mul s invi |
| 1350 | end |
| 1351 | |
| 1352 | fn res_deriv_solve cfg view uint 5363 ws edit uint 3564 stage ulong to void |
| 1353 | m = ulong get cfg RES_CFG_RED_CNT |
| 1354 | inv_cap = bitcast f32 get cfg RES_CFG_INV_CAP |
| 1355 | for u ulong m bound 64 |
| 1356 | rn = ulong get cfg add RES_CFG_RED_NODE u |
| 1357 | bu = bitcast f32 get ws add RES_WS_B rn |
| 1358 | put ws add RES_WS_R u bitcast uint mul bu inv_cap |
| 1359 | end |
| 1360 | for i ulong m bound 64 |
| 1361 | res_deriv_fwd_row cfg ws i |
| 1362 | end |
| 1363 | for step ulong m bound 64 |
| 1364 | res_deriv_bwd_row cfg ws m sub sub m 1 step |
| 1365 | end |
| 1366 | nc = ulong get cfg 0 |
| 1367 | kv_base = add RES_WS_KV mul stage 64 |
| 1368 | for i ulong nc bound 64 |
| 1369 | u = get cfg add RES_CFG_RED_IDX i |
| 1370 | if eq u 0xffffffff |
| 1371 | put ws add kv_base i 0 |
| 1372 | else |
| 1373 | put ws add kv_base i get ws add RES_WS_R ulong u |
| 1374 | end |
| 1375 | end |
| 1376 | end |
| 1377 | |
| 1378 | fn res_derivative cfg view uint 5363 ws edit uint 3564 stage ulong want_lam bool to f32 |
| 1379 | nc = ulong get cfg 0 |
| 1380 | for i ulong nc bound 64 |
| 1381 | put ws add RES_WS_B i get ws add RES_WS_CUR i |
| 1382 | end |
| 1383 | lmax = res_deriv_edges cfg ws stage want_lam |
| 1384 | res_deriv_solve cfg ws stage |
| 1385 | ret lmax |
| 1386 | end |
| 1387 | |
| 1388 | fn res_stiffness_side cfg view uint 5363 s view uint 932 e ulong side ulong to f32 |
| 1389 | na = ulong get cfg add RES_TOPO_EDGES mul e 2 |
| 1390 | nb = ulong get cfg add RES_TOPO_EDGES add mul e 2 1 |
| 1391 | dv = sub bitcast f32 get s add RES_ST_V na bitcast f32 get s add RES_ST_V nb |
| 1392 | if ne side 0 |
| 1393 | na = nb |
| 1394 | dv = neg dv |
| 1395 | end |
| 1396 | tmp = new f32 1 0.0f32 |
| 1397 | _d = res_side_rate cfg na dv bitcast f32 get s add RES_ST_Y add mul e 2 side tmp |
| 1398 | ret get tmp 0 |
| 1399 | end |
| 1400 | |
| 1401 | fn res_stiffness_edge cfg view uint 5363 s view uint 932 e ulong to f32 |
| 1402 | lmax = res_stiffness_side cfg s e 0 |
| 1403 | lb = res_stiffness_side cfg s e 1 |
| 1404 | if not le lb lmax |
| 1405 | lmax = lb |
| 1406 | end |
| 1407 | pores = add res_math_exp bitcast f32 get s add RES_ST_Y mul e 2 res_math_exp bitcast f32 get s add RES_ST_Y add mul e 2 1 |
| 1408 | lv = mul bitcast f32 get cfg RES_CFG_LAM_UNIT pores |
| 1409 | if not le lv lmax |
| 1410 | lmax = lv |
| 1411 | end |
| 1412 | ret lmax |
| 1413 | end |
| 1414 | |
| 1415 | fn res_stiffness cfg view uint 5363 s view uint 932 lam_out edit f32 1 to int |
| 1416 | st = res_state_check cfg s |
| 1417 | if ne st RES_OK |
| 1418 | ret st |
| 1419 | end |
| 1420 | ec = ulong get cfg 1 |
| 1421 | lmax = 0.0f32 |
| 1422 | for e ulong ec bound 192 |
| 1423 | le_val = res_stiffness_edge cfg s e |
| 1424 | if not le le_val lmax |
| 1425 | lmax = le_val |
| 1426 | end |
| 1427 | end |
| 1428 | if not res_f32_fin lmax |
| 1429 | ret RES_ERR_NONFINITE |
| 1430 | end |
| 1431 | put lam_out 0 lmax |
| 1432 | ret RES_OK |
| 1433 | end |
| 1434 | |
| 1435 | note ================================================================ |
| 1436 | note Input check & interpolation, RK4 step, membrane stats |
| 1437 | note ================================================================ |
| 1438 | |
| 1439 | fn res_knot_check tab view f32 * nc ulong k ulong to int |
| 1440 | base = mul k nc |
| 1441 | for i ulong nc bound 64 |
| 1442 | v = get tab add base i |
| 1443 | if not res_f32_fin v |
| 1444 | ret RES_ERR_NONFINITE |
| 1445 | end |
| 1446 | if gt res_f32_abs v 1.0e-6f32 |
| 1447 | ret RES_ERR_INPUT |
| 1448 | end |
| 1449 | end |
| 1450 | ret RES_OK |
| 1451 | end |
| 1452 | |
| 1453 | fn res_input_interp tab view f32 * ws edit uint 3564 b0 ulong nc ulong f f32 to void |
| 1454 | for i ulong nc bound 64 |
| 1455 | v0 = get tab add b0 i |
| 1456 | v1 = get tab add add b0 nc i |
| 1457 | v0 = add v0 mul f sub v1 v0 |
| 1458 | put ws add RES_WS_CUR i bitcast uint v0 |
| 1459 | end |
| 1460 | end |
| 1461 | |
| 1462 | fn res_input_at cfg view uint 5363 tab view f32 * ws edit uint 3564 k ulong phase_hs uint to void |
| 1463 | nc = ulong get cfg 0 |
| 1464 | f = div f32 phase_hs f32 mul get cfg RES_CFG_K 2 |
| 1465 | res_input_interp tab ws mul k nc nc f |
| 1466 | end |
| 1467 | |
| 1468 | fn res_rk4_stage_prep cur view uint 932 ws edit uint 3564 c f32 prev_st ulong to void |
| 1469 | nc = ulong get cur RES_ST_NODES |
| 1470 | ns = ulong get cur RES_ST_SIDES |
| 1471 | kv_b = add RES_WS_KV mul prev_st 64 |
| 1472 | ky_b = add RES_WS_KY mul prev_st 384 |
| 1473 | for i ulong nc bound 64 |
| 1474 | vi = bitcast f32 get cur add RES_ST_V i |
| 1475 | kvi = bitcast f32 get ws add kv_b i |
| 1476 | put ws add RES_WS_SV i bitcast uint add vi mul c kvi |
| 1477 | end |
| 1478 | for j ulong ns bound 384 |
| 1479 | yj = bitcast f32 get cur add RES_ST_Y j |
| 1480 | kyj = bitcast f32 get ws add ky_b j |
| 1481 | put ws add RES_WS_SY j bitcast uint add yj mul c kyj |
| 1482 | end |
| 1483 | end |
| 1484 | |
| 1485 | fn res_twosum_put ws edit uint 3564 off_v ulong off_c ulong x f32 y f32 to void |
| 1486 | sv = add x y |
| 1487 | bp = sub sv x |
| 1488 | ap = sub sv bp |
| 1489 | err = add sub x ap sub y bp |
| 1490 | put ws off_v bitcast uint sv |
| 1491 | put ws off_c bitcast uint err |
| 1492 | end |
| 1493 | |
| 1494 | fn res_rk4_combine_v h6 f32 nc ulong cur view uint 932 ws edit uint 3564 to void |
| 1495 | for i ulong nc bound 64 |
| 1496 | d = bitcast f32 get ws add RES_WS_KV i |
| 1497 | d = add d mul 2.0f32 bitcast f32 get ws add add RES_WS_KV 64 i |
| 1498 | d = add d mul 2.0f32 bitcast f32 get ws add add RES_WS_KV 128 i |
| 1499 | d = add d bitcast f32 get ws add add RES_WS_KV 192 i |
| 1500 | d = mul h6 d |
| 1501 | x = bitcast f32 get cur add RES_ST_V i |
| 1502 | y = add d bitcast f32 get cur add RES_ST_VC i |
| 1503 | ov = add RES_WS_SPARE add RES_ST_V i |
| 1504 | oc = add RES_WS_SPARE add RES_ST_VC i |
| 1505 | res_twosum_put ws ov oc x y |
| 1506 | end |
| 1507 | end |
| 1508 | |
| 1509 | fn res_rk4_combine_y h6 f32 ns ulong cur view uint 932 ws edit uint 3564 to void |
| 1510 | for j ulong ns bound 384 |
| 1511 | d = bitcast f32 get ws add RES_WS_KY j |
| 1512 | d = add d mul 2.0f32 bitcast f32 get ws add add RES_WS_KY 384 j |
| 1513 | d = add d mul 2.0f32 bitcast f32 get ws add add RES_WS_KY 768 j |
| 1514 | d = add d bitcast f32 get ws add add RES_WS_KY 1152 j |
| 1515 | d = mul h6 d |
| 1516 | x = bitcast f32 get cur add RES_ST_Y j |
| 1517 | y = add d bitcast f32 get cur add RES_ST_YC j |
| 1518 | ov = add RES_WS_SPARE add RES_ST_Y j |
| 1519 | oc = add RES_WS_SPARE add RES_ST_YC j |
| 1520 | res_twosum_put ws ov oc x y |
| 1521 | end |
| 1522 | end |
| 1523 | |
| 1524 | fn res_rk4_combine cfg view uint 5363 cur view uint 932 ws edit uint 3564 to void |
| 1525 | h6 = bitcast f32 get cfg RES_CFG_SIXTH_H |
| 1526 | nc = ulong get cur RES_ST_NODES |
| 1527 | ns = ulong get cur RES_ST_SIDES |
| 1528 | for i ulong RES_STATE_WORDS bound 932 |
| 1529 | put ws add RES_WS_SPARE i 0 |
| 1530 | end |
| 1531 | for i ulong 32 bound 32 |
| 1532 | put ws add RES_WS_SPARE i get cur i |
| 1533 | end |
| 1534 | nstep = add res_state_get_step cur 1 |
| 1535 | put ws add RES_WS_SPARE RES_ST_STEP_LO uint and nstep 0xffffffff |
| 1536 | put ws add RES_WS_SPARE RES_ST_STEP_HI uint shr nstep 32 |
| 1537 | put ws add RES_WS_SPARE RES_ST_NODES uint nc |
| 1538 | put ws add RES_WS_SPARE RES_ST_SIDES uint ns |
| 1539 | res_rk4_combine_v h6 nc cur ws |
| 1540 | res_rk4_combine_y h6 ns cur ws |
| 1541 | end |
| 1542 | |
| 1543 | fn res_step_init_sv cur view uint 932 ws edit uint 3564 to void |
| 1544 | for i ulong ulong get cur RES_ST_NODES bound 64 |
| 1545 | put ws add RES_WS_SV i get cur add RES_ST_V i |
| 1546 | end |
| 1547 | for j ulong ulong get cur RES_ST_SIDES bound 384 |
| 1548 | put ws add RES_WS_SY j get cur add RES_ST_Y j |
| 1549 | end |
| 1550 | end |
| 1551 | |
| 1552 | fn res_step_check_spare cfg view uint 5363 ws view uint 3564 to int |
| 1553 | spare_v = new uint 932 0 |
| 1554 | for i ulong RES_STATE_WORDS bound 932 |
| 1555 | put spare_v i get ws add RES_WS_SPARE i |
| 1556 | end |
| 1557 | ret res_state_check cfg view spare_v |
| 1558 | end |
| 1559 | |
| 1560 | fn res_step_stages cfg view uint 5363 tab view f32 * ws edit uint 3564 cur view uint 932 kp view ulong 2 to int |
| 1561 | res_step_init_sv cur ws |
| 1562 | k64 = get kp 0 |
| 1563 | p2 = mul uint get kp 1 2 |
| 1564 | res_input_at cfg tab ws k64 p2 |
| 1565 | lam = res_derivative cfg ws 0 true |
| 1566 | if not le mul bitcast f32 get cfg RES_CFG_STEP_S lam 2.0f32 |
| 1567 | if res_f32_fin lam |
| 1568 | ret RES_ERR_STIFF |
| 1569 | end |
| 1570 | ret RES_ERR_NONFINITE |
| 1571 | end |
| 1572 | res_rk4_stage_prep cur ws bitcast f32 get cfg RES_CFG_HALF_H 0 |
| 1573 | res_input_at cfg tab ws k64 add p2 1 |
| 1574 | lam = res_derivative cfg ws 1 false |
| 1575 | res_rk4_stage_prep cur ws bitcast f32 get cfg RES_CFG_HALF_H 1 |
| 1576 | res_input_at cfg tab ws k64 add p2 1 |
| 1577 | lam = res_derivative cfg ws 2 false |
| 1578 | res_rk4_stage_prep cur ws bitcast f32 get cfg RES_CFG_STEP_S 2 |
| 1579 | res_input_at cfg tab ws k64 add p2 2 |
| 1580 | lam = res_derivative cfg ws 3 false |
| 1581 | res_rk4_combine cfg cur ws |
| 1582 | ret res_step_check_spare cfg view ws |
| 1583 | end |
| 1584 | |
| 1585 | note One RK4 step from cur into ws->spare. meta: [0]=in_node_count [1]=in_knot_count. |
| 1586 | fn res_step_spare cfg view uint 5363 tab view f32 * meta view uint 2 ws edit uint 3564 cur view uint 932 to int |
| 1587 | st = res_state_check cfg cur |
| 1588 | if ne st RES_OK |
| 1589 | ret st |
| 1590 | end |
| 1591 | if or ne get meta 0 get cfg 0 lt get meta 1 2 |
| 1592 | ret RES_ERR_INPUT |
| 1593 | end |
| 1594 | if lt len tab mul ulong get meta 0 ulong get meta 1 |
| 1595 | ret RES_ERR_INPUT |
| 1596 | end |
| 1597 | k64 = div res_state_get_step cur ulong get cfg RES_CFG_K |
| 1598 | if ge add k64 1 ulong get meta 1 |
| 1599 | ret RES_ERR_INPUT |
| 1600 | end |
| 1601 | st = res_knot_check tab ulong get cfg 0 k64 |
| 1602 | if ne st RES_OK |
| 1603 | ret st |
| 1604 | end |
| 1605 | st = res_knot_check tab ulong get cfg 0 add k64 1 |
| 1606 | if ne st RES_OK |
| 1607 | ret st |
| 1608 | end |
| 1609 | kp = new ulong 2 0 |
| 1610 | put kp 0 k64 |
| 1611 | put kp 1 rem res_state_get_step cur ulong get cfg RES_CFG_K |
| 1612 | ret res_step_stages cfg tab ws cur view kp |
| 1613 | end |
| 1614 | |
| 1615 | note One RK4 step using a 2-knot window pair (knot j at [0..nc-1], knot j+1 at [nc..2*nc-1]). |
| 1616 | fn res_step_pair cfg view uint 5363 pair view f32 128 ws edit uint 3564 cur view uint 932 to int |
| 1617 | st = res_state_check cfg cur |
| 1618 | if ne st RES_OK |
| 1619 | ret st |
| 1620 | end |
| 1621 | nc = ulong get cfg 0 |
| 1622 | st = res_knot_check pair nc 0 |
| 1623 | if ne st RES_OK |
| 1624 | ret st |
| 1625 | end |
| 1626 | st = res_knot_check pair nc 1 |
| 1627 | if ne st RES_OK |
| 1628 | ret st |
| 1629 | end |
| 1630 | kp = new ulong 2 0 |
| 1631 | put kp 0 0 |
| 1632 | put kp 1 rem res_state_get_step cur ulong get cfg RES_CFG_K |
| 1633 | ret res_step_stages cfg pair ws cur view kp |
| 1634 | end |
| 1635 | |
| 1636 | fn res_copy_spare_to_state ws view uint 3564 dst edit uint 932 to void |
| 1637 | for i ulong RES_STATE_WORDS bound 932 |
| 1638 | put dst i get ws add RES_WS_SPARE i |
| 1639 | end |
| 1640 | end |
| 1641 | |
| 1642 | fn res_membrane_stats_init st edit uint 200 to void |
| 1643 | for i ulong RES_MSTATS_WORDS bound 200 |
| 1644 | put st i 0 |
| 1645 | end |
| 1646 | end |
| 1647 | |
| 1648 | fn res_ws_mstats_init ws edit uint 3564 to void |
| 1649 | for i ulong RES_MSTATS_WORDS bound 200 |
| 1650 | put ws add RES_WS_MSTATS i 0 |
| 1651 | end |
| 1652 | end |
| 1653 | |
| 1654 | fn res_ws_mstats_copy ws view uint 3564 dst edit uint 200 to void |
| 1655 | for i ulong RES_MSTATS_WORDS bound 200 |
| 1656 | put dst i get ws add RES_WS_MSTATS i |
| 1657 | end |
| 1658 | end |
| 1659 | |
| 1660 | fn res_edge_abs_dv cfg view uint 5363 s view uint 932 e ulong to f32 |
| 1661 | off = add RES_TOPO_EDGES mul e 2 |
| 1662 | va = bitcast f32 get s add RES_ST_V ulong get cfg off |
| 1663 | vb = bitcast f32 get s add RES_ST_V ulong get cfg add off 1 |
| 1664 | ret res_f32_abs sub va vb |
| 1665 | end |
| 1666 | |
| 1667 | fn res_membrane_observe_edges cfg view uint 5363 s view uint 932 st edit uint 200 to bool |
| 1668 | max_mv = bitcast f32 get st RES_MS_MAX_MV |
| 1669 | any = false |
| 1670 | for e ulong ulong get cfg 1 bound 192 |
| 1671 | dv = res_edge_abs_dv cfg s e |
| 1672 | if gt dv max_mv |
| 1673 | max_mv = dv |
| 1674 | end |
| 1675 | if gt dv 0.3f32 |
| 1676 | any = true |
| 1677 | if eq get st add RES_MS_EFLAG e 0 |
| 1678 | put st add RES_MS_EFLAG e 1 |
| 1679 | put st RES_MS_MEM_IMP add get st RES_MS_MEM_IMP 1 |
| 1680 | end |
| 1681 | end |
| 1682 | end |
| 1683 | put st RES_MS_MAX_MV bitcast uint max_mv |
| 1684 | ret any |
| 1685 | end |
| 1686 | |
| 1687 | fn res_membrane_observe cfg view uint 5363 s view uint 932 st edit uint 200 to int |
| 1688 | for i ulong 32 bound 32 |
| 1689 | if ne get s i get cfg add RES_CFG_ID i |
| 1690 | ret RES_ERR_MISMATCH |
| 1691 | end |
| 1692 | end |
| 1693 | nc = get cfg 0 |
| 1694 | if ne get s RES_ST_NODES nc |
| 1695 | ret RES_ERR_MISMATCH |
| 1696 | end |
| 1697 | max_nv = bitcast f32 get st RES_MS_MAX_NV |
| 1698 | for i ulong ulong nc bound 64 |
| 1699 | m = res_f32_abs bitcast f32 get s add RES_ST_V i |
| 1700 | if gt m max_nv |
| 1701 | max_nv = m |
| 1702 | end |
| 1703 | end |
| 1704 | put st RES_MS_MAX_NV bitcast uint max_nv |
| 1705 | any = res_membrane_observe_edges cfg s st |
| 1706 | obs = add or shl ulong get st RES_MS_OBS_HI 32 ulong get st RES_MS_OBS_LO 1 |
| 1707 | put st RES_MS_OBS_LO uint and obs 0xffffffff |
| 1708 | put st RES_MS_OBS_HI uint shr obs 32 |
| 1709 | if any |
| 1710 | imp = add or shl ulong get st RES_MS_IMP_HI 32 ulong get st RES_MS_IMP_LO 1 |
| 1711 | put st RES_MS_IMP_LO uint and imp 0xffffffff |
| 1712 | put st RES_MS_IMP_HI uint shr imp 32 |
| 1713 | end |
| 1714 | ret RES_OK |
| 1715 | end |
| 1716 | |
| 1717 | fn res_ws_observe_state cfg view uint 5363 s view uint 932 ws edit uint 3564 to void |
| 1718 | ms = new uint 200 0 |
| 1719 | res_ws_mstats_copy view ws ms |
| 1720 | _rc = res_membrane_observe cfg s ms |
| 1721 | for i ulong RES_MSTATS_WORDS bound 200 |
| 1722 | put ws add RES_WS_MSTATS i get ms i |
| 1723 | end |
| 1724 | end |
| 1725 | |
| 1726 | note Advance state s by steps. |
| 1727 | note adv_meta: [0]=in_node_count [1]=in_knot_count [2]=steps_lo [3]=steps_hi [4]=observe_flag |
| 1728 | fn res_advance_meta cfg view uint 5363 tab view f32 * adv_meta view uint 5 ws edit uint 3564 s edit uint 932 to int |
| 1729 | imeta = new uint 2 0 |
| 1730 | put imeta 0 get adv_meta 0 |
| 1731 | put imeta 1 get adv_meta 1 |
| 1732 | steps = or shl ulong get adv_meta 3 32 ulong get adv_meta 2 |
| 1733 | obs = ne get adv_meta 4 0 |
| 1734 | st = RES_OK |
| 1735 | for i ulong steps bound 50000000 |
| 1736 | st = res_step_spare cfg tab view imeta ws view s |
| 1737 | if ne st RES_OK |
| 1738 | ret st |
| 1739 | end |
| 1740 | res_copy_spare_to_state view ws s |
| 1741 | if obs |
| 1742 | res_ws_observe_state cfg view s ws |
| 1743 | end |
| 1744 | end |
| 1745 | ret RES_OK |
| 1746 | end |
| 1747 | |
| 1748 | note ================================================================ |
| 1749 | note Receipts: input_digest, state_digest, run_receipt |
| 1750 | note ================================================================ |
| 1751 | |
| 1752 | fn res_input_digest_body ctx edit uint 74 tab view f32 * total ulong to void |
| 1753 | buf = new uchar 1024 0 |
| 1754 | used = ulong 0 |
| 1755 | for i ulong total bound 30000000 |
| 1756 | u = res_f32_bits_canon get tab i |
| 1757 | put buf used uchar and u 0xff |
| 1758 | put buf add used 1 uchar and shr u 8 0xff |
| 1759 | put buf add used 2 uchar and shr u 16 0xff |
| 1760 | put buf add used 3 uchar and shr u 24 0xff |
| 1761 | used = add used 4 |
| 1762 | if eq used 1024 |
| 1763 | res_sha_update ctx view buf |
| 1764 | used = 0 |
| 1765 | end |
| 1766 | end |
| 1767 | if gt used 0 |
| 1768 | res_sha_update ctx part buf 0 used |
| 1769 | end |
| 1770 | end |
| 1771 | |
| 1772 | fn res_input_digest_begin ctx edit uint 74 nc uint kn uint to void |
| 1773 | res_sha_init ctx |
| 1774 | res_h_domain ctx res_dom_input |
| 1775 | res_h_u32 ctx nc |
| 1776 | res_h_u32 ctx kn |
| 1777 | end |
| 1778 | |
| 1779 | fn res_input_digest_row ctx edit uint 74 row view f32 64 nc uint to void |
| 1780 | buf = new uchar 256 0 |
| 1781 | for i ulong ulong nc bound 64 |
| 1782 | u = res_f32_bits_canon get row i |
| 1783 | off = mul i 4 |
| 1784 | put buf off uchar and u 0xff |
| 1785 | put buf add off 1 uchar and shr u 8 0xff |
| 1786 | put buf add off 2 uchar and shr u 16 0xff |
| 1787 | put buf add off 3 uchar and shr u 24 0xff |
| 1788 | end |
| 1789 | res_sha_update ctx part buf 0 mul ulong nc 4 |
| 1790 | end |
| 1791 | |
| 1792 | fn res_input_digest tab view f32 * meta view uint 2 out_d edit uchar 32 to int |
| 1793 | nc = get meta 0 |
| 1794 | kn = get meta 1 |
| 1795 | total = mul ulong nc ulong kn |
| 1796 | if lt len tab total |
| 1797 | ret RES_ERR_INPUT |
| 1798 | end |
| 1799 | ctx = new uint 74 0 |
| 1800 | res_h_domain ctx res_dom_input |
| 1801 | res_h_u32 ctx nc |
| 1802 | res_h_u32 ctx kn |
| 1803 | res_input_digest_body ctx tab total |
| 1804 | res_sha_final ctx out_d |
| 1805 | ret RES_OK |
| 1806 | end |
| 1807 | |
| 1808 | fn res_state_digest s view uint 932 out_d edit uchar 32 to int |
| 1809 | nc = get s RES_ST_NODES |
| 1810 | sc = get s RES_ST_SIDES |
| 1811 | if or gt nc 64 gt sc 384 |
| 1812 | ret RES_ERR_MISMATCH |
| 1813 | end |
| 1814 | ctx = new uint 74 0 |
| 1815 | res_h_domain ctx res_dom_state |
| 1816 | cid = new uchar 32 0 |
| 1817 | for i ulong 32 bound 32 |
| 1818 | put cid i uchar and get s i 0xff |
| 1819 | end |
| 1820 | res_sha_update ctx view cid |
| 1821 | res_h_u64 ctx res_state_get_step s |
| 1822 | res_h_u32 ctx nc |
| 1823 | res_h_u32 ctx sc |
| 1824 | for i ulong ulong nc bound 64 |
| 1825 | res_h_f32 ctx bitcast f32 get s add RES_ST_V i |
| 1826 | end |
| 1827 | for k ulong ulong sc bound 384 |
| 1828 | res_h_f32 ctx bitcast f32 get s add RES_ST_Y k |
| 1829 | end |
| 1830 | for i ulong ulong nc bound 64 |
| 1831 | res_h_f32 ctx bitcast f32 get s add RES_ST_VC i |
| 1832 | end |
| 1833 | for k ulong ulong sc bound 384 |
| 1834 | res_h_f32 ctx bitcast f32 get s add RES_ST_YC k |
| 1835 | end |
| 1836 | res_sha_final ctx out_d |
| 1837 | ret RES_OK |
| 1838 | end |
| 1839 | |
| 1840 | fn res_receipt_finalize cdig view uchar 32 idig view uchar 32 sdig view uchar 32 steps ulong out_r edit uint 130 to void |
| 1841 | ctx = new uint 74 0 |
| 1842 | res_h_domain ctx res_dom_run |
| 1843 | res_sha_update ctx cdig |
| 1844 | res_sha_update ctx idig |
| 1845 | res_h_u64 ctx steps |
| 1846 | res_sha_update ctx sdig |
| 1847 | rdig = new uchar 32 0 |
| 1848 | res_sha_final ctx rdig |
| 1849 | for i ulong 32 bound 32 |
| 1850 | put out_r i uint get cdig i |
| 1851 | put out_r add 32 i uint get idig i |
| 1852 | put out_r add 64 i uint get sdig i |
| 1853 | put out_r add 98 i uint get rdig i |
| 1854 | end |
| 1855 | put out_r 96 uint and steps 0xffffffff |
| 1856 | put out_r 97 uint shr steps 32 |
| 1857 | end |
| 1858 | |
| 1859 | fn res_copy_cfg_id cfg view uint 5363 dst edit uchar 32 to void |
| 1860 | for i ulong 32 bound 32 |
| 1861 | put dst i uchar and get cfg add RES_CFG_ID i 0xff |
| 1862 | end |
| 1863 | end |
| 1864 | |
| 1865 | fn res_run_receipt_from_idig cfg view uint 5363 idig view uchar 32 s view uint 932 out_r edit uint 130 to int |
| 1866 | st = res_state_check cfg s |
| 1867 | if ne st RES_OK |
| 1868 | ret st |
| 1869 | end |
| 1870 | sdig = new uchar 32 0 |
| 1871 | st = res_state_digest s sdig |
| 1872 | if ne st RES_OK |
| 1873 | ret st |
| 1874 | end |
| 1875 | cdig = new uchar 32 0 |
| 1876 | res_copy_cfg_id cfg cdig |
| 1877 | res_receipt_finalize view cdig idig view sdig res_state_get_step s out_r |
| 1878 | ret RES_OK |
| 1879 | end |
| 1880 | |
| 1881 | fn res_run_receipt cfg view uint 5363 tab view f32 * meta view uint 2 s view uint 932 out_r edit uint 130 to int |
| 1882 | st = res_state_check cfg s |
| 1883 | if ne st RES_OK |
| 1884 | ret st |
| 1885 | end |
| 1886 | idig = new uchar 32 0 |
| 1887 | st = res_input_digest tab meta idig |
| 1888 | if ne st RES_OK |
| 1889 | ret st |
| 1890 | end |
| 1891 | sdig = new uchar 32 0 |
| 1892 | st = res_state_digest s sdig |
| 1893 | if ne st RES_OK |
| 1894 | ret st |
| 1895 | end |
| 1896 | cdig = new uchar 32 0 |
| 1897 | res_copy_cfg_id cfg cdig |
| 1898 | res_receipt_finalize view cdig view idig view sdig res_state_get_step s out_r |
| 1899 | ret RES_OK |
| 1900 | end |
| 1901 | |
| 1902 | note ================================================================ |
| 1903 | note Binary64 Ridge Readout & NRMSE (ported from reservoir_readout.om) |
| 1904 | note ================================================================ |
| 1905 | |
| 1906 | fn res_fin64 bits ulong to bool |
| 1907 | ret lt and bits 0x7ff0000000000000 0x7ff0000000000000 |
| 1908 | end |
| 1909 | |
| 1910 | fn res_design_val x view f64 * t ulong nf ulong col ulong to f64 |
| 1911 | if eq col 0 |
| 1912 | ret 1.0f64 |
| 1913 | end |
| 1914 | ret get x add mul t nf sub col 1 |
| 1915 | end |
| 1916 | |
| 1917 | fn res_dot_xx x view f64 * meta view ulong 4 i ulong j ulong to f64 |
| 1918 | s = 0.0f64 |
| 1919 | for t ulong get meta 0 bound 2000000 |
| 1920 | xi = res_design_val x t get meta 1 i |
| 1921 | xj = res_design_val x t get meta 1 j |
| 1922 | s = add s mul xi xj |
| 1923 | end |
| 1924 | ret s |
| 1925 | end |
| 1926 | |
| 1927 | fn res_dot_xy x view f64 * y view f64 * meta view ulong 4 io ulong to f64 |
| 1928 | s = 0.0f64 |
| 1929 | for t ulong get meta 0 bound 2000000 |
| 1930 | xi = res_design_val x t get meta 1 div io 4 |
| 1931 | idx = mul t get meta 2 |
| 1932 | idx = add idx rem io 4 |
| 1933 | s = add s mul xi get y idx |
| 1934 | end |
| 1935 | ret s |
| 1936 | end |
| 1937 | |
| 1938 | fn res_ro_check_args x view f64 * y view f64 * meta view ulong 4 to int |
| 1939 | ns = get meta 0 |
| 1940 | nf = get meta 1 |
| 1941 | no = get meta 2 |
| 1942 | lam_b = get meta 3 |
| 1943 | if or lt nf 1 gt nf 256 |
| 1944 | ret RES_ERR_PARAM |
| 1945 | end |
| 1946 | if or lt no 1 gt no 4 |
| 1947 | ret RES_ERR_PARAM |
| 1948 | end |
| 1949 | if or lt ns 1 not res_fin64 lam_b |
| 1950 | ret RES_ERR_PARAM |
| 1951 | end |
| 1952 | if lt bitcast f64 lam_b 0.0f64 |
| 1953 | ret RES_ERR_PARAM |
| 1954 | end |
| 1955 | if or lt len x mul ns nf lt len y mul ns no |
| 1956 | ret RES_ERR_PARAM |
| 1957 | end |
| 1958 | for i ulong mul ns nf bound 50000000 |
| 1959 | if not res_fin64 bitcast ulong get x i |
| 1960 | ret RES_ERR_NONFINITE |
| 1961 | end |
| 1962 | end |
| 1963 | for i ulong mul ns no bound 20000000 |
| 1964 | if not res_fin64 bitcast ulong get y i |
| 1965 | ret RES_ERR_NONFINITE |
| 1966 | end |
| 1967 | end |
| 1968 | ret RES_OK |
| 1969 | end |
| 1970 | |
| 1971 | fn res_ro_fill_g x view f64 * ws edit ulong 67334 meta view ulong 4 to void |
| 1972 | for i ulong add get meta 1 1 bound 257 |
| 1973 | for j ulong add i 1 bound 257 |
| 1974 | s = res_dot_xx x meta i j |
| 1975 | if and ne i 0 eq j i |
| 1976 | s = add s bitcast f64 get meta 3 |
| 1977 | end |
| 1978 | put ws add mul i RES_RO_DIM j bitcast ulong s |
| 1979 | end |
| 1980 | end |
| 1981 | end |
| 1982 | |
| 1983 | fn res_ro_fill_r x view f64 * y view f64 * ws edit ulong 67334 meta view ulong 4 to void |
| 1984 | for io ulong mul add get meta 1 1 4 bound 1028 |
| 1985 | if lt rem io 4 get meta 2 |
| 1986 | s = res_dot_xy x y meta io |
| 1987 | put ws add RES_RWS_R_OFF io bitcast ulong s |
| 1988 | end |
| 1989 | end |
| 1990 | end |
| 1991 | |
| 1992 | fn res_ro_ldlt_pivot ws edit ulong 67334 j ulong to int |
| 1993 | gjj = bitcast f64 get ws add mul j RES_RO_DIM j |
| 1994 | dj = gjj |
| 1995 | for k ulong j bound 257 |
| 1996 | ljk = bitcast f64 get ws add mul j RES_RO_DIM k |
| 1997 | dk = bitcast f64 get ws add RES_RWS_D_OFF k |
| 1998 | dj = sub dj mul mul ljk ljk dk |
| 1999 | end |
| 2000 | dj_b = bitcast ulong dj |
| 2001 | tol = mul bitcast f64 ulong 0x3d3c25c268497682 gjj |
| 2002 | if or not res_fin64 dj_b le dj tol |
| 2003 | ret RES_ERR_SINGULAR |
| 2004 | end |
| 2005 | put ws add RES_RWS_D_OFF j dj_b |
| 2006 | ret RES_OK |
| 2007 | end |
| 2008 | |
| 2009 | fn res_ro_ldlt_entry ws edit ulong 67334 row ulong j ulong to void |
| 2010 | dj = bitcast f64 get ws add RES_RWS_D_OFF j |
| 2011 | s = bitcast f64 get ws add mul row RES_RO_DIM j |
| 2012 | for k ulong j bound 257 |
| 2013 | lik = bitcast f64 get ws add mul row RES_RO_DIM k |
| 2014 | ljk = bitcast f64 get ws add mul j RES_RO_DIM k |
| 2015 | dk = bitcast f64 get ws add RES_RWS_D_OFF k |
| 2016 | s = sub s mul mul lik ljk dk |
| 2017 | end |
| 2018 | put ws add mul row RES_RO_DIM j bitcast ulong div s dj |
| 2019 | end |
| 2020 | |
| 2021 | fn res_ro_solve_fwd_row ws edit ulong 67334 i ulong o ulong to void |
| 2022 | s = bitcast f64 get ws add RES_RWS_R_OFF add mul i RES_RO_MAX_OUT o |
| 2023 | for k ulong i bound 257 |
| 2024 | lik = bitcast f64 get ws add mul i RES_RO_DIM k |
| 2025 | rk = bitcast f64 get ws add RES_RWS_R_OFF add mul k RES_RO_MAX_OUT o |
| 2026 | s = sub s mul lik rk |
| 2027 | end |
| 2028 | put ws add RES_RWS_R_OFF add mul i RES_RO_MAX_OUT o bitcast ulong s |
| 2029 | end |
| 2030 | |
| 2031 | fn res_ro_solve_fwd ws edit ulong 67334 p ulong o ulong to void |
| 2032 | for i ulong p bound 257 |
| 2033 | res_ro_solve_fwd_row ws i o |
| 2034 | end |
| 2035 | for i ulong p bound 257 |
| 2036 | ri = bitcast f64 get ws add RES_RWS_R_OFF add mul i RES_RO_MAX_OUT o |
| 2037 | di = bitcast f64 get ws add RES_RWS_D_OFF i |
| 2038 | put ws add RES_RWS_R_OFF add mul i RES_RO_MAX_OUT o bitcast ulong div ri di |
| 2039 | end |
| 2040 | end |
| 2041 | |
| 2042 | fn res_ro_solve_bwd_row ws edit ulong 67334 p ulong ii ulong o ulong to int |
| 2043 | s = bitcast f64 get ws add RES_RWS_R_OFF add mul ii RES_RO_MAX_OUT o |
| 2044 | k = add ii 1 |
| 2045 | while lt k p bound 257 |
| 2046 | lki = bitcast f64 get ws add mul k RES_RO_DIM ii |
| 2047 | rk = bitcast f64 get ws add RES_RWS_R_OFF add mul k RES_RO_MAX_OUT o |
| 2048 | s = sub s mul lki rk |
| 2049 | k = add k 1 |
| 2050 | end |
| 2051 | sb = bitcast ulong s |
| 2052 | if not res_fin64 sb |
| 2053 | ret RES_ERR_NONFINITE |
| 2054 | end |
| 2055 | put ws add RES_RWS_R_OFF add mul ii RES_RO_MAX_OUT o sb |
| 2056 | ret RES_OK |
| 2057 | end |
| 2058 | |
| 2059 | fn res_ro_solve ws edit ulong 67334 nf ulong no ulong to int |
| 2060 | p = add nf 1 |
| 2061 | for j ulong p bound 257 |
| 2062 | rc = res_ro_ldlt_pivot ws j |
| 2063 | if ne rc RES_OK |
| 2064 | ret rc |
| 2065 | end |
| 2066 | for i ulong sub p add j 1 bound 257 |
| 2067 | res_ro_ldlt_entry ws add add j 1 i j |
| 2068 | end |
| 2069 | end |
| 2070 | for o ulong no bound 4 |
| 2071 | res_ro_solve_fwd ws p o |
| 2072 | for step ulong p bound 257 |
| 2073 | rc = res_ro_solve_bwd_row ws p sub sub p 1 step o |
| 2074 | if ne rc RES_OK |
| 2075 | ret rc |
| 2076 | end |
| 2077 | end |
| 2078 | end |
| 2079 | ret RES_OK |
| 2080 | end |
| 2081 | |
| 2082 | fn res_ro_ws_zero ws edit ulong 67334 nf ulong to void |
| 2083 | p = add nf 1 |
| 2084 | for i ulong p bound 257 |
| 2085 | for j ulong add i 1 bound 257 |
| 2086 | put ws add mul i RES_RO_DIM j 0 |
| 2087 | end |
| 2088 | for o ulong 4 bound 4 |
| 2089 | put ws add RES_RWS_R_OFF add mul i 4 o 0 |
| 2090 | end |
| 2091 | end |
| 2092 | end |
| 2093 | |
| 2094 | fn res_ro_ws_accum_i ws edit ulong 67334 xrow view f64 256 yrow view f64 4 i ulong no ulong to void |
| 2095 | xi = 1.0f64 |
| 2096 | if gt i 0 |
| 2097 | xi = get xrow sub i 1 |
| 2098 | end |
| 2099 | for j ulong add i 1 bound 257 |
| 2100 | xj = 1.0f64 |
| 2101 | if gt j 0 |
| 2102 | xj = get xrow sub j 1 |
| 2103 | end |
| 2104 | idx = add mul i RES_RO_DIM j |
| 2105 | put ws idx bitcast ulong add bitcast f64 get ws idx mul xi xj |
| 2106 | end |
| 2107 | for o ulong no bound 4 |
| 2108 | ridx = add RES_RWS_R_OFF add mul i 4 o |
| 2109 | put ws ridx bitcast ulong add bitcast f64 get ws ridx mul xi get yrow o |
| 2110 | end |
| 2111 | end |
| 2112 | |
| 2113 | fn res_ro_ws_accum_row ws edit ulong 67334 xrow view f64 256 yrow view f64 4 nf ulong no ulong to int |
| 2114 | for f ulong nf bound 256 |
| 2115 | if not res_fin64 bitcast ulong get xrow f |
| 2116 | ret RES_ERR_NONFINITE |
| 2117 | end |
| 2118 | end |
| 2119 | for o ulong no bound 4 |
| 2120 | if not res_fin64 bitcast ulong get yrow o |
| 2121 | ret RES_ERR_NONFINITE |
| 2122 | end |
| 2123 | end |
| 2124 | for i ulong add nf 1 bound 257 |
| 2125 | res_ro_ws_accum_i ws xrow yrow i no |
| 2126 | end |
| 2127 | ret RES_OK |
| 2128 | end |
| 2129 | |
| 2130 | fn res_ro_ws_finish ws edit ulong 67334 meta view ulong 4 to int |
| 2131 | nf = get meta 1 |
| 2132 | no = get meta 2 |
| 2133 | lam_b = get meta 3 |
| 2134 | if or lt nf 1 gt nf 256 |
| 2135 | ret RES_ERR_PARAM |
| 2136 | end |
| 2137 | if or lt no 1 gt no 4 |
| 2138 | ret RES_ERR_PARAM |
| 2139 | end |
| 2140 | if not res_fin64 lam_b |
| 2141 | ret RES_ERR_PARAM |
| 2142 | end |
| 2143 | lam = bitcast f64 lam_b |
| 2144 | if lt lam 0.0f64 |
| 2145 | ret RES_ERR_PARAM |
| 2146 | end |
| 2147 | for i ulong nf bound 256 |
| 2148 | idx = add mul add i 1 RES_RO_DIM add i 1 |
| 2149 | put ws idx bitcast ulong add bitcast f64 get ws idx lam |
| 2150 | end |
| 2151 | ret res_ro_solve ws nf no |
| 2152 | end |
| 2153 | |
| 2154 | note Fit ridge readout into ws (caller copies weights out via res_readout_extract). |
| 2155 | note meta: [0]=n_samples [1]=n_features [2]=n_outputs [3]=bitcast ulong lambda |
| 2156 | fn res_readout_fit_ws x view f64 * y view f64 * meta view ulong 4 ws edit ulong 67334 to int |
| 2157 | rc = res_ro_check_args x y meta |
| 2158 | if ne rc RES_OK |
| 2159 | ret rc |
| 2160 | end |
| 2161 | res_ro_fill_g x ws meta |
| 2162 | res_ro_fill_r x y ws meta |
| 2163 | ret res_ro_solve ws get meta 1 get meta 2 |
| 2164 | end |
| 2165 | |
| 2166 | fn res_readout_extract ws view ulong 67334 meta view ulong 4 out_r edit ulong 1031 to void |
| 2167 | for i ulong RES_RO_WORDS bound 1031 |
| 2168 | put out_r i 0 |
| 2169 | end |
| 2170 | put out_r 0 get meta 1 |
| 2171 | put out_r 1 get meta 2 |
| 2172 | put out_r 2 get meta 3 |
| 2173 | n = mul add get meta 1 1 4 |
| 2174 | for idx ulong n bound 1028 |
| 2175 | if lt rem idx 4 get meta 2 |
| 2176 | put out_r add 3 idx get ws add RES_RWS_R_OFF idx |
| 2177 | end |
| 2178 | end |
| 2179 | end |
| 2180 | |
| 2181 | fn res_ro_apply_one r view ulong 1031 x view f64 * nf ulong o ulong to f64 |
| 2182 | s = bitcast f64 get r add 3 o |
| 2183 | for f ulong nf bound 256 |
| 2184 | idx = mul add 1 f RES_RO_MAX_OUT |
| 2185 | idx = add idx add 3 o |
| 2186 | s = add s mul get x f bitcast f64 get r idx |
| 2187 | end |
| 2188 | ret s |
| 2189 | end |
| 2190 | |
| 2191 | fn res_readout_apply r view ulong 1031 x view f64 * y_out edit f64 * to int |
| 2192 | nf = get r 0 |
| 2193 | no = get r 1 |
| 2194 | if or lt nf 1 gt nf 256 |
| 2195 | ret RES_ERR_PARAM |
| 2196 | end |
| 2197 | if or lt no 1 gt no 4 |
| 2198 | ret RES_ERR_PARAM |
| 2199 | end |
| 2200 | if or lt len x nf lt len y_out no |
| 2201 | ret RES_ERR_PARAM |
| 2202 | end |
| 2203 | for f ulong nf bound 256 |
| 2204 | if not res_fin64 bitcast ulong get x f |
| 2205 | ret RES_ERR_NONFINITE |
| 2206 | end |
| 2207 | end |
| 2208 | for o ulong no bound 4 |
| 2209 | s = res_ro_apply_one r x nf o |
| 2210 | if not res_fin64 bitcast ulong s |
| 2211 | ret RES_ERR_NONFINITE |
| 2212 | end |
| 2213 | put y_out o s |
| 2214 | end |
| 2215 | ret RES_OK |
| 2216 | end |
| 2217 | |
| 2218 | fn res_nrmse_f64 ref view f64 * pred view f64 * out_v edit f64 1 to int |
| 2219 | n = len ref |
| 2220 | if or lt n 2 ne len pred n |
| 2221 | ret RES_ERR_PARAM |
| 2222 | end |
| 2223 | sum = 0.0f64 |
| 2224 | for i ulong n bound 2000000 |
| 2225 | rv = get ref i |
| 2226 | pv = get pred i |
| 2227 | if or not res_fin64 bitcast ulong rv not res_fin64 bitcast ulong pv |
| 2228 | ret RES_ERR_NONFINITE |
| 2229 | end |
| 2230 | sum = add sum rv |
| 2231 | end |
| 2232 | mean = div sum f64 n |
| 2233 | num = 0.0f64 |
| 2234 | den = 0.0f64 |
| 2235 | for i ulong n bound 2000000 |
| 2236 | e = sub get ref i get pred i |
| 2237 | c = sub get ref i mean |
| 2238 | num = add num mul e e |
| 2239 | den = add den mul c c |
| 2240 | end |
| 2241 | if or not res_fin64 bitcast ulong num not res_fin64 bitcast ulong den |
| 2242 | ret RES_ERR_SINGULAR |
| 2243 | end |
| 2244 | if le den 0.0f64 |
| 2245 | ret RES_ERR_SINGULAR |
| 2246 | end |
| 2247 | put out_v 0 fsqrt div num den |
| 2248 | ret RES_OK |
| 2249 | end |