Instruction
DEC
Subtracts one from an operand without changing the carry flag; APX adds three-operand forms and forms that leave the flags unchanged.
- Extensions
- Forms
- 19 (7 Legacy, 12 EVEX)
- Data
- Intel XED v2026.08.23
DEC subtracts 1 from its operand and sets the flags as sub x, 1 would,
except that CF keeps its value, as after INC. ZF tells
when the operand reaches 0, and OF is set only when the smallest
negative number wraps to the largest positive one.
Counting down
DEC followed by JNZ is the classic loop counter: the loop runs until the
count reaches 0, with no separate comparison. For
do { f(n); } while (--n);, Clang 23 at -O2 counts with dec rbx and
jne, GCC 14 with sub rbx, 1 and jne; both set ZF the same way.
Because DEC leaves CF alone, the same loop can carry a borrow or carry
from one iteration to the next, as Intel’s Optimization Reference Manual
shows for a multi-word addition with ADC (Example 3-26).
LOOP decrements rCX and jumps in one instruction without
touching any flag, but the same manual advises against LOOP and other
instructions of more than four micro-ops (Coding Rule 28).
When to use SUB instead
Keeping CF is a partial write of the flags, which Intel’s manual counts as a cost:
- A shift by CL may leave the flags unchanged, so it must merge with the
flags written before it. In Section 3.5.1.5 the manual shows a loop
with
shl eax, clanddec edxin which, on Sandy Bridge, the shift runs its slower three-micro-op flow; replacing DEC with SUB, which writes all the flags, lets it run the two-micro-op one. - Intel’s Table 3-1 (Sandy Bridge) and Table 3-2 (Haswell) fuse DEC with a following jump only for the equal and signed conditions, not for those that read CF. AMD’s guide lists DEC among the instructions that fuse on Zen 5 (Section 2.9.3).
Reference counts
With LOCK and a memory operand, DEC decrements atomically and ZF says
whether the count reached 0, which is what releasing a reference needs.
For atomic_fetch_sub(p, 1) == 1, Clang emits
lock dec qword ptr [rdi] and sete al, GCC
lock sub qword ptr [rdi], 1 and the same SETE. As with INC, the
one-byte forms 48h to 4Fh are REX prefixes in 64-bit mode, so DEC is
encoded as FF /1 (FE /1 for bytes).
In the optimization guide
What the optimization guide says about DEC, 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
Out of order
- Partial register and flag writes: Some instructions write only part of the flags:
incanddecleave the carry flag as it was, andbtleaves the zero flag.
Silvermont to Goldmont Plus
- Out-of-order and in-order execution: A branch on the flags of inc or dec costs a cycle more than one on add, sub or cmp.
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.
Tremont
- Integer execution: LLVM prefers add and sub to inc and dec on Tremont, where a partial flags write costs a cycle.
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.
Forms
| Instruction | Encoding | Requires |
|---|---|---|
DEC m8
DEC_MEMb | FE /1 | Base instruction set |
DEC r8
DEC_GPR8 | FE /1 | Base instruction set |
DEC m16/32/64
DEC_MEMv | FF /1 | Base instruction set |
DEC r16/32/64
DEC_GPRv_FFr1 | FF /1 | Base instruction set |
DEC r16/32/64 not in 64-bit mode
DEC_GPRv_48 | 48+r | Base instruction set |
LOCK DEC m8
DEC_LOCK_MEMb | FE /1 | Base instruction set |
LOCK DEC m16/32/64
DEC_LOCK_MEMv | FF /1 | Base instruction set |
DEC r8
DEC_GPR8i8_APX | EVEX.LLZ.NP.MAP4.WIG FE /1 | APX_F |
DEC m8
DEC_MEMi8_APX | EVEX.LLZ.NP.MAP4.WIG FE /1 | APX_F |
DEC r8 NF
DEC_GPR8i8_APX_N3 | EVEX.LLZ.NP.MAP4.WIG FE /1 NF=1 | APX_F + APX_NCI_NDD_NF |
DEC m8 NF
DEC_MEMi8_APX_N3 | EVEX.LLZ.NP.MAP4.WIG FE /1 NF=1 | APX_F + APX_NCI_NDD_NF |
DEC r8, r8 NDD NF optional
DEC_GPR8i8_GPR8i8_APX_N3 | EVEX.LLZ.NP.MAP4.WIG FE /1 ND=1 NF=0/1 | APX_F + APX_NCI_NDD_NF |
DEC r8, m8 NDD NF optional
DEC_GPR8i8_MEMi8_APX_N3 | EVEX.LLZ.NP.MAP4.WIG FE /1 ND=1 NF=0/1 | APX_F + APX_NCI_NDD_NF |
DEC r16/32/64
DEC_GPRv_APX | EVEX.LLZ.NP.MAP4.SCALABLE FF /1 | APX_F |
DEC m16/32/64
DEC_MEMv_APX | EVEX.LLZ.NP.MAP4.SCALABLE FF /1 | APX_F |
DEC r16/32/64 NF
DEC_GPRv_APX_N3 | EVEX.LLZ.NP.MAP4.SCALABLE FF /1 NF=1 | APX_F + APX_NCI_NDD_NF |
DEC m16/32/64 NF
DEC_MEMv_APX_N3 | EVEX.LLZ.NP.MAP4.SCALABLE FF /1 NF=1 | APX_F + APX_NCI_NDD_NF |
DEC r16/32/64, r16/32/64 NDD NF optional
DEC_GPRv_GPRv_APX_N3 | EVEX.LLZ.NP.MAP4.SCALABLE FF /1 ND=1 NF=0/1 | APX_F + APX_NCI_NDD_NF |
DEC r16/32/64, m16/32/64 NDD NF optional
DEC_GPRv_MEMv_APX_N3 | EVEX.LLZ.NP.MAP4.SCALABLE FF /1 ND=1 NF=0/1 | APX_F + APX_NCI_NDD_NF |
Latency and throughput
| Model | m8 | r8r16r32 | m16m32m64 | r64 | DEC m8 | DEC m16DEC m32DEC m64 |
|---|---|---|---|---|---|---|
| Sandy Bridge | 7 / 1p015 | 1 / 0.33p015 | 7 / 1p015 | 1 / 0.33p015 | 7 / 1p015 | 7 / 1p015 |
| Haswell, Broadwell, Skylake, Skylake-SP | 7 / 1p0156 | 1 / 0.25p0156 | 7 / 1p0156 | 1 / 0.25p0156 | 7 / 1p0156 | 7 / 1p0156 |
| Ice Lake | 7 / 0.5p0156 | 1 / 0.25p0156 | 7 / 0.5p0156 | 1 / 0.25p0156 | 7 / 0.5p0156 | 7 / 0.5p0156 |
| Alder Lake, Sapphire Rapids | 13 / 0.5p0 p1 p5 p6 p11 | 1 / 0.2p0 p1 p5 p6 p11 | 12 / 0.5p0 p1 p5 p6 p11 | 1 / 0 | 13 / 0.5p0 p1 p5 p6 p11 | 12 / 0.5p0 p1 p5 p6 p11 |
| Lunar Lake | 13 / 0.5p012345 | 1 / 0.33p135 | 12 / 0.67p135 | 1 / 0 | 13 / 0.5p012345 | 12 / 0.67p135 |
| Bonnell | 1 / 1p0 | 1 / 0.5p01 | 1 / 1p0 | 1 / 0.5p01 | 1 / 1p0 | 1 / 1p0 |
| Silvermont | 5 / 2IEC0 IEC1 MEC | 1 / 0.5IEC0 IEC1 | 5 / 2IEC0 IEC1 MEC | 1 / 0.5IEC0 IEC1 | 5 / 2IEC0 IEC1 MEC | 5 / 2IEC0 IEC1 MEC |
| Piledriver | 6 / 2EX0 EX1 | 1 / 1EX0 EX1 | 6 / 2EX0 EX1 | 1 / 1EX0 EX1 | 6 / 2EX0 EX1 | 6 / 2EX0 EX1 |
| Zen | 5 / 0.5ALU0 ALU1 ALU2 ALU3 | 1 / 0.25ALU0 ALU1 ALU2 ALU3 | 5 / 0.5ALU0 ALU1 ALU2 ALU3 | 1 / 0.25ALU0 ALU1 ALU2 ALU3 | 5 / 0.5ALU0 ALU1 ALU2 ALU3 | 5 / 0.5ALU0 ALU1 ALU2 ALU3 |
| Zen 2 | 5 / 0.33ALU0 ALU1 ALU2 ALU3 | 1 / 0.25ALU0 ALU1 ALU2 ALU3 | 5 / 0.33ALU0 ALU1 ALU2 ALU3 | 1 / 0.25ALU0 ALU1 ALU2 ALU3 | 5 / 0.33ALU0 ALU1 ALU2 ALU3 | 5 / 0.33ALU0 ALU1 ALU2 ALU3 |
| Zen 3, Zen 4 | 6 / 0.67ALU0 ALU1 ALU2 ALU3 | 1 / 0.25ALU0 ALU1 ALU2 ALU3 | 6 / 0.67ALU0 ALU1 ALU2 ALU3 | 1 / 0.25ALU0 ALU1 ALU2 ALU3 | 6 / 0.67ALU0 ALU1 ALU2 ALU3 | 6 / 0.67ALU0 ALU1 ALU2 ALU3 |
| Jaguar | 6 / 1ALU0 ALU1 | 1 / 0.5ALU0 ALU1 | 6 / 1ALU0 ALU1 | 1 / 0.5ALU0 ALU1 | 19 / 19ALU0 ALU1 | 19 / 19ALU0 ALU1 |
Operation
What the forms do, in pseudocode that our tests run against results recorded on real processors. How to read.
a = dest.read() # with LOCK: one atomic read-modify-write
r = low(a - 1)
if not nf: # NF=1 (APX) leaves the flags as they were
of = a == 1 << (OSIZE - 1) # the smallest negative number wrapped
sf = msb(r)
zf = r == 0
af = (a & 0xF) == 0 # a borrow into bit 3
pf = parity(r)
# CF keeps its value, unlike after SUB dest, 1
dest.write(r) # a 32-bit register: bits 63:32 cleared
Run on AMD Zen 5 (EPYC 9645): 72 cases. The APX forms were not run: none of these processors has APX. The forms that 64-bit mode lacks were not run: the probe runs in 64-bit mode.
a = src.read()
r = low(a - 1)
if not nf: # NF=1 leaves the flags as they were
of = a == 1 << (OSIZE - 1) # the smallest negative number wrapped
sf = msb(r)
zf = r == 0
af = (a & 0xF) == 0 # a borrow into bit 3
pf = parity(r)
# CF keeps its value
dest.write(r) # the new destination: bits 63:OSIZE
# cleared, even for 8 and 16 bits
Not run on a processor: none of these processors has APX.
Operands
| Operand | Kind | Action |
|---|---|---|
DEC r/m8 DEC r/m16/32/64 LOCK DEC m8 LOCK DEC m16/32/64 | ||
r/m8/16/32/64 | explicit | read, write |
RFLAGS | implicit | write |
DEC r/m8 NF DEC r/m16/32/64 NF | ||
r/m8/16/32/64 | explicit | read, write |
DEC r8, r/m8 NDD NF optional DEC r16/32/64, r/m16/32/64 NDD NF optional | ||
r8/16/32/64 | explicit | write |
r/m8/16/32/64 | explicit | read |
RFLAGS | implicit | write (NF=0) |
Flags
| When | OF | SF | ZF | AF | PF |
|---|---|---|---|---|---|
| NF forms | |||||
| Not affected | |||||
| NDD forms | |||||
| NF=0 | M | M | M | M | M |
| Other forms | |||||
| M | M | M | M | M | |
Exceptions
The exception class is XED's name for the set of exception conditions that a form follows. How to read.
- Legacy forms
- none in XED
- EVEX forms
APX-EVEX-INT
Attributes
What else XED records about the forms. How to read.
- Legacy memory forms without LOCK
- accepts a LOCK prefix
- LOCK forms
- locked: atomic read-modify-write; accepts XACQUIRE; accepts XRELEASE
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 |
APX_F | APX_F | Nova Lake, Diamond Rapids |
APX_F_N3 | APX_F + APX_NCI_NDD_NF | Same as APX_F |
More General-purpose instructions
- BTR
- BTS
- CALL
- CBW
- CDQ
- CDQE
- CLC
- CLD
- CLI
- CMC
- CMP
- CMPS
- CMPXCHG
- CMPXCHG8B
- CPUID
- CQO
- CWD
- CWDE
- DAA
- DAS
- DIV
- ENTER
- IDIV
- IMUL
- IN
- INC
- INS
- INT
- INT1
- INT3
- INTO
- IRET
- Jcc
- JCXZ
- JECXZ
- JMP
- JRCXZ
- LDS
- LEA
- LEAVE
All 109 General-purpose 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. - 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-262 (DEC)
- 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 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.