nx_essentials.nx
buildroot/runtime/nx_essentials.nx
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
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
| 42 | const NX_ESS_Q: nx_int = 1024 |
| 43 | const NX_ESS_I64_MAX: nx_int = 9223372036854775807 |
| 44 | const NX_ESS_I64_MIN: nx_int = -9223372036854775807 |
functions
| 48 | func nx_min2(a: nx_int, b: nx_int) -> nx_int called by 1: main |
| 53 | func nx_max2(a: nx_int, b: nx_int) -> nx_int called by 1: main |
| 58 | func nx_min3(a: nx_int, b: nx_int, c: nx_int) -> nx_int called by 1: main |
| 65 | func nx_max3(a: nx_int, b: nx_int, c: nx_int) -> nx_int called by 1: main |
| 73 | func nx_clamp(v: nx_int, lo: nx_int, hi: nx_int) -> nx_int called by 1: main |
| 80 | func nx_clamp_q10(v: nx_int) -> nx_int called by 1: main |
| 86 | func nx_clamp_signed_q10(v: nx_int) -> nx_int called by 1: main |
| 94 | func nx_abs(v: nx_int) -> nx_int |
| 100 | func nx_sign(v: nx_int) -> nx_int |
| 108 | func nx_swap_via_ptr(a: *nx_int, b: *nx_int) -> nx_int called by 1: main |
| 121 | func nx_sat_add(a: nx_int, b: nx_int) -> nx_int called by 1: main |
| 131 | func nx_sat_sub(a: nx_int, b: nx_int) -> nx_int called by 1: main |
| 141 | func nx_sat_mul(a: nx_int, b: nx_int) -> nx_int |
| 158 | func nx_q10_mul(a: nx_int, b: nx_int) -> nx_int called by 1: main |
| 163 | func nx_q10_div(a: nx_int, b: nx_int) -> nx_int called by 1: main |
| 169 | func nx_lerp_q10(a: nx_int, b: nx_int, t_q10: nx_int) -> nx_int called by 1: main |
| 174 | func nx_pct_to_q10(pct: nx_int) -> nx_int called by 1: main |
| 181 | func nx_q10_to_pct(q10: nx_int) -> nx_int called by 1: main |
| 188 | func nx_gcd(a: nx_int, b: nx_int) -> nx_int |
| 200 | func nx_lcm(a: nx_int, b: nx_int) -> nx_int |
| 208 | func nx_pow_int(base: nx_int, exp: nx_int) -> nx_int |
| 225 | func nx_mod_floor(a: nx_int, m: nx_int) -> nx_int called by 1: main |
| 244 | func nx_popcount(x: nx_int) -> nx_int |
| 250 | func nx_clz(x: nx_int) -> nx_int calls 1: nx_bits_clz64 |
| 256 | func nx_ctz(x: nx_int) -> nx_int calls 1: nx_bits_ctz64 |
| 261 | func nx_log2_floor(n: nx_int) -> nx_int |
| 273 | func nx_log2_ceil(n: nx_int) -> nx_int |
| 283 | func nx_bit_test(x: nx_int, p: nx_int) -> nx_int |
| 292 | func nx_bit_set(x: nx_int, p: nx_int) -> nx_int |
| 300 | func nx_bit_clear(x: nx_int, p: nx_int) -> nx_int |
| 310 | func nx_memcpy(dst: *u8, src: *u8, n: nx_int) -> nx_int called by 1: main |
| 320 | func nx_memset(buf: *u8, v: u8, n: nx_int) -> nx_int called by 1: main |
| 330 | func nx_memcmp(a: *u8, b: *u8, n: nx_int) -> nx_int called by 1: main |
| 343 | func nx_char_is_digit(c: u8) -> nx_int |
| 350 | func nx_char_is_alpha(c: u8) -> nx_int |
| 360 | func nx_char_is_alnum(c: u8) -> nx_int |
| 366 | func nx_char_is_whitespace(c: u8) -> nx_int |
| 377 | func nx_to_lower(c: u8) -> u8 called by 1: main |
| 384 | func nx_to_upper(c: u8) -> u8 called by 1: main |
| 392 | func nx_hex_to_nibble(c: u8) -> nx_int called by 1: main |
| 405 | func nx_nibble_to_hex(n: nx_int) -> u8 called by 1: main |