AMOCAS.W

RISC-V AMOCAS.W Instruction Details

Instruction ManualAMO-type

Atomic compare-and-swap word: compare memory with rd[31:0], store rs2[31:0] on match, return old value in rd

Instruction Syntax

amocas.w rd, rs2, (rs1)

Syntax is rd, rs2, (rs1), with no offset or rs3. Pre-execution rd supplies the comparison, rs2 the replacement, and rs1 the address.

32-bit encoding: funct5=00101, funct3=010, opcode=0101111; aq is bit26 and rl is bit25.

RV32/RV64, requiring Zacas (which depends on Zaamo). Compare 32-bit old memory and replace on equality; normal completion returns old memory, not a Boolean flag.

Use only the low 32 rd/rs2 bits for comparison/replacement; sign-extend old memory to XLEN. x0 sources are zero and x0 destination writes are discarded.

Use 4-byte alignment, or a PMA with all accessed bytes in one misaligned atomicity granule; otherwise address-misaligned or access-fault is raised. The region must support this AMOCAS; overlapping registers read one pre-execution value, and aq/rl orders only the accessed domain.

ZacasAtomic Operation

Instruction Behavior

AMOCAS.W (Zacas extension) atomically loads a 32-bit value from the address in rs1 and compares it with the low 32 bits of rd; on a bitwise match it stores the low 32 bits of rs2 back to the same address. The loaded old value is sign-extended to XLEN and written to rd regardless of success. aq/rl ordering bits are supported. These data results assume normal completion of the access; rd=x0 discards writeback. Zacas depends on Zaamo.

AMOCAS.W Atomic Decode And Execute Animation

Shows Zacas word atomic field decode, aq/rl ordering bits, a memory read, and CAS state update.

rd
rs2
rs1
amocas.w
,
,(
)
Execution Context
31..27
26
25
24..20
19..15
14..12
11..7
6..0
00101
funct5
1
aq
0
rl
01011
rs2
01010
rs1
010
funct3
00101
rd
0101111
opcode
Atomic Data Path
instruction
0x2CB522AF
opcode
0101111 -> AMO
funct3
010 -> word width
funct5
00101 -> AMOCAS.W
aq / rl
1/0 -> acquire
write permission
permitted: CAS may proceed
rd / rs2 / rs1
t0(x5) compare=0x80000002 / a1(x11) swap=0x0000007F / a0(x10)=0x00001000
memory read
mem[0x00001000] -> old word 0x80000002
RMW
Not computed
rd / memory
t0(x5) = 0x80000002; mem[0x00001000] = 0x80000002
Current Step

Concept Step: receive the 32-bit atomic instruction encoding

Zacas-extension word atomic instructions use a 32-bit encoding; the animation starts by splitting the fields.

encoding: 0x2CB522AF
syntax : amocas.w.aq t0(x5), a1(x11), (a0(x10))
Pending result : Not computed
Current architectural state
t0(x5) = 0x80000002; mem[0x00001000] = 0x80000002

Assumes enabled extensions at the selected XLEN, successful fetch, and a region supporting this AMOCAS with successful access; W/D/Q can select a missing-write-permission exception path. Misalignment requires the stated PMA condition. Sources are read before writeback; x0 sources are zero (both halves for a pair). aq/rl orders only the accessed domain. Steps are a teaching sequence.

Quick Understanding & Search Notes

AMOCAS.W atomically loads a 32-bit memory value, compares it bitwise with the compare value supplied in rd, stores the rs2 swap value on equality, and on normal completion returns the old value in rd (discarding the write for rd=x0).

After normal completion with nonzero rd, old memory overwrites the compare operand rd (rd/rd+1 for pair forms), not a Boolean success flag.
On RV64, AMOCAS.W sign-extends the loaded 32-bit old value into rd.
AMOCAS.W/D/Q requires write permission even on comparison failure. aq=1 supplies acquire on success or failure; rl=1 supplies release only on success. Failure may omit a write or write back the old value, without release.

Common Usage Scenarios

Atomic & Sync

RV32/RV64 with required extensions enabled; assume a1=0x1000 and a supporting region with normal completion. With old memory=5, expected=5 and replacement=3, the example returns 5 and stores 3; changing expected to 4 returns 5 and leaves memory data at 5.

Pre-Use Checklist

Syntax Check
  • Syntax is rd, rs2, (rs1), with no offset or rs3. Pre-execution rd supplies the comparison, rs2 the replacement, and rs1 the address.
  • 32-bit encoding: funct5=00101, funct3=010, opcode=0101111; aq is bit26 and rl is bit25.
Semantic Check
  • RV32/RV64, requiring Zacas (which depends on Zaamo). Compare 32-bit old memory and replace on equality; normal completion returns old memory, not a Boolean flag.
  • Use only the low 32 rd/rs2 bits for comparison/replacement; sign-extend old memory to XLEN. x0 sources are zero and x0 destination writes are discarded.
  • Use 4-byte alignment, or a PMA with all accessed bytes in one misaligned atomicity granule; otherwise address-misaligned or access-fault is raised. The region must support this AMOCAS; overlapping registers read one pre-execution value, and aq/rl orders only the accessed domain.

Pitfalls / Common Confusions

The compare value is the input value in rd; rd is also overwritten with the old memory value and is not a Boolean success flag.
Comparison and replacement use only the low 32 bits; upper register bits are ignored. rd=x0 compares zero; rs2=x0 replaces with zero.
The target address must be naturally aligned to the operand width unless relaxed by the platform misaligned atomicity granule PMA.

FAQ

How do I tell whether AMOCAS.W succeeded?

After normal completion with nonzero rd, compare the low 32 bits of the value returned in rd with the low 32 bits of the expected value saved before execution; equality indicates that the swap store occurred. On RV64, the returned word is sign-extended in rd.

Does AMOCAS.W take the compare value from rs2?

No. The low operand-width bits of rd before execution supply the compare value; the same-width low bits of rs2 supply the replacement.