Instruction
BZHI
Clears the bits of the source from a given bit position upward; APX adds a form that leaves the flags unchanged.
BZHI keeps the low n bits of its source and clears the others, n
being the index in the third operand: bzhi eax, edi, esi computes
EDI with every bit from position ESI up cleared. It is the C
expression x & ((1ULL << n) - 1) in one instruction, and with
-march=x86-64-v3 GCC 14 and Clang 23 at -O2 compile it, for 32 and 64
bits, to a single BZHI. With SHRX it extracts a field
whose start and length are variables, (x >> s) & ((1ULL << n) - 1),
which both compilers prefer to BEXTR.
The index
Only bits 7:0 of the index register count, read as a number from 0 to 255:
- n = 0 gives 0;
- n from the operand size to 255 keeps every bit and sets CF, so BZHI
is defined where C’s
1u << 32is not; - the bits above 7:0 are ignored: on Zen 5, BZHI of all ones with the index 0x105 gave 0x1F, five bits, and the index 0x100 gave 0 from C4658624H.
Both manuals say in their text that a large index saturates at the operand size minus 1, which, read with AMD’s rule that the bits from the index up are cleared, would clear the top bit. Intel’s Operation section clears nothing in that case, and neither did Zen 5: an index of 32 or 255 left a 32-bit source of all ones as it was.
CF and ZF
CF says whether the index was at least the operand size; SF and ZF follow the result, OF is cleared, and AF and PF are undefined.
Speed
Intel’s optimization manual places BZHI among the instructions of the two bit-manipulation units of Golden Cove (Table 2-4); the instruction table of AMD’s Zen 5 guide lists it as one macro-op with a latency of one cycle.
BZHI belongs to BMI2, part of x86-64-v3; APX adds an EVEX form and one that leaves the flags unchanged (NF).
Forms
| Instruction | Encoding | Requires |
|---|---|---|
BZHI r32, m32, r32
BZHI_GPR32d_MEMd_GPR32d | VEX.LZ.NP.0F38.W0 F5 /r | BMI2 |
BZHI r32, r32, r32
BZHI_GPR32d_GPR32d_GPR32d | VEX.LZ.NP.0F38.W0 F5 /r | BMI2 |
BZHI r64, m64, r64 64-bit mode only
BZHI_GPR64q_MEMq_GPR64q | VEX.LZ.NP.0F38.W1 F5 /r | BMI2 |
BZHI r64, r64, r64 64-bit mode only
BZHI_GPR64q_GPR64q_GPR64q | VEX.LZ.NP.0F38.W1 F5 /r | BMI2 |
BZHI r32, r32, r32
BZHI_GPR32i32_GPR32i32_GPR32i32_APX | EVEX.LLZ.NP.0F38.W0 F5 /r | APX_F + BMI2 |
BZHI r32, m32, r32
BZHI_GPR32i32_MEMi32_GPR32i32_APX | EVEX.LLZ.NP.0F38.W0 F5 /r | APX_F + BMI2 |
BZHI r32, r32, r32 NF
BZHI_GPR32i32_GPR32i32_GPR32i32_APX_N3 | EVEX.LLZ.NP.0F38.W0 F5 /r NF=1 | APX_F + APX_NCI_NDD_NF + BMI2 |
BZHI r32, m32, r32 NF
BZHI_GPR32i32_MEMi32_GPR32i32_APX_N3 | EVEX.LLZ.NP.0F38.W0 F5 /r NF=1 | APX_F + APX_NCI_NDD_NF + BMI2 |
BZHI r64, r64, r64
BZHI_GPR64i64_GPR64i64_GPR64i64_APX | EVEX.LLZ.NP.0F38.W1 F5 /r | APX_F + BMI2 |
BZHI r64, m64, r64
BZHI_GPR64i64_MEMi64_GPR64i64_APX | EVEX.LLZ.NP.0F38.W1 F5 /r | APX_F + BMI2 |
BZHI r64, r64, r64 NF
BZHI_GPR64i64_GPR64i64_GPR64i64_APX_N3 | EVEX.LLZ.NP.0F38.W1 F5 /r NF=1 | APX_F + APX_NCI_NDD_NF + BMI2 |
BZHI r64, m64, r64 NF
BZHI_GPR64i64_MEMi64_GPR64i64_APX_N3 | EVEX.LLZ.NP.0F38.W1 F5 /r NF=1 | APX_F + APX_NCI_NDD_NF + BMI2 |
Latency and throughput
| Model | r32, m32, r32r64, m64, r64 | r32, r32, r32r64, r64, r64 |
|---|---|---|
| Haswell, Broadwell, Skylake, Skylake-SP, Ice Lake | 6 / 0.5p15 | 1 / 0.5p15 |
| Alder Lake, Sapphire Rapids | 8 / 1p1 | 3 / 1p1 |
| Lunar Lake | 7 / 0.33p135 | 3 / 0.33p135 |
| Zen | 5 / 0.5ALU0 ALU1 ALU2 ALU3 | 1 / 0.25ALU0 ALU1 ALU2 ALU3 |
| Zen 2 | 5 / 0.33ALU0 ALU1 ALU2 ALU3 | 1 / 0.25ALU0 ALU1 ALU2 ALU3 |
| Zen 3, Zen 4 | 5 / 0.5ALU1 ALU2 | 1 / 0.5ALU1 ALU2 |
Operation
What the forms do, in pseudocode that our tests run against results recorded on real processors. How to read.
n = index & 0xFF # bits 7:0 of the index register
r = src & (1 << n) - 1 # bits n and up cleared: none when n is
# OSIZE or more
if not nf: # NF=1 (APX) leaves the flags as they were
cf = n >= OSIZE
sf = msb(r)
zf = r == 0
of = 0
af = pf = UNDEFINED
dest.write(r) # a 32-bit register: bits 63:32 cleared
Run on AMD Zen 5 (EPYC 9645): 40 cases. The APX forms were not run: none of these processors has APX.
Operands
| Operand | Kind | Action |
|---|---|---|
BZHI r32, r/m32, r32 BZHI r64, r/m64, r64 64-bit mode only | ||
r32/64 | explicit | write |
r/m32/64 | explicit | read |
r32/64 | explicit | read |
RFLAGS | implicit | write |
BZHI r32, r/m32, r32 NF BZHI r64, r/m64, r64 NF | ||
r32/64 | explicit | write |
r/m32/64 | explicit | read |
r32/64 | explicit | read |
Flags
| OF | SF | ZF | AF | PF | CF |
|---|---|---|---|---|---|
| NF forms | |||||
| Not affected | |||||
| Other forms | |||||
| 0 | M | 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.
- VEX forms
- none in XED
- EVEX forms
APX-EVEX-BMI
Intrinsics
The C intrinsics of Clang 23.1.2 that compile to BZHI, 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 |
|---|---|
_bzhi_u32
unsigned int _bzhi_u32(unsigned int __X, unsigned int __Y) |
|
_bzhi_u64
unsigned long long _bzhi_u64(unsigned long long __X, unsigned long long __Y) |
|
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 |
|---|---|---|
BMI2 | BMI2 | 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, Zen, Zen+, Zen 2, Zen 3, Zen 4, Zen 5, Zen 6 |
APX_F_BMI2 | APX_F + BMI2 | Nova Lake, Diamond Rapids |
APX_F_BMI2_N3 | APX_F + APX_NCI_NDD_NF + BMI2 | Same as APX_F_BMI2 |
More BMI2 instructions
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 2A, page 3-120 (BZHI)
- 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 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.