nx_bit.nx
buildroot/runtime/nx_bit.nx
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
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
structs
| none |
consts
| none |
functions
| 55 | func nx_popcount32(x: i64) -> i64 |
| 59 | func nx_popcount64(x: i64) -> i64 |
| 68 | func nx_clz32(x: i64) -> i64 |
| 72 | func nx_clz64(x: i64) -> i64 |
| 79 | func nx_ctz32(x: i64) -> i64 |
| 83 | func nx_ctz64(x: i64) -> i64 |
| 91 | func nx_bswap32(x: i64) -> i64 |
| 95 | func nx_bswap64(x: i64) -> i64 calls 1: nx_bits_bswap64 |
| 106 | func nx_rotl32(x: i64, n: i64) -> i64 |
| 110 | func nx_rotr32(x: i64, n: i64) -> i64 |
| 114 | func nx_rotl64(x: i64, n: i64) -> i64 calls 1: nx_bits_rotl64 |
| 118 | func nx_rotr64(x: i64, n: i64) -> i64 calls 1: nx_bits_rotr64 |
| 125 | func nx_log2_floor(x: i64) -> i64 |
| 131 | func nx_is_pow2(x: i64) -> i64 called by 1: main |
| 139 | func nx_align_up(x: i64, align: i64) -> i64 called by 1: main |
| 146 | func main() -> i64 |