code wiki / (root) / nx_bit.nx

nx_bit.nx

buildroot/runtime/nx_bit.nx

7906 B200 linesdepth 4pulls 4 transitivereach 66 importersview sourcekind tooltopic bit
docsdependenciesstructsconstsfunctions

about

nx_bit.nx -- low-level bit primitives. FOUNDATION LAYER. Every crypto module, every bitmap, every CRC-style checksum, every compressed-encoding parser eventually wants: popcount, count-leading-zeros, count-trailing-zeros, byte-swap, rotate-left/right. Today every module re-implements these via shift+mask loops. Centralising matches: * R1 cache-conscious: one canonical implementation per primitive means one inlining decision the optimiser sees everywhere * R2 attestable: SHA-256 + ChaCha20 + others share the same few primitives -- audit once instead of N copies * Future: lower to RV64 Zbb (bit-manipulation extension) when the target supports it; ~2-5x speedup over scalar fallback Bit primitives shipped: nx_popcount32 / nx_popcount64 Hamming weight nx_clz32 / nx_clz64 leading zero count nx_ctz32 / nx_ctz64 trailing zero count nx_bswap32 / nx_bswap64 byte-reverse nx_rotl32 / nx_rotl64 rotate left nx_rotr32 / nx_rotr64 rotate right nx_log2_floor log2 floor (= 63 - clz) nx_is_pow2 power-of-two check nx_align_up round up to align (must be POT) Algorithm references: * popcount: Hamming weight via parallel-prefix (folklore; Knuth TAoCP Vol 4A 7.1.3). Constant 12 ops for 64-bit. * clz / ctz: branch-free de Bruijn sequence multiply (Leiserson et al, "Using De Bruijn sequences to index a 1 in a computer word", 1998 unpublished). Constant 8-10 ops. * bswap: 4-step shuffle (Schneier, Practical Cryptography 2003). Forward path: when nxc2 grows the Zbb intrinsic, lower these to `cpop`, `clz`, `ctz`, `rev8`, `rori`, `rorw` instructions. Estimated 3-5x speedup on hot crypto / hashing paths.

dependencies 2 imports · 4 importers

syscalls.nx nx_bits.nx nx_bit.nx nx_bench_popcount.nx nx_bit_kat.nx nx_sketch_hll.nx sketch_hll.nx

imports: syscalls.nxnx_bits.nx

imported by: nx_bench_popcount.nxnx_bit_kat.nxnx_sketch_hll.nxsketch_hll.nx

call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown

main nx_popcount32 nx_bits_popcount32 nx_popcount64 nx_bits_popcount64 nx_clz32 nx_bits_clz32 nx_clz64 nx_bits_clz64 nx_ctz32 nx_bits_ctz32 nx_ctz64 nx_bits_ctz64 nx_bswap32 nx_bits_bswap32 nx_rotl32 nx_bits_rotl32 nx_rotr32 nx_bits_rotr32 nx_log2_floor nx_clz64 ↻ nx_is_pow2 nx_align_up

structs

none

consts

none

functions

55func nx_popcount32(x: i64) -> i64
called by 2: mainmain calls 1: nx_bits_popcount32
59func nx_popcount64(x: i64) -> i64
called by 3: mainmainmain calls 1: nx_bits_popcount64
68func nx_clz32(x: i64) -> i64
72func nx_clz64(x: i64) -> i64
called by 2: nx_log2_floormain calls 1: nx_bits_clz64
79func nx_ctz32(x: i64) -> i64
called by 2: mainmain calls 1: nx_bits_ctz32
83func nx_ctz64(x: i64) -> i64
called by 1: main calls 1: nx_bits_ctz64
91func nx_bswap32(x: i64) -> i64
called by 2: mainmain calls 1: nx_bits_bswap32
95func nx_bswap64(x: i64) -> i64
calls 1: nx_bits_bswap64
106func nx_rotl32(x: i64, n: i64) -> i64
called by 2: mainmain calls 1: nx_bits_rotl32
110func nx_rotr32(x: i64, n: i64) -> i64
called by 2: mainmain calls 1: nx_bits_rotr32
114func nx_rotl64(x: i64, n: i64) -> i64
calls 1: nx_bits_rotl64
118func nx_rotr64(x: i64, n: i64) -> i64
calls 1: nx_bits_rotr64
125func nx_log2_floor(x: i64) -> i64
called by 1: main calls 1: nx_clz64
131func nx_is_pow2(x: i64) -> i64
called by 1: main
139func nx_align_up(x: i64, align: i64) -> i64
called by 1: main
146func main() -> i64