nx_npy_lib.nx
buildroot/runtime/nx_npy_lib.nx
about
nx_npy_lib.nx -- THE .npy MEMBER PARSER. The first byte of the GNM / NishiGen lane (procgen PG38).
WHY THIS AND NOTHING MORE. GNM Head ships its model as .npz, and .npz is a ZIP of .npy members. The ZIP
half is ALREADY SHIPPED and is NOT rebuilt here: nx_zip_header.nx carries zip_find_eocd, zip_parse_eocd,
zip_parse_lfh and zip_method_known, nx_zip.nx carries the central-directory field accessors and CRC32,
and nx_safe_archive_ingest already composes them under a never-poison envelope. Measured 2026-09-03
before writing a line: the estate reads ZIP well enough that nx_varfacts measures 91 VirtaMate .var
archives (which ARE zips) straight into the refcorpus journal. What was genuinely ABSENT -- confirmed,
not assumed, after the two "NUMPY" hits in the tree turned out to be a benchmark constant
NX_INCUMBENT_NUMPY=600 -- is the .npy header parser. So this file is the ONE missing layer, not a
second archive reader.
FORMAT (numpy NEP 1): magic 0x93 'N' 'U' 'M' 'P' 'Y', major, minor, then a header LENGTH
(u16 little-endian for v1, u32 for v2 and v3), then that many bytes of a Python dict literal:
{'descr': '<f4', 'fortran_order': False, 'shape': (5023, 3), }
then the raw array bytes, C-contiguous unless fortran_order says otherwise.
FAIL CLOSED ON THE THREE THINGS THAT CORRUPT SILENTLY, because each produces PLAUSIBLE NUMBERS:
1. fortran_order True -- the same bytes transposed. A reader that ignores it returns a matrix that is
the WRONG SHAPE-ORDER while every length check still passes, so a vertex basis would come back
scrambled and nothing would error. REFUSED by name; supporting it later is a rung, not a default.
2. an unknown dtype -- guessing an itemsize makes every downstream offset wrong by a factor.
3. a declared element count that does not match the bytes actually present. The partition
data_off + count*itemsize == total MUST hold; it is arithmetic, so it is its own oracle.
license_tier: ORIGINAL
dependencies 1 imports · 1 importers
imports: nx_syscalls.nx
imported by: nx_npy_gate.nx
structs
| none |
consts
| 29 | const NPY_MAGIC0: i64 = 147 // 0x93, the non-ASCII lead byte NEP 1 uses so a text tool cannot mistake it |
| 30 | const NPY_MAGIC_LEN: i64 = 6 // 0x93 N U M P Y |
| 31 | const NPY_VER_OFF: i64 = 6 |
| 32 | const NPY_HLEN_OFF: i64 = 8 |
| 33 | const NPY_V1_PREAMBLE: i64 = 10 // magic(6) + version(2) + u16 len(2) |
| 34 | const NPY_V2_PREAMBLE: i64 = 12 // magic(6) + version(2) + u32 len(4) |
| 35 | const NPY_CH_QUOTE: i64 = 39 // '\'' emitted BY NAME -- the header's own quoting, never an escape |
| 36 | const NPY_CH_LPAREN: i64 = 40 |
| 37 | const NPY_CH_RPAREN: i64 = 41 |
| 38 | const NPY_D0: i64 = 48 |
| 39 | const NPY_D9: i64 = 57 |
| 40 | const NPY_MAX_DIMS: i64 = 8 |
| 42 | const NPY_OK: i64 = 0 |
| 43 | const NPY_ERR_MAGIC: i64 = 0 - 1 |
| 44 | const NPY_ERR_TRUNC: i64 = 0 - 2 |
| 45 | const NPY_ERR_NO_DESCR: i64 = 0 - 3 |
| 46 | const NPY_ERR_DTYPE: i64 = 0 - 4 |
| 47 | const NPY_ERR_FORTRAN: i64 = 0 - 5 |
| 48 | const NPY_ERR_SHAPE: i64 = 0 - 6 |
| 49 | const NPY_ERR_SIZE: i64 = 0 - 7 |
| 52 | const NPY_KIND_F: i64 = 1 // 'f' float |
| 53 | const NPY_KIND_I: i64 = 2 // 'i' signed int |
| 54 | const NPY_KIND_U: i64 = 3 // 'u' unsigned int |
| 55 | const NPY_KIND_B: i64 = 4 // 'b' bool |
functions
| 57 | func npy_streq_at(buf: *u8, s: i64, e: i64, lit: *u8) -> i64 |
| 68 | func npy_find(buf: *u8, s: i64, e: i64, lit: *u8) -> i64 |
| 81 | func npy_quote_after(buf: *u8, s: i64, e: i64, nth: i64) -> i64 called by 1: npy_dtype |
| 94 | func npy_u16(buf: *u8, o: i64) -> i64 called by 1: npy_hdr_len |
| 98 | func npy_u32(buf: *u8, o: i64) -> i64 called by 1: npy_hdr_len |
| 102 | func npy_is_npy(buf: *u8, n: i64) -> i64 |
| 109 | func npy_major(buf: *u8, n: i64) -> i64 |
| 115 | func npy_hdr_off(buf: *u8, n: i64) -> i64 |
| 122 | func npy_hdr_len(buf: *u8, n: i64) -> i64 |
| 134 | func npy_data_off(buf: *u8, n: i64) -> i64 |
| 144 | func npy_fortran_order(buf: *u8, n: i64) -> i64 |
| 166 | func npy_dtype(buf: *u8, n: i64, out_itemsize: *i64) -> i64 |
| 213 | func npy_shape(buf: *u8, n: i64, dims: *i64) -> i64 |
| 248 | func npy_elem_count(dims: *i64, nd: i64) -> i64 called by 1: npy_open |
| 259 | func npy_open(buf: *u8, n: i64, dims: *i64, out: *i64) -> i64 |