xref: /llvm-project/openmp/runtime/test/atomic/omp-atomic-compare-signedness.c (revision 0c6f2f629cc0017361310fa4c132090413a874db)
1 // Check that omp atomic compare handles signedness of integer comparisons
2 // correctly.
3 //
4 // At one time, a bug sometimes reversed the signedness.
5 
6 // RUN: %libomp-compile
7 // RUN: %libomp-run | FileCheck %s
8 
9 // This test uses -fopenmp-version, which is not a compiler flag that GCC
10 // supports.
11 // UNSUPPORTED: gcc
12 
13 // High parallelism increases our chances of detecting a lack of atomicity.
14 #define NUM_THREADS_TRY 256
15 
16 #include <limits.h>
17 #include <omp.h>
18 #include <stdio.h>
19 
main()20 int main() {
21   //      CHECK: signed: num_threads=[[#NUM_THREADS:]]{{$}}
22   // CHECK-NEXT: signed: xs=[[#NUM_THREADS-1]]{{$}}
23   int xs = -1;
24   int numThreads;
25   #pragma omp parallel for num_threads(NUM_THREADS_TRY)
26   for (int i = 0; i < omp_get_num_threads(); ++i) {
27     #pragma omp atomic compare
28     if (xs < i) { xs = i; }
29     if (i == 0)
30       numThreads = omp_get_num_threads();
31   }
32   printf("signed: num_threads=%d\n", numThreads);
33   printf("signed: xs=%d\n", xs);
34 
35   // CHECK-NEXT: unsigned: xu=0x0{{$}}
36   unsigned xu = UINT_MAX;
37   #pragma omp parallel for num_threads(NUM_THREADS_TRY)
38   for (int i = 0; i < omp_get_num_threads(); ++i) {
39     #pragma omp atomic compare
40     if (xu > i) { xu = i; }
41   }
42   printf("unsigned: xu=0x%x\n", xu);
43   return 0;
44 }
45