Instruction
POPCNT
Counts the bits that are set in an operand; APX adds a form that leaves the flags unchanged.
POPCNT counts the bits that are set in its source, the population
count or Hamming weight. It answers how many members a bitset has,
and after an XOR how many bits two words differ in: Clang 23 at -O2,
allowed POPCNT, compiles __builtin_popcountl(a ^ b) to
xor rdi, rsi and popcnt rax, rdi. The lowest bit of the count is
the parity of the whole operand, which the parity flag cannot give,
since PF covers only the low byte of a result; with POPCNT available,
GCC 14 and Clang 23 compile __builtin_parity to POPCNT and an AND
with 1.
Availability
POPCNT has a CPUID bit of its own, CPUID.01H:ECX[23], apart from
SSE4.2’s. Among Intel’s processors Nehalem had it first, among AMD’s
K10, as the list below shows, and it belongs to x86-64-v2, not
to the x86-64 baseline. A compiler targeting the baseline therefore
cannot emit it: for __builtin_popcount, GCC 14 calls __popcountdi2
in its support library, and Clang 23 inlines some fifteen
instructions, shifts, masks, additions and a multiplication. With
-mpopcnt or -march=x86-64-v2, both emit a single POPCNT.
A processor without POPCNT raises #UD for its encoding, F3 0F B8. That is unlike TZCNT and LZCNT, whose F3-prefixed encodings older processors run as BSF and BSR, giving results that may differ.
Other forms
For counting in vectors, AVX-512 has per-element versions: VPOPCNTB and VPOPCNTW in AVX512_BITALG, VPOPCNTD and VPOPCNTQ in AVX512_VPOPCNTDQ. APX adds an EVEX form of POPCNT itself and one that leaves the flags unchanged (NF).
In the optimization guide
What the optimization guide says about POPCNT, 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
Crestmont and Gracemont
- Integer execution: LLVM treats popcnt on these cores as waiting for its destination’s old value.
Golden Cove and Raptor Cove
- Integer execution: Port 1 alone runs the slower integer operations: multiplication, PDEP and PEXT,
popcnt,bsr,crc32and the double shifts with an immediate count, which LLVM’s models give a latency of three cycles and one per cycle.
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.
Ice Lake to Rocket Lake
- Integer execution: LLVM’s tuning for these cores, unlike its tuning for Skylake, no longer zeroes the destination of
popcntto break a dependency on its old value.
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.
Silvermont to Goldmont Plus
- Out-of-order and in-order execution: On Silvermont shifts and rotates run on integer port 0, and jumps, multiplication,
popcnt,crc32and bit operations on port 1. sources differ - Out-of-order and in-order execution:
popcnttakes 3 cycles, one a cycle, on Silvermont and Goldmont. - Instruction set and counters: Silvermont is the first Atom core with SSE4.2 and POPCNT; Goldmont Plus adds RDPID and PTWRITE.
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. - Integer execution:
popcntwaits for the old value of its destination register, which it overwrites; compilers tuning for these cores zero the destination first to break the chain.
Tremont
- Integer execution: Three ALU ports run simple integer operations, three a cycle; shifts go to port 00 only, and multiplication, division,
popcnt,crc32and the bit operations to port 01 only. - Integer execution: LLVM’s tuning for Tremont, as for Goldmont Plus, does not make
popcntwait for its destination register’s old value, which its Goldmont tuning does.
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 |
|---|---|---|
POPCNT r16/32/64, m16/32/64
POPCNT_GPRv_MEMv | F3 0F B8 /r | POPCNT |
POPCNT r16/32/64, r16/32/64
POPCNT_GPRv_GPRv | F3 0F B8 /r | POPCNT |
POPCNT r16/32/64, r16/32/64
POPCNT_GPRv_GPRv_APX | EVEX.LLZ.NP.MAP4.SCALABLE 88 /r | APX_F + POPCNT |
POPCNT r16/32/64, m16/32/64
POPCNT_GPRv_MEMv_APX | EVEX.LLZ.NP.MAP4.SCALABLE 88 /r | APX_F + POPCNT |
POPCNT r16/32/64, r16/32/64 NF
POPCNT_GPRv_GPRv_APX_N3 | EVEX.LLZ.NP.MAP4.SCALABLE 88 /r NF=1 | APX_F + APX_NCI_NDD_NF + POPCNT |
POPCNT r16/32/64, m16/32/64 NF
POPCNT_GPRv_MEMv_APX_N3 | EVEX.LLZ.NP.MAP4.SCALABLE 88 /r NF=1 | APX_F + APX_NCI_NDD_NF + POPCNT |
Latency and throughput
| Model | r16, m16r32, m32r64, m64 | r16, r16 | r32, r32r64, r64 |
|---|---|---|---|
| Sandy Bridge | 9 / 1p1 | 3 / 1p1 | 3 / 1p1 |
| Haswell, Broadwell, Skylake, Skylake-SP, Ice Lake, Alder Lake, Sapphire Rapids | 8 / 1p1 | 3 / 1p1 | 3 / 1p1 |
| Lunar Lake | 7 / 0.33p135 | 3 / 0.33p135 | 3 / 0.33p135 |
| Silvermont | 6 / 1IEC0 MEC | 3 / 1IEC0 | 3 / 1IEC0 |
| Piledriver | 8 / 2EX0 EX1 | 4 / 2EX0 EX1 | 4 / 2EX0 EX1 |
| Zen | 5 / 0.5ALU0 ALU1 ALU2 ALU3 | 1 / 0.25ALU0 ALU1 ALU2 ALU3 | 1 / 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):
n += s >> i & 1 # count the 1 bits
if not nf: # NF=1 (APX) leaves the flags as they were
of = sf = af = pf = cf = 0
zf = s == 0
dest.write(n) # a 32-bit register: bits 63:32 cleared
Run on AMD Zen 5 (EPYC 9645) and an Intel processor under KVM (model hidden): 138 cases. The APX forms were not run: none of these processors has APX.
Operands
| Operand | Kind | Action |
|---|---|---|
POPCNT r16/32/64, r/m16/32/64 | ||
r16/32/64 | explicit | write |
r/m16/32/64 | explicit | read |
RFLAGS | implicit | write |
POPCNT 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 | |||||
| 0 | 0 | M | 0 | 0 | 0 |
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
Intrinsics
The C intrinsics of Clang 23.1.2 that compile to POPCNT, each with the form it compiles to and the Clang options it needs. The forms come from compiling every intrinsic and decoding the result with Intel XED; all intrinsics A–Z explains the method.
| Intrinsic | Compiles to |
|---|---|
_mm_popcnt_u32
int _mm_popcnt_u32(unsigned int __A) |
|
_mm_popcnt_u64
long long _mm_popcnt_u64(unsigned long long __A) |
|
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 |
|---|---|---|
POPCNT | POPCNT | Nehalem, Westmere, Sandy Bridge, Ivy Bridge, 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, Silvermont, Goldmont, Goldmont Plus, Tremont, Snow Ridge, Lakefield, 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_POPCNT | APX_F + POPCNT | Nova Lake, Diamond Rapids |
APX_F_POPCNT_N3 | APX_F + APX_NCI_NDD_NF + POPCNT | Same as APX_F_POPCNT |
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. - Clang's x86 intrinsic headers, LLVM 23.1.2 (commit
85ac56026243): the intrinsics, their signatures and doc comments, and the Clang of that release, which compiled each of them. - 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 2B, page 4-405 (POPCNT)
- 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)
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 intrinsics and their signatures come from Clang and the timings from LLVM's scheduling models, both part of the LLVM Project, licensed under the Apache License 2.0 with LLVM Exceptions. The text is our own.