首页/指令库/AMOCAS-D
AMOCAS.D

RISC-V AMOCAS.D 指令详解

指令手册AMO-type

原子比较并交换双字:比较内存旧值与 rd/寄存器对,匹配时写入 rs2/寄存器对;rd 返回旧值

指令语法

amocas.d rd, rs2, (rs1)
操作数说明
rd:接收原子读取的旧内存值;对 AMOCAS,执行前的 rd 同时提供比较值。
rs1:直接提供原子内存访问地址;AMO 汇编语法没有 offset 立即数字段。
rs2:提供 AMO 的源数据;对 AMOCAS,它是在比较相等时写入的交换值。
AMO-type 是本站对 AMO 专用 R-type 字段布局的展示标签:funct5、aq、rl、rs2、rs1、funct3、rd 和 AMO opcode 共同编码该操作。
Zacas原子操作

指令行为说明

AMOCAS.D(Zacas 扩展)原子地加载 rs1 地址处的 64 位双字并与比较操作数比较;匹配时写入交换操作数。RV64 中比较值来自 rd、交换值来自 rs2,旧值写回 rd。RV32 中,偶数 rd 为比较对的低 32 位、rd+1 为高 32 位;偶数 rs2 为交换对的低 32 位、rs2+1 为高 32 位,旧值按相同顺序写回 rd/rd+1;奇数 rd 或 rs2 编码保留。寄存器对起点为 x0 时,源对两半均读作零;rd 对为 x0 时,整个结果被丢弃。支持 aq/rl 内存序位。

AMOCAS.D 原子指令解析与执行动画

展示 Zacas doubleword 原子指令的字段解析、aq/rl 排序位、内存读取和 CAS 状态更新。

rd(low)/rd+1(high)
rs2(low)/rs2+1(high)
rs1
amocas.d
,
,(
)
执行环境
31..27
26
25
24..20
19..15
14..12
11..7
6..0
00101
funct5
1
aq
0
rl
01100
rs2
01011
rs1
011
funct3
01010
rd
0101111
opcode
原子数据路径
instruction
0x2CC5B52F
opcode
0101111 -> AMO
funct3
011 -> doubleword width
funct5
00101 -> AMOCAS.D
aq / rl
1/0 -> acquire
写权限
已允许:可进入 CAS
rd / rs2 / rs1
a0(x10) (low)/a1(x11) (high) compare=0x8000000000000002 / a2(x12) (low)/a3(x13) (high) swap=0x000000000000007F / a1(x11)=0x00001000
memory read
mem[0x00001000] -> old doubleword 0x8000000000000002
RMW
0x8000000000000002 == 0x8000000000000002 -> store 0x000000000000007F
rd / memory
a0(x10) (low)/a1(x11) (high) = old doubleword 0x8000000000000002; mem[0x00001000] = 0x000000000000007F
当前步骤

概念步骤:拿到 32-bit 原子指令编码

Zacas 扩展 doubleword 原子指令使用 32-bit 编码;动画先展示字段切分。

encoding: 0x2CC5B52F
syntax : amocas.d a0(x10), a2(x12), (a1(x11))
result : a0(x10) (low)/a1(x11) (high) = old doubleword 0x8000000000000002; mem[0x00001000] = 0x000000000000007F
64-bit doubleword 结果视图
63
62
61
60
59
58
57
56
55
54
53
52
51
50
49
48
47
46
45
44
43
42
41
40
39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
old doubleword
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
rd compare
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
1
1
rs2 swap
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
CAS match
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-

该动画展示 ISA 可见的原子读改写、比较并交换、LR/SC 预留状态和内存排序位,不表示任何特定 CPU 的缓存一致性实现、流水线或时序。

快速理解与检索要点

AMOCAS.D 原子加载 64 位内存值,与比较操作数按位比较;相等时把交换操作数写回内存。RV64 使用 rd/rs2,RV32 使用 rd/rd+1 与 rs2/rs2+1 寄存器对,并始终返回旧内存值。

rd 在执行前是比较值来源,执行后被旧内存值覆盖;它不是布尔成功标志。
RV32 上 AMOCAS.D 使用偶数寄存器对;RV64 上使用单个 64 位寄存器。
AMOCAS.W/D/Q(包括比较失败)都需要写权限;aq/rl 的成功和失败排序语义按 Zacas 规则处理。

常见使用场景

原子与同步

结合 «amocas.d a0, a2, (a1)» 等实际代码理解该场景。

循环与迭代

结合 «amocas.d a0, a2, (a1)» 等实际代码理解该场景。

使用前检查清单

语法检查
  • 确认当前指令格式为 AMO-type。
  • 确认操作数排列顺序与示例一致。
语义检查
  • 确认目标寄存器用途和调用约定兼容。
  • 确认该指令不是伪指令展开后的底层形式。

容易混淆 / 常见误区

AMOCAS.D 不是 RV64-only;RV32 使用偶/奇寄存器对。
比较值来自 rd(或 RV32 的 rd/rd+1),不是 rs2;rs2 是成功时写入的新值。
rd 返回旧内存值而非成功标志;目标地址必须按 8 字节自然对齐,除非 PMA 放宽。

常见问题

AMOCAS.D 如何判断成功?

RV64 上比较执行后的 rd 旧值与期望值;RV32 上比较 rd/rd+1 返回的完整 64 位旧值。如果相同,说明交换写入发生。

AMOCAS.D 的比较值来自 rs2 吗?

不是。rs2 提供成功时要写入的新值;比较值来自执行前的 rd(或寄存器对)。