@@ -6,10 +6,10 @@ endif::[]
66
77== RISC-V atomics mappings
88
9- This specifies mappings of C and C\++ atomic operations to RISC-V
9+ This specifies mappings of C and {Cpp} atomic operations to RISC-V
1010machine instructions. Other languages, for example Java, provide similar
1111facilities that should be implemented in a consistent manner, usually
12- by applying the mapping for the corresponding C++ primitive.
12+ by applying the mapping for the corresponding {Cpp} primitive.
1313
1414NOTE: Because different programming languages may be used within the same
1515process, these mappings must be compatible across programming languages. For
1919
2020NOTE: Though many mappings are possible, not all of them will interoperate
2121correctly. In particular, many mapping combinations will not
22- correctly enforce ordering between a C++ `memory_order_seq_cst`
22+ correctly enforce ordering between a {Cpp} `memory_order_seq_cst`
2323store and a subsequent `memory_order_seq_cst` load.
2424
2525NOTE: These mappings are very similar to those that originally appeared in the
2626appendix of the RISC-V "unprivileged" architecture specification as
27- "Mappings from C/C++ primitives to RISC-V Primitives", which we will
27+ "Mappings from C/{Cpp} primitives to RISC-V Primitives", which we will
2828refer to by their 2019 historical label of "Table A.6". That mapping may
2929be used, _except_ that `atomic_store(memory_order_seq_cst)` must have an
3030an extra trailing fence for compatibility with the "Hypothetical mappings ..."
@@ -55,10 +55,10 @@ section deals with other read-modify-write operations that require
5555the `lr` and `sc` instructions.
5656
5757[[tab:c11mappings]]
58- .Mappings from C/C++ primitives to RISC-V primitives
58+ .Mappings from C/{Cpp} primitives to RISC-V primitives
5959[cols="<22,<18,<4",options="header",]
6060|===
61- |C/C++ Construct |RVWMO Mapping |Notes
61+ |C/{Cpp} Construct |RVWMO Mapping |Notes
6262
6363|Non-atomic load |`l{b\|h\|w\|d}` |
6464
@@ -97,7 +97,7 @@ the `lr` and `sc` instructions.
9797
9898[cols="<20,<20,<4",options="header",]
9999|===
100- |C/C++ Construct |RVWMO AMO Mapping |Notes
100+ |C/{Cpp} Construct |RVWMO AMO Mapping |Notes
101101
102102|`atomic_<op>(memory_order_relaxed)` |`amo<op>.{w\|d}` |4
103103
@@ -113,7 +113,7 @@ the `lr` and `sc` instructions.
113113
114114[cols="<16,<24,<4",options="header",]
115115|===
116- |C/C++ Construct |RVWMO LR/SC Mapping |Notes
116+ |C/{Cpp} Construct |RVWMO LR/SC Mapping |Notes
117117
118118|`atomic_<op>(memory_order_relaxed)` |`loop:lr.{w\|d}; <op>; sc.{w\|d}; bnez loop` |4
119119
@@ -136,7 +136,7 @@ the `lr` and `sc` instructions.
136136=== Meaning of notes in table
137137
1381381) Depends on a load instruction with an RCsc aquire annotation,
139- or a store instruction with an RCsc release annotation. These are curently
139+ or a store instruction with an RCsc release annotation. These are currently
140140under discussion, but the specification has not yet been approved.
141141
1421422) An RCpc load or store would also suffice, if it were to be introduced
@@ -148,11 +148,80 @@ mappings with code generated by a compiler using those older mappings.
148148
1491494) Currently only directly possible for 32- and 64-bit operands.
150150
151- === Other conventions
151+ == Ztso atomics mappings
152152
153- It is expected that the RVWMO AMO Mappings will be used for atomic read-modify-write
154- operations that are directly supported by corresponding AMO instructions,
155- and that LR/SC mappings will be used for the remainder, currently
153+ This specifies additional mappings of C and {Cpp} atomic operations to RISC-V
154+ machine instructions.
155+
156+ For each construct, we provide a mapping that assumes only the A and Ztso
157+ extension.
158+
159+ All mappings interoperate correctly with the RVWMO mappings, and with the
160+ original "Table A.6" mappings.
161+
162+ We present the mappings as a table in 3 sections, as above.
163+
164+ [[tab:c11mappingsztso]]
165+ .Mappings with Ztso extension from C/{Cpp} primitives to RISC-V primitives
166+ [cols="<22,<18,<4",options="header",]
167+ |===
168+ |C/{Cpp} Construct |Ztso Mapping |Notes
169+
170+ |`atomic_load(memory_order_acquire)` |`l{b\|h\|w\|d}` | 5
171+
172+ |`atomic_load(memory_order_seq_cst)` |`fence rw,rw; l{b\|h\|w\|d}` | 5
173+
174+ |`atomic_store(memory_order_release)` |`s{b\|h\|w\|d}` | 5
175+
176+ |`atomic_store(memory_order_seq_cst)` |`s{b\|h\|w\|d}; fence rw, rw` | 5
177+
178+ |`atomic_thread_fence(memory_order_acquire)` |`nop` | 5
179+
180+ |`atomic_thread_fence(memory_order_release)` |`nop` | 5
181+
182+ |`atomic_thread_fence(memory_order_acq_rel)` |`nop` | 5
183+ |===
184+
185+ [cols="<20,<20,<4",options="header",]
186+ |===
187+ |C/{Cpp} Construct |Ztso AMO Mapping |Notes
188+
189+ |`atomic_<op>(memory_order_acquire)` |`amo<op>.{w\|d}` |4, 5
190+
191+ |`atomic_<op>(memory_order_release)` |`amo<op>.{w\|d}` |4, 5
192+
193+ |`atomic_<op>(memory_order_acq_rel)` |`amo<op>.{w\|d}` |4, 5
194+
195+ |`atomic_<op>(memory_order_seq_cst)` |`amo<op>.{w\|d}` |4, 5
196+
197+ |===
198+
199+ [cols="<16,<24,<4",options="header",]
200+ |===
201+ |C/{Cpp} Construct |Ztso LR/SC Mapping |Notes
202+
203+ |`atomic_<op>(memory_order_acquire)`
204+ |`loop:lr.{w\|d}; <op>; sc.{w\|d}; bnez loop` |4, 5
205+
206+ |`atomic_<op>(memory_order_release)`
207+ |`loop:lr.{w\|d}; <op>; sc.{w\|d}; bnez loop` |4, 5
208+
209+ |`atomic_<op>(memory_order_acq_rel)`
210+ |`loop:lr.{w\|d}; <op>; sc.{w\|d}; bnez loop` |4, 5
211+
212+ |===
213+
214+ === Meaning of notes in table
215+
216+ 4) Currently only directly possible for 32- and 64-bit operands.
217+
218+ 5) Requires the Ztso extension.
219+
220+ == Other conventions
221+
222+ It is expected that the RVWMO and Ztso AMO Mappings will be used for atomic
223+ read-modify-write operations that are directly supported by corresponding AMO
224+ instructions, and that LR/SC mappings will be used for the remainder, currently
156225including compare-exchange operations. Compare-exchange LR/SC sequences
157226on the containing 32-bit word should be used for shorter operands. Thus,
158227a `fetch_add` operation on a 16-bit quantity would use a 32-bit LR/SC sequence.
0 commit comments