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: %libomptarget-compile-generic -fopenmp-version=51
7 // RUN: %libomptarget-run-generic | %fcheck-generic
8 
9 // High parallelism increases our chances of detecting a lack of atomicity.
10 #define NUM_THREADS_TRY 256
11 
12 #include <limits.h>
13 #include <omp.h>
14 #include <stdio.h>
15 
16 int main() {
17   //      CHECK: signed: num_threads=[[#NUM_THREADS:]]{{$}}
18   // CHECK-NEXT: signed: xs=[[#NUM_THREADS-1]]{{$}}
19   int xs = -1;
20   int numThreads;
21   #pragma omp target parallel for num_threads(NUM_THREADS_TRY) \
22       map(tofrom:xs, numThreads)
23   for (int i = 0; i < omp_get_num_threads(); ++i) {
24     #pragma omp atomic compare
25     if (xs < i) { xs = i; }
26     if (i == 0)
27       numThreads = omp_get_num_threads();
28   }
29   printf("signed: num_threads=%d\n", numThreads);
30   printf("signed: xs=%d\n", xs);
31 
32   // CHECK-NEXT: unsigned: xu=0x0{{$}}
33   unsigned xu = UINT_MAX;
34   #pragma omp target parallel for num_threads(NUM_THREADS_TRY) \
35       map(tofrom:xu)
36   for (int i = 0; i < omp_get_num_threads(); ++i) {
37     #pragma omp atomic compare
38     if (xu > i) { xu = i; }
39   }
40   printf("unsigned: xu=0x%x\n", xu);
41   return 0;
42 }
43