Extensions
Mnemonics
CMPS, CMPSB, CMPSW, CMPSD, CMPSQ
Forms
12 (12 Legacy)
Data
Intel XED v2026.08.23

CMPS compares the element at [RSI] with the one at [RDI], setting the flags as a CMP of the first with the second would, and moves both pointers to the next element. With a REPE prefix it walks two buffers until they differ, the core of memcmp; with REPNE it walks them until a pair is equal. The doubleword form shares its name with SSE2’s CMPSD, a comparison of doubles.

REPE CMPS as memcmp

With the buffers in RSI and RDI, the length in RCX and the direction flag clear, repe cmpsb stops after the first pair of bytes that differ, or when RCX reaches 0. Afterwards:

  • ZF set means every byte compared was equal.
  • ZF clear means the bytes at RSI - 1 and RDI - 1 differ, and CF gives their order as unsigned numbers, as memcmp needs: JB when the byte of the first buffer is the smaller.
  • RCX holds the number of bytes left uncompared.

The trap is a length of 0. The instruction then compares nothing and changes no flag, so ZF still holds whatever an earlier instruction left there; the code must test RCX first, or set ZF itself, for instance with cmp rcx, rcx.

Speed

Intel’s optimization manual advises against REP CMPS and REP SCAS for performance and points to SIMD code instead (Section 3.7.5). Its processors from Golden Cove on report faster REPE and REPNE CMPSB and SCASB, for 1 to 128 bytes held in the first-level cache, in CPUID.(EAX=7,ECX=1):EAX[12], and the manual gives them a flat 15 cycles in that range (Section 3.8.3). The instruction table of AMD’s Zen 5 guide lists CMPSB as microcoded.

GCC 14 and Clang 23 at -O2 call memcmp for comparisons of unknown length, and inline those of a small known size without CMPS: memcmp(a, b, 16) == 0 becomes two quadword XORs and an OR in GCC, and PCMPEQB with PMOVMSKB in Clang.

A fault in the middle of a REPE or REPNE CMPS, such as a page fault on a later element, leaves RSI, RDI and RCX at the element that faulted, and the flags as they were before the instruction, so it resumes cleanly; see MOVS for the direction flag and the 67 prefix.

In the optimization guide

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

Memory

  • REP MOVS and STOS: Intel’s cores made rep movsb and rep stosb fast from Ivy Bridge, and short strings fast from Ice Lake and Golden Cove, as CPUID reports.

Golden Cove and Raptor Cove

  • Loads, stores and caches: Short rep movsb, rep stosb, rep cmpsb and rep scasb are fast: with the data in the L1 cache, any length up to 128 bytes takes a flat 9, 12, 15 and 15 cycles, zero included except for the compares.

Forms

Every encoding of CMPS that Intel XED knows, one row per XED instruction form (iform). How to read the notation.
InstructionEncodingRequires
CMPSB CMPSBA6Base instruction set
CMPSW CMPSW66 A7Base instruction set
CMPSD CMPSDA7Base instruction set
CMPSQ 64-bit mode only CMPSQREX.W A7LM
REPE CMPSB REPE_CMPSBF3 A6Base instruction set
REPNE CMPSB REPNE_CMPSBF2 A6Base instruction set
REPE CMPSW REPE_CMPSW66 F3 A7Base instruction set
REPNE CMPSW REPNE_CMPSW66 F2 A7Base instruction set
REPE CMPSD REPE_CMPSDF3 A7Base instruction set
REPNE CMPSD REPNE_CMPSDF2 A7Base instruction set
REPE CMPSQ 64-bit mode only REPE_CMPSQF3 REX.W A7LM
REPNE CMPSQ 64-bit mode only REPNE_CMPSQF2 REX.W A7LM

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.
ModelCMPSB
CMPSW
CMPSD
CMPSQ
CMPSB
CMPSB
CMPSW
CMPSW
CMPSD
CMPSD
CMPSQ
CMPSQ
Sandy Bridge8 / 1p0 p1 p5 p23
Haswell4 / 1p012356
Broadwell, Skylake, Skylake-SP, Ice Lake100 / 0.25p0156
Alder Lake, Sapphire Rapids6 / 1p0 p1 p2 p3 p5 p6 p10 p11
Lunar Lake6 / 0.83p0 p1 p2 p3 p4 p5 p20 p21 p22
Bonnell3 / 1.5p01
Silvermont100 / 1FPC0
Piledriver100 / 0.5EX0 EX1
Zen, Zen 2100 / 0.25
Zen 3, Zen 4100 / 25ALU0 ALU1 ALU2 ALU3
Jaguar100 / 0.5ALU0 ALU1

Operation

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

All forms but the REP forms
step = -(OSIZE // 8) if df else OSIZE // 8
a = load(aSI, OSIZE)
b = load(aDI, OSIZE)
flags = sub_flags(a, b)          # [rSI] - [rDI]
aSI = low(aSI + step, ASIZE)
aDI = low(aDI + step, ASIZE)

Run on AMD Zen 5 (EPYC 9645): 16 cases.

REPE CMPSB REPE CMPSW REPE CMPSD REPE CMPSQ 64-bit mode only
step = -(OSIZE // 8) if df else OSIZE // 8
s = aSI
d = aDI
c = aCX
for i in range(aCX):             # no flag changes when aCX is 0
    a = load(s, OSIZE)
    b = load(d, OSIZE)
    flags = sub_flags(a, b)
    s = low(s + step, ASIZE)
    d = low(d + step, ASIZE)
    c -= 1
    if a != b:
        break                    # ZF clear: the first difference
aSI = s
aDI = d
aCX = c

Run on AMD Zen 5 (EPYC 9645): 24 cases.

REPNE CMPSB REPNE CMPSW REPNE CMPSD REPNE CMPSQ 64-bit mode only
step = -(OSIZE // 8) if df else OSIZE // 8
s = aSI
d = aDI
c = aCX
for i in range(aCX):
    a = load(s, OSIZE)
    b = load(d, OSIZE)
    flags = sub_flags(a, b)
    s = low(s + step, ASIZE)
    d = low(d + step, ASIZE)
    c -= 1
    if a == b:
        break                    # ZF set: the first match
aSI = s
aDI = d
aCX = c

Run on AMD Zen 5 (EPYC 9645): 24 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
CMPSB CMPSW CMPSD CMPSQ 64-bit mode only
m8/16/32/64 at [rSI]implicitread
rSIimplicitread, write
m8/16/32/64 at ES:[rDI]implicitread
rDIimplicitread, write
RFLAGSimplicitread, write
REPE CMPSB REPNE CMPSB REPE CMPSW REPNE CMPSW REPE CMPSD REPNE CMPSD REPE CMPSQ 64-bit mode only REPNE CMPSQ 64-bit mode only
m8/16/32/64 at [rSI]implicitconditional read
rSIimplicitread, conditional write
m8/16/32/64 at ES:[rDI]implicitconditional read
rDIimplicitread, conditional write
rCXimplicitread, conditional write
RFLAGSimplicitread, conditional write

Flags

T tested, M modified; blank: not affected. How to read.
WhenOFSFZFAFPFCFDF
REP forms
in some casesMMTMMMMT
Other forms
MMMMMMT

Attributes

What else XED records about the forms. How to read.

REP forms
repeated: count in rCX

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
I86

No CPUID bit: part of the base instruction set.

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

No CPUID bit: part of the base instruction set.

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

LM
CPUID.80000001H:EDX[29] LM

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. LLVM 23.1.2's x86 scheduling models (commit 85ac56026243): the latency, reciprocal throughput and ports of each form, as llvm-mca reports them.
  4. Intel 64 and IA-32 Architectures Software Developer's Manual, Volume 2 (325383-093, September 2026): Volume 2A, page 3-181 (CMPS/CMPSB/CMPSW/CMPSD/CMPSQ)
  5. AMD64 Architecture Programmer's Manual, Volume 3: General-Purpose and System Instructions (24594, rev. 3.39, October 2026)
  6. Intel 64 and IA-32 Architectures Optimization Reference Manual, Volume 1 (248966-051, 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 timings come from LLVM's scheduling models, part of the LLVM Project, licensed under the Apache License 2.0 with LLVM Exceptions. The text is our own.