Extensions
Forms
2 (2 Legacy)
Data
Intel XED v2026.08.23

BSF finds the lowest set bit of its source and writes that bit’s index to the destination: for 0x28 it gives 3. ZF says whether there was a bit to find. For a nonzero source BSF clears ZF; for zero it sets ZF and does not write the destination at all, and the other flags are undefined. BSR is the scan from the other end.

Testing ZF

Code that may see a zero source checks ZF right after the scan instead of testing the source first. __builtin_ffs(x), which returns the index plus 1, or 0 for no bit, shows how. GCC 14 at -O2 compiles it to

  • bsf eax, edi,
  • mov edx, -1 and cmove eax, edx, taking -1 when ZF is set,
  • add eax, 1;

Clang 23 does the same with CMOVNE the other way round.

For __builtin_ctz, which C leaves undefined for 0, both compilers emit rep bsf instead, which runs as TZCNT where BMI1 exists and as BSF elsewhere; the TZCNT page explains where the two differ.

The unchanged destination

Both vendors’ manuals now say that a zero source leaves the destination alone, so a register loaded beforehand acts as the result for zero: mov eax, 32 followed by bsf eax, edi gives 32 for x = 0. Intel’s manual adds that some older processors clear bits 63:32 of a 64-bit register when a 32-bit BSF finds no bit. Zen 5 and the Intel processor under KVM that recorded this page kept all 64 bits in every such case. Code meant for any x86-64 processor should still load the fallback value into the full register, or use TZCNT, which returns the operand size for zero.

Walking the set bits

A loop that visits the set bits of a mask takes the lowest with BSF or TZCNT, handles it, and clears it with BLSR or with m & (m - 1), until the mask is 0; since the loop condition already excludes zero, the scan never meets an empty source. The instruction table of AMD’s Zen 5 guide lists BSF, like TZCNT, as one macro-op with a latency of one cycle.

In the optimization guide

What the optimization guide says about BSF, a line per claim. Each links to the claim in the guide, with its sources and where they differ.

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, bsr and 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

Every encoding of BSF that Intel XED knows, one row per XED instruction form (iform). How to read the notation.
InstructionEncodingRequires
BSF r16/32/64, m16/32/64 BSF_GPRv_MEMv0F BC /r
F3 0F BC /r (without BMI1)
Base instruction set
BSF r16/32/64, r16/32/64 BSF_GPRv_GPRv0F BC /r
F3 0F BC /r (without BMI1)
Base instruction set

Latency and throughput

What the scheduling models of LLVM 23.1.2 give for each form, not measurements: latency / reciprocal throughput in cycles, the ports under them. A row is a model, or models that agree; a dash, a form that its processors lack. Memory forms: latency from the address, ports without the load and store units. Which processors each model covers.
Modelr16, m16
r32, m32
r64, m64
r16, r16
r32, r32
r64, r64
Sandy Bridge, Haswell, Broadwell, Skylake, Skylake-SP, Ice Lake, Alder Lake, Sapphire Rapids8 / 1p13 / 1p1
Lunar Lake7 / 0.33p1353 / 0.33p135
Bonnell16 / 16p116 / 16p01
Silvermont13 / 10IEC0 IEC1 MEC10 / 10IEC0 IEC1
Piledriver7 / 3EX0 EX13 / 3EX0 EX1
Zen, Zen 27 / 3ALU0 ALU1 ALU2 ALU33 / 3ALU0 ALU1 ALU2 ALU3
Zen 37 / 3ALU13 / 3ALU1
Zen 45 / 1ALU11 / 11ALU1
Jaguar7 / 4ALU0 ALU14 / 4ALU0 ALU1
  1. Zen 4 model: AMD's spreadsheets give reciprocal throughput 0.25 for Zen 4 (AMD 57647, row 55) and 0.33 for Zen 5 (AMD 58455, row 1978).

Operation

What the forms do, in pseudocode that our tests run against results recorded on real processors. How to read.

s = src.read()
if s == 0:
    zf = 1
    # dest is not written, as both manuals say; Intel's adds
    # that some older processors clear bits 63:32 of it at a
    # 32-bit operand size
else:
    zf = 0
    for i in range(OSIZE):
        if s >> i & 1:
            dest.write(i)        # the number of the lowest 1 bit
            break
of = sf = af = pf = cf = UNDEFINED

Run on AMD Zen 5 (EPYC 9645) and an Intel processor under KVM (model hidden): 138 cases.

Operands

What each form reads and writes, as Intel XED records it: the operands of its syntax, in order, and the implicit ones that the syntax does not name. How to read.
OperandKindAction
BSF r16/32/64, r/m16/32/64
r16/32/64explicitconditional write
r/m16/32/64explicitread
RFLAGSimplicitwrite

Flags

M modified, U undefined; blank: not affected. How to read.
OFSFZFAFPFCF
All forms
UUMUUU

Intrinsics

The C intrinsics of Clang 23.1.2 that compile to BSF, 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.

IntrinsicCompiles to
_bit_scan_forward int _bit_scan_forward(int A)

BSF r16/32/64, r16/32/64 Legacy

__bsfd int __bsfd(int __A)

BSF r16/32/64, r16/32/64 Legacy

__bsfq int __bsfq(long long __A)

BSF r16/32/64, r16/32/64 Legacy

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.

CPUID requirements and processors of the XED ISA sets above. Alternatives are separated by “or”; processors follow XED’s chip model, whose AMD processors end with Zen 2, and the CPU data of this site for Zen 3 to Zen 6.
ISA setCPUIDProcessors
I386

No CPUID bit: part of the base instruction set.

Every x86-64 processor in XED’s model and Zen 3 to Zen 6

More General-purpose instructions

All 109 General-purpose instructions

Sources

  1. Intel XED v2026.08.23 (commit 0bcb6237345c): forms, encodings, operands, flags, exception classes, attributes, ISA sets, CPUID bits and chip model.
  2. The CPU data of x86-64.net: Zen 3 to Zen 6, from GCC’s -march feature sets and AMD’s Processor Programming References, which the page of each processor cites.
  3. 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.
  4. LLVM 23.1.2's x86 scheduling models (commit 85ac56026243): the latency, reciprocal throughput and ports of each form, as llvm-mca reports them.
  5. Intel 64 and IA-32 Architectures Software Developer's Manual, Volume 2 (325383-093, September 2026): Volume 2A, page 3-107 (BSF)
  6. AMD64 Architecture Programmer's Manual, Volume 3: General-Purpose and System Instructions (24594, rev. 3.39, October 2026)
  7. 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.