PR testsuite/86660
[official-gcc.git] / libgomp / testsuite / libgomp.c++ / for-15.C
blobddb9ce629dd89c6b3926e2a8211af9bb3fd27ab0
1 // PR c++/86443
2 // { dg-do run }
3 // { dg-additional-options "-std=c++17" }
5 typedef __PTRDIFF_TYPE__ ptrdiff_t;
6 extern "C" void abort ();
8 #pragma omp declare target
9 template <typename T>
10 class I
12 public:
13   typedef ptrdiff_t difference_type;
14   I ();
15   ~I ();
16   I (T *);
17   I (const I &);
18   T &operator * ();
19   T *operator -> ();
20   T &operator [] (const difference_type &) const;
21   I &operator = (const I &);
22   I &operator ++ ();
23   I operator ++ (int);
24   I &operator -- ();
25   I operator -- (int);
26   I &operator += (const difference_type &);
27   I &operator -= (const difference_type &);
28   I operator + (const difference_type &) const;
29   I operator - (const difference_type &) const;
30   template <typename S> friend bool operator == (I<S> &, I<S> &);
31   template <typename S> friend bool operator == (const I<S> &, const I<S> &);
32   template <typename S> friend bool operator < (I<S> &, I<S> &);
33   template <typename S> friend bool operator < (const I<S> &, const I<S> &);
34   template <typename S> friend bool operator <= (I<S> &, I<S> &);
35   template <typename S> friend bool operator <= (const I<S> &, const I<S> &);
36   template <typename S> friend bool operator > (I<S> &, I<S> &);
37   template <typename S> friend bool operator > (const I<S> &, const I<S> &);
38   template <typename S> friend bool operator >= (I<S> &, I<S> &);
39   template <typename S> friend bool operator >= (const I<S> &, const I<S> &);
40   template <typename S> friend typename I<S>::difference_type operator - (I<S> &, I<S> &);
41   template <typename S> friend typename I<S>::difference_type operator - (const I<S> &, const I<S> &);
42   template <typename S> friend I<S> operator + (typename I<S>::difference_type , const I<S> &);
43 private:
44   T *p;
46 template <typename T> I<T>::I () : p (0) {}
47 template <typename T> I<T>::~I () {}
48 template <typename T> I<T>::I (T *x) : p (x) {}
49 template <typename T> I<T>::I (const I &x) : p (x.p) {}
50 template <typename T> T &I<T>::operator * () { return *p; }
51 template <typename T> T *I<T>::operator -> () { return p; }
52 template <typename T> T &I<T>::operator [] (const difference_type &x) const { return p[x]; }
53 template <typename T> I<T> &I<T>::operator = (const I &x) { p = x.p; return *this; }
54 template <typename T> I<T> &I<T>::operator ++ () { ++p; return *this; }
55 template <typename T> I<T> I<T>::operator ++ (int) { return I (p++); }
56 template <typename T> I<T> &I<T>::operator -- () { --p; return *this; }
57 template <typename T> I<T> I<T>::operator -- (int) { return I (p--); }
58 template <typename T> I<T> &I<T>::operator += (const difference_type &x) { p += x; return *this; }
59 template <typename T> I<T> &I<T>::operator -= (const difference_type &x) { p -= x; return *this; }
60 template <typename T> I<T> I<T>::operator + (const difference_type &x) const { return I (p + x); }
61 template <typename T> I<T> I<T>::operator - (const difference_type &x) const { return I (p - x); }
62 template <typename T> bool operator == (I<T> &x, I<T> &y) { return x.p == y.p; }
63 template <typename T> bool operator == (const I<T> &x, const I<T> &y) { return x.p == y.p; }
64 template <typename T> bool operator != (I<T> &x, I<T> &y) { return !(x == y); }
65 template <typename T> bool operator != (const I<T> &x, const I<T> &y) { return !(x == y); }
66 template <typename T> bool operator < (I<T> &x, I<T> &y) { return x.p < y.p; }
67 template <typename T> bool operator < (const I<T> &x, const I<T> &y) { return x.p < y.p; }
68 template <typename T> bool operator <= (I<T> &x, I<T> &y) { return x.p <= y.p; }
69 template <typename T> bool operator <= (const I<T> &x, const I<T> &y) { return x.p <= y.p; }
70 template <typename T> bool operator > (I<T> &x, I<T> &y) { return x.p > y.p; }
71 template <typename T> bool operator > (const I<T> &x, const I<T> &y) { return x.p > y.p; }
72 template <typename T> bool operator >= (I<T> &x, I<T> &y) { return x.p >= y.p; }
73 template <typename T> bool operator >= (const I<T> &x, const I<T> &y) { return x.p >= y.p; }
74 template <typename T> typename I<T>::difference_type operator - (I<T> &x, I<T> &y) { return x.p - y.p; }
75 template <typename T> typename I<T>::difference_type operator - (const I<T> &x, const I<T> &y) { return x.p - y.p; }
76 template <typename T> I<T> operator + (typename I<T>::difference_type x, const I<T> &y) { return I<T> (x + y.p); }
78 template <typename T>
79 class J
81 public:
82   J(const I<T> &x, const I<T> &y) : b (x), e (y) {}
83   const I<T> &begin ();
84   const I<T> &end ();
85 private:
86   I<T> b, e;
89 template <typename T> const I<T> &J<T>::begin () { return b; }
90 template <typename T> const I<T> &J<T>::end () { return e; }
92 int results[2000];
94 template <typename T>
95 void
96 baz (I<T> &i)
98   if (*i < 0 || *i >= 2000)
99     abort ();
100   results[*i]++;
103 void
104 baz (int i)
106   if (i < 0 || i >= 2000)
107     abort ();
108   results[i]++;
111 void
112 f1 (J<int> j)
114 #pragma omp distribute parallel for default(none)
115   for (I<int> i = j.begin (); i < j.end (); i += 3)
116     baz (*i);
119 void
120 f2 (J<int> j)
122   I<int> i;
123 #pragma omp distribute parallel for default(none)
124   for (i = j.begin (); i < j.end (); ++i)
125     baz (*i);
128 template <int N>
129 void
130 f3 (J<int> j)
132 #pragma omp distribute parallel for default(none)
133   for (I<int> i = j.begin (); i < j.end (); i += 6)
134     baz (*i);
137 template <int N>
138 void
139 f4 (J<int> j)
141   I<int> i;
142 #pragma omp distribute parallel for default(none)
143   for (i = j.begin (); i < j.end (); i += 9)
144     baz (*i);
147 template <typename T>
148 void
149 f5 (J<T> j)
151 #pragma omp distribute parallel for default(none)
152   for (I<T> i = j.begin (); i < j.end (); i += 4)
153     baz (*i);
156 template <typename T>
157 void
158 f6 (J<T> j)
160   I<T> i;
161 #pragma omp distribute parallel for default(none)
162   for (i = j.begin (); i < j.end (); i += 7)
163     baz (*i);
166 #pragma omp end declare target
168 #define check(expr) \
169   for (int i = 0; i < 2000; i++)                        \
170     if (expr)                                           \
171       {                                                 \
172         if (results[i] != 1)                            \
173           abort ();                                     \
174         results[i] = 0;                                 \
175       }                                                 \
176     else if (results[i])                                \
177       abort ()
180 main ()
182   int a[2000];
183   for (int i = 0; i < 2000; i++)
184     a[i] = i;
185   #pragma omp target data map (to: a)
186   {
187     #pragma omp target teams map (always, tofrom: results)
188     {
189       J<int> j (&a[75], &a[1945]);
190       f1 (j);
191     }
192     check (i >= 75 && i < 1945 && (i - 75) % 3 == 0);
193     #pragma omp target teams map (always, tofrom: results)
194     {
195       J<int> j (&a[63], &a[1949]);
196       f2 (j);
197     }
198     check (i >= 63 && i < 1949);
199     #pragma omp target teams map (always, tofrom: results)
200     {
201       J<int> j (&a[58], &a[1979]);
202       f3 <2> (j);
203     }
204     check (i >= 58 && i < 1979 && (i - 58) % 6 == 0);
205     #pragma omp target teams map (always, tofrom: results)
206     {
207       J<int> j (&a[59], &a[1981]);
208       f4 <9> (j);
209     }
210     check (i >= 59 && i < 1981 && (i - 59) % 9 == 0);
211     #pragma omp target teams map (always, tofrom: results)
212     {
213       J<int> j (&a[52], &a[1972]);
214       f5 (j);
215     }
216     check (i >= 52 && i < 1972 && (i - 52) % 4 == 0);
217     #pragma omp target teams map (always, tofrom: results)
218     {
219       J<int> j (&a[31], &a[1827]);
220       f6 (j);
221     }
222     check (i >= 31 && i < 1827 && (i - 31) % 7 == 0);
223   }