Instruction
BEXTR
Extracts a bit field whose start and length come from a second register; APX adds a form that leaves the flags unchanged.
BEXTR copies a field of bits from its source to the low bits of the
destination and clears the rest. A control register gives the field:
its bits 7:0 the field’s lowest bit, its bits 15:8 the number of bits,
and the bits above them do not count. To take bits 11:7 of EAX, the
control is 0x0507: bextr ecx, eax, edx with EDX = 0x0507 puts them
in bits 4:0 of ECX.
The control’s edges are defined, unlike C’s shifts by the operand width or more: a length of 0 gives 0, a start past the top of the operand gives 0, and a field that runs past the top is filled with zeros there.
Rarely chosen by compilers
The control must be built in a register, which costs as much as the
shift and AND it replaces. So GCC 14 and Clang 23 at -O2, even with
-march=x86-64-v3, compile a constant field such as (x >> 7) & 0x1F
to SHR and AND, and a variable field with BMI2 to SHRX and
BZHI. With -mbmi alone, Clang 23 builds the control
from s and n with a shift and an OR and uses BEXTR for
(x >> s) & ((1ULL << n) - 1); GCC 14 builds a mask for
ANDN. The intrinsic _bextr_u64(x, start, len) also
packs the control first; _bextr2_u64(x, control) takes one ready-made.
What the flags say
ZF tells whether the field was zero. The manuals clear CF and OF, while XED leaves them undefined; Zen 5 cleared both in every recorded case, as the page’s pseudocode and its note on Intel’s documentation say. SF, AF and PF are undefined.
AMD’s immediate form
AMD’s TBM extension, from the Piledriver generation, added a BEXTR whose control is a 32-bit immediate, in the XOP encoding. Zen 5 has no TBM: it raised #UD for each of these forms. Code that needs a constant field on current processors uses a shift and an AND.
Speed
The instruction table of AMD’s Zen 5 guide lists BEXTR as one macro-op with a latency of one cycle; Intel’s optimization manual places it among the instructions of the two bit-manipulation units of Golden Cove (Table 2-4).
BEXTR belongs to BMI1, part of x86-64-v3; APX adds an EVEX form and one that leaves the flags unchanged (NF).
Forms
| Instruction | Encoding | Requires |
|---|---|---|
BEXTR r32, m32, r32
BEXTR_GPR32d_MEMd_GPR32d | VEX.LZ.NP.0F38.W0 F7 /r | BMI1 |
BEXTR r32, r32, r32
BEXTR_GPR32d_GPR32d_GPR32d | VEX.LZ.NP.0F38.W0 F7 /r | BMI1 |
BEXTR r64, m64, r64 64-bit mode only
BEXTR_GPR64q_MEMq_GPR64q | VEX.LZ.NP.0F38.W1 F7 /r | BMI1 |
BEXTR r64, r64, r64 64-bit mode only
BEXTR_GPR64q_GPR64q_GPR64q | VEX.LZ.NP.0F38.W1 F7 /r | BMI1 |
BEXTR r32, r32, r32
BEXTR_GPR32i32_GPR32i32_GPR32i32_APX | EVEX.LLZ.NP.0F38.W0 F7 /r | APX_F + BMI1 |
BEXTR r32, m32, r32
BEXTR_GPR32i32_MEMi32_GPR32i32_APX | EVEX.LLZ.NP.0F38.W0 F7 /r | APX_F + BMI1 |
BEXTR r32, r32, r32 NF
BEXTR_GPR32i32_GPR32i32_GPR32i32_APX_N3 | EVEX.LLZ.NP.0F38.W0 F7 /r NF=1 | APX_F + APX_NCI_NDD_NF + BMI1 |
BEXTR r32, m32, r32 NF
BEXTR_GPR32i32_MEMi32_GPR32i32_APX_N3 | EVEX.LLZ.NP.0F38.W0 F7 /r NF=1 | APX_F + APX_NCI_NDD_NF + BMI1 |
BEXTR r64, r64, r64
BEXTR_GPR64i64_GPR64i64_GPR64i64_APX | EVEX.LLZ.NP.0F38.W1 F7 /r | APX_F + BMI1 |
BEXTR r64, m64, r64
BEXTR_GPR64i64_MEMi64_GPR64i64_APX | EVEX.LLZ.NP.0F38.W1 F7 /r | APX_F + BMI1 |
BEXTR r64, r64, r64 NF
BEXTR_GPR64i64_GPR64i64_GPR64i64_APX_N3 | EVEX.LLZ.NP.0F38.W1 F7 /r NF=1 | APX_F + APX_NCI_NDD_NF + BMI1 |
BEXTR r64, m64, r64 NF
BEXTR_GPR64i64_MEMi64_GPR64i64_APX_N3 | EVEX.LLZ.NP.0F38.W1 F7 /r NF=1 | APX_F + APX_NCI_NDD_NF + BMI1 |
BEXTR r32, m32, imm32 not in 64-bit mode
BEXTR_XOP_GPR32d_MEMd_IMMd | XOP.LZ.NP.0A.WIG 10 /r id | TBM |
BEXTR r32/64, m32/64, imm32 64-bit mode only
BEXTR_XOP_GPRyy_MEMy_IMMd | XOP.LZ.NP.0A.WIG 10 /r id | TBM |
BEXTR r32, r32, imm32 not in 64-bit mode
BEXTR_XOP_GPR32d_GPR32d_IMMd | XOP.LZ.NP.0A.WIG 10 /r id | TBM |
BEXTR r32/64, r32/64, imm32 64-bit mode only
BEXTR_XOP_GPRyy_GPRyy_IMMd | XOP.LZ.NP.0A.WIG 10 /r id | TBM |
Latency and throughput
| Model | r32, m32, r32r64, m64, r64 | r32, r32, r32r64, r64, r64 |
|---|---|---|
| Haswell, Broadwell, Skylake, Skylake-SP, Ice Lake | 7 / 0.5p0156 | 2 / 0.5p0156 |
| Alder Lake, Sapphire Rapids | 11 / 1p016 | 6 / 1p016 |
| Lunar Lake | 6 / 0.67p135 | |
| Zen | 5 / 0.5ALU0 ALU1 ALU2 ALU3 | 1 / 0.25ALU0 ALU1 ALU2 ALU3 |
| Zen 3, Zen 4 | 5 / 0.5ALU1 ALU2 | 1 / 0.5ALU1 ALU2 |
| Jaguar | 4 / 1ALU0 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.
start = control & 0xFF # bits 7:0: the field's lowest bit
length = control >> 8 & 0xFF # bits 15:8: its number of bits
r = src >> start & (1 << length) - 1 # bits past the top of src read
# as 0, so a start of OSIZE or
# more gives 0
if not nf: # NF=1 (APX) leaves the flags as they were
zf = r == 0
of = cf = 0
sf = af = pf = UNDEFINED
dest.write(r) # a 32-bit register: bits 63:32 cleared
Run on AMD Zen 5 (EPYC 9645): 42 cases. The APX forms were not run: none of these processors has APX.
features = cpuid_leaf(0x80000001, 0)[2]
if not features >> 21 & 1:
fault(UD) # CPUID: no TBM
start = control & 0xFF # imm32 bits 7:0: the field's lowest bit
length = control >> 8 & 0xFF # bits 15:8: its number of bits; bits
# 31:16 are ignored
r = src >> start & (1 << length) - 1 # bits past the top of src read
# as 0
zf = r == 0
of = cf = 0
sf = af = pf = UNDEFINED
dest.write(r) # a 32-bit register: bits 63:32 cleared
Run on AMD Zen 5 (EPYC 9645): 4 cases. Its cases ran where CPUID reported no TBM, and every one raised an exception. The forms that 64-bit mode lacks were not run: the probe runs in 64-bit mode.
Operands
| Operand | Kind | Action |
|---|---|---|
BEXTR r32, r/m32, r32 BEXTR r64, r/m64, r64 64-bit mode only | ||
r32/64 | explicit | write |
r/m32/64 | explicit | read |
r32/64 | explicit | read |
RFLAGS | implicit | write |
BEXTR r32, r/m32, r32 NF BEXTR r64, r/m64, r64 NF | ||
r32/64 | explicit | write |
r/m32/64 | explicit | read |
r32/64 | explicit | read |
BEXTR r32, r/m32, imm32 not in 64-bit mode BEXTR r32/64, r/m32/64, imm32 64-bit mode only | ||
r32/64 | explicit | write |
r/m32/64 | explicit | read |
imm32 | explicit | read |
RFLAGS | implicit | write |
Flags
| OF | SF | ZF | AF | PF | CF |
|---|---|---|---|---|---|
| NF forms | |||||
| Not affected | |||||
| XOP forms | |||||
| 0 | U | M | U | U | 0 |
| Other forms | |||||
| U | U | M | U | U | U |
Exceptions
The exception class is XED's name for the set of exception conditions that a form follows. How to read.
- VEX and XOP forms
- none in XED
- EVEX forms
APX-EVEX-BMI
Intrinsics
The C intrinsics of Clang 23.1.2 that compile to BEXTR, 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 |
|---|---|
_bextr2_u32
unsigned int _bextr2_u32(unsigned int __X, unsigned int __Y) |
|
_bextr2_u64
unsigned long long _bextr2_u64(unsigned long long __X, unsigned long long __Y) |
|
__bextr_u32
unsigned int __bextr_u32(unsigned int __X, unsigned int __Y) |
|
__bextr_u64
unsigned long long __bextr_u64(unsigned long long __X, unsigned long long __Y) |
|
__bextri_u32
unsigned int __bextri_u32(unsigned int a, const unsigned int b) |
|
__bextri_u64
unsigned long long __bextri_u64(unsigned long long a, const unsigned long long b) |
|
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 |
|---|---|---|
BMI1 | BMI1 | 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 3, Zen 4, Zen 5, Zen 6 Note: XED's Piledriver adds TBM and FMA to Bulldozer but not BMI1, and its Zen chips inherit that gap; GCC's -march=bdver2 and later and -march=znver1 and later include BMI1. |
APX_F_BMI1 | APX_F + BMI1 | Nova Lake, Diamond Rapids |
APX_F_BMI1_N3 | APX_F + APX_NCI_NDD_NF + BMI1 | Same as APX_F_BMI1 |
TBM | TBM | Piledriver, Zen, Zen+, Zen 2 Note: XED's Zen chips inherit TBM from its Piledriver chip; GCC's -march=znver1 and later do not enable TBM. |
More BMI1 instructions
More APX instructions
- AADD
- AAND
- ADC
- ADCX
- ADD
- ADOX
- AND
- ANDN
- AOR
- AXOR
- BLSI
- BLSMSK
- BLSR
- BZHI
- CCMPscc
- CFCMOVcc
- CMOVcc
- CMPccXADD
- CRC32
- CTESTscc
- DEC
- DIV
- ENQCMD
- ENQCMDS
- IDIV
- IMUL
- INC
- INVEPT
- INVPCID
- INVVPID
- JMPABS
- KMOVB
- KMOVD
- KMOVQ
- KMOVW
- LDTILECFG
- LZCNT
- MOVBE
- MOVDIR64B
- MOVDIRI
More TBM 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-77 (BEXTR)
- 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.