Instruction
CMP
Compares two operands by subtracting the second from the first, setting the arithmetic flags as SUB would and discarding the result.
- Extensions
- Forms
- 18 (18 Legacy)
- Data
- Intel XED v2026.08.23
CMP a, b computes a - b, sets the flags as SUB would and throws the difference away. Its meaning comes from the instruction that reads the flags next, a Jcc, SETcc or CMOVcc, whose condition code says both which relation is tested and whether a and b are signed:
| Relation | Unsigned a and b | Signed a and b |
|---|---|---|
| a == b | E | E |
| a != b | NE | NE |
| a < b | B | L |
| a <= b | BE | LE |
| a > b | A | G |
| a >= b | AE | GE |
The same flags serve both columns. CF is the borrow out of the subtraction, so B (CF = 1) holds exactly when a < b as unsigned numbers. For signed numbers the sign of the difference is right unless the subtraction overflowed, and L tests SF XOR OF to correct for that. The manuals keep the words apart in the same way: “below” and “above” for unsigned, “less” and “greater” for signed.
Operand order
In Intel syntax the first operand is the minuend: cmp rdi, rsi then
jl jumps when RDI < RSI. AT&T syntax writes the operands the other
way round, so the same instruction is cmpq %rsi, %rdi followed by
jl, which is what GCC 14 at -O2 prints for if (a < b) with a in
RDI. Reading cmp in AT&T as “compare the first with the second” gets
every condition but E and NE backwards.
Constants
An immediate is 8 or 32 bits, sign-extended to the operand size; no
form takes a 64-bit immediate. Comparing a 64-bit register with
0x123456789 therefore takes two instructions in both GCC 14 and
Clang 23: movabs rax, 0x123456789 and cmp rdi, rax. A comparison
with zero needs no immediate at all: compilers use
TEST with the register twice, which is shorter and sets
every condition the same way.
APX adds CCMPscc, which compares only if an earlier
condition holds, so that a == 1 && b > c needs no branch between its
terms.
In the optimization guide
What the optimization guide says about CMP, a line per claim. Each links to the claim in the guide, with its sources and where they differ.
Front end
- Macro-fusion: Intel’s cores since Sandy Bridge (Skylake) fuse
testandandwith any conditional branch;cmp,addandsubwith branches on the equal, unsigned and signed comparisons, but not on the sign, parity or overflow flag alone; andincanddeconly with branches on equality and the signed comparisons. - Macro-fusion: AMD’s Zen 3 to Zen 5 fuse more:
cmp,test,add,sub,and,or,xor,incanddecwith any conditional branch, except that on Zen 3 and Zen 4incanddecdo not fuse with a branch that reads the carry flag, which Intel’s cores do not fuse either (Zen 5, Zen 4, Zen 3). sources differ - Macro-fusion: With a memory operand, Intel’s Sandy Bridge to Skylake do not fuse an instruction that also has an immediate, while AMD’s Zen 3 and Zen 4 still fuse
cmpandtestunless a displacement comes with the immediate or the address is relative to RIP. sources differ - Macro-fusion: On Intel’s cores the choice of instruction decides fusion more often, so Intel prefers
testtocmpfor a comparison with zero, astestfuses with every condition.
Out of order
- Zeroing and dependency-breaking idioms: AMD’s cores recognize two more:
cmp eax, eax, which takes no cycle, andsbb eax, eax, which turns the carry flag into 0 or all ones in one cycle without waiting for EAX.
Bulldozer to Excavator
- Branches: A
cmportestdirectly followed by a conditional branch fuses into one op, unless the compare uses a RIP-relative address or has a displacement as well as an immediate; no other instruction fuses. sources differ
Haswell and Broadwell
- Front end: CMP, ADD and SUB fuse with a following conditional jump that tests carry, zero, or a signed or unsigned comparison; INC and DEC only with one that tests zero or a signed comparison; TEST and AND with any.
Ice Lake to Rocket Lake
- Front end: A
cmp,add,sub,incordecfuses with a following jump on zero, carry or an unsigned or signed comparison, andtestorandwith any conditional jump;or,xor,adc,sbb, shifts andjs,joandjpnever fuse.
Sandy Bridge and Ivy Bridge
- Front end: Sandy Bridge widens macro-fusion from Nehalem’s CMP and TEST to also fuse ADD, SUB, AND, INC and DEC with a following conditional branch, fusing when the first instruction’s first operand is a register and its second operand, if any, is an immediate, a register or memory that is not RIP-relative; the pair does not fuse only when the first instruction ends on the last byte of a 64-byte line and the branch begins the next line’s first byte.
Skylake to Cooper Lake
- Front end:
testandandfuse with any following conditional jump;cmp,addandsubwith the jumps on equality and on the unsigned and signed comparisons, but not withjs,jporjoand their inverses;incanddeconly with those on equality and the signed comparisons.
Zen and Zen+
- Integer execution: Only a
cmportestdirectly followed by a conditional branch fuses into one macro-op, and not when it has a displacement together with an immediate, or an address relative to RIP; no other arithmetic or logic instruction fuses.
Zen 2
- Integer execution: Only a
cmportestfollowed by a conditional branch fuses into one macro-op, not one with a displacement together with an immediate or an address relative to RIP; the arithmetic and logic instructions that fuse on Zen 3 do not.
Zen 3
- Integer execution: A
cmp,test,add,sub,inc,dec,and,ororxorfollowed by a conditional branch becomes one macro-op, also with a memory operand, unless it carries a displacement together with an immediate, addresses relative to RIP or makes the pair longer than 15 bytes;incanddecdo not fuse with a branch on the carry flag, andjrcxznever fuses.
Zen 4
- Integer execution: A
cmp,test,add,sub,inc,dec,and,ororxorfollowed by a conditional branch becomes one macro-op, also with a memory operand, unless it carries a displacement together with an immediate, addresses relative to RIP or makes the pair longer than 15 bytes;incanddecdo not fuse with a branch on the carry flag.
Zen 5
- Integer execution: A
cmp,test,add,sub,inc,dec,and,ororxorfollowed by a conditional branch becomes one macro-op, also with a memory operand, within limits on the sizes of its displacement and immediate.
Forms
| Instruction | Encoding | Requires |
|---|---|---|
CMP m8, imm8
CMP_MEMb_IMMb_80r7 | 80 /7 ib | Base instruction set |
CMP r8, imm8
CMP_GPR8_IMMb_80r7 | 80 /7 ib | Base instruction set |
CMP m16/32/64, imm16/32
CMP_MEMv_IMMz | 81 /7 iw/id | Base instruction set |
CMP r16/32/64, imm16/32
CMP_GPRv_IMMz | 81 /7 iw/id | Base instruction set |
CMP m8, imm8 not in 64-bit mode
CMP_MEMb_IMMb_82r7 | 82 /7 ib | Base instruction set |
CMP r8, imm8 not in 64-bit mode
CMP_GPR8_IMMb_82r7 | 82 /7 ib | Base instruction set |
CMP m16/32/64, imm8
CMP_MEMv_IMMb | 83 /7 ib | Base instruction set |
CMP r16/32/64, imm8
CMP_GPRv_IMMb | 83 /7 ib | Base instruction set |
CMP m8, r8
CMP_MEMb_GPR8 | 38 /r | Base instruction set |
CMP r8, r8
CMP_GPR8_GPR8_38 | 38 /r | Base instruction set |
CMP m16/32/64, r16/32/64
CMP_MEMv_GPRv | 39 /r | Base instruction set |
CMP r16/32/64, r16/32/64
CMP_GPRv_GPRv_39 | 39 /r | Base instruction set |
CMP r8, m8
CMP_GPR8_MEMb | 3A /r | Base instruction set |
CMP r8, r8
CMP_GPR8_GPR8_3A | 3A /r | Base instruction set |
CMP r16/32/64, m16/32/64
CMP_GPRv_MEMv | 3B /r | Base instruction set |
CMP r16/32/64, r16/32/64
CMP_GPRv_GPRv_3B | 3B /r | Base instruction set |
CMP AL, imm8
CMP_AL_IMMb | 3C ib | Base instruction set |
CMP rAX, imm16/32
CMP_OrAX_IMMz | 3D iw/id | Base instruction set |
Latency and throughput
| Model | m8, imm8m16, imm16m32, imm32m64, imm32m16, imm8m32, imm8m64, imm8m8, r8m16, r16m32, r32m64, r64r8, m8r16, m16r32, m32r64, m64 | r8, imm8r16, imm16r32, imm32r64, imm32r16, imm8r32, imm8r64, imm8r8, r8r16, r16r32, r32r64, r64AL, imm8rAX, imm16rAX, imm32 |
|---|---|---|
| Sandy Bridge | 6 / 0.5p015 | 1 / 0.33p015 |
| Haswell, Broadwell, Skylake, Skylake-SP, Ice Lake | 6 / 0.5p0156 | 1 / 0.25p0156 |
| Alder Lake, Sapphire Rapids | 6 / 0.33p0 p1 p5 p6 p11 | 1 / 0.2p0 p1 p5 p6 p11 |
| Lunar Lake | 6 / 0.33p012345 | 1 / 0.33p135 |
| Bonnell | 1 / 1p0 | 1 / 0.5p01 |
| Silvermont | 4 / 1IEC0 IEC1 MEC | 1 / 0.5IEC0 IEC1 |
| Piledriver | 5 / 1.5EX0 EX1 | 1 / 1EX0 EX1 |
| Zen | 5 / 0.5ALU0 ALU1 ALU2 ALU3 | 1 / 0.25ALU0 ALU1 ALU2 ALU3 |
| Zen 2, Zen 3, Zen 4 | 5 / 0.33ALU0 ALU1 ALU2 ALU3 | 1 / 0.25ALU0 ALU1 ALU2 ALU3 |
| 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.
b = sext(src2) # an immediate is sign-extended to OSIZE
flags = sub_flags(src1, b) # as SUB, but nothing is written
Run on AMD Zen 5 (EPYC 9645): 244 cases. The forms that 64-bit mode lacks were not run: the probe runs in 64-bit mode.
Operands
| Operand | Kind | Action |
|---|---|---|
CMP r/m8, imm8 CMP r/m16/32/64, imm16/32 CMP r/m16/32/64, imm8 | ||
r/m8/16/32/64 | explicit | read |
imm8/16/32 | explicit | read |
RFLAGS | implicit | write |
CMP r/m8, r8 CMP r/m16/32/64, r16/32/64 | ||
r/m8/16/32/64 | explicit | read |
r8/16/32/64 | explicit | read |
RFLAGS | implicit | write |
CMP r8, r/m8 CMP r16/32/64, r/m16/32/64 | ||
r8/16/32/64 | explicit | read |
r/m8/16/32/64 | explicit | read |
RFLAGS | implicit | write |
CMP AL, imm8 CMP rAX, imm16/32 | ||
AL, rAX | explicit | read |
imm8/16/32 | explicit | read |
RFLAGS | implicit | write |
Flags
| OF | SF | ZF | AF | PF | CF |
|---|---|---|---|---|---|
| All forms | |||||
| M | M | M | M | M | M |
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 |
|---|---|---|
I86 | 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
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. - 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-161 (CMP)
- Intel 64 and IA-32 Architectures Software Developer's Manual, Volume 1 (253665-093, September 2026)
- AMD64 Architecture Programmer's Manual, Volume 3: General-Purpose and System Instructions (24594, rev. 3.39, October 2026)
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.