swp

Swap Word

SWP <Ws>, <Wt>, [<Xn|SP>]

Atomic swap of a word.

Details

Atomic swap of a 32-bit word. Atomically exchanges the value in Ws with the memory location at address Xn, storing Ws to memory and loading the old memory value into Wt. This AArch64-only LSE instruction provides full sequential consistency without explicit acquire/release semantics. Condition flags are not affected.

Pseudocode Operation

address ← Xn; old_value ← [address]; [address] ← Ws; Wt ← old_value;

Example

SWP w6, w3, [x1]

Encoding

Binary Layout
10
111
0
00
0
0
1
Rs
1
000
00
Rn
Rt
 
Format Atomic
Opcode 0xB8208000
Extension LSE (Atomics)

Operands

  • Ws
    Shift amount 32-bit register
  • Wt
    Transfer 32-bit integer register (load/store)
  • Xn
    First source / base 64-bit integer register

Reference (Arm A64 ISA)

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);