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