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

XCHG swaps two operands. Between registers that is all it does. With a memory operand it is also an atomic instruction: the processor locks the location for the read and the write as if a LOCK prefix were there, whether or not it is, so no other processor can change the value in between.

Uses of the lock

  • Atomic exchange. C11’s atomic_exchange on a long compiles to a MOV of the new value into RAX and xchg qword ptr [rip + g], rax, with GCC 14 and Clang 23 at -O2.
  • Sequentially consistent stores. A locked instruction is never reordered with loads or stores before or after it, while plain MOVs let a load complete before an earlier store. Both compilers therefore compile a sequentially consistent atomic_store to XCHG and drop the old value (see MOV).
  • Test-and-set locks. mov eax, 1 followed by xchg eax, [lock] takes a lock word and leaves in EAX whether it was already taken; test eax, eax and a branch retry while it was, with PAUSE in the waiting loop. Releasing such a lock is a plain store of 0.

XCHG always writes, and returns only the old value. An update that depends on that value, such as “store x only if the location still holds y”, needs CMPXCHG with LOCK; an atomic addition is XADD.

Split locks

The lock covers any operand, aligned or not, but one that spans two cache lines cannot be locked within a cache: Intel’s and AMD’s manuals both make it a bus lock, which involves every processor of the system. Where the processor supports split-lock disable, system software can make such an access raise #AC(0) instead (Intel’s Volume 3A, Section 11.1.2.3).

The one-byte forms

90+r swaps rAX with another register in one byte. The slot for rAX itself, 90, is NOP, and F3 90 is PAUSE; the Operation section below shows which register forms clear bits 63:32.

In the optimization guide

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

Out of order

  • Move elimination: A register-to-register copy can be made at renaming, by pointing the destination at the physical register of the source; the move then has no latency and uses no execution unit.

Zen and Zen+

  • Integer execution: Register-to-register moves of 32-bit and 64-bit general registers and of XMM registers, and xchg of general registers, take no execution cycle, while a YMM move renames only its lower half and moves the upper one in a pipe. sources differ

Zen 2

  • Integer execution: Register-to-register moves of general registers of 32 and 64 bits, xchg of them and moves of whole XMM and YMM registers take no execution cycle, where Zen 1 moves the upper half of a YMM register in a pipe. sources differ

Zen 3

  • Integer execution: Register-to-register moves of general registers of 32 and 64 bits, xchg of them and moves of whole XMM and YMM registers take no execution cycle, nor do the zeroing idioms such as xor eax, eax and vpxor, which also break the dependency on the old value; sbb eax, eax depends only on the carry flag, and pcmpeqd xmm0, xmm0 sets all ones without waiting. sources differ

Zen 4

  • Integer execution: Register-to-register moves of general registers of 32 and 64 bits, xchg of them and moves of whole vector registers take no execution cycle, nor do the zeroing idioms such as xor eax, eax and vpxor, which also break the dependency on the old value; sbb eax, eax depends only on the carry flag. sources differ

Forms

Every encoding of XCHG that Intel XED knows, one row per XED instruction form (iform). How to read the notation.
InstructionEncodingRequires
XCHG m8, r8 XCHG_MEMb_GPR886 /rBase instruction set
XCHG r8, r8 XCHG_GPR8_GPR886 /rBase instruction set
XCHG m16/32/64, r16/32/64 XCHG_MEMv_GPRv87 /rBase instruction set
XCHG r16/32/64, r16/32/64 XCHG_GPRv_GPRv87 /rBase instruction set
XCHG r16/32/64, rAX XCHG_GPRv_OrAX90+rBase 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.
Modelm8, r8r8, r8m16, r16m32, r32m64, r64r16, r16
r16, rAX
r32, r32
r64, r64
r32, rAX
r64, rAX
Sandy Bridge6 / 1p0152 / 1p0156 / 1p0156 / 1p0156 / 1p0152 / 1p0152 / 1p015
Haswell9 / 1p01562 / 0.75p01569 / 1p01569 / 1p01569 / 1p01562 / 0.75p01562 / 0.75p0156
Broadwell8 / 1p01562 / 0.75p01568 / 1p01568 / 1p01568 / 1p01562 / 0.75p01562 / 0.75p0156
Skylake, Skylake-SP, Ice Lake10 / 1.25p01562 / 0.75p015610 / 1.25p015610 / 1.25p015610 / 1.25p01562 / 0.75p01562 / 0.75p0156
Alder Lake, Sapphire Rapids40 / 1p0 p1 p5 p6 p112 / 0.6p0 p1 p5 p6 p1139 / 1p0 p1 p5 p6 p1139 / 1p0 p1 p5 p6 p1139 / 1.2p0 p1 p5 p6 p112 / 0.6p0 p1 p5 p6 p112 / 0.6p0 p1 p5 p6 p11
Lunar Lake40 / 0.83p0123452 / 0.5p01234539 / 0.83p01234539 / 0.83p01234539 / 1p0123452 / 0.5p0123452 / 0.5p012345
Bonnell3 / 1.5p12 / 1p013 / 1.5p13 / 1.5p13 / 1.5p12 / 1p012 / 1p01
Silvermont4 / 2IEC0 IEC1 MEC3 / 1.5IEC0 IEC14 / 2IEC0 IEC1 MEC4 / 2IEC0 IEC1 MEC4 / 2IEC0 IEC1 MEC3 / 1.5IEC0 IEC13 / 1.5IEC0 IEC1
Piledriver5 / 2EX0 EX11 / 1EX15 / 2EX0 EX15 / 2EX0 EX15 / 2EX0 EX11 / 1EX11 / 1EX1
Zen5 / 0.5ALU0 ALU1 ALU2 ALU31 / 0.5ALU0 ALU1 ALU2 ALU35 / 0.5ALU0 ALU1 ALU2 ALU35 / 0.5ALU0 ALU1 ALU2 ALU35 / 0.5ALU0 ALU1 ALU2 ALU31 / 0.5ALU0 ALU1 ALU2 ALU31 / 0.5ALU0 ALU1 ALU2 ALU3
Zen 25 / 0.33ALU0 ALU1 ALU2 ALU31 / 0.25ALU0 ALU1 ALU2 ALU35 / 0.33ALU0 ALU1 ALU2 ALU35 / 0.33ALU0 ALU1 ALU2 ALU35 / 0.33ALU0 ALU1 ALU2 ALU31 / 0.25ALU0 ALU1 ALU2 ALU31 / 0.25ALU0 ALU1 ALU2 ALU3
Zen 3, Zen 47 / 0.5ALU0 ALU1 ALU2 ALU31 / 0.5ALU0 ALU1 ALU2 ALU37 / 0.5ALU0 ALU1 ALU2 ALU36 / 0.5ALU0 ALU1 ALU2 ALU36 / 0.5ALU0 ALU1 ALU2 ALU31 / 0.5ALU0 ALU1 ALU2 ALU30 / 0.33
Jaguar16 / 16ALU0 ALU12 / 1.5ALU0 ALU116 / 16ALU0 ALU116 / 16ALU0 ALU116 / 16ALU0 ALU11 / 1ALU0 ALU11 / 1ALU0 ALU1

Operation

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

a = dest.read()                  # memory: one atomic read and write, with
b = src.read()                   # or without LOCK
dest.write(b)                    # 32-bit registers: bits 63:32 cleared in
src.write(a)                     # both, even for XCHG EAX, EAX (87 C0)
# the byte 90, which would be XCHG EAX, EAX, is NOP instead: it clears
# nothing (XCHG R8D, EAX, 41 90, does)

Run on AMD Zen 5 (EPYC 9645): 67 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
XCHG r/m8, r8 XCHG r/m16/32/64, r16/32/64
r/m8/16/32/64explicitread, write
r8/16/32/64explicitread, write
XCHG r16/32/64, rAX
r16/32/64explicitread, write
rAXexplicitread, write

Flags

None: XED records no flag that the forms test or change.

Attributes

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

Memory 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.

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

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 2D, page 6-32 (XCHG)
  5. Intel 64 and IA-32 Architectures Software Developer's Manual, Volume 3 (325384-093, September 2026)
  6. AMD64 Architecture Programmer's Manual, Volume 2: System Programming (24593, rev. 3.46, October 2026)
  7. 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.