LmCast :: Stay tuned in

Vectorized CLZ and CTZ

Recorded: Oct. 12, 2026, 2:09 a.m.

Original Summarized

Vectorized CLZ and CTZ | purplesyringa's blogpurplesyringaaboutblogkitchen sinkVectorized CLZ and CTZOctober 9, 2026clz and ctz are instructions that compute the number of leading (trailing) zero bits in a fixed-size integer. They are natively supported by modern CPUs, though they are not always fast, e.g. tzcnt has a latency of 3 on Arrow Lake.I used ctz in an FPU emulator I’m working on, but figured out how to avoid it with floating-point trickery, and I just realized that this generalizes to a vectorizable ctz polyfill in a round-about way. I also implemented clz for completeness.clzLet’s start with clz, the easier of the two:fn clz(x: u32) -> u32 {
let a = 2.0f64.powi(-970);
32 - ((f64::from_bits(a.to_bits() | x as u64) - a).to_bits() >> 52) as u32
}
The general idea goes like this:Floating-point exponents are biased logarithms of their values. By substituting a 32-bit number x into the mantissa of some value 2k, we get a double representing 2k(1+2−52x). We can then subtract 2k as a double to get 2k−52x. Extracting the exponent gives k−52+31−clz(x), from which clz can be computed with a bitwise subtraction. From that, k can be chosen such that clz behaves correctly for x=0.We need 64-bit doubles to handle 32-bit inputs; unfortunately, this means that this trick can’t work for arbitrary 64-bit inputs, only up to 52 bits.Assuming the inputs and outputs are stored in u64x4, this compiles to: vpbroadcastq ymm1, [rip + bias]

vorpd ymm0, ymm0, [rip + a]
vsubpd ymm0, ymm0, [rip + a]
vpsrlq ymm0, ymm0, 52
vpsubq ymm0, ymm1, ymm0

a:
.quad 0x350000000000000, 0x350000000000000, 0x350000000000000, 0x350000000000000
bias:
.quad 32
On my Haswell, this runs at 0.45 ns/iteration, compared to 1 ns for the scalar version. When latency-bound, the numbers rise to 2 ns vs 1 ns (but if you’re latency-bound on vectorized ctz, you’re probably doing something wrong).Ian Qvist tested this on Alder Lake (thanks!) and got 0.29 ns/iteration, compared to 0.85 ns for the scalar version, and 1.3 ns vs 0.85 ns when latency-bound. On modern Intel CPUs, the numbers should be the same or better.AMD CPUs make lzcnt so cheap that a scalar version will likely win. Though keep in mind that Zen CPUs support AVX-512, which has vplzcntd, so that’s an option, too.ctzNow for ctz:fn ctz(x: u32) -> u32 {
let a = f64::from_bits((0x340000100000001 ^ x as u64) ^ (x as u64 + u32::MAX as u64))
- f64::from_bits(0x340000000000000);
(a.to_bits() >> 52) as u32
}
We start with x⊕︎(x−1) to isolate the lowest set bit. ctz equals the logarithm of that value, which we determine by adding 2k bitwise and subtracting 2k as a double, then inspecting the exponent, which with a well-chosen k contains the unbiased ctz. We pre-mix 232 into the mantissa and use x+232−1 instead of x−1 to handle x=0 correctly, and pre-mix 1 into the mantissa to ensure odd x generate a=0 and not a slow subnormal. (Can you imagine how much time I spent arranging this?)This function compiles to: vpxor ymm1, ymm0, [rip + c1]
vpaddq ymm0, ymm0, [rip + u32_max]
vpxor ymm0, ymm1, ymm0
vsubpd ymm0, ymm0, [rip + c2]
vpsrlq ymm0, ymm0, 52

c1:
.qword 0x340000100000001, 0x340000100000001, 0x340000100000001, 0x340000100000001
u32_max:
.qword 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff
c2:
.qword 0x340000000000000, 0x340000000000000, 0x340000000000000, 0x340000000000000
On Haswell, this runs at 0.49 ns/iteration and 2.3 ns when latency-bound. On Alder Lake, it’s 0.35 ns/iteration and 1.3 ns when latency-bound. The slowdown compared to clz is due to using one more instruction. It can be avoided by using vpternlogq if AVX-512 is present, but at that point you might as well run vpopcntd on (x - 1) & !x. The scalar version behaves no differently from clz.Added on October 10:Nikolay Malkovsky pointed out that de Bruijn sequences offer another vectorizable approach. After some testing, I arrived at the following code:const char table[32] = {
0, 4, 5, 6, 11, 9, 7, 12, 15, 3, 10, 8, 14, 2, 13, 1,
0, 4, 5, 6, 11, 9, 7, 12, 15, 3, 10, 8, 14, 2, 13, 1,
};
__m256i bit = _mm256_andnot_si256(x, _mm256_sub_epi32(x, _mm256_set1_epi32(1)));
__m256i high = _mm256_madd_epi16(
_mm256_cmpeq_epi16(bit, _mm256_set1_epi16(-1)),
_mm256_set1_epi16(-16)
);
__m256i index = _mm256_srli_epi32(_mm256_mullo_epi32(bit, _mm256_set1_epi32(0xf0a6f0a7)), 28);
__m256i low = _mm256_shuffle_epi8(_mm256_loadu_si256((__m256i*)table), index);
return _mm256_add_epi32(low, high);
We can’t use a true 32-byte LUT because vpshufb cannot cross 16-byte lanes. The approach I used instead is tricky to explain, but essentially we use a 16-bit de Bruijn sequence repeated twice to compute bits 0-3 of the ctz, and then add 16 or 32 depending on which halves are zeroes. Six seven 0xf0a6f0a7 is one of only four magic constants that make this work.This takes 1 ns on Haswell (0.7 ns on Alder Lake), but has twice the throughput, so it may be a little faster than the FP-based approach if it helps avoid shuffling.If you don’t need to deal with x=0 (or want ctz(0) to be 0 and not 32), using__m256i high = _mm256_and_si256(
_mm256_cmpgt_epi32(bit, _mm256_set1_epi32(0x7fff)),
_mm256_set1_epi32(16)
);
brings the time down to 0.82 ns.Added on October 11:arya dradjica and I brainstormed a 64-bit version of the algorithm and managed to find what I think is the optimal algorithm:const char table[32] = { // x -> 4 - popcnt(x)
4, 3, 3, 2, 3, 2, 2, 1, 3, 2, 2, 1, 2, 1, 1, 0,
4, 3, 3, 2, 3, 2, 2, 1, 3, 2, 2, 1, 2, 1, 1, 0,
};
__m256i lut = _mm256_loadu_si256((__m256i*)table);

__m256i bit = _mm256_andnot_si256(x, _mm256_sub_epi64(x, _mm256_set1_epi64x(1)));
__m256i low = _mm256_shuffle_epi8(lut, bit);
__m256i high = _mm256_shuffle_epi8(lut, _mm256_srli_epi16(bit, 4));
return _mm256_sad_epu8(_mm256_add_epi8(low, high), _mm256_set1_epi8(8));
This correctly returns ctz(0)=64 and takes 0.63 ns on my Haswell (2.8 ns when latency-bound). It computes inverted popcounts of the two nibbles of each byte of (x - 1) & !x separately (inverted because of some idiosyncrasies of pshufb) and then uses vpsadbw to perform the horizontal sum and correct the inversion in one go.vpsadbw can only horizontally sum exactly 8 bytes, which is why this algorithm is specialized to 64 bits and doesn’t work with 32-bit inputs. The approach based on the de Bruijn sequence is still the fastest in that scenario.Made with my own bare hands (why.)

The work investigates the implementation of Count Leading Zero (CLZ) and Count Trailing Zero (CTZ) instructions, which are fundamental for determining the number of leading or trailing zero bits in a fixed-size integer. Although these operations are natively supported by modern CPUs, their performance can be variable, and the author explored methods for creating vectorized polyfills for these instructions.

For CLZ, the approach involves utilizing floating-point properties. The method leverages the relationship between floating-point exponents and logarithms. By substituting an input integer into the mantissa of a specific double-precision value and performing subtractions, the author can extract information about the bit count. This technique requires 64-bit doubles to handle 32-bit inputs, which inherently limits the effectiveness of this specific trick to inputs up to 52 bits. The implementation details involve specific sequence of floating-point and bitwise operations designed to extract the necessary information from the exponents. Performance comparisons were made across different CPU architectures, noting the trade-off between latency and iteration speed.

The method for CTZ is based on isolating the lowest set bit using the expression x XOR (x-1). The count is then derived by inspecting the exponent of a carefully constructed double-precision value, which provides the unbiased CTZ. The author details how adjustments are made to the input to correctly handle the case where the input is zero, ensuring the function behaves as expected. Similar performance comparisons were conducted for the CTZ implementation against the CLZ method.

To achieve faster, more parallel computation, the author explored alternative vectorizable methods, including those based on de Bruijn sequences. This approach utilizes lookup tables and bitwise shuffling operations, attempting to compute the zero counts by looking up indices, which is necessary because standard shuffling instructions limit vector operations to 16-byte lanes. The de Bruijn sequence method was shown to be competitive, demonstrating good throughput, especially when avoiding complex shuffling.

Further optimization led to the development of a specialized 64-bit algorithm which the author determined was optimal. This algorithm operates on the properties of inverted population counts of the nibbles of the result of (x-1) AND NOT x. This method computes the inverted popcounts for the two halves of each byte separately and then uses horizontal summation to combine the results efficiently. This 64-bit technique successfully computes CTZ, including handling the case where the input is zero correctly, and demonstrated superior performance compared to both the floating-point methods and the de Bruijn sequence approach. The final optimized algorithm relies on specialized instructions to perform the horizontal sum across 8 bytes, leveraging the specific structure of the derived data to achieve high speed on modern architectures.