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