omega-helix
This project has not described itself yet.
Brought by drakestapleton
omega-helix / native / mio.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note omegallm lio.om (aien-train#52): the Linux platform unit, the only unit that uses host words. |
| 3 | note It is concatenated last for targets aarch64 and x86linux; eio.om replaces it for x86efi. Both |
| 4 | note define the same functions: oargc, oargr, oflush, ocwopen, ocwput, ocwend, ofsz, ogradw, ocsinit, |
| 5 | note ocsg, ocsp, odread, owflush, owopen, owclose, ooffrd, oprd, obmsave and main. Every other unit is |
| 6 | note free of host words. |
| 7 | global owfd int 2 |
| 8 | global opth uchar 2100 |
| 9 | global opk uchar 4200 |
| 10 | global ocfd int 1 |
| 11 | global opp uchar 8 |
| 12 | global ost uchar 8 |
| 13 | global owrf int 64 |
| 14 | global owpd int 64 |
| 15 | global ommap_sz_g ulong 1 |
| 16 | |
| 17 | note the argument count (argv[0] included) |
| 18 | fn oargc to ulong |
| 19 | ret ulong argcnt |
| 20 | end |
| 21 | |
| 22 | note argv[i] into oarg (at most 4096 bytes); returns the argread result (negative: no such argument) |
| 23 | fn oargr i ulong to long |
| 24 | ret argread i oarg 0 4096 |
| 25 | end |
| 26 | |
| 27 | fn main to int |
| 28 | if lt oargc 2 |
| 29 | exit 2 |
| 30 | end |
| 31 | ret ocmd |
| 32 | end |
| 33 | |
| 34 | note onsnow: monotonic nanoseconds (kvcmp's step timing in cli.om) |
| 35 | fn onsnow to ulong |
| 36 | ret ulong monons |
| 37 | end |
| 38 | |
| 39 | fn oflush to void |
| 40 | n = get oon 0 |
| 41 | p = ulong 0 |
| 42 | while lt p n bound 4097 |
| 43 | w = write 1 oout p sub n p |
| 44 | if le w 0 |
| 45 | exit 70 |
| 46 | end |
| 47 | p = add p ulong w |
| 48 | end |
| 49 | put oon 0 0 |
| 50 | end |
| 51 | |
| 52 | note v1 checkpoint stream (ckpt.om): open PATH.tmp for the path in argv[ai] (created, emptied) |
| 53 | fn ocwopen ai ulong to void |
| 54 | pl = oargp ai |
| 55 | if gt pl 2000 |
| 56 | exit 2 |
| 57 | end |
| 58 | for i ulong pl |
| 59 | put opth i get oarg i |
| 60 | end |
| 61 | put opth pl 46 |
| 62 | put opth add pl 1 116 |
| 63 | put opth add pl 2 109 |
| 64 | put opth add pl 3 112 |
| 65 | fd = int fopen opth 0 add pl 4 577 420 |
| 66 | if lt fd 0 |
| 67 | exit 70 |
| 68 | end |
| 69 | put ocfd 0 fd |
| 70 | end |
| 71 | |
| 72 | note write ock[0 .. n) at byte position pos of the open PATH.tmp |
| 73 | fn ocwput pos ulong n ulong to void |
| 74 | fd = get ocfd 0 |
| 75 | p = ulong 0 |
| 76 | while lt p n bound 999999 |
| 77 | w = pwrite fd ock p sub n p long add pos p |
| 78 | if le w 0 |
| 79 | exit 70 |
| 80 | end |
| 81 | p = add p ulong w |
| 82 | end |
| 83 | end |
| 84 | |
| 85 | note fsync and close PATH.tmp, then rename it onto the path in argv[ai] |
| 86 | fn ocwend ai ulong to void |
| 87 | fd = get ocfd 0 |
| 88 | if ne fsync fd 0 |
| 89 | exit 70 |
| 90 | end |
| 91 | if ne fclose fd 0 |
| 92 | exit 70 |
| 93 | end |
| 94 | ocrename oargp ai |
| 95 | end |
| 96 | |
| 97 | note rename opth[0 .. pl+4) to opth[0 .. pl) via a packed two-path list |
| 98 | fn ocrename pl ulong to void |
| 99 | put opk 0 2 |
| 100 | put opk 1 0 |
| 101 | put opk 2 uchar and add pl 4 255 |
| 102 | put opk 3 uchar shr add pl 4 8 |
| 103 | for i ulong add pl 4 |
| 104 | put opk add 4 i get opth i |
| 105 | end |
| 106 | q = add 8 pl |
| 107 | put opk q uchar and pl 255 |
| 108 | put opk add q 1 uchar shr pl 8 |
| 109 | for j ulong pl |
| 110 | put opk add add q 2 j get opth j |
| 111 | end |
| 112 | r = renames opk 0 add add q 2 pl 0 |
| 113 | if ne r 0 |
| 114 | exit 70 |
| 115 | end |
| 116 | end |
| 117 | |
| 118 | note size in bytes of the file named by argv[ai] (seek to its end) |
| 119 | fn ofsz ai ulong to ulong |
| 120 | n = oargp ai |
| 121 | fd = int fopen oarg 0 n 0 0 |
| 122 | if lt fd 0 |
| 123 | exit 70 |
| 124 | end |
| 125 | sz = fseek fd 0 2 |
| 126 | if lt sz 0 |
| 127 | exit 70 |
| 128 | end |
| 129 | if ne fclose fd 0 |
| 130 | exit 70 |
| 131 | end |
| 132 | ret ulong sz |
| 133 | end |
| 134 | |
| 135 | note cold store (CONTRACT-V3 section 2): GRD, Adam moments, and solver/specialist scratch up to OCSN |
| 136 | note f64 values in a dynamic om2 pool grown on demand via enlarge (zero-filled on growth; failed |
| 137 | note enlarge is an I/O refusal, exit 70; indices >= OCSN trap BOUNDS, exit 103). |
| 138 | const OCSN ulong 1500000000 |
| 139 | const OCSMIN ulong 65536 |
| 140 | global ocs f64 * |
| 141 | global ocsf ulong 1 |
| 142 | |
| 143 | fn ocs_ensure need ulong to bool |
| 144 | if gt need OCSN |
| 145 | ret false |
| 146 | end |
| 147 | cur = get ocsf 0 |
| 148 | if le need cur |
| 149 | ret true |
| 150 | end |
| 151 | cap = cur |
| 152 | if lt cap OCSMIN |
| 153 | cap = OCSMIN |
| 154 | end |
| 155 | while lt cap need bound 64 |
| 156 | cap = mul cap 2 |
| 157 | end |
| 158 | if gt cap OCSN |
| 159 | cap = OCSN |
| 160 | end |
| 161 | r = enlarge ocs cap |
| 162 | if ne r 0 |
| 163 | ret false |
| 164 | end |
| 165 | put ocsf 0 cap |
| 166 | ret true |
| 167 | end |
| 168 | |
| 169 | fn ocsinit to ulong |
| 170 | need = mul 3 get og 8 |
| 171 | if gt need OCSN |
| 172 | ret OCSN |
| 173 | end |
| 174 | if lt need OCSMIN |
| 175 | need = OCSMIN |
| 176 | end |
| 177 | if not ocs_ensure need |
| 178 | exit 70 |
| 179 | end |
| 180 | ret OCSN |
| 181 | end |
| 182 | |
| 183 | fn ocsgrow need ulong to bool |
| 184 | ret ocs_ensure need |
| 185 | end |
| 186 | |
| 187 | fn ocsg i ulong to f64 |
| 188 | if lt i get ocsf 0 |
| 189 | ret get ocs i |
| 190 | end |
| 191 | if lt i OCSN |
| 192 | ret 0.0f64 |
| 193 | end |
| 194 | ret get ocs i |
| 195 | end |
| 196 | |
| 197 | fn ocsp i ulong v f64 to void |
| 198 | if ge i get ocsf 0 |
| 199 | if ge i OCSN |
| 200 | put ocs i v |
| 201 | ret |
| 202 | end |
| 203 | if not ocs_ensure add i 1 |
| 204 | exit 70 |
| 205 | end |
| 206 | end |
| 207 | put ocs i v |
| 208 | end |
| 209 | |
| 210 | fn ogradw to f64 |
| 211 | k = get og 45 |
| 212 | if gt k 1 |
| 213 | exit 70 |
| 214 | end |
| 215 | ret ograd |
| 216 | end |
| 217 | |
| 218 | fn odread i ulong to ulong |
| 219 | n = oargp i |
| 220 | fd = int fopen oarg 0 n 0 0 |
| 221 | if lt fd 0 |
| 222 | exit 70 |
| 223 | end |
| 224 | p = ulong 0 |
| 225 | dn = false |
| 226 | for o ulong 64 |
| 227 | q = ulong 0 |
| 228 | while and not dn lt q 999999 bound 999999 |
| 229 | q = add q 1 |
| 230 | r = long 0 |
| 231 | if lt p ODHALF |
| 232 | r = read fd od0 p sub ODHALF p |
| 233 | else |
| 234 | if lt p ODCAP |
| 235 | r = read fd od1 sub p ODHALF sub ODCAP p |
| 236 | else |
| 237 | r = read fd ochk 0 1 |
| 238 | end |
| 239 | end |
| 240 | if lt r 0 |
| 241 | exit 70 |
| 242 | end |
| 243 | if eq r 0 |
| 244 | dn = true |
| 245 | else |
| 246 | p = add p ulong r |
| 247 | if gt p ODCAP |
| 248 | exit 21 |
| 249 | end |
| 250 | end |
| 251 | end |
| 252 | if dn |
| 253 | break |
| 254 | end |
| 255 | end |
| 256 | if not dn |
| 257 | exit 70 |
| 258 | end |
| 259 | c = fclose fd |
| 260 | put odn 0 p |
| 261 | ret p |
| 262 | end |
| 263 | |
| 264 | note prep output: two buffered writers (k = 0 TRAIN, k = 1 HELD). |
| 265 | fn owflush k ulong to void |
| 266 | n = get owbn k |
| 267 | fd = get owfd k |
| 268 | p = ulong 0 |
| 269 | while lt p n bound 65537 |
| 270 | w = long 0 |
| 271 | if eq k 0 |
| 272 | w = write fd owb0 p sub n p |
| 273 | else |
| 274 | w = write fd owb1 p sub n p |
| 275 | end |
| 276 | if le w 0 |
| 277 | exit 70 |
| 278 | end |
| 279 | p = add p ulong w |
| 280 | end |
| 281 | put owbn k 0 |
| 282 | end |
| 283 | |
| 284 | fn owopen k ulong i ulong to void |
| 285 | n = oargp i |
| 286 | fd = int fopen oarg 0 n 577 420 |
| 287 | if lt fd 0 |
| 288 | exit 70 |
| 289 | end |
| 290 | put owfd k fd |
| 291 | put owbn k 0 |
| 292 | end |
| 293 | |
| 294 | fn owclose k ulong to void |
| 295 | owflush k |
| 296 | fd = get owfd k |
| 297 | if ne fsync fd 0 |
| 298 | exit 70 |
| 299 | end |
| 300 | if ne fclose fd 0 |
| 301 | exit 70 |
| 302 | end |
| 303 | end |
| 304 | |
| 305 | note read argv[ai] (B little-endian u64 offsets) into ochk; exit 21 unless it holds exactly 8 B bytes |
| 306 | fn ooffrd ai ulong to void |
| 307 | n = oargp ai |
| 308 | fd = int fopen oarg 0 n 0 0 |
| 309 | if lt fd 0 |
| 310 | exit 70 |
| 311 | end |
| 312 | p = ulong 0 |
| 313 | dn = false |
| 314 | while not dn bound 70000 |
| 315 | r = read fd ochk p sub 65536 p |
| 316 | if lt r 0 |
| 317 | exit 70 |
| 318 | end |
| 319 | if eq r 0 |
| 320 | dn = true |
| 321 | else |
| 322 | p = add p ulong r |
| 323 | if ge p 65536 |
| 324 | exit 21 |
| 325 | end |
| 326 | end |
| 327 | end |
| 328 | c = fclose fd |
| 329 | if ne p mul 8 get og 6 |
| 330 | exit 21 |
| 331 | end |
| 332 | end |
| 333 | |
| 334 | |
| 335 | note v1 positioned read (CONTRACT-V1 section 3): read up to n bytes of the file named by argv[ai], |
| 336 | note from byte pos, into opw[off .. off + n). The file is opened and closed on every call. Returns |
| 337 | note the bytes read: n, fewer only when the file ends first, 0 at or after its end. Exit 70 on an |
| 338 | note I/O error, 2 on a bad argument; off + n over len opw traps 3. Each pread pass adds at least one |
| 339 | note byte or ends, so n + 1 <= 2097153 passes suffice: 3 outer passes of at most 999999 inner ones. |
| 340 | fn oprd ai ulong pos ulong off ulong n ulong to ulong |
| 341 | pl = oargp ai |
| 342 | fd = int fopen oarg 0 pl 0 0 |
| 343 | if lt fd 0 |
| 344 | exit 70 |
| 345 | end |
| 346 | p = ulong 0 |
| 347 | dn = false |
| 348 | for o ulong 3 |
| 349 | while and not dn lt p n bound 999999 |
| 350 | r = pread fd opw add off p sub n p long add pos p |
| 351 | if lt r 0 |
| 352 | exit 70 |
| 353 | end |
| 354 | if eq r 0 |
| 355 | dn = true |
| 356 | else |
| 357 | p = add p ulong r |
| 358 | end |
| 359 | end |
| 360 | end |
| 361 | if ne fclose fd 0 |
| 362 | exit 70 |
| 363 | end |
| 364 | ret p |
| 365 | end |
| 366 | |
| 367 | note v1 (CONTRACT-V1 section 7): write obm[0 .. n) (bpe.om) to the path in argv[ai] as ocwrite does: |
| 368 | note PATH.tmp, fsync, rename |
| 369 | fn obmsave ai ulong n ulong to void |
| 370 | pl = oargp ai |
| 371 | if gt pl 2000 |
| 372 | exit 2 |
| 373 | end |
| 374 | for i ulong pl |
| 375 | put opth i get oarg i |
| 376 | end |
| 377 | put opth pl 46 |
| 378 | put opth add pl 1 116 |
| 379 | put opth add pl 2 109 |
| 380 | put opth add pl 3 112 |
| 381 | fd = int fopen opth 0 add pl 4 577 420 |
| 382 | if lt fd 0 |
| 383 | exit 70 |
| 384 | end |
| 385 | p = ulong 0 |
| 386 | while lt p n bound 999999 |
| 387 | w = write fd obm p sub n p |
| 388 | if le w 0 |
| 389 | exit 70 |
| 390 | end |
| 391 | p = add p ulong w |
| 392 | end |
| 393 | if ne fsync fd 0 |
| 394 | exit 70 |
| 395 | end |
| 396 | if ne fclose fd 0 |
| 397 | exit 70 |
| 398 | end |
| 399 | ocrename pl |
| 400 | end |
| 401 | |
| 402 | fn ocwput_opw pos ulong n ulong to void |
| 403 | fd = get ocfd 0 |
| 404 | p = ulong 0 |
| 405 | while lt p n bound 999999 |
| 406 | w = pwrite fd opw p sub n p long add pos p |
| 407 | if le w 0 |
| 408 | exit 70 |
| 409 | end |
| 410 | p = add p ulong w |
| 411 | end |
| 412 | end |
| 413 | |
| 414 | fn ommap_open ai ulong to long |
| 415 | put ommap_sz_g 0 ofsz ai |
| 416 | ret neg long 1 |
| 417 | end |
| 418 | |
| 419 | fn ommap_sz to ulong |
| 420 | ret get ommap_sz_g 0 |
| 421 | end |
| 422 | |
| 423 | fn ommap_read h long off ulong n ulong to void |
| 424 | exit 70 |
| 425 | end |
| 426 | |
| 427 | fn ommap_drop h long to void |
| 428 | ret |
| 429 | end |
| 430 | |
| 431 | fn ocswrite off ulong n ulong to bool |
| 432 | ret false |
| 433 | end |
| 434 | |
| 435 | fn ocsread off ulong n ulong to bool |
| 436 | ret false |
| 437 | end |
| 438 | |
| 439 | fn ommap_fnv64 h long off ulong nw ulong init_h ulong to ulong |
| 440 | ret init_h |
| 441 | end |