Debian Codegraph @

gcc/testsuite/gcc.target/riscv/amo/inline-atomics-4.c

raw
1 /* Check all short alignments. */
2 /* Duplicate logic as libatomic/testsuite/libatomic.c/atomic-op-2.c */
3 /* Test __atomic routines for existence and proper execution on 2 byte
4 values with each valid memory model. */
5 /* { dg-do run { target { riscv_a } } } */
6 /* { dg-options "-minline-atomics -Wno-address-of-packed-member" } */
7
8 /* Test the execution of the __atomic_*OP builtin routines for a short. */
9
10 extern void abort(void);
11
12 short count, res;
13 const short init = ~0;
14
15 struct A
16 {
17 short a;
18 short b;
19 } __attribute__ ((packed)) A;
20
21 /* The fetch_op routines return the original value before the operation. */
22
23 void
24 test_fetch_add (short* v)
25 {
26 *v = 0;
27 count = 1;
28
29 if (__atomic_fetch_add (v, count, __ATOMIC_RELAXED) != 0)
30 abort ();
31
32 if (__atomic_fetch_add (v, 1, __ATOMIC_CONSUME) != 1)
33 abort ();
34
35 if (__atomic_fetch_add (v, count, __ATOMIC_ACQUIRE) != 2)
36 abort ();
37
38 if (__atomic_fetch_add (v, 1, __ATOMIC_RELEASE) != 3)
39 abort ();
40
41 if (__atomic_fetch_add (v, count, __ATOMIC_ACQ_REL) != 4)
42 abort ();
43
44 if (__atomic_fetch_add (v, 1, __ATOMIC_SEQ_CST) != 5)
45 abort ();
46 }
47
48
49 void
50 test_fetch_sub (short* v)
51 {
52 *v = res = 20;
53 count = 0;
54
55 if (__atomic_fetch_sub (v, count + 1, __ATOMIC_RELAXED) != res--)
56 abort ();
57
58 if (__atomic_fetch_sub (v, 1, __ATOMIC_CONSUME) != res--)
59 abort ();
60
61 if (__atomic_fetch_sub (v, count + 1, __ATOMIC_ACQUIRE) != res--)
62 abort ();
63
64 if (__atomic_fetch_sub (v, 1, __ATOMIC_RELEASE) != res--)
65 abort ();
66
67 if (__atomic_fetch_sub (v, count + 1, __ATOMIC_ACQ_REL) != res--)
68 abort ();
69
70 if (__atomic_fetch_sub (v, 1, __ATOMIC_SEQ_CST) != res--)
71 abort ();
72 }
73
74 void
75 test_fetch_and (short* v)
76 {
77 *v = init;
78
79 if (__atomic_fetch_and (v, 0, __ATOMIC_RELAXED) != init)
80 abort ();
81
82 if (__atomic_fetch_and (v, init, __ATOMIC_CONSUME) != 0)
83 abort ();
84
85 if (__atomic_fetch_and (v, 0, __ATOMIC_ACQUIRE) != 0)
86 abort ();
87
88 *v = ~*v;
89 if (__atomic_fetch_and (v, init, __ATOMIC_RELEASE) != init)
90 abort ();
91
92 if (__atomic_fetch_and (v, 0, __ATOMIC_ACQ_REL) != init)
93 abort ();
94
95 if (__atomic_fetch_and (v, 0, __ATOMIC_SEQ_CST) != 0)
96 abort ();
97 }
98
99 void
100 test_fetch_nand (short* v)
101 {
102 *v = init;
103
104 if (__atomic_fetch_nand (v, 0, __ATOMIC_RELAXED) != init)
105 abort ();
106
107 if (__atomic_fetch_nand (v, init, __ATOMIC_CONSUME) != init)
108 abort ();
109
110 if (__atomic_fetch_nand (v, 0, __ATOMIC_ACQUIRE) != 0 )
111 abort ();
112
113 if (__atomic_fetch_nand (v, init, __ATOMIC_RELEASE) != init)
114 abort ();
115
116 if (__atomic_fetch_nand (v, init, __ATOMIC_ACQ_REL) != 0)
117 abort ();
118
119 if (__atomic_fetch_nand (v, 0, __ATOMIC_SEQ_CST) != init)
120 abort ();
121 }
122
123 void
124 test_fetch_xor (short* v)
125 {
126 *v = init;
127 count = 0;
128
129 if (__atomic_fetch_xor (v, count, __ATOMIC_RELAXED) != init)
130 abort ();
131
132 if (__atomic_fetch_xor (v, ~count, __ATOMIC_CONSUME) != init)
133 abort ();
134
135 if (__atomic_fetch_xor (v, 0, __ATOMIC_ACQUIRE) != 0)
136 abort ();
137
138 if (__atomic_fetch_xor (v, ~count, __ATOMIC_RELEASE) != 0)
139 abort ();
140
141 if (__atomic_fetch_xor (v, 0, __ATOMIC_ACQ_REL) != init)
142 abort ();
143
144 if (__atomic_fetch_xor (v, ~count, __ATOMIC_SEQ_CST) != init)
145 abort ();
146 }
147
148 void
149 test_fetch_or (short* v)
150 {
151 *v = 0;
152 count = 1;
153
154 if (__atomic_fetch_or (v, count, __ATOMIC_RELAXED) != 0)
155 abort ();
156
157 count *= 2;
158 if (__atomic_fetch_or (v, 2, __ATOMIC_CONSUME) != 1)
159 abort ();
160
161 count *= 2;
162 if (__atomic_fetch_or (v, count, __ATOMIC_ACQUIRE) != 3)
163 abort ();
164
165 count *= 2;
166 if (__atomic_fetch_or (v, 8, __ATOMIC_RELEASE) != 7)
167 abort ();
168
169 count *= 2;
170 if (__atomic_fetch_or (v, count, __ATOMIC_ACQ_REL) != 15)
171 abort ();
172
173 count *= 2;
174 if (__atomic_fetch_or (v, count, __ATOMIC_SEQ_CST) != 31)
175 abort ();
176 }
177
178 /* The OP_fetch routines return the new value after the operation. */
179
180 void
181 test_add_fetch (short* v)
182 {
183 *v = 0;
184 count = 1;
185
186 if (__atomic_add_fetch (v, count, __ATOMIC_RELAXED) != 1)
187 abort ();
188
189 if (__atomic_add_fetch (v, 1, __ATOMIC_CONSUME) != 2)
190 abort ();
191
192 if (__atomic_add_fetch (v, count, __ATOMIC_ACQUIRE) != 3)
193 abort ();
194
195 if (__atomic_add_fetch (v, 1, __ATOMIC_RELEASE) != 4)
196 abort ();
197
198 if (__atomic_add_fetch (v, count, __ATOMIC_ACQ_REL) != 5)
199 abort ();
200
201 if (__atomic_add_fetch (v, count, __ATOMIC_SEQ_CST) != 6)
202 abort ();
203 }
204
205
206 void
207 test_sub_fetch (short* v)
208 {
209 *v = res = 20;
210 count = 0;
211
212 if (__atomic_sub_fetch (v, count + 1, __ATOMIC_RELAXED) != --res)
213 abort ();
214
215 if (__atomic_sub_fetch (v, 1, __ATOMIC_CONSUME) != --res)
216 abort ();
217
218 if (__atomic_sub_fetch (v, count + 1, __ATOMIC_ACQUIRE) != --res)
219 abort ();
220
221 if (__atomic_sub_fetch (v, 1, __ATOMIC_RELEASE) != --res)
222 abort ();
223
224 if (__atomic_sub_fetch (v, count + 1, __ATOMIC_ACQ_REL) != --res)
225 abort ();
226
227 if (__atomic_sub_fetch (v, count + 1, __ATOMIC_SEQ_CST) != --res)
228 abort ();
229 }
230
231 void
232 test_and_fetch (short* v)
233 {
234 *v = init;
235
236 if (__atomic_and_fetch (v, 0, __ATOMIC_RELAXED) != 0)
237 abort ();
238
239 *v = init;
240 if (__atomic_and_fetch (v, init, __ATOMIC_CONSUME) != init)
241 abort ();
242
243 if (__atomic_and_fetch (v, 0, __ATOMIC_ACQUIRE) != 0)
244 abort ();
245
246 *v = ~*v;
247 if (__atomic_and_fetch (v, init, __ATOMIC_RELEASE) != init)
248 abort ();
249
250 if (__atomic_and_fetch (v, 0, __ATOMIC_ACQ_REL) != 0)
251 abort ();
252
253 *v = ~*v;
254 if (__atomic_and_fetch (v, 0, __ATOMIC_SEQ_CST) != 0)
255 abort ();
256 }
257
258 void
259 test_nand_fetch (short* v)
260 {
261 *v = init;
262
263 if (__atomic_nand_fetch (v, 0, __ATOMIC_RELAXED) != init)
264 abort ();
265
266 if (__atomic_nand_fetch (v, init, __ATOMIC_CONSUME) != 0)
267 abort ();
268
269 if (__atomic_nand_fetch (v, 0, __ATOMIC_ACQUIRE) != init)
270 abort ();
271
272 if (__atomic_nand_fetch (v, init, __ATOMIC_RELEASE) != 0)
273 abort ();
274
275 if (__atomic_nand_fetch (v, init, __ATOMIC_ACQ_REL) != init)
276 abort ();
277
278 if (__atomic_nand_fetch (v, 0, __ATOMIC_SEQ_CST) != init)
279 abort ();
280 }
281
282
283
284 void
285 test_xor_fetch (short* v)
286 {
287 *v = init;
288 count = 0;
289
290 if (__atomic_xor_fetch (v, count, __ATOMIC_RELAXED) != init)
291 abort ();
292
293 if (__atomic_xor_fetch (v, ~count, __ATOMIC_CONSUME) != 0)
294 abort ();
295
296 if (__atomic_xor_fetch (v, 0, __ATOMIC_ACQUIRE) != 0)
297 abort ();
298
299 if (__atomic_xor_fetch (v, ~count, __ATOMIC_RELEASE) != init)
300 abort ();
301
302 if (__atomic_xor_fetch (v, 0, __ATOMIC_ACQ_REL) != init)
303 abort ();
304
305 if (__atomic_xor_fetch (v, ~count, __ATOMIC_SEQ_CST) != 0)
306 abort ();
307 }
308
309 void
310 test_or_fetch (short* v)
311 {
312 *v = 0;
313 count = 1;
314
315 if (__atomic_or_fetch (v, count, __ATOMIC_RELAXED) != 1)
316 abort ();
317
318 count *= 2;
319 if (__atomic_or_fetch (v, 2, __ATOMIC_CONSUME) != 3)
320 abort ();
321
322 count *= 2;
323 if (__atomic_or_fetch (v, count, __ATOMIC_ACQUIRE) != 7)
324 abort ();
325
326 count *= 2;
327 if (__atomic_or_fetch (v, 8, __ATOMIC_RELEASE) != 15)
328 abort ();
329
330 count *= 2;
331 if (__atomic_or_fetch (v, count, __ATOMIC_ACQ_REL) != 31)
332 abort ();
333
334 count *= 2;
335 if (__atomic_or_fetch (v, count, __ATOMIC_SEQ_CST) != 63)
336 abort ();
337 }
338
339
340 /* Test the OP routines with a result which isn't used. Use both variations
341 within each function. */
342
343 void
344 test_add (short* v)
345 {
346 *v = 0;
347 count = 1;
348
349 __atomic_add_fetch (v, count, __ATOMIC_RELAXED);
350 if (*v != 1)
351 abort ();
352
353 __atomic_fetch_add (v, count, __ATOMIC_CONSUME);
354 if (*v != 2)
355 abort ();
356
357 __atomic_add_fetch (v, 1 , __ATOMIC_ACQUIRE);
358 if (*v != 3)
359 abort ();
360
361 __atomic_fetch_add (v, 1, __ATOMIC_RELEASE);
362 if (*v != 4)
363 abort ();
364
365 __atomic_add_fetch (v, count, __ATOMIC_ACQ_REL);
366 if (*v != 5)
367 abort ();
368
369 __atomic_fetch_add (v, count, __ATOMIC_SEQ_CST);
370 if (*v != 6)
371 abort ();
372 }
373
374
375 void
376 test_sub (short* v)
377 {
378 *v = res = 20;
379 count = 0;
380
381 __atomic_sub_fetch (v, count + 1, __ATOMIC_RELAXED);
382 if (*v != --res)
383 abort ();
384
385 __atomic_fetch_sub (v, count + 1, __ATOMIC_CONSUME);
386 if (*v != --res)
387 abort ();
388
389 __atomic_sub_fetch (v, 1, __ATOMIC_ACQUIRE);
390 if (*v != --res)
391 abort ();
392
393 __atomic_fetch_sub (v, 1, __ATOMIC_RELEASE);
394 if (*v != --res)
395 abort ();
396
397 __atomic_sub_fetch (v, count + 1, __ATOMIC_ACQ_REL);
398 if (*v != --res)
399 abort ();
400
401 __atomic_fetch_sub (v, count + 1, __ATOMIC_SEQ_CST);
402 if (*v != --res)
403 abort ();
404 }
405
406 void
407 test_and (short* v)
408 {
409 *v = init;
410
411 __atomic_and_fetch (v, 0, __ATOMIC_RELAXED);
412 if (*v != 0)
413 abort ();
414
415 *v = init;
416 __atomic_fetch_and (v, init, __ATOMIC_CONSUME);
417 if (*v != init)
418 abort ();
419
420 __atomic_and_fetch (v, 0, __ATOMIC_ACQUIRE);
421 if (*v != 0)
422 abort ();
423
424 *v = ~*v;
425 __atomic_fetch_and (v, init, __ATOMIC_RELEASE);
426 if (*v != init)
427 abort ();
428
429 __atomic_and_fetch (v, 0, __ATOMIC_ACQ_REL);
430 if (*v != 0)
431 abort ();
432
433 *v = ~*v;
434 __atomic_fetch_and (v, 0, __ATOMIC_SEQ_CST);
435 if (*v != 0)
436 abort ();
437 }
438
439 void
440 test_nand (short* v)
441 {
442 *v = init;
443
444 __atomic_fetch_nand (v, 0, __ATOMIC_RELAXED);
445 if (*v != init)
446 abort ();
447
448 __atomic_fetch_nand (v, init, __ATOMIC_CONSUME);
449 if (*v != 0)
450 abort ();
451
452 __atomic_nand_fetch (v, 0, __ATOMIC_ACQUIRE);
453 if (*v != init)
454 abort ();
455
456 __atomic_nand_fetch (v, init, __ATOMIC_RELEASE);
457 if (*v != 0)
458 abort ();
459
460 __atomic_fetch_nand (v, init, __ATOMIC_ACQ_REL);
461 if (*v != init)
462 abort ();
463
464 __atomic_nand_fetch (v, 0, __ATOMIC_SEQ_CST);
465 if (*v != init)
466 abort ();
467 }
468
469
470
471 void
472 test_xor (short* v)
473 {
474 *v = init;
475 count = 0;
476
477 __atomic_xor_fetch (v, count, __ATOMIC_RELAXED);
478 if (*v != init)
479 abort ();
480
481 __atomic_fetch_xor (v, ~count, __ATOMIC_CONSUME);
482 if (*v != 0)
483 abort ();
484
485 __atomic_xor_fetch (v, 0, __ATOMIC_ACQUIRE);
486 if (*v != 0)
487 abort ();
488
489 __atomic_fetch_xor (v, ~count, __ATOMIC_RELEASE);
490 if (*v != init)
491 abort ();
492
493 __atomic_fetch_xor (v, 0, __ATOMIC_ACQ_REL);
494 if (*v != init)
495 abort ();
496
497 __atomic_xor_fetch (v, ~count, __ATOMIC_SEQ_CST);
498 if (*v != 0)
499 abort ();
500 }
501
502 void
503 test_or (short* v)
504 {
505 *v = 0;
506 count = 1;
507
508 __atomic_or_fetch (v, count, __ATOMIC_RELAXED);
509 if (*v != 1)
510 abort ();
511
512 count *= 2;
513 __atomic_fetch_or (v, count, __ATOMIC_CONSUME);
514 if (*v != 3)
515 abort ();
516
517 count *= 2;
518 __atomic_or_fetch (v, 4, __ATOMIC_ACQUIRE);
519 if (*v != 7)
520 abort ();
521
522 count *= 2;
523 __atomic_fetch_or (v, 8, __ATOMIC_RELEASE);
524 if (*v != 15)
525 abort ();
526
527 count *= 2;
528 __atomic_or_fetch (v, count, __ATOMIC_ACQ_REL);
529 if (*v != 31)
530 abort ();
531
532 count *= 2;
533 __atomic_fetch_or (v, count, __ATOMIC_SEQ_CST);
534 if (*v != 63)
535 abort ();
536 }
537
538 int
539 main () {
540 short* V[] = {&A.a, &A.b};
541
542 for (int i = 0; i < 2; i++) {
543 test_fetch_add (V[i]);
544 test_fetch_sub (V[i]);
545 test_fetch_and (V[i]);
546 test_fetch_nand (V[i]);
547 test_fetch_xor (V[i]);
548 test_fetch_or (V[i]);
549
550 test_add_fetch (V[i]);
551 test_sub_fetch (V[i]);
552 test_and_fetch (V[i]);
553 test_nand_fetch (V[i]);
554 test_xor_fetch (V[i]);
555 test_or_fetch (V[i]);
556
557 test_add (V[i]);
558 test_sub (V[i]);
559 test_and (V[i]);
560 test_nand (V[i]);
561 test_xor (V[i]);
562 test_or (V[i]);
563 }
564
565 return 0;
566 }