]> git.ipfire.org Git - thirdparty/glibc.git/blob - misc/tst-atomic.c
aarch64: add STO_AARCH64_VARIANT_PCS and DT_AARCH64_VARIANT_PCS
[thirdparty/glibc.git] / misc / tst-atomic.c
1 /* Tests for atomic.h macros.
2 Copyright (C) 2003-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20 #include <stdio.h>
21 #include <atomic.h>
22
23 #ifndef atomic_t
24 # define atomic_t int
25 #endif
26
27 /* Test various atomic.h macros. */
28 static int
29 do_test (void)
30 {
31 atomic_t mem, expected;
32 int ret = 0;
33
34 #ifdef atomic_compare_and_exchange_val_acq
35 mem = 24;
36 if (atomic_compare_and_exchange_val_acq (&mem, 35, 24) != 24
37 || mem != 35)
38 {
39 puts ("atomic_compare_and_exchange_val_acq test 1 failed");
40 ret = 1;
41 }
42
43 mem = 12;
44 if (atomic_compare_and_exchange_val_acq (&mem, 10, 15) != 12
45 || mem != 12)
46 {
47 puts ("atomic_compare_and_exchange_val_acq test 2 failed");
48 ret = 1;
49 }
50
51 mem = -15;
52 if (atomic_compare_and_exchange_val_acq (&mem, -56, -15) != -15
53 || mem != -56)
54 {
55 puts ("atomic_compare_and_exchange_val_acq test 3 failed");
56 ret = 1;
57 }
58
59 mem = -1;
60 if (atomic_compare_and_exchange_val_acq (&mem, 17, 0) != -1
61 || mem != -1)
62 {
63 puts ("atomic_compare_and_exchange_val_acq test 4 failed");
64 ret = 1;
65 }
66 #endif
67
68 mem = 24;
69 if (atomic_compare_and_exchange_bool_acq (&mem, 35, 24)
70 || mem != 35)
71 {
72 puts ("atomic_compare_and_exchange_bool_acq test 1 failed");
73 ret = 1;
74 }
75
76 mem = 12;
77 if (! atomic_compare_and_exchange_bool_acq (&mem, 10, 15)
78 || mem != 12)
79 {
80 puts ("atomic_compare_and_exchange_bool_acq test 2 failed");
81 ret = 1;
82 }
83
84 mem = -15;
85 if (atomic_compare_and_exchange_bool_acq (&mem, -56, -15)
86 || mem != -56)
87 {
88 puts ("atomic_compare_and_exchange_bool_acq test 3 failed");
89 ret = 1;
90 }
91
92 mem = -1;
93 if (! atomic_compare_and_exchange_bool_acq (&mem, 17, 0)
94 || mem != -1)
95 {
96 puts ("atomic_compare_and_exchange_bool_acq test 4 failed");
97 ret = 1;
98 }
99
100 mem = 64;
101 if (atomic_exchange_acq (&mem, 31) != 64
102 || mem != 31)
103 {
104 puts ("atomic_exchange_acq test failed");
105 ret = 1;
106 }
107
108 mem = 2;
109 if (atomic_exchange_and_add (&mem, 11) != 2
110 || mem != 13)
111 {
112 puts ("atomic_exchange_and_add test failed");
113 ret = 1;
114 }
115
116 mem = 2;
117 if (atomic_exchange_and_add_acq (&mem, 11) != 2
118 || mem != 13)
119 {
120 puts ("atomic_exchange_and_add test failed");
121 ret = 1;
122 }
123
124 mem = 2;
125 if (atomic_exchange_and_add_rel (&mem, 11) != 2
126 || mem != 13)
127 {
128 puts ("atomic_exchange_and_add test failed");
129 ret = 1;
130 }
131
132 mem = -21;
133 atomic_add (&mem, 22);
134 if (mem != 1)
135 {
136 puts ("atomic_add test failed");
137 ret = 1;
138 }
139
140 mem = -1;
141 atomic_increment (&mem);
142 if (mem != 0)
143 {
144 puts ("atomic_increment test failed");
145 ret = 1;
146 }
147
148 mem = 2;
149 if (atomic_increment_val (&mem) != 3)
150 {
151 puts ("atomic_increment_val test failed");
152 ret = 1;
153 }
154
155 mem = 0;
156 if (atomic_increment_and_test (&mem)
157 || mem != 1)
158 {
159 puts ("atomic_increment_and_test test 1 failed");
160 ret = 1;
161 }
162
163 mem = 35;
164 if (atomic_increment_and_test (&mem)
165 || mem != 36)
166 {
167 puts ("atomic_increment_and_test test 2 failed");
168 ret = 1;
169 }
170
171 mem = -1;
172 if (! atomic_increment_and_test (&mem)
173 || mem != 0)
174 {
175 puts ("atomic_increment_and_test test 3 failed");
176 ret = 1;
177 }
178
179 mem = 17;
180 atomic_decrement (&mem);
181 if (mem != 16)
182 {
183 puts ("atomic_decrement test failed");
184 ret = 1;
185 }
186
187 if (atomic_decrement_val (&mem) != 15)
188 {
189 puts ("atomic_decrement_val test failed");
190 ret = 1;
191 }
192
193 mem = 0;
194 if (atomic_decrement_and_test (&mem)
195 || mem != -1)
196 {
197 puts ("atomic_decrement_and_test test 1 failed");
198 ret = 1;
199 }
200
201 mem = 15;
202 if (atomic_decrement_and_test (&mem)
203 || mem != 14)
204 {
205 puts ("atomic_decrement_and_test test 2 failed");
206 ret = 1;
207 }
208
209 mem = 1;
210 if (! atomic_decrement_and_test (&mem)
211 || mem != 0)
212 {
213 puts ("atomic_decrement_and_test test 3 failed");
214 ret = 1;
215 }
216
217 mem = 1;
218 if (atomic_decrement_if_positive (&mem) != 1
219 || mem != 0)
220 {
221 puts ("atomic_decrement_if_positive test 1 failed");
222 ret = 1;
223 }
224
225 mem = 0;
226 if (atomic_decrement_if_positive (&mem) != 0
227 || mem != 0)
228 {
229 puts ("atomic_decrement_if_positive test 2 failed");
230 ret = 1;
231 }
232
233 mem = -1;
234 if (atomic_decrement_if_positive (&mem) != -1
235 || mem != -1)
236 {
237 puts ("atomic_decrement_if_positive test 3 failed");
238 ret = 1;
239 }
240
241 mem = -12;
242 if (! atomic_add_negative (&mem, 10)
243 || mem != -2)
244 {
245 puts ("atomic_add_negative test 1 failed");
246 ret = 1;
247 }
248
249 mem = 0;
250 if (atomic_add_negative (&mem, 100)
251 || mem != 100)
252 {
253 puts ("atomic_add_negative test 2 failed");
254 ret = 1;
255 }
256
257 mem = 15;
258 if (atomic_add_negative (&mem, -10)
259 || mem != 5)
260 {
261 puts ("atomic_add_negative test 3 failed");
262 ret = 1;
263 }
264
265 mem = -12;
266 if (atomic_add_negative (&mem, 14)
267 || mem != 2)
268 {
269 puts ("atomic_add_negative test 4 failed");
270 ret = 1;
271 }
272
273 mem = 0;
274 if (! atomic_add_negative (&mem, -1)
275 || mem != -1)
276 {
277 puts ("atomic_add_negative test 5 failed");
278 ret = 1;
279 }
280
281 mem = -31;
282 if (atomic_add_negative (&mem, 31)
283 || mem != 0)
284 {
285 puts ("atomic_add_negative test 6 failed");
286 ret = 1;
287 }
288
289 mem = -34;
290 if (atomic_add_zero (&mem, 31)
291 || mem != -3)
292 {
293 puts ("atomic_add_zero test 1 failed");
294 ret = 1;
295 }
296
297 mem = -36;
298 if (! atomic_add_zero (&mem, 36)
299 || mem != 0)
300 {
301 puts ("atomic_add_zero test 2 failed");
302 ret = 1;
303 }
304
305 mem = 113;
306 if (atomic_add_zero (&mem, -13)
307 || mem != 100)
308 {
309 puts ("atomic_add_zero test 3 failed");
310 ret = 1;
311 }
312
313 mem = -18;
314 if (atomic_add_zero (&mem, 20)
315 || mem != 2)
316 {
317 puts ("atomic_add_zero test 4 failed");
318 ret = 1;
319 }
320
321 mem = 10;
322 if (atomic_add_zero (&mem, -20)
323 || mem != -10)
324 {
325 puts ("atomic_add_zero test 5 failed");
326 ret = 1;
327 }
328
329 mem = 10;
330 if (! atomic_add_zero (&mem, -10)
331 || mem != 0)
332 {
333 puts ("atomic_add_zero test 6 failed");
334 ret = 1;
335 }
336
337 mem = 0;
338 atomic_bit_set (&mem, 1);
339 if (mem != 2)
340 {
341 puts ("atomic_bit_set test 1 failed");
342 ret = 1;
343 }
344
345 mem = 8;
346 atomic_bit_set (&mem, 3);
347 if (mem != 8)
348 {
349 puts ("atomic_bit_set test 2 failed");
350 ret = 1;
351 }
352
353 #ifdef TEST_ATOMIC64
354 mem = 16;
355 atomic_bit_set (&mem, 35);
356 if (mem != 0x800000010LL)
357 {
358 puts ("atomic_bit_set test 3 failed");
359 ret = 1;
360 }
361 #endif
362
363 mem = 0;
364 if (atomic_bit_test_set (&mem, 1)
365 || mem != 2)
366 {
367 puts ("atomic_bit_test_set test 1 failed");
368 ret = 1;
369 }
370
371 mem = 8;
372 if (! atomic_bit_test_set (&mem, 3)
373 || mem != 8)
374 {
375 puts ("atomic_bit_test_set test 2 failed");
376 ret = 1;
377 }
378
379 #ifdef TEST_ATOMIC64
380 mem = 16;
381 if (atomic_bit_test_set (&mem, 35)
382 || mem != 0x800000010LL)
383 {
384 puts ("atomic_bit_test_set test 3 failed");
385 ret = 1;
386 }
387
388 mem = 0x100000000LL;
389 if (! atomic_bit_test_set (&mem, 32)
390 || mem != 0x100000000LL)
391 {
392 puts ("atomic_bit_test_set test 4 failed");
393 ret = 1;
394 }
395 #endif
396
397 #ifdef catomic_compare_and_exchange_val_acq
398 mem = 24;
399 if (catomic_compare_and_exchange_val_acq (&mem, 35, 24) != 24
400 || mem != 35)
401 {
402 puts ("catomic_compare_and_exchange_val_acq test 1 failed");
403 ret = 1;
404 }
405
406 mem = 12;
407 if (catomic_compare_and_exchange_val_acq (&mem, 10, 15) != 12
408 || mem != 12)
409 {
410 puts ("catomic_compare_and_exchange_val_acq test 2 failed");
411 ret = 1;
412 }
413
414 mem = -15;
415 if (catomic_compare_and_exchange_val_acq (&mem, -56, -15) != -15
416 || mem != -56)
417 {
418 puts ("catomic_compare_and_exchange_val_acq test 3 failed");
419 ret = 1;
420 }
421
422 mem = -1;
423 if (catomic_compare_and_exchange_val_acq (&mem, 17, 0) != -1
424 || mem != -1)
425 {
426 puts ("catomic_compare_and_exchange_val_acq test 4 failed");
427 ret = 1;
428 }
429 #endif
430
431 mem = 24;
432 if (catomic_compare_and_exchange_bool_acq (&mem, 35, 24)
433 || mem != 35)
434 {
435 puts ("catomic_compare_and_exchange_bool_acq test 1 failed");
436 ret = 1;
437 }
438
439 mem = 12;
440 if (! catomic_compare_and_exchange_bool_acq (&mem, 10, 15)
441 || mem != 12)
442 {
443 puts ("catomic_compare_and_exchange_bool_acq test 2 failed");
444 ret = 1;
445 }
446
447 mem = -15;
448 if (catomic_compare_and_exchange_bool_acq (&mem, -56, -15)
449 || mem != -56)
450 {
451 puts ("catomic_compare_and_exchange_bool_acq test 3 failed");
452 ret = 1;
453 }
454
455 mem = -1;
456 if (! catomic_compare_and_exchange_bool_acq (&mem, 17, 0)
457 || mem != -1)
458 {
459 puts ("catomic_compare_and_exchange_bool_acq test 4 failed");
460 ret = 1;
461 }
462
463 mem = 2;
464 if (catomic_exchange_and_add (&mem, 11) != 2
465 || mem != 13)
466 {
467 puts ("catomic_exchange_and_add test failed");
468 ret = 1;
469 }
470
471 mem = -21;
472 catomic_add (&mem, 22);
473 if (mem != 1)
474 {
475 puts ("catomic_add test failed");
476 ret = 1;
477 }
478
479 mem = -1;
480 catomic_increment (&mem);
481 if (mem != 0)
482 {
483 puts ("catomic_increment test failed");
484 ret = 1;
485 }
486
487 mem = 2;
488 if (catomic_increment_val (&mem) != 3)
489 {
490 puts ("catomic_increment_val test failed");
491 ret = 1;
492 }
493
494 mem = 17;
495 catomic_decrement (&mem);
496 if (mem != 16)
497 {
498 puts ("catomic_decrement test failed");
499 ret = 1;
500 }
501
502 if (catomic_decrement_val (&mem) != 15)
503 {
504 puts ("catomic_decrement_val test failed");
505 ret = 1;
506 }
507
508 /* Tests for C11-like atomics. */
509 mem = 11;
510 if (atomic_load_relaxed (&mem) != 11 || atomic_load_acquire (&mem) != 11)
511 {
512 puts ("atomic_load_{relaxed,acquire} test failed");
513 ret = 1;
514 }
515
516 atomic_store_relaxed (&mem, 12);
517 if (mem != 12)
518 {
519 puts ("atomic_store_relaxed test failed");
520 ret = 1;
521 }
522 atomic_store_release (&mem, 13);
523 if (mem != 13)
524 {
525 puts ("atomic_store_release test failed");
526 ret = 1;
527 }
528
529 mem = 14;
530 expected = 14;
531 if (!atomic_compare_exchange_weak_relaxed (&mem, &expected, 25)
532 || mem != 25 || expected != 14)
533 {
534 puts ("atomic_compare_exchange_weak_relaxed test 1 failed");
535 ret = 1;
536 }
537 if (atomic_compare_exchange_weak_relaxed (&mem, &expected, 14)
538 || mem != 25 || expected != 25)
539 {
540 puts ("atomic_compare_exchange_weak_relaxed test 2 failed");
541 ret = 1;
542 }
543 mem = 14;
544 expected = 14;
545 if (!atomic_compare_exchange_weak_acquire (&mem, &expected, 25)
546 || mem != 25 || expected != 14)
547 {
548 puts ("atomic_compare_exchange_weak_acquire test 1 failed");
549 ret = 1;
550 }
551 if (atomic_compare_exchange_weak_acquire (&mem, &expected, 14)
552 || mem != 25 || expected != 25)
553 {
554 puts ("atomic_compare_exchange_weak_acquire test 2 failed");
555 ret = 1;
556 }
557 mem = 14;
558 expected = 14;
559 if (!atomic_compare_exchange_weak_release (&mem, &expected, 25)
560 || mem != 25 || expected != 14)
561 {
562 puts ("atomic_compare_exchange_weak_release test 1 failed");
563 ret = 1;
564 }
565 if (atomic_compare_exchange_weak_release (&mem, &expected, 14)
566 || mem != 25 || expected != 25)
567 {
568 puts ("atomic_compare_exchange_weak_release test 2 failed");
569 ret = 1;
570 }
571
572 mem = 23;
573 if (atomic_exchange_acquire (&mem, 42) != 23 || mem != 42)
574 {
575 puts ("atomic_exchange_acquire test failed");
576 ret = 1;
577 }
578 mem = 23;
579 if (atomic_exchange_release (&mem, 42) != 23 || mem != 42)
580 {
581 puts ("atomic_exchange_release test failed");
582 ret = 1;
583 }
584
585 mem = 23;
586 if (atomic_fetch_add_relaxed (&mem, 1) != 23 || mem != 24)
587 {
588 puts ("atomic_fetch_add_relaxed test failed");
589 ret = 1;
590 }
591 mem = 23;
592 if (atomic_fetch_add_acquire (&mem, 1) != 23 || mem != 24)
593 {
594 puts ("atomic_fetch_add_acquire test failed");
595 ret = 1;
596 }
597 mem = 23;
598 if (atomic_fetch_add_release (&mem, 1) != 23 || mem != 24)
599 {
600 puts ("atomic_fetch_add_release test failed");
601 ret = 1;
602 }
603 mem = 23;
604 if (atomic_fetch_add_acq_rel (&mem, 1) != 23 || mem != 24)
605 {
606 puts ("atomic_fetch_add_acq_rel test failed");
607 ret = 1;
608 }
609
610 mem = 3;
611 if (atomic_fetch_and_acquire (&mem, 2) != 3 || mem != 2)
612 {
613 puts ("atomic_fetch_and_acquire test failed");
614 ret = 1;
615 }
616
617 mem = 4;
618 if (atomic_fetch_or_relaxed (&mem, 2) != 4 || mem != 6)
619 {
620 puts ("atomic_fetch_or_relaxed test failed");
621 ret = 1;
622 }
623 mem = 4;
624 if (atomic_fetch_or_acquire (&mem, 2) != 4 || mem != 6)
625 {
626 puts ("atomic_fetch_or_acquire test failed");
627 ret = 1;
628 }
629
630 /* This is a single-threaded test, so we can't test the effects of the
631 fences. */
632 atomic_thread_fence_acquire ();
633 atomic_thread_fence_release ();
634 atomic_thread_fence_seq_cst ();
635
636 return ret;
637 }
638
639 #include <support/test-driver.c>