1 // Copyright 2022 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 #include "go_asm.h"
6 #include "textflag.h"
7
8 // bool cas(uint32 *ptr, uint32 old, uint32 new)
9 // Atomically:
10 // if(*ptr == old){
11 // *ptr = new;
12 // return 1;
13 // } else
14 // return 0;
15 TEXT ·Cas(SB), NOSPLIT, $0-17
16 MOVV ptr+0(FP), R4
17 MOVW old+8(FP), R5
18 MOVW new+12(FP), R6
19 DBAR
20 cas_again:
21 MOVV R6, R7
22 LL (R4), R8
23 BNE R5, R8, cas_fail
24 SC R7, (R4)
25 BEQ R7, cas_again
26 MOVV $1, R4
27 MOVB R4, ret+16(FP)
28 DBAR
29 RET
30 cas_fail:
31 MOVV $0, R4
32 JMP -4(PC)
33
34 // bool cas64(uint64 *ptr, uint64 old, uint64 new)
35 // Atomically:
36 // if(*ptr == old){
37 // *ptr = new;
38 // return 1;
39 // } else {
40 // return 0;
41 // }
42 TEXT ·Cas64(SB), NOSPLIT, $0-25
43 MOVV ptr+0(FP), R4
44 MOVV old+8(FP), R5
45 MOVV new+16(FP), R6
46 DBAR
47 cas64_again:
48 MOVV R6, R7
49 LLV (R4), R8
50 BNE R5, R8, cas64_fail
51 SCV R7, (R4)
52 BEQ R7, cas64_again
53 MOVV $1, R4
54 MOVB R4, ret+24(FP)
55 DBAR
56 RET
57 cas64_fail:
58 MOVV $0, R4
59 JMP -4(PC)
60
61 TEXT ·Casuintptr(SB), NOSPLIT, $0-25
62 JMP ·Cas64(SB)
63
64 TEXT ·CasRel(SB), NOSPLIT, $0-17
65 JMP ·Cas(SB)
66
67 TEXT ·Loaduintptr(SB), NOSPLIT|NOFRAME, $0-16
68 JMP ·Load64(SB)
69
70 TEXT ·Loaduint(SB), NOSPLIT|NOFRAME, $0-16
71 JMP ·Load64(SB)
72
73 TEXT ·Storeuintptr(SB), NOSPLIT, $0-16
74 JMP ·Store64(SB)
75
76 TEXT ·Xadduintptr(SB), NOSPLIT, $0-24
77 JMP ·Xadd64(SB)
78
79 TEXT ·Loadint64(SB), NOSPLIT, $0-16
80 JMP ·Load64(SB)
81
82 TEXT ·Xaddint32(SB),NOSPLIT,$0-20
83 JMP ·Xadd(SB)
84
85 TEXT ·Xaddint64(SB), NOSPLIT, $0-24
86 JMP ·Xadd64(SB)
87
88 // bool casp(void **val, void *old, void *new)
89 // Atomically:
90 // if(*val == old){
91 // *val = new;
92 // return 1;
93 // } else
94 // return 0;
95 TEXT ·Casp1(SB), NOSPLIT, $0-25
96 JMP ·Cas64(SB)
97
98 // uint32 Xadd(uint32 volatile *ptr, int32 delta)
99 // Atomically:
100 // *val += delta;
101 // return *val;
102 TEXT ·Xadd(SB), NOSPLIT, $0-20
103 MOVV ptr+0(FP), R4
104 MOVW delta+8(FP), R5
105 AMADDDBW R5, (R4), R6
106 ADDV R6, R5, R4
107 MOVW R4, ret+16(FP)
108 RET
109
110 // func Xadd64(ptr *uint64, delta int64) uint64
111 TEXT ·Xadd64(SB), NOSPLIT, $0-24
112 MOVV ptr+0(FP), R4
113 MOVV delta+8(FP), R5
114 AMADDDBV R5, (R4), R6
115 ADDV R6, R5, R4
116 MOVV R4, ret+16(FP)
117 RET
118
119 // func Xchg(ptr *uint32, new uint32) uint32
120 TEXT ·Xchg(SB), NOSPLIT, $0-20
121 MOVV ptr+0(FP), R4
122 MOVW new+8(FP), R5
123 AMSWAPDBW R5, (R4), R6
124 MOVW R6, ret+16(FP)
125 RET
126
127 // func Xchg64(ptr *uint64, new uint64) uint64
128 TEXT ·Xchg64(SB), NOSPLIT, $0-24
129 MOVV ptr+0(FP), R4
130 MOVV new+8(FP), R5
131 AMSWAPDBV R5, (R4), R6
132 MOVV R6, ret+16(FP)
133 RET
134
135 TEXT ·Xchguintptr(SB), NOSPLIT, $0-24
136 JMP ·Xchg64(SB)
137
138 // func Xchgint32(ptr *int32, new int32) int32
139 TEXT ·Xchgint32(SB), NOSPLIT, $0-20
140 JMP ·Xchg(SB)
141
142 // func Xchgint64(ptr *int64, new int64) int64
143 TEXT ·Xchgint64(SB), NOSPLIT, $0-24
144 JMP ·Xchg64(SB)
145
146 TEXT ·StorepNoWB(SB), NOSPLIT, $0-16
147 JMP ·Store64(SB)
148
149 TEXT ·StoreRel(SB), NOSPLIT, $0-12
150 JMP ·Store(SB)
151
152 TEXT ·StoreRel64(SB), NOSPLIT, $0-16
153 JMP ·Store64(SB)
154
155 TEXT ·StoreReluintptr(SB), NOSPLIT, $0-16
156 JMP ·Store64(SB)
157
158 TEXT ·Store(SB), NOSPLIT, $0-12
159 MOVV ptr+0(FP), R4
160 MOVW val+8(FP), R5
161 AMSWAPDBW R5, (R4), R0
162 RET
163
164 TEXT ·Store8(SB), NOSPLIT, $0-9
165 MOVV ptr+0(FP), R4
166 MOVB val+8(FP), R5
167 MOVBU internal∕cpu·Loong64+const_offsetLoong64HasLAM_BH(SB), R6
168 BEQ R6, _legacy_store8_
169 AMSWAPDBB R5, (R4), R0
170 RET
171 _legacy_store8_:
172 // StoreRelease barrier
173 DBAR $0x12
174 MOVB R5, 0(R4)
175 DBAR $0x18
176 RET
177
178 TEXT ·Store64(SB), NOSPLIT, $0-16
179 MOVV ptr+0(FP), R4
180 MOVV val+8(FP), R5
181 AMSWAPDBV R5, (R4), R0
182 RET
183
184 // void Or8(byte volatile*, byte);
185 TEXT ·Or8(SB), NOSPLIT, $0-9
186 MOVV ptr+0(FP), R4
187 MOVBU val+8(FP), R5
188 // R6 = ptr & (~3)
189 MOVV $~3, R6
190 AND R4, R6
191 // R7 = ((ptr & 3) * 8)
192 AND $3, R4, R7
193 SLLV $3, R7
194 // R5 = val << R7
195 SLLV R7, R5
196 AMORDBW R5, (R6), R0
197 RET
198
199 // void And8(byte volatile*, byte);
200 TEXT ·And8(SB), NOSPLIT, $0-9
201 MOVV ptr+0(FP), R4
202 MOVBU val+8(FP), R5
203 // R6 = ptr & (~3)
204 MOVV $~3, R6
205 AND R4, R6
206 // R7 = ((ptr & 3) * 8)
207 AND $3, R4, R7
208 SLLV $3, R7
209 // R5 = ((val ^ 0xFF) << R7) ^ (-1)
210 XOR $255, R5
211 SLLV R7, R5
212 XOR $-1, R5
213 AMANDDBW R5, (R6), R0
214 RET
215
216 // func Or(addr *uint32, v uint32)
217 TEXT ·Or(SB), NOSPLIT, $0-12
218 MOVV ptr+0(FP), R4
219 MOVW val+8(FP), R5
220 AMORDBW R5, (R4), R0
221 RET
222
223 // func And(addr *uint32, v uint32)
224 TEXT ·And(SB), NOSPLIT, $0-12
225 MOVV ptr+0(FP), R4
226 MOVW val+8(FP), R5
227 AMANDDBW R5, (R4), R0
228 RET
229
230 // func Or32(addr *uint32, v uint32) old uint32
231 TEXT ·Or32(SB), NOSPLIT, $0-20
232 MOVV ptr+0(FP), R4
233 MOVW val+8(FP), R5
234 AMORDBW R5, (R4), R6
235 MOVW R6, ret+16(FP)
236 RET
237
238 // func And32(addr *uint32, v uint32) old uint32
239 TEXT ·And32(SB), NOSPLIT, $0-20
240 MOVV ptr+0(FP), R4
241 MOVW val+8(FP), R5
242 AMANDDBW R5, (R4), R6
243 MOVW R6, ret+16(FP)
244 RET
245
246 // func Or64(addr *uint64, v uint64) old uint64
247 TEXT ·Or64(SB), NOSPLIT, $0-24
248 MOVV ptr+0(FP), R4
249 MOVV val+8(FP), R5
250 AMORDBV R5, (R4), R6
251 MOVV R6, ret+16(FP)
252 RET
253
254 // func And64(addr *uint64, v uint64) old uint64
255 TEXT ·And64(SB), NOSPLIT, $0-24
256 MOVV ptr+0(FP), R4
257 MOVV val+8(FP), R5
258 AMANDDBV R5, (R4), R6
259 MOVV R6, ret+16(FP)
260 RET
261
262 // func Anduintptr(addr *uintptr, v uintptr) old uintptr
263 TEXT ·Anduintptr(SB), NOSPLIT, $0-24
264 JMP ·And64(SB)
265
266 // func Oruintptr(addr *uintptr, v uintptr) old uintptr
267 TEXT ·Oruintptr(SB), NOSPLIT, $0-24
268 JMP ·Or64(SB)
269
270 // uint32 internal∕runtime∕atomic·Load(uint32 volatile* ptr)
271 TEXT ·Load(SB),NOSPLIT|NOFRAME,$0-12
272 MOVV ptr+0(FP), R19
273 MOVWU 0(R19), R19
274 DBAR $0x14 // LoadAcquire barrier
275 MOVW R19, ret+8(FP)
276 RET
277
278 // uint8 internal∕runtime∕atomic·Load8(uint8 volatile* ptr)
279 TEXT ·Load8(SB),NOSPLIT|NOFRAME,$0-9
280 MOVV ptr+0(FP), R19
281 MOVBU 0(R19), R19
282 DBAR $0x14
283 MOVB R19, ret+8(FP)
284 RET
285
286 // uint64 internal∕runtime∕atomic·Load64(uint64 volatile* ptr)
287 TEXT ·Load64(SB),NOSPLIT|NOFRAME,$0-16
288 MOVV ptr+0(FP), R19
289 MOVV 0(R19), R19
290 DBAR $0x14
291 MOVV R19, ret+8(FP)
292 RET
293
294 // void *internal∕runtime∕atomic·Loadp(void *volatile *ptr)
295 TEXT ·Loadp(SB),NOSPLIT|NOFRAME,$0-16
296 JMP ·Load64(SB)
297
298 // uint32 internal∕runtime∕atomic·LoadAcq(uint32 volatile* ptr)
299 TEXT ·LoadAcq(SB),NOSPLIT|NOFRAME,$0-12
300 JMP ·Load(SB)
301
302 // uint64 ·LoadAcq64(uint64 volatile* ptr)
303 TEXT ·LoadAcq64(SB),NOSPLIT|NOFRAME,$0-16
304 JMP ·Load64(SB)
305
306 // uintptr ·LoadAcquintptr(uintptr volatile* ptr)
307 TEXT ·LoadAcquintptr(SB),NOSPLIT|NOFRAME,$0-16
308 JMP ·Load64(SB)
309
310
View as plain text