← All Engineering Notes / SCARLET Quadrilogy, Paper III
[kernels] Paper III · SCARLET Quadrilogy · 29 November 2026POPCNT: State of the Art in the New Art of State
Bit-exact zero-multiply ternary matrix multiplication via dual-bitfield population count on commodity x86_64 and ARM64 silicon. A ternary weight is a signed bitmask; treating it as a tiny multiplier is the mistake this kernel removes.
1. Abstract
The standard CPU workaround for 1.58-bit ternary inference packs weights into 2-bit codes, unpacks them at runtime through lookup tables and feeds them into integer multiply-accumulate instructions (vpmaddubsw). That pipeline treats a ternary weight w ∈ {−1, 0, +1} as a tiny multiplier instead of what it mathematically is: a signed bitmask.
This paper specifies TRT2, the pure-Rust (rustc 1.99.0), zero-C++ zero-multiply ternary kernel at the base of the SCARLET stack. It gives the Bit-Exact Zero-Multiply POPCNT Origin Identity, details the 64-byte aligned word-interleaved DualTrit { pos: u64, neg: u64 } layout mapped via MADV_HUGEPAGE, and documents runtime SIMD dispatch across AVX-512 VPOPCNTDQ, AVX2 Muła vpshufb, ARM64 NEON vcntq_u8, scalar u64 POPCNT and scalar SWAR.
Across production workloads with popcnt_max_err = 0, TRT2 outperforms legacy 2-bit maddubs kernels by +20.9% on BitMamba-2 1B (24.97 → 30.20 tok/s, with cold boot 5.2× faster at 144.7 ms on an Intel i7-13620H), reaches 1,199.2 tok/s at recurrent depth R = 1 and 211.6 tok/s at R = 3 on FM-MOSTEST core sweeps, and drives BitNet 2B-4T on a NZ$3 batteryless 2013 Haswell laptop (i5-4300U) from 1.30 → 12.19 tok/s (9.4× scalar, bit-exact argmax). It also documents the forensic discovery of a silent 0.683× double-attenuation export bug and the packing invariant that removes it.
2. Why multiply by one?
Multiplying an 8-bit activation by +1, 0 or −1 with a dedicated integer multiply-accumulate unit is the silicon equivalent of hiring a freight train to move a postcard. Yet most ternary CPU runtimes inherit floating-point habits: they store 2-bit packed weights, burn register pressure unpacking them through lookup tables, and dispatch vpmaddubsw as if the weights were arbitrary integers.
Every x86_64 processor shipped since 2008, and every modern ARM64 core, already exposes a single-cycle or high-throughput SIMD instruction made for ternary algebra: population count. This paper specifies the kernel that proves bit-plane decomposition plus dual-bitfield POPCNT eliminates weight multiplications completely, unifies dense transformers, O(1) SSMs and 7-expert block-diffusion routers onto one inner loop, and beats legacy maddubs kernels on the same silicon.
3. The Zero-Multiply POPCNT Origin Identity
Theorem 1 (Bit-Exact Zero-Multiply POPCNT Origin Identity). Let w ∈ {−1, 0, +1} be encoded as disjoint bitfields posj = 1[wj = +1] and negj = 1[wj = −1], with non-zero support mask D = pos ⊕ neg. Let x ∈ [−128, 127] be two's-complement INT8 activations decomposed into eight bit-planes xj = Σk ckBk,j, where ck = 2k for k < 7 and c7 = −128. Defining mk = neg ⊕ (D ∧ Bk) yields exact integer parity with zero multiplications per weight:
sum_j w_j x_j = sum_{k=0..7} c_k popcnt(m_k) + popcnt(neg)
Proof. Because Σk ck = (1 + 2 + 4 + 8 + 16 + 32 + 64) − 128 = −1, inspect each coordinate j by weight value:
- wj = 0 ⟹ Dj = negj = 0 ⟹ mk,j = 0, contributing 0.
- wj = +1 ⟹ Dj = 1, negj = 0 ⟹ mk,j = Bk,j, contributing Σk ckBk,j = xj.
- wj = −1 ⟹ Dj = 1, negj = 1 ⟹ mk,j = 1 − Bk,j, contributing Σk ck(1 − Bk,j) + 1 = (−1) − xj + 1 = −xj.
Summing over j gives the exact dot product with zero quantisation error. □
4. Memory layout and hardware dispatch
4.1 Cache-aligned DualTrit
Each row of ternary weights is stored as 64-byte aligned, word-interleaved DualTrit { pos: u64, neg: u64 } tiles, with two small sidecars per matrix:
.negpop.u32— precomputed popcnt(neg) per row, removing the constant offset term from the inner loop entirely..rowscale.f32— optional per-row dequantisation scale, applied once after the integer accumulator completes.
Weight blobs are memory-mapped with MADV_HUGEPAGE, collapsing TLB misses and enabling sub-150 ms cold boot without copying weight tensors into heap RAM.
4.2 Multi-architecture SIMD backends
One pure-Rust crate (tritcore.rs) dispatches the identical identity across five execution tiers:
- AVX-512 VPOPCNTDQ — native 512-bit vector population count on Ice Lake, Tiger Lake and newer, tested on an Intel i7-1165G7.
- AVX2 Muła vpshufb — nibble-indexed bytewise popcount across 256-bit registers from Haswell through Raptor Lake, tested on i5-4300U, i5-4570 and i7-13620H.
- ARM64 NEON vcntq_u8 — native 128-bit vector byte-popcount, cross-compiled to arm64-v8a, deployed on Snapdragon 8 Elite.
- Scalar u64 POPCNT and scalar SWAR — portable fallbacks guaranteeing bit-identical execution without vector popcount.
5. Forensic catch: the silent 0.683× double-attenuation bug
Auditing legacy .trit2 reload paths during a block-diffusion export exposed a subtle numerical trap: re-quantising ternary weights that already carried per-row scales silently attenuated magnitudes by 0.683×, degrading logits without throwing a single runtime error.
Packing directly from FP32 training state into native TRT2 (25 dual-bitfield layers, 23.4 MB, max_err = 0) alongside 45 protected INT8 edge matrices (156.5 MB, 179.8 MB total against 1,093.6 MB of unpacked FP32, a 6.08× compression ratio) restored exact 1.000× per-row scales and JAX numerical parity (cos(hkv) = 0.999959, cos(logits63) = 0.995246).
6. Kernel benchmarks across thirteen years of silicon
All kernel benchmarks were measured with rustc 1.99.0 --release, with no Python and no C++.
| Kernel / workload | Cold start | Throughput | Parity | Silicon and delta |
|---|---|---|---|---|
| BitNet 2B-4T (scalar → maddubs) | — | 1.30 → 6.58 tok/s | exact | 2013 15 W i5-4300U (NZ$3 laptop) |
| BitNet 2B-4T (TRT2, BF16 head) | 58.1 ms† | 10.05 tok/s | exact | i5-4300U, 521 MB at 8.97 GB/s |
| BitNet 2B-4T (TRT2 + INT8 head) | 58.1 ms† | 12.19 tok/s | argmax exact | i5-4300U, 9.4× scalar, 160 of 160 + 894 of 894 |
| BitNet 2B-4T (exact / top-64) | — | 15.89 / 27.16 tok/s | exact | 2013 i5-4570 versus 11.79 maddubs |
| BitMamba-2 1B (legacy 2-bit) | 754.8 ms | 24.97 tok/s | exact | i7-13620H, 644.3 MB, unpack + maddubs |
| BitMamba-2 1B (native TRT2) | 144.7 ms | 30.20 tok/s | max_err = 0 | i7-13620H, 228.6 MB, 5.2× boot, +20.9% |
| FM-MOSTEST core, R = 1 (AVX2) | 0.69 ms | 1,199.2 tok/s | exact | i7-13620H, 0.83 ms/token, Muła vpshufb |
| FM-MOSTEST core, R = 3 (AVX2) | 0.69 ms | 211.6 tok/s | exact | i7-13620H, +20.9% versus maddubs |
| Episodic SimHash (256-bit) | under 1 ms | 112.9 μs per 10k | exact | same POPCNT primitive for recall |
† The 58.1 ms figure is a cold 30-layer ternary pass over 521 MB of weights at 8.97 GB/s — a weight pass, not a process start.
7. Conclusion and limits
Ternary weights do not need multipliers; they need bitmasks and a counter. By reducing dense transformers, O(1) state-space models, 7-expert block-diffusion routers and 256-bit SimHash episodic recall to a single zero-multiply POPCNT substrate in pure Rust, TRT2 turns thirteen-year-old thrift-store laptops and modern handsets into first-class inference targets.
Head-to-head throughput runs against llama.cpp and bitnet.cpp, and sustained mobile thermal profiles, are scheduled for v1.5. This paper specifies the kernel and its measurements; it does not claim that a third party has reproduced them, and no external baseline is asserted here.
References
- S. Ma et al., “The Era of 1-bit LLMs,” arXiv:2402.17764, 2024.
- T. Dao and A. Gu, “Transformers are SSMs,” ICML, 2024.
- M. Arriola et al., “Block Diffusion,” ICLR, 2025.
- W. Muła, N. Kurz and D. Lemire, “Faster Population Counts Using AVX2 Instructions,” The Computer Journal, 61(1):111–120, 2018.
- D. Kelly, “TritCore v1.4,” Paper IV, 29 Nov 2026.