PR c++/86342 - -Wdeprecated-copy and system headers.
[official-gcc.git] / libgomp / testsuite / libgomp.oacc-c-c++-common / reduction-dbl.c
blob987c4ccba5ffab1cc897a787848e162d70ecb099
2 /* Double float has 53 bits of fraction. */
3 #define FRAC (1.0 / (1LL << 48))
4 typedef double Type;
6 int close_enough (Type a, Type b)
8 Type diff = a - b;
9 if (diff < 0)
10 diff = -diff;
12 return diff / a < FRAC;
15 #define N 100
17 static int __attribute__ ((noinline))
18 vector (Type ary[N], Type sum, Type prod)
20 Type tsum = 0, tprod = 1;
22 #pragma acc parallel vector_length(32) copyin(ary[0:N])
24 #pragma acc loop vector reduction(+:tsum) reduction (*:tprod)
25 for (int ix = 0; ix < N; ix++)
27 tsum += ary[ix];
28 tprod *= ary[ix];
32 if (!close_enough (sum, tsum))
33 return 1;
35 if (!close_enough (prod, tprod))
36 return 1;
38 return 0;
41 static int __attribute__ ((noinline))
42 worker (Type ary[N], Type sum, Type prod)
44 Type tsum = 0, tprod = 1;
46 #pragma acc parallel num_workers(32) copyin(ary[0:N])
48 #pragma acc loop worker reduction(+:tsum) reduction (*:tprod)
49 for (int ix = 0; ix < N; ix++)
51 tsum += ary[ix];
52 tprod *= ary[ix];
56 if (!close_enough (sum, tsum))
57 return 1;
59 if (!close_enough (prod, tprod))
60 return 1;
62 return 0;
65 static int __attribute__ ((noinline))
66 gang (Type ary[N], Type sum, Type prod)
68 Type tsum = 0, tprod = 1;
70 #pragma acc parallel num_gangs (32) copyin(ary[0:N])
72 #pragma acc loop gang reduction(+:tsum) reduction (*:tprod)
73 for (int ix = 0; ix < N; ix++)
75 tsum += ary[ix];
76 tprod *= ary[ix];
80 if (!close_enough (sum, tsum))
81 return 1;
83 if (!close_enough (prod, tprod))
84 return 1;
86 return 0;
89 int main (void)
91 Type ary[N], sum = 0, prod = 1;
93 for (int ix = 0; ix < N; ix++)
95 float frac = ix * (1.0f / 1024) + 1.0f;
97 ary[ix] = frac;
98 sum += ary[ix];
99 prod *= ary[ix];
102 if (vector (ary, sum, prod))
103 return 1;
105 if (worker (ary, sum, prod))
106 return 1;
108 if (gang (ary, sum, prod))
109 return 1;
111 return 0;