Instruction
CLD
Clears the direction flag (DF), so that string instructions step upward through memory; the System V and Microsoft ABIs require it clear at calls.
- Extensions
- Forms
- 1 (1 Legacy)
- Data
- Intel XED v2026.08.23
CLD clears the direction flag, DF, so that the string instructions, MOVS, STOS, LODS, CMPS, SCAS, INS and OUTS, step RSI and RDI up after each element. It changes no other flag. STD sets DF. Zen 5 cleared bit 10 in its two recorded cases that had it set and changed nothing in the four that did not.
When it is needed
The System V and Microsoft x64 calling conventions require DF clear at every call and return (see the registers), so ordinary code never needs CLD: GCC 14 at -O2 copies and clears a 512-byte struct with REP MOVS and REP STOS of quadwords and no CLD. CLD belongs where that guarantee does not hold yet or was broken:
- after a sequence that ran STD for a backward copy, before the code calls or returns;
- at entry points that the convention does not cover, such as a handler entered through an interrupt or trap gate: delivery clears TF, NT, RF and VM, and IF for an interrupt gate, but leaves DF as the interrupted code had it (Intel’s Volume 3, Section 7.12.1.3);
- in inline assembly that wants to be safe whatever its caller did:
glibc’s
<sys/io.h>puts a CLD before its REP INS and REP OUTS, soinsw(port, buf, n)compiles tocldandrep inswin both compilers.
GCC 14 also has the option -mcld, which makes it emit CLD in the functions where it generates string instructions: with it, the struct copy above becomes a CLD followed by the REP MOVS. Clang 23 does not accept the option.
Cost
The instruction table of AMD’s Zen 5 guide lists CLD as microcoded, as it does STD, while CLC and STC are single macro-ops. A CLD that is not needed is thus not free on Zen 5.
Forms
| Instruction | Encoding | Requires |
|---|---|---|
CLD
CLD | FC | Base instruction set |
Latency and throughput
| Model | CLD |
|---|---|
| Sandy Bridge | 1 / 0.33p015 |
| Haswell, Broadwell, Skylake, Skylake-SP, Ice Lake | 3 / 1p0156 |
| Alder Lake, Sapphire Rapids | 3 / 0.5p0 p1 p5 p6 p11 |
| Lunar Lake | 3 / 0.33p012345 |
| Bonnell | 3 / 1.5p01 |
| Silvermont | 1 / 0.5IEC0 IEC1 |
| Piledriver | 1 / 1EX0 EX1 |
| Zen, Zen 2, Zen 3, Zen 4 | 1 / 0.25ALU0 ALU1 ALU2 ALU3 |
| Jaguar | 1 / 0.5ALU0 ALU1 |
Operation
What the forms do, in pseudocode that our tests run against results recorded on real processors. How to read.
df = 0 # string instructions step rSI and rDI up
Run on AMD Zen 5 (EPYC 9645): 6 cases.
Operands
| Operand | Kind | Action |
|---|---|---|
CLD | ||
RFLAGS | implicit | write |
Flags
| DF |
|---|
| All forms |
| 0 |
Attributes
What else XED records about the forms. How to read.
- All forms
- may abort a TSX transaction in some cases
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-141 (CLD)
- Intel 64 and IA-32 Architectures Software Developer's Manual, Volume 3 (325384-093, September 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 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.