AMOCAS.D

RISC-V AMOCAS.D Instruction Details

Instruction ManualR-type

Atomic compare-and-swap doubleword: compare memory with rd/register pair, store rs2/register pair on match, return old value in rd

Instruction Syntax

amocas.d rd, rs2, (rs1)
Operand Breakdown
Destination rd: register receiving the operation result.
Source rs1: register holding the first operand.
Source rs2: register holding the second operand.
ZacasAtomic Operation

Instruction Behavior

AMOCAS.D (Zacas extension) atomically loads a 64-bit doubleword from the address in rs1 and compares it with the compare operand; on a match it stores the swap operand. On RV64, the compare value is rd, the swap value is rs2, and the old value is written to rd. On RV32, the compare value is the even rd/rd+1 register pair, the swap value is the even rs2/rs2+1 pair, and the old value is written to rd/rd+1; odd rd or rs2 encodings are reserved. aq/rl ordering bits are supported.

Quick Understanding & Search Notes

AMOCAS.D atomically loads a 64-bit memory value, compares it bitwise with the compare value supplied in rd, stores the rs2 swap value on equality, and always writes the old memory value back to rd.

Before execution rd supplies the compare value; after execution rd is overwritten by the old memory value, not a Boolean success flag.
On RV32, AMOCAS.D uses even-numbered register pairs; on RV64 it uses a single 64-bit register.
The address must be naturally aligned to the operand size; aq/rl ordering on success and failure follows the Zacas rules.

Common Usage Scenarios

Atomic & Sync

Understand this scenario with real code like «amocas.d a0, a2, (a1)».

Network & Byte Order

Understand this scenario with real code like «amocas.d a0, a2, (a1)».

Pre-Use Checklist

Syntax Check
  • Confirm the current instruction format is R-type.
  • Confirm the operand order matches the example.
Semantic Check
  • Ensure the destination register usage is compatible with the calling convention.
  • Confirm this is not the lower-level form of a pseudo-instruction expansion.

Pitfalls / Common Confusions

AMOCAS.D is not RV64-only; RV32 uses even/odd register pairs.
The compare value comes from rd (or rd/rd+1 on RV32), not rs2; rs2 is the new value stored on success.
rd returns the old memory value, not a success flag; the target address must be naturally 8-byte aligned unless relaxed by PMA.

FAQ

How do I tell whether AMOCAS.D succeeded?

Compare the old value returned in rd with the expected value; equality indicates that the swap store occurred.

Does AMOCAS.D take the compare value from rs2?

No. rs2 supplies the new value to store on success; the compare value comes from rd before execution, or from the rd register pair.