code wiki / (root) / nx_npy_lib.nx

nx_npy_lib.nx

buildroot/runtime/nx_npy_lib.nx

11359 B282 linesdepth 2pulls 2 transitivereach 1 importersview sourcekind library
docsdependenciesstructsconstsfunctions

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

nx_syscalls.nx nx_npy_lib.nx nx_npy_gate.nx

imports: nx_syscalls.nx

imported by: nx_npy_gate.nx

structs

none

consts

29const NPY_MAGIC0: i64 = 147 // 0x93, the non-ASCII lead byte NEP 1 uses so a text tool cannot mistake it
30const NPY_MAGIC_LEN: i64 = 6 // 0x93 N U M P Y
31const NPY_VER_OFF: i64 = 6
32const NPY_HLEN_OFF: i64 = 8
33const NPY_V1_PREAMBLE: i64 = 10 // magic(6) + version(2) + u16 len(2)
34const NPY_V2_PREAMBLE: i64 = 12 // magic(6) + version(2) + u32 len(4)
35const NPY_CH_QUOTE: i64 = 39 // '\'' emitted BY NAME -- the header's own quoting, never an escape
36const NPY_CH_LPAREN: i64 = 40
37const NPY_CH_RPAREN: i64 = 41
38const NPY_D0: i64 = 48
39const NPY_D9: i64 = 57
40const NPY_MAX_DIMS: i64 = 8
42const NPY_OK: i64 = 0
43const NPY_ERR_MAGIC: i64 = 0 - 1
44const NPY_ERR_TRUNC: i64 = 0 - 2
45const NPY_ERR_NO_DESCR: i64 = 0 - 3
46const NPY_ERR_DTYPE: i64 = 0 - 4
47const NPY_ERR_FORTRAN: i64 = 0 - 5
48const NPY_ERR_SHAPE: i64 = 0 - 6
49const NPY_ERR_SIZE: i64 = 0 - 7
52const NPY_KIND_F: i64 = 1 // 'f' float
53const NPY_KIND_I: i64 = 2 // 'i' signed int
54const NPY_KIND_U: i64 = 3 // 'u' unsigned int
55const NPY_KIND_B: i64 = 4 // 'b' bool

functions

57func npy_streq_at(buf: *u8, s: i64, e: i64, lit: *u8) -> i64
called by 2: npy_findnpy_is_npy
68func npy_find(buf: *u8, s: i64, e: i64, lit: *u8) -> i64
81func npy_quote_after(buf: *u8, s: i64, e: i64, nth: i64) -> i64
called by 1: npy_dtype
94func npy_u16(buf: *u8, o: i64) -> i64
called by 1: npy_hdr_len
98func npy_u32(buf: *u8, o: i64) -> i64
called by 1: npy_hdr_len
102func npy_is_npy(buf: *u8, n: i64) -> i64
called by 3: mainnpy_majornpy_open calls 1: npy_streq_at
109func npy_major(buf: *u8, n: i64) -> i64
115func npy_hdr_off(buf: *u8, n: i64) -> i64
122func npy_hdr_len(buf: *u8, n: i64) -> i64
134func npy_data_off(buf: *u8, n: i64) -> i64
called by 1: npy_open calls 2: npy_hdr_offnpy_hdr_len
144func npy_fortran_order(buf: *u8, n: i64) -> i64
166func npy_dtype(buf: *u8, n: i64, out_itemsize: *i64) -> i64
213func npy_shape(buf: *u8, n: i64, dims: *i64) -> i64
248func npy_elem_count(dims: *i64, nd: i64) -> i64
called by 1: npy_open
259func npy_open(buf: *u8, n: i64, dims: *i64, out: *i64) -> i64