code wiki / (root) / nx_essentials.nx

nx_essentials.nx

buildroot/runtime/nx_essentials.nx

11219 B408 linesdepth 4pulls 4 transitivereach 42 importersview sourcekind library
docsdependenciesstructsconstsfunctions

about

nx_essentials.nx -- the gajillion tiny reused functions. User cardinal 2026-05-15: "I'm sure there's a gajillion basic or tiny functions that could live closer to the bits faster and better, that are reused over and over again that means they really should probably just be primitives." This file is the substrate's batch of MICRO-PRIMITIVES every program in the substrate reuses. Built closer to the bits so callers stop reimplementing them per-file. ~25 functions across 7 families: 1. Comparison / clamp min / max / clamp / signed-clamp 2. Sign / abs / swap abs / sign / swap_via_pointer 3. Saturating arithmetic sat_add / sat_sub / sat_mul 4. Q10 fixed-point helpers q10_mul / q10_div / lerp / pct 5. Integer number theory gcd / lcm / pow_int / mod_floor 6. Bit-level popcount / clz / ctz / log2_floor / log2_ceil / bit_test / set / clear / rotate_left / rotate_right 7. Byte / char essentials memcpy / memset / memcmp / char_is_* / to_lower / to_upper Every function: pure, deterministic, nx_int / *u8. No allocations. No fidelity reporting (these are too small to warrant dual-reading; they ARE the bits-up substrate that the dual-reading primitives use). genealogy_id: warren_2013_hackers_delight + knuth_taocp_v2_seminum + c_standard_iso_9899 + python_stdlib_2025 lineage_id: essentials_micro_primitives_q10

dependencies 3 imports · 5 importers

nx_syscalls.nx nx_tier.nx nx_bits.nx nx_essentials.nx nx_collocation.nx nx_collocation_test.nx nx_essentials_test.nx nx_string_ops.nx nx_string_ops_test.nx

imports: nx_syscalls.nxnx_tier.nxnx_bits.nx

imported by: nx_collocation.nxnx_collocation_test.nxnx_essentials_test.nxnx_string_ops.nxnx_string_ops_test.nx

structs

none

consts

42const NX_ESS_Q: nx_int = 1024
43const NX_ESS_I64_MAX: nx_int = 9223372036854775807
44const NX_ESS_I64_MIN: nx_int = -9223372036854775807

functions

48func nx_min2(a: nx_int, b: nx_int) -> nx_int
called by 1: main
53func nx_max2(a: nx_int, b: nx_int) -> nx_int
called by 1: main
58func nx_min3(a: nx_int, b: nx_int, c: nx_int) -> nx_int
called by 1: main
65func nx_max3(a: nx_int, b: nx_int, c: nx_int) -> nx_int
called by 1: main
73func nx_clamp(v: nx_int, lo: nx_int, hi: nx_int) -> nx_int
called by 1: main
80func nx_clamp_q10(v: nx_int) -> nx_int
called by 1: main
86func nx_clamp_signed_q10(v: nx_int) -> nx_int
called by 1: main
94func nx_abs(v: nx_int) -> nx_int
100func nx_sign(v: nx_int) -> nx_int
called by 2: nx_sat_mulmain
108func nx_swap_via_ptr(a: *nx_int, b: *nx_int) -> nx_int
called by 1: main
121func nx_sat_add(a: nx_int, b: nx_int) -> nx_int
called by 1: main
131func nx_sat_sub(a: nx_int, b: nx_int) -> nx_int
called by 1: main
141func nx_sat_mul(a: nx_int, b: nx_int) -> nx_int
called by 1: main calls 2: nx_absnx_sign
158func nx_q10_mul(a: nx_int, b: nx_int) -> nx_int
called by 1: main
163func nx_q10_div(a: nx_int, b: nx_int) -> nx_int
called by 1: main
169func nx_lerp_q10(a: nx_int, b: nx_int, t_q10: nx_int) -> nx_int
called by 1: main
174func nx_pct_to_q10(pct: nx_int) -> nx_int
called by 1: main
181func nx_q10_to_pct(q10: nx_int) -> nx_int
called by 1: main
188func nx_gcd(a: nx_int, b: nx_int) -> nx_int
called by 2: nx_lcmmain calls 1: nx_abs
200func nx_lcm(a: nx_int, b: nx_int) -> nx_int
called by 1: main calls 2: nx_gcdnx_abs
208func nx_pow_int(base: nx_int, exp: nx_int) -> nx_int
225func nx_mod_floor(a: nx_int, m: nx_int) -> nx_int
called by 1: main
244func nx_popcount(x: nx_int) -> nx_int
called by 1: main calls 1: nx_bits_popcount64
250func nx_clz(x: nx_int) -> nx_int
calls 1: nx_bits_clz64
256func nx_ctz(x: nx_int) -> nx_int
calls 1: nx_bits_ctz64
261func nx_log2_floor(n: nx_int) -> nx_int
273func nx_log2_ceil(n: nx_int) -> nx_int
called by 1: main calls 2: nx_log2_floornx_pow_int
283func nx_bit_test(x: nx_int, p: nx_int) -> nx_int
called by 1: main calls 1: nx_pow_int
292func nx_bit_set(x: nx_int, p: nx_int) -> nx_int
called by 1: main calls 1: nx_pow_int
300func nx_bit_clear(x: nx_int, p: nx_int) -> nx_int
called by 1: main calls 1: nx_pow_int
310func nx_memcpy(dst: *u8, src: *u8, n: nx_int) -> nx_int
called by 1: main
320func nx_memset(buf: *u8, v: u8, n: nx_int) -> nx_int
called by 1: main
330func nx_memcmp(a: *u8, b: *u8, n: nx_int) -> nx_int
called by 1: main
343func nx_char_is_digit(c: u8) -> nx_int
350func nx_char_is_alpha(c: u8) -> nx_int
360func nx_char_is_alnum(c: u8) -> nx_int
366func nx_char_is_whitespace(c: u8) -> nx_int
377func nx_to_lower(c: u8) -> u8
called by 1: main
384func nx_to_upper(c: u8) -> u8
called by 1: main
392func nx_hex_to_nibble(c: u8) -> nx_int
called by 1: main
405func nx_nibble_to_hex(n: nx_int) -> u8
called by 1: main