PR target/81988
[official-gcc.git] / libgomp / testsuite / libgomp.c++ / reduction-8.C
blobcffd7cc2d4c9ec7b334ecab97aa63fbc896a5f4a
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 __attribute__((noinline, noclone)) void
57 foo (A<int> (*&x)[3][2], M<int> *y, B<long> (&w)[1][2], int p1, long p2, long p3, int p4,
58      int p5, long p6, short p7)
60   A<unsigned long long> a[p7 + 4];
61   short bb[p7];
62   short (&b)[p7] = bb;
63   for (int i = 0; i < p7; i++)
64     bb[i] = -6;
65   #pragma omp parallel for reduction(+:x[0:p1 + 1][:p2], z[:p3]) \
66                            reduction(*:y[:p4]) reduction(|:a[:p5]) \
67                            reduction(&:w[0:p6 - 1][:p6]) reduction(maxb:b)
68   for (int i = 0; i < 128; i++)
69     {
70       x[i / 64][i % 3][(i / 4) & 1].t += i;
71       if ((i & 15) == 1)
72         y[0].t *= 3;
73       if ((i & 31) == 2)
74         y[1].t *= 7;
75       if ((i & 63) == 3)
76         y[2].t *= 17;
77       z[i / 32].t += (i & 3);
78       if (i < 4)
79         z[i].t += i;
80       a[i / 32].t |= 1ULL << (i & 30);
81       w[0][i & 1].t &= ~(1L << (i / 17 * 3));
82       if ((i % 79) > b[0])
83         b[0] = i % 79;
84       if ((i % 13) > b[1])
85         b[1] = i % 13;
86       if ((i % 23) > b[2])
87         b[2] = i % 23;
88       if ((i % 85) > b[3])
89         b[3] = i % 85;
90       if ((i % 192) > b[4])
91         b[4] = i % 192;
92     }
93   for (int i = 0; i < 9; i++)
94     if (a[i].t != (i < 4 ? 0x55555555ULL : 0))
95       __builtin_abort ();
96   if (bb[0] != 78 || bb[1] != 12 || bb[2] != 22 || bb[3] != 84 || bb[4] != 127)
97     __builtin_abort ();
100 A<int> a3[4][3][2];
101 A<int> (*p3)[3][2] = &a3[1];
102 M<int> y3[5] = { 0, 1, 1, 1, 0 };
103 B<long> w3[1][2];
105 struct S
107   A<int> (*&x)[3][2];
108   M<int> *y;
109   B<long> (&w)[1][2];
110   A<char> z[10];
111   short b[5];
112   A<unsigned long long> a[9];
113   S() : x(p3), y(y3+1), w(w3), z(), a(), b() {}
114   __attribute__((noinline, noclone)) void foo (int, long, long, int, int, long, short);
117 void
118 S::foo (int p1, long p2, long p3, int p4, int p5, long p6, short p7)
120   #pragma omp parallel for reduction(+:x[0:p1 + 1][:p2][0:2], z[:p3]) \
121                            reduction(*:y[:p4]) reduction(|:a[:p5]) \
122                            reduction(&:w[0:p6 - 1][:p6]) reduction(maxb:b)
123   for (int i = 0; i < 128; i++)
124     {
125       x[i / 64][i % 3][(i / 4) & 1].t += i;
126       if ((i & 15) == 1)
127         y[0].t *= 3;
128       if ((i & 31) == 2)
129         y[1].t *= 7;
130       if ((i & 63) == 3)
131         y[2].t *= 17;
132       z[i / 32].t += (i & 3);
133       if (i < 4)
134         z[i].t += i;
135       a[i / 32].t |= 1ULL << (i & 30);
136       w[0][i & 1].t &= ~(1L << (i / 17 * 3));
137       if ((i % 79) > b[0])
138         b[0] = i % 79;
139       if ((i % 13) > b[1])
140         b[1] = i % 13;
141       if ((i % 23) > b[2])
142         b[2] = i % 23;
143       if ((i % 85) > b[3])
144         b[3] = i % 85;
145       if ((i % 192) > b[4])
146         b[4] = i % 192;
147     }
151 main ()
153   A<int> a[4][3][2];
154   static int a2[4][3][2] = {{{ 0, 0 }, { 0, 0 }, { 0, 0 }},
155                             {{ 312, 381 }, { 295, 356 }, { 337, 335 }},
156                             {{ 1041, 975 }, { 1016, 1085 }, { 935, 1060 }},
157                             {{ 0, 0 }, { 0, 0 }, { 0, 0 }}};
158   A<int> (*p)[3][2] = &a[1];
159   M<int> y[5] = { 0, 1, 1, 1, 0 };
160   int y2[5] = { 0, 6561, 2401, 289, 0 };
161   char z2[10] = { 48, 49, 50, 51, 0, 0, 0, 0, 0, 0 };
162   B<long> w[1][2];
163   foo (p, y + 1, w, 1, 3L, 4L, 3, 4, 2L, 5);
164   for (int i = 0; i < 4; i++)
165     for (int j = 0; j < 3; j++)
166       for (int k = 0; k < 2; k++)
167         if (a[i][j][k].t != a2[i][j][k])
168           __builtin_abort ();
169   for (int i = 0; i < 5; i++)
170     if (y[i].t != y2[i])
171       __builtin_abort ();
172   for (int i = 0; i < 10; i++)
173     if (z[i].t != z2[i])
174       __builtin_abort ();
175   if (w[0][0].t != ~0x249249L || w[0][1].t != ~0x249249L)
176     __builtin_abort ();
177   S s;
178   s.foo (1, 3L, 4L, 3, 4, 2L, 5);
179   for (int i = 0; i < 9; i++)
180     if (s.a[i].t != (i < 4 ? 0x55555555ULL : 0))
181       __builtin_abort ();
182   for (int i = 0; i < 4; i++)
183     for (int j = 0; j < 3; j++)
184       for (int k = 0; k < 2; k++)
185         if (a3[i][j][k].t != a2[i][j][k])
186           __builtin_abort ();
187   for (int i = 0; i < 5; i++)
188     if (y3[i].t != y2[i])
189       __builtin_abort ();
190   for (int i = 0; i < 10; i++)
191     if (s.z[i].t != z2[i])
192       __builtin_abort ();
193   if (w3[0][0].t != ~0x249249L || w3[0][1].t != ~0x249249L)
194     __builtin_abort ();
195   if (s.b[0] != 78 || s.b[1] != 12 || s.b[2] != 22
196       || s.b[3] != 84 || s.b[4] != 127)
197     __builtin_abort ();