2018-01-22 Sebastian Perta <sebastian.perta@renesas.com>
[official-gcc.git] / libgomp / testsuite / libgomp.c++ / reduction-10.C
blob2254430f168c60b4636103db4d853710c75e12c3
1 template <typename T>
2 struct A
4   A () { t = 0; }
5   A (T x) { t = x; }
6   A (const A &x) { t = x.t; }
7   ~A () {}
8   T t;
9 };
10 template <typename T>
11 struct M
13   M () { t = 1; }
14   M (T x) { t = x; }
15   M (const M &x) { t = x.t; }
16   ~M () {}
17   T t;
19 template <typename T>
20 struct B
22   B () { t = ~(T) 0; }
23   B (T x) { t = x; }
24   B (const B &x) { t = x.t; }
25   ~B () {}
26   T t;
28 template <typename T>
29 void
30 add (T &x, T &y)
32   x.t += y.t;
34 template <typename T>
35 void
36 zero (T &x)
38   x.t = 0;
40 template <typename T>
41 void
42 orit (T *x, T *y)
44   y->t |= x->t;
46 B<long> bb;
47 #pragma omp declare reduction(+:A<int>:omp_out.t += omp_in.t)
48 #pragma omp declare reduction(+:A<char>:add (omp_out, omp_in)) initializer(zero (omp_priv))
49 #pragma omp declare reduction(*:M<int>:omp_out.t *= omp_in.t) initializer(omp_priv = 1)
50 #pragma omp declare reduction(|:A<unsigned long long>:orit (&omp_in, &omp_out))
51 #pragma omp declare reduction(&:B<long>:omp_out.t = omp_out.t & omp_in.t) initializer(orit (&omp_priv, &omp_orig))
52 #pragma omp declare reduction(maxb:short:omp_out = omp_in > omp_out ? omp_in : omp_out) initializer(omp_priv = -6)
54 A<char> z[10];
56 template <int N>
57 __attribute__((noinline, noclone)) void
58 foo (A<int> (*&x)[3][N], M<int> *y, B<long> (&w)[1][N], int p1, long p2, long p3, int p4,
59      int p5, long p6, short p7)
61   A<unsigned long long> a[p7 + 4];
62   short bb[p7];
63   short (&b)[p7] = bb;
64   for (int i = 0; i < p7; i++)
65     bb[i] = -6;
66   #pragma omp parallel for reduction(+:x[0:p1 + 1][:p2 + N - 2], z[:p3]) \
67                            reduction(*:y[:p4]) reduction(|:a[:p5 - N + 2]) \
68                            reduction(&:w[0:p6 - 3 + N][:p6]) reduction(maxb:b)
69   for (int i = 0; i < 128; i++)
70     {
71       x[i / 64][i % 3][(i / 4) & 1].t += i;
72       if ((i & 15) == 1)
73         y[0].t *= 3;
74       if ((i & 31) == N)
75         y[1].t *= 7;
76       if ((i & 63) == 3)
77         y[N].t *= 17;
78       z[i / 32].t += (i & 3);
79       if (i < 4)
80         z[i].t += i;
81       a[i / 32].t |= 1ULL << (i & 30);
82       w[0][i & 1].t &= ~(1L << (i / 17 * 3));
83       if ((i % 79) > b[0])
84         b[0] = i % 79;
85       if ((i % 13) > b[1])
86         b[1] = i % 13;
87       if ((i % 23) > b[N])
88         b[N] = i % 23;
89       if ((i % 85) > b[3])
90         b[3] = i % 85;
91       if ((i % 192) > b[4])
92         b[4] = i % 192;
93     }
94   for (int i = 0; i < 9; i++)
95     if (a[i].t != (i < 4 ? 0x55555555ULL : 0))
96       __builtin_abort ();
97   if (bb[0] != 78 || bb[1] != 12 || bb[N] != 22 || bb[3] != 84 || bb[4] != 127)
98     __builtin_abort ();
101 A<int> a3[4][3][2];
102 A<int> (*p3)[3][2] = &a3[1];
103 M<int> y3[5] = { 0, 1, 1, 1, 0 };
104 B<long> w3[1][2];
106 template <int N>
107 struct S
109   A<int> (*&x)[3][N];
110   M<int> *y;
111   B<long> (&w)[1][N];
112   A<char> z[10];
113   short b[5];
114   A<unsigned long long> a[9];
115   S() : x(p3), y(y3+1), w(w3), z(), a(), b() {}
116   __attribute__((noinline, noclone)) void foo (int, long, long, int, int, long, short);
119 template <int N>
120 void
121 S<N>::foo (int p1, long p2, long p3, int p4, int p5, long p6, short p7)
123   #pragma omp parallel for reduction(+:x[0:p1 + 1][:p2][0:N], z[:p3 + N - 2]) \
124                            reduction(*:y[:p4]) reduction(|:a[:p5]) \
125                            reduction(&:w[0:p6 - 3 + N][:p6]) reduction(maxb:b)
126   for (int i = 0; i < 128; i++)
127     {
128       x[i / 64][i % 3][(i / 4) & 1].t += i;
129       if ((i & 15) == 1)
130         y[0].t *= 3;
131       if ((i & 31) == N)
132         y[1].t *= 7;
133       if ((i & 63) == 3)
134         y[N].t *= 17;
135       z[i / 32].t += (i & 3);
136       if (i < 4)
137         z[i].t += i;
138       a[i / 32].t |= 1ULL << (i & 30);
139       w[0][i & 1].t &= ~(1L << (i / 17 * 3));
140       if ((i % 79) > b[0])
141         b[0] = i % 79;
142       if ((i % 13) > b[1])
143         b[1] = i % 13;
144       if ((i % 23) > b[N])
145         b[N] = i % 23;
146       if ((i % 85) > b[3])
147         b[3] = i % 85;
148       if ((i % 192) > b[4])
149         b[4] = i % 192;
150     }
154 main ()
156   A<int> a[4][3][2];
157   static int a2[4][3][2] = {{{ 0, 0 }, { 0, 0 }, { 0, 0 }},
158                             {{ 312, 381 }, { 295, 356 }, { 337, 335 }},
159                             {{ 1041, 975 }, { 1016, 1085 }, { 935, 1060 }},
160                             {{ 0, 0 }, { 0, 0 }, { 0, 0 }}};
161   A<int> (*p)[3][2] = &a[1];
162   M<int> y[5] = { 0, 1, 1, 1, 0 };
163   int y2[5] = { 0, 6561, 2401, 289, 0 };
164   char z2[10] = { 48, 49, 50, 51, 0, 0, 0, 0, 0, 0 };
165   B<long> w[1][2];
166   foo<2> (p, y + 1, w, 1, 3L, 4L, 3, 4, 2L, 5);
167   for (int i = 0; i < 4; i++)
168     for (int j = 0; j < 3; j++)
169       for (int k = 0; k < 2; k++)
170         if (a[i][j][k].t != a2[i][j][k])
171           __builtin_abort ();
172   for (int i = 0; i < 5; i++)
173     if (y[i].t != y2[i])
174       __builtin_abort ();
175   for (int i = 0; i < 10; i++)
176     if (z[i].t != z2[i])
177       __builtin_abort ();
178   if (w[0][0].t != ~0x249249L || w[0][1].t != ~0x249249L)
179     __builtin_abort ();
180   S<2> s;
181   s.foo (1, 3L, 4L, 3, 4, 2L, 5);
182   for (int i = 0; i < 9; i++)
183     if (s.a[i].t != (i < 4 ? 0x55555555ULL : 0))
184       __builtin_abort ();
185   for (int i = 0; i < 4; i++)
186     for (int j = 0; j < 3; j++)
187       for (int k = 0; k < 2; k++)
188         if (a3[i][j][k].t != a2[i][j][k])
189           __builtin_abort ();
190   for (int i = 0; i < 5; i++)
191     if (y3[i].t != y2[i])
192       __builtin_abort ();
193   for (int i = 0; i < 10; i++)
194     if (s.z[i].t != z2[i])
195       __builtin_abort ();
196   if (w3[0][0].t != ~0x249249L || w3[0][1].t != ~0x249249L)
197     __builtin_abort ();
198   if (s.b[0] != 78 || s.b[1] != 12 || s.b[2] != 22
199       || s.b[3] != 84 || s.b[4] != 127)
200     __builtin_abort ();