Debian Codegraph @

gcc/testsuite/gcc.dg/atomic/stdatomic-bitint-1.c

raw
1 /* PR c/102989 */
2 /* { dg-do run { target bitint } } */
3 /* { dg-options "-std=c23 -pedantic-errors" } */
4
5 #include <stdatomic.h>
6
7 extern void abort (void);
8
9 #if __BITINT_MAXWIDTH__ >= 127
10 _Atomic _BitInt(127) v;
11 _BitInt(127) count, res;
12 const _BitInt(127) init = ~(_BitInt(127)) 0wb;
13
14 void
15 test_fetch_add ()
16 {
17 atomic_init (&v, 13505789527944801758751150119415226784wb);
18 count = -64910836855286429164283779649638556795wb;
19
20 if (atomic_fetch_add_explicit (&v, count, memory_order_relaxed)
21 != 13505789527944801758751150119415226784wb)
22 abort ();
23
24 if (atomic_fetch_add_explicit (&v, 2227507280963412295355244564739509222wb,
25 memory_order_consume)
26 != -51405047327341627405532629530223330011wb)
27 abort ();
28
29 if (atomic_fetch_add_explicit (&v, count, memory_order_acquire)
30 != -49177540046378215110177384965483820789wb)
31 abort ();
32
33 if (atomic_fetch_add_explicit (&v, 42245667388877614520169143618236120405wb,
34 memory_order_release)
35 != 56052806558804587457226139100761728144wb)
36 abort ();
37
38 if (atomic_fetch_add_explicit (&v, count, memory_order_acq_rel)
39 != -71842709512787029754292020996886257179wb)
40 abort ();
41
42 if (atomic_fetch_add_explicit (&v, 77995075987640754057679086146674392947wb,
43 memory_order_seq_cst)
44 != 33387637092395772813111503069359291754wb)
45 abort ();
46
47 if (atomic_fetch_add (&v, 11810767284628435493328779846084830297wb)
48 != -58758470380432704860896714499850421027wb)
49 abort ();
50
51 if (atomic_load (&v) != -46947703095804269367567934653765590730wb)
52 abort ();
53 }
54
55 void
56 test_fetch_sub ()
57 {
58 atomic_store_explicit (&v, 30796781768365552851024605388374299173wb,
59 memory_order_release);
60 count = 32457177597484647488149720668185011722wb;
61
62 if (atomic_fetch_sub_explicit (&v, count, memory_order_relaxed)
63 != 30796781768365552851024605388374299173wb)
64 abort ();
65
66 if (atomic_fetch_sub_explicit (&v, 54614103079293459991417218347656369566wb,
67 memory_order_consume)
68 != -1660395829119094637125115279810712549wb)
69 abort ();
70
71 if (atomic_fetch_sub_explicit (&v, count, memory_order_acquire)
72 != -56274498908412554628542333627467082115wb)
73 abort ();
74
75 if (atomic_fetch_sub_explicit (&v, -44514083923735151931107302009741400482wb,
76 memory_order_release)
77 != 81409506954572029614995249420232011891wb)
78 abort ();
79
80 if (atomic_fetch_sub_explicit (&v, count, memory_order_acq_rel)
81 != -44217592582162050185584752285910693355wb)
82 abort ();
83
84 if (atomic_fetch_sub_explicit (&v, 30348078982452392099140613411731040827wb,
85 memory_order_seq_cst)
86 != -76674770179646697673734472954095705077wb)
87 abort ();
88
89 if (atomic_fetch_sub (&v, -82224045897086857020012824788652775087wb)
90 != 63118334298370141958812217350057359824wb)
91 abort ();
92
93 if (atomic_load_explicit (&v, memory_order_acquire)
94 != -24798803265012232752862261577173970817wb)
95 abort ();
96 }
97
98 void
99 test_fetch_and ()
100 {
101 atomic_store (&v, init);
102
103 if (atomic_fetch_and_explicit (&v, 0, memory_order_relaxed) != init)
104 abort ();
105
106 if (atomic_fetch_and_explicit (&v, init, memory_order_consume) != 0)
107 abort ();
108
109 if (atomic_fetch_and_explicit (&v, 0, memory_order_acquire) != 0)
110 abort ();
111
112 v = ~v;
113 if (atomic_fetch_and_explicit (&v, init, memory_order_release) != init)
114 abort ();
115
116 if (atomic_fetch_and_explicit (&v, 0, memory_order_acq_rel) != init)
117 abort ();
118
119 if (atomic_fetch_and_explicit (&v, 0, memory_order_seq_cst) != 0)
120 abort ();
121
122 if (atomic_fetch_and (&v, 0) != 0)
123 abort ();
124 }
125
126 void
127 test_fetch_xor ()
128 {
129 v = init;
130 count = 0;
131
132 if (atomic_fetch_xor_explicit (&v, count, memory_order_relaxed) != init)
133 abort ();
134
135 if (atomic_fetch_xor_explicit (&v, ~count, memory_order_consume) != init)
136 abort ();
137
138 if (atomic_fetch_xor_explicit (&v, 0, memory_order_acquire) != 0)
139 abort ();
140
141 if (atomic_fetch_xor_explicit (&v, ~count, memory_order_release) != 0)
142 abort ();
143
144 if (atomic_fetch_xor_explicit (&v, 0, memory_order_acq_rel) != init)
145 abort ();
146
147 if (atomic_fetch_xor_explicit (&v, ~count, memory_order_seq_cst) != init)
148 abort ();
149
150 if (atomic_fetch_xor (&v, ~count) != 0)
151 abort ();
152 }
153
154 void
155 test_fetch_or ()
156 {
157 v = 0;
158 count = 17592186044416wb;
159
160 if (atomic_fetch_or_explicit (&v, count, memory_order_relaxed) != 0)
161 abort ();
162
163 count *= 2;
164 if (atomic_fetch_or_explicit (&v, 35184372088832wb, memory_order_consume)
165 != 17592186044416wb)
166 abort ();
167
168 count *= 2;
169 if (atomic_fetch_or_explicit (&v, count, memory_order_acquire)
170 != 52776558133248wb)
171 abort ();
172
173 count *= 2;
174 if (atomic_fetch_or_explicit (&v, 140737488355328wb, memory_order_release)
175 != 123145302310912wb)
176 abort ();
177
178 count *= 2;
179 if (atomic_fetch_or_explicit (&v, count, memory_order_acq_rel)
180 != 263882790666240wb)
181 abort ();
182
183 count *= 2;
184 if (atomic_fetch_or_explicit (&v, count, memory_order_seq_cst)
185 != 545357767376896wb)
186 abort ();
187
188 count *= 2;
189 if (atomic_fetch_or (&v, count) != 1108307720798208wb)
190 abort ();
191 }
192
193
194 /* Test the OP routines with a result which isn't used. */
195
196 void
197 test_add ()
198 {
199 v = 0;
200 count = 4722366482869645213696wb;
201
202 atomic_fetch_add (&v, count);
203 if (v != 4722366482869645213696wb)
204 abort ();
205
206 atomic_fetch_add_explicit (&v, count, memory_order_consume);
207 if (v != 9444732965739290427392wb)
208 abort ();
209
210 atomic_fetch_add (&v, 4722366482869645213696wb);
211 if (v != 14167099448608935641088wb)
212 abort ();
213
214 atomic_fetch_add_explicit (&v, 4722366482869645213696wb,
215 memory_order_release);
216 if (v != 18889465931478580854784wb)
217 abort ();
218
219 atomic_fetch_add (&v, 4722366482869645213696wb);
220 if (v != 23611832414348226068480wb)
221 abort ();
222
223 atomic_fetch_add_explicit (&v, count, memory_order_seq_cst);
224 if (v != 28334198897217871282176wb)
225 abort ();
226 }
227
228 void
229 test_sub ()
230 {
231 v = res = -3638804536836293398783417724445294828wb;
232 count = 0;
233
234 atomic_fetch_sub (&v, count + 1);
235 if (v != --res)
236 abort ();
237
238 atomic_fetch_sub_explicit (&v, count + 1, memory_order_consume);
239 if (v != --res)
240 abort ();
241
242 atomic_fetch_sub (&v, 1);
243 if (v != --res)
244 abort ();
245
246 atomic_fetch_sub_explicit (&v, 1, memory_order_release);
247 if (v != --res)
248 abort ();
249
250 atomic_fetch_sub (&v, count + 1);
251 if (v != --res)
252 abort ();
253
254 atomic_fetch_sub_explicit (&v, count + 1, memory_order_seq_cst);
255 if (v != --res)
256 abort ();
257 }
258
259 void
260 test_and ()
261 {
262 v = init;
263
264 atomic_fetch_and (&v, 0);
265 if (v != 0)
266 abort ();
267
268 v = init;
269 atomic_fetch_and_explicit (&v, init, memory_order_consume);
270 if (v != init)
271 abort ();
272
273 atomic_fetch_and (&v, 0);
274 if (v != 0)
275 abort ();
276
277 v = ~v;
278 atomic_fetch_and_explicit (&v, init, memory_order_release);
279 if (v != init)
280 abort ();
281
282 atomic_fetch_and (&v, 0);
283 if (v != 0)
284 abort ();
285
286 v = ~v;
287 atomic_fetch_and_explicit (&v, 0, memory_order_seq_cst);
288 if (v != 0)
289 abort ();
290 }
291
292 void
293 test_xor ()
294 {
295 v = init;
296 count = 0;
297
298 atomic_fetch_xor (&v, count);
299 if (v != init)
300 abort ();
301
302 atomic_fetch_xor_explicit (&v, ~count, memory_order_consume);
303 if (v != 0)
304 abort ();
305
306 atomic_fetch_xor (&v, 0);
307 if (v != 0)
308 abort ();
309
310 atomic_fetch_xor_explicit (&v, ~count, memory_order_release);
311 if (v != init)
312 abort ();
313
314 atomic_fetch_xor_explicit (&v, 0, memory_order_acq_rel);
315 if (v != init)
316 abort ();
317
318 atomic_fetch_xor (&v, ~count);
319 if (v != 0)
320 abort ();
321 }
322
323 void
324 test_or ()
325 {
326 v = 0;
327 count = 19342813113834066795298816wb;
328
329 atomic_fetch_or (&v, count);
330 if (v != 19342813113834066795298816wb)
331 abort ();
332
333 count *= 2;
334 atomic_fetch_or_explicit (&v, count, memory_order_consume);
335 if (v != 58028439341502200385896448wb)
336 abort ();
337
338 count *= 2;
339 atomic_fetch_or (&v, 77371252455336267181195264wb);
340 if (v != 135399691796838467567091712wb)
341 abort ();
342
343 count *= 2;
344 atomic_fetch_or_explicit (&v, 154742504910672534362390528wb,
345 memory_order_release);
346 if (v != 290142196707511001929482240wb)
347 abort ();
348
349 count *= 2;
350 atomic_fetch_or (&v, count);
351 if (v != 599627206528856070654263296wb)
352 abort ();
353
354 count *= 2;
355 atomic_fetch_or_explicit (&v, count, memory_order_seq_cst);
356 if (v != 1218597226171546208103825408wb)
357 abort ();
358 }
359
360 void
361 test_exchange (void)
362 {
363 atomic_store (&v, 15794812138349191682564933935017390008wb);
364 if (atomic_exchange (&v, -2166613183393424891717146518563613668wb)
365 != 15794812138349191682564933935017390008wb
366 || atomic_load (&v) != -2166613183393424891717146518563613668wb)
367 abort ();
368 if (atomic_exchange_explicit (&v, 61251098386268815852902382804483910638wb,
369 memory_order_relaxed)
370 != -2166613183393424891717146518563613668wb
371 || (atomic_load_explicit (&v, memory_order_acquire)
372 != 61251098386268815852902382804483910638wb))
373 abort ();
374
375 count = -2166613183393424891717146518563613668wb;
376 if (atomic_compare_exchange_strong (&v, &count,
377 -36677332297536901313774263310237646448wb))
378 abort ();
379 if (count != 61251098386268815852902382804483910638wb
380 || atomic_load (&v) != 61251098386268815852902382804483910638wb)
381 abort ();
382 if (!atomic_compare_exchange_strong (&v, &count,
383 -36677332297536901313774263310237646448wb))
384 abort ();
385 if (count != 61251098386268815852902382804483910638wb
386 || atomic_load (&v) != -36677332297536901313774263310237646448wb)
387 abort ();
388
389 count = -2166613183393424891717146518563613668wb;
390 if (atomic_compare_exchange_strong_explicit (&v, &count,
391 73949932022761409003352953944661689416wb,
392 memory_order_seq_cst,
393 memory_order_relaxed))
394 abort ();
395 if (count != -36677332297536901313774263310237646448wb
396 || atomic_load (&v) != -36677332297536901313774263310237646448wb)
397 abort ();
398 if (!atomic_compare_exchange_strong_explicit (&v, &count,
399 73949932022761409003352953944661689416wb,
400 memory_order_seq_cst,
401 memory_order_seq_cst))
402 abort ();
403 if (count != -36677332297536901313774263310237646448wb
404 || atomic_load (&v) != 73949932022761409003352953944661689416wb)
405 abort ();
406
407 count = atomic_load (&v);
408 do
409 res = count + -82256758205518164043596305502815392646wb;
410 while (!atomic_compare_exchange_weak (&v, &count, res));
411 if (atomic_load (&v) != -8306826182756755040243351558153703230wb)
412 abort ();
413
414 count = atomic_load_explicit (&v, memory_order_acquire);
415 do
416 res = count + 48855144829609538366772317026461909818wb;
417 while (!atomic_compare_exchange_weak_explicit (&v, &count, res,
418 memory_order_relaxed,
419 memory_order_relaxed));
420 if (atomic_load (&v) != 40548318646852783326528965468308206588wb)
421 abort ();
422 }
423 #endif
424
425 int
426 main ()
427 {
428 #if __BITINT_MAXWIDTH__ >= 127
429 test_fetch_add ();
430 test_fetch_sub ();
431 test_fetch_and ();
432 test_fetch_xor ();
433 test_fetch_or ();
434 test_add ();
435 test_sub ();
436 test_and ();
437 test_xor ();
438 test_or ();
439 test_exchange ();
440 #endif
441 return 0;
442 }