cas
Compare and Swap Doubleword
CAS <Xs>, <Xt>, [<Xn|SP>]
Atomic Compare and Swap (64-bit).
Details
Atomic compare and swap of a 64-bit doubleword. Compares the value in Xs with the memory location at address Xn; if equal, stores Xt to that location and loads the old memory value into Xs; otherwise loads the memory value into Xs. This is an AArch64-only instruction requiring LSE extension support. The instruction does not modify condition flags; it provides full sequential consistency without explicit acquire/release semantics.
Pseudocode Operation
address ← Xn; old_value ← [address]; if Xs == old_value then [address] ← Xt; Xs ← old_value; else Xs ← old_value;
Example
CAS x6, x3, [x1]
Encoding
Binary Layout
11
0010001
0
1
Rs
0
11111
Rn
Rt
Operands
-
Xs
Compare -
Xt
Swap -
Xn
First source / base 64-bit integer register
Reference (Arm A64 ISA)
Instruction Forms
| Encoding | Instruction | ISA | Bit pattern | ||
|---|---|---|---|---|---|
| 0x88A07C00 | CAS <Ws>, <Wt>, [<Xn|SP>{, #0}] | A64 | 10 | 0010001 | 0 | 1 | Rs | 0 | 11111 | Rn | Rt | ||
| 0xC8A07C00 | CAS <Xs>, <Xt>, [<Xn|SP>{, #0}] | A64 | 11 | 0010001 | 0 | 1 | Rs | 0 | 11111 | Rn | Rt |
Description
Compare and Swap word or doubleword in memory reads a 32-bit word or 64-bit doubleword from memory, and compares it against the value held in a first register. If the comparison is equal, the value in a second register is written to memory. If the write is performed, the read and write occur atomically such that no other modification of the memory location can take place between the read and write.
For more information about memory ordering semantics, see Load-Acquire, Store-Release.
For information about memory accesses, see Load/Store addressing modes.
The architecture permits that the data read clears any exclusive monitors associated with that location, even if the compare subsequently fails.
If the instruction generates a synchronous Data Abort, the register which is compared and loaded, that is <Ws>, or <Xs>, is restored to the value held in the register before the instruction was executed.
Operation
bits(64) address;
bits(datasize) comparevalue;
bits(datasize) newvalue;
bits(datasize) data;
AccessDescriptor accdesc = CreateAccDescAtomicOp(MemAtomicOp_CAS, acquire, release, tagchecked);
comparevalue = X[s, datasize];
newvalue = X[t, datasize];
if n == 31 then
CheckSPAlignment();
address = SP[];
else
address = X[n, 64];
data = MemAtomic(address, comparevalue, newvalue, accdesc);
X[s, regsize] = ZeroExtend(data, regsize);