Instruction
BLSMSK
Sets all bits up to and including the lowest set bit of the source and clears the rest; APX adds a form that leaves the flags unchanged.
BLSMSK sets every bit from bit 0 up to and including the lowest set bit
of its source, and clears the rest: for 0x28 it gives 0xF, for 1 it
gives 1. It is the C idiom x ^ (x - 1) in one instruction, and with
-mbmi or -march=x86-64-v3 GCC 14 and Clang 23 at -O2 compile that
idiom to blsmsk eax, edi. Of its BMI1 siblings, BLSI
keeps the lowest set bit alone and BLSR clears it;
BLSMSK’s result is BLSI’s times two, minus one.
A source of zero
With no bit set there is no lowest one, and BLSMSK returns all ones: Zen 5 gave FFFFFFFFH for a zero 32-bit source and FFFFFFFFFFFFFFFFH for a zero 64-bit one. CF tells that case apart, as it is set only for a zero source. ZF is always cleared, since the result always has bit 0 set, so ZF says nothing here, unlike after BLSI and BLSR. SF copies the result’s top bit, which is set only when the source is zero or its lowest set bit is the top one, and OF is cleared.
Both manuals leave AF and PF undefined. In every recorded case Zen 5 cleared AF and set PF from the low byte of the result, but code should not rely on either.
Uses
A mask from a compare, such as the one PMOVMSKB
makes from a PCMPEQB with one bit per byte, often
needs everything up to and including the first match: BLSMSK gives it
in one step. The related masks have no BMI1 instruction. ~x & (x - 1),
the trailing zeros alone, compiles to LEA and ANDN, and
x | (x - 1), which fills them in, to LEA and OR. AMD’s
TBM extension has single instructions for both,
TZMSK and BLSFILL.
Speed
AMD’s Zen 5 instruction table lists BLSMSK as one macro-op with a latency of one cycle, three per cycle. The guide’s warning about a false dependency on the destination in some versions of Zen 5, which the BLSI page describes, names BLSMSK too (Section 2.9.9). BLSMSK belongs to BMI1, part of x86-64-v3; APX adds an EVEX form and one that leaves the flags unchanged (NF).
In the optimization guide
What the optimization guide says about BLSMSK, a line per claim. Each links to the claim in the guide, with its sources and where they differ.
Zen 5
- Integer execution: Some versions of Zen 5 make TZCNT, BLSI, BLSR and BLSMSK wait for the old value of their destination.
Forms
| Instruction | Encoding | Requires |
|---|---|---|
BLSMSK r32, m32
BLSMSK_GPR32d_MEMd | VEX.LZ.NP.0F38.W0 F3 /2 | BMI1 |
BLSMSK r32, r32
BLSMSK_GPR32d_GPR32d | VEX.LZ.NP.0F38.W0 F3 /2 | BMI1 |
BLSMSK r64, m64 64-bit mode only
BLSMSK_GPR64q_MEMq | VEX.LZ.NP.0F38.W1 F3 /2 | BMI1 |
BLSMSK r64, r64 64-bit mode only
BLSMSK_GPR64q_GPR64q | VEX.LZ.NP.0F38.W1 F3 /2 | BMI1 |
BLSMSK r32, r32
BLSMSK_GPR32i32_GPR32i32_APX | EVEX.LLZ.NP.0F38.W0 F3 /2 | APX_F + BMI1 |
BLSMSK r32, m32
BLSMSK_GPR32i32_MEMi32_APX | EVEX.LLZ.NP.0F38.W0 F3 /2 | APX_F + BMI1 |
BLSMSK r32, r32 NF
BLSMSK_GPR32i32_GPR32i32_APX_N3 | EVEX.LLZ.NP.0F38.W0 F3 /2 NF=1 | APX_F + APX_NCI_NDD_NF + BMI1 |
BLSMSK r32, m32 NF
BLSMSK_GPR32i32_MEMi32_APX_N3 | EVEX.LLZ.NP.0F38.W0 F3 /2 NF=1 | APX_F + APX_NCI_NDD_NF + BMI1 |
BLSMSK r64, r64
BLSMSK_GPR64i64_GPR64i64_APX | EVEX.LLZ.NP.0F38.W1 F3 /2 | APX_F + BMI1 |
BLSMSK r64, m64
BLSMSK_GPR64i64_MEMi64_APX | EVEX.LLZ.NP.0F38.W1 F3 /2 | APX_F + BMI1 |
BLSMSK r64, r64 NF
BLSMSK_GPR64i64_GPR64i64_APX_N3 | EVEX.LLZ.NP.0F38.W1 F3 /2 NF=1 | APX_F + APX_NCI_NDD_NF + BMI1 |
BLSMSK r64, m64 NF
BLSMSK_GPR64i64_MEMi64_APX_N3 | EVEX.LLZ.NP.0F38.W1 F3 /2 NF=1 | APX_F + APX_NCI_NDD_NF + BMI1 |
Latency and throughput
| Model | r32, m32r64, m64 | r32, r32r64, r64 |
|---|---|---|
| Haswell, Broadwell, Skylake, Skylake-SP, Ice Lake | 6 / 0.5p15 | 1 / 0.5p15 |
| Alder Lake, Sapphire Rapids | 7 / 0.33p1 p5 p11 | 2 / 0.33p1 p5 p11 |
| Lunar Lake | 5 / 1.33p135 | 1 / 0.33p135 |
| Zen, Zen 3 | 6 / 0.5ALU0 ALU1 ALU2 ALU3 | 2 / 0.5ALU0 ALU1 ALU2 ALU3 |
| Zen 4 | 5 / 0.33ALU0 ALU1 ALU2 ALU3 | 1 / 0.25ALU0 ALU1 ALU2 ALU3 |
| Jaguar | 5 / 1ALU0 ALU1 | 2 / 1ALU0 ALU1 |
Operation
What the forms do, in pseudocode that our tests run against results recorded on real processors. How to read.
s = src.read()
r = low(s ^ (s - 1)) # ones up to the lowest 1 bit; all ones
# for a zero source
if not nf: # NF=1 (APX) leaves the flags as they were
cf = s == 0
sf = msb(r)
zf = 0 # the result is never zero
of = 0
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.
Operands
| Operand | Kind | Action |
|---|---|---|
BLSMSK r32, r/m32 BLSMSK r64, r/m64 64-bit mode only | ||
r32/64 | explicit | write |
r/m32/64 | explicit | read |
RFLAGS | implicit | write |
BLSMSK r32, r/m32 NF BLSMSK r64, r/m64 NF | ||
r32/64 | explicit | write |
r/m32/64 | explicit | read |
Flags
| OF | SF | ZF | AF | PF | CF |
|---|---|---|---|---|---|
| NF forms | |||||
| Not affected | |||||
| Other forms | |||||
| 0 | M | 0 | 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 BLSMSK, 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 |
|---|---|
__blsmsk_u32
unsigned int __blsmsk_u32(unsigned int __X) |
|
__blsmsk_u64
unsigned long long __blsmsk_u64(unsigned long long __X) |
|
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 |
More BMI1 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-88 (BLSMSK)
- 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)
- 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.