Instruction
LZCNT
Counts the leading zero bits of an operand; APX adds a form that leaves the flags unchanged.
LZCNT counts the zero bits above the highest set bit of its source: for the 32-bit value 0x28 it gives 26. A zero source returns the operand size, 16, 32 or 64, and sets CF, so every input has a defined result, and ZF is set when the count is 0, that is, when the top bit is set. It is the counterpart of TZCNT, which counts from the bottom.
Availability
AMD’s manual files LZCNT under ABM, its advanced bit manipulation, and
both vendors report it with the same bit, CPUID.80000001H:ECX[5]; it
belongs to x86-64-v3. Its encoding, F3 0F BD, is
BSR with an F3 prefix, and a processor without LZCNT runs
it as BSR, which returns the index of the highest set bit instead of the
number of zeros above it: 5 instead of 26 for 0x28. AMD’s manual
therefore tells software to check the CPUID bit before using it. For the
same reason, GCC 14 and Clang 23 at -O2 do not emit LZCNT for the
baseline: __builtin_clz(x) becomes BSR and an XOR with 31.
With -mlzcnt
Allowed LZCNT, both compilers turn __builtin_clz(x) into an LZCNT,
and so, since the zero case is now defined, x ? __builtin_clz(x) : 32
and __builtin_clzg(x, 32). GCC puts an xor eax, eax before the
LZCNT of __builtin_clz, Clang does not. The bit width of x,
32 - __builtin_clz(x), becomes mov eax, 32, LZCNT and SUB, and the
leading ones of x are the leading zeros of ~x, a NOT and an LZCNT.
Other forms
AVX-512CD counts per element: VPLZCNTD and VPLZCNTQ; Intel’s optimization manual builds permute controls with them in its conflict-detection examples. APX adds an EVEX form of LZCNT and one that leaves the flags unchanged (NF). The instruction table of AMD’s Zen 5 guide lists LZCNT as one macro-op with a latency of one cycle.
In the optimization guide
What the optimization guide says about LZCNT, a line per claim. Each links to the claim in the guide, with its sources and where they differ.
Out of order
- Zeroing and dependency-breaking idioms: Some instructions that overwrite their whole destination still wait for its old value, a flaw of particular cores: LLVM’s tunings mark
popcntfrom Sandy Bridge to the Skylake family andlzcntandtzcnton Haswell, AMD namestzcntand three BMI1 instructions on some versions of Zen 5, and Intel several AVX-512 permutations and multiplications on Golden Cove (Skylake, Zen 5, Golden Cove).
Bulldozer to Excavator
- Integer execution: POPCNT and LZCNT, new with Family 15h, beat any sequence that does their work: POPCNT takes 4 cycles, 6 with 64-bit operands, and starts every 2 or 4 cycles, and LZCNT is a double instruction of 2 cycles. sources differ
Haswell and Broadwell
- Integer execution and bypass: LLVM’s tuning marks POPCNT, LZCNT and TZCNT on both cores as waiting for whatever last wrote the register they overwrite, though they never use its value, as on Skylake.
Jaguar and Puma
- Integer execution: LZCNT and POPCNT take one cycle on either ALU and TZCNT two, while BSF and BSR are microcoded, of seven and eight macro-ops and 4 and 5 cycles, so the newer instructions are the better choice where the zero case allows.
Skylake to Cooper Lake
- Integer execution: Port 1 alone runs integer multiplication, PDEP and PEXT,
popcnt,lzcnt,tzcnt,bsf,bsrand the double shifts, with a latency of three cycles and one a cycle in LLVM’s models, as on Golden Cove.
Zen and Zen+
- Integer execution: BSF and BSR are six macro-ops with a latency of three and four cycles, while LZCNT and POPCNT are one macro-op of one cycle and TZCNT two of two cycles. sources differ
Zen 2
- Integer execution: BSF and BSR are six macro-ops with a latency of three and four cycles, while LZCNT and POPCNT are one macro-op of one cycle and TZCNT two; LLVM’s model agrees.
Forms
| Instruction | Encoding | Requires |
|---|---|---|
LZCNT r16/32/64, m16/32/64
LZCNT_GPRv_MEMv | F3 0F BD /r | LZCNT |
LZCNT r16/32/64, r16/32/64
LZCNT_GPRv_GPRv | F3 0F BD /r | LZCNT |
LZCNT r16/32/64, r16/32/64
LZCNT_GPRv_GPRv_APX | EVEX.LLZ.NP.MAP4.SCALABLE F5 /r | APX_F + LZCNT |
LZCNT r16/32/64, m16/32/64
LZCNT_GPRv_MEMv_APX | EVEX.LLZ.NP.MAP4.SCALABLE F5 /r | APX_F + LZCNT |
LZCNT r16/32/64, r16/32/64 NF
LZCNT_GPRv_GPRv_APX_N3 | EVEX.LLZ.NP.MAP4.SCALABLE F5 /r NF=1 | APX_F + APX_NCI_NDD_NF + LZCNT |
LZCNT r16/32/64, m16/32/64 NF
LZCNT_GPRv_MEMv_APX_N3 | EVEX.LLZ.NP.MAP4.SCALABLE F5 /r NF=1 | APX_F + APX_NCI_NDD_NF + LZCNT |
Latency and throughput
| Model | r16, m16r32, m32r64, m64 | r16, r16 | r32, r32r64, r64 |
|---|---|---|---|
| Haswell, Broadwell, Skylake, Skylake-SP, Ice Lake, Alder Lake, Sapphire Rapids | 8 / 1p1 | 3 / 1p1 | 3 / 1p1 |
| Lunar Lake | 7 / 1p1 | 3 / 1p1 | 3 / 1p1 |
| Piledriver | 6 / 2EX0 | 2 / 2EX0 | 2 / 2EX0 |
| Zen | 6 / 0.5ALU0 ALU1 ALU2 ALU3 | 2 / 0.25ALU0 ALU1 ALU2 ALU3 | 2 / 0.25ALU0 ALU1 ALU2 ALU3 |
| Zen 2 | 5 / 0.33ALU0 ALU1 ALU2 ALU3 | 1 / 0.25ALU0 ALU1 ALU2 ALU3 | 1 / 0.25ALU0 ALU1 ALU2 ALU3 |
| Zen 3, Zen 4 | 5 / 0.33ALU0 ALU1 ALU2 ALU3 | 1 / 1ALU0 ALU1 ALU2 ALU3 | 1 / 0.25ALU0 ALU1 ALU2 ALU3 |
| Jaguar | 4 / 1ALU0 ALU1 | 1 / 0.5ALU0 ALU1 | 1 / 0.5ALU0 ALU1 |
Operation
What the forms do, in pseudocode that our tests run against results recorded on real processors. How to read.
s = src.read()
n = 0
for i in range(OSIZE):
if s >> (OSIZE - 1 - i) & 1:
break
n += 1 # the 0 bits above the highest 1 bit
if not nf: # NF=1 (APX) leaves the flags as they were
cf = s == 0 # and then n is OSIZE
zf = n == 0
of = sf = af = pf = UNDEFINED
dest.write(n) # a 32-bit register: bits 63:32 cleared
# without LZCNT (AMD's ABM), a processor runs the legacy encoding
# (F3 0F BD) as BSRRun on AMD Zen 5 (EPYC 9645): 78 cases. The APX forms were not run: none of these processors has APX.
Operands
| Operand | Kind | Action |
|---|---|---|
LZCNT r16/32/64, r/m16/32/64 | ||
r16/32/64 | explicit | write |
r/m16/32/64 | explicit | read |
RFLAGS | implicit | write |
LZCNT r16/32/64, r/m16/32/64 NF | ||
r16/32/64 | explicit | write |
r/m16/32/64 | explicit | read |
Flags
| OF | SF | ZF | AF | PF | CF |
|---|---|---|---|---|---|
| NF forms | |||||
| Not affected | |||||
| Other forms | |||||
| U | U | M | U | U | M |
Exceptions
The exception class is XED's name for the set of exception conditions that a form follows. How to read.
- Legacy forms
- none in XED
- EVEX forms
APX-EVEX-INT
CPUID and processors
Each form belongs to an XED ISA set. A form can be used when the processor reports every CPUID bit of one of its ISA set's alternatives.
| ISA set | CPUID | Processors |
|---|---|---|
LZCNT | LZCNT | Haswell, Broadwell, Skylake, Comet Lake, Cannon Lake, Ice Lake (client), Tiger Lake, Alder Lake, Arrow Lake, Panther Lake, Nova Lake, Skylake-SP, Cascade Lake, Cooper Lake, Ice Lake (server), Sapphire Rapids, Emerald Rapids, Granite Rapids, Diamond Rapids, Sierra Forest, Clearwater Forest, Knights Landing, Knights Mill, K10 (Family 10h), Bulldozer, Piledriver, Zen, Zen+, Zen 2, Zen 3, Zen 4, Zen 5, Zen 6 |
APX_F_LZCNT | APX_F + LZCNT | Nova Lake, Diamond Rapids |
APX_F_LZCNT_N3 | APX_F + APX_NCI_NDD_NF + LZCNT | Same as APX_F_LZCNT |
More APX instructions
Sources
- Intel XED v2026.08.23 (commit
0bcb6237345c): forms, encodings, operands, flags, exception classes, attributes, ISA sets, CPUID bits and chip model. - The CPU data of x86-64.net: Zen 3 to Zen 6, from GCC’s
-marchfeature sets and AMD’s Processor Programming References, which the page of each processor cites. - LLVM 23.1.2's x86 scheduling models (commit
85ac56026243): the latency, reciprocal throughput and ports of each form, as llvm-mca reports them. - Intel 64 and IA-32 Architectures Software Developer's Manual, Volume 2 (325383-093, September 2026): Volume 2A, page 3-578 (LZCNT)
- Intel Advanced Performance Extensions (Intel APX) Architecture Specification, revision 9.0 (355828-009, August 2026)
- AMD64 Architecture Programmer's Manual, Volume 3: General-Purpose and System Instructions (24594, rev. 3.39, October 2026)
- Intel 64 and IA-32 Architectures Optimization Reference Manual, Volume 1 (248966-051, October 2026)
- Software Optimization Guide for the AMD Zen5 Microarchitecture (58455, rev. 1.00, August 2024), Wayback Machine copy of AMD's ZIP
The tables are derived from Intel XED, Copyright Intel Corporation, licensed under the Apache License 2.0; x86-64.net converted and reformatted them. The timings come from LLVM's scheduling models, part of the LLVM Project, licensed under the Apache License 2.0 with LLVM Exceptions. The text is our own.