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