swp

Swap Doubleword

SWP <Xs>, <Xt>, [<Xn|SP>]

Atomic swap of a doubleword.

Pseudocode Operation

address = Xn                        // or SP, which must be 16-byte aligned
old = atomic { load 64 bits at address; store Xs at address }
Xt = old

Example

SWP x6, x3, [x1]

Encoding

Binary Layout
11111000001
31:21
Rs
20:16
100000
15:10
Rn
9:5
Rt
4:0
 
Format Atomic
Opcode 0xF8208000
Extension LSE (Atomics)

Operands

  • Xs
    Register holding the doubleword to store
  • Xt
    Register that receives the doubleword loaded from memory
  • Xn
    Base register (or SP) holding the address

Related

Other forms of swp

  • swp Swap Word

Across architectures

Atomic Exchange : how x86, ARM, RISC-V, and PowerISA each do this.

More in LSE (Atomics)

Reference

Instruction Forms

Encoding Instruction ISA Bit pattern
0xB8208000 SWP <Ws>, <Wt>, [<Xn|SP>] A64 10 | 111 | 0 | 00 | 0 | 0 | 1 | Rs | 1 | 000 | 00 | Rn | Rt
0xF8208000 SWP <Xs>, <Xt>, [<Xn|SP>] A64 11 | 111 | 0 | 00 | 0 | 0 | 1 | Rs | 1 | 000 | 00 | Rn | Rt

Description

Swap word or doubleword in memory atomically loads a 32-bit word or 64-bit doubleword from a memory location, and stores the value held in a register back to the same memory location. The value initially loaded from memory is returned in the destination register. For more information about memory ordering semantics, see Load-Acquire, Store-Release. For information about memory accesses, see Load/Store addressing modes.

Operation

bits(64) address;
bits(datasize) data;
bits(datasize) store_value;
AccessDescriptor accdesc = CreateAccDescAtomicOp(MemAtomicOp_SWP, acquire, release, tagchecked);

if n == 31 then
    CheckSPAlignment();
    address = SP[];
else
    address = X[n, 64];

store_value = X[s, datasize];

bits(datasize) comparevalue = bits(datasize) UNKNOWN;    // Irrelevant when not executing CAS
data = MemAtomic(address, comparevalue, store_value, accdesc);

X[t, regsize] = ZeroExtend(data, regsize);