1 // { dg-do run }
2
3 typedef __PTRDIFF_TYPE__ ptrdiff_t;
4 extern "C" void abort ();
5
6 template <typename T>
7 class I
8 {
9 public:
10 typedef ptrdiff_t difference_type;
11 I ();
12 ~I ();
13 I (T *);
14 I (const I &);
15 T &operator * ();
16 T *operator -> ();
17 T &operator [] (const difference_type &) const;
18 I &operator = (const I &);
19 I &operator ++ ();
20 I operator ++ (int);
21 I &operator -- ();
22 I operator -- (int);
23 I &operator += (const difference_type &);
24 I &operator -= (const difference_type &);
25 I operator + (const difference_type &) const;
26 I operator - (const difference_type &) const;
27 template <typename S> friend bool operator == (I<S> &, I<S> &);
28 template <typename S> friend bool operator == (const I<S> &, const I<S> &);
29 template <typename S> friend bool operator < (I<S> &, I<S> &);
30 template <typename S> friend bool operator < (const I<S> &, const I<S> &);
31 template <typename S> friend bool operator <= (I<S> &, I<S> &);
32 template <typename S> friend bool operator <= (const I<S> &, const I<S> &);
33 template <typename S> friend bool operator > (I<S> &, I<S> &);
34 template <typename S> friend bool operator > (const I<S> &, const I<S> &);
35 template <typename S> friend bool operator >= (I<S> &, I<S> &);
36 template <typename S> friend bool operator >= (const I<S> &, const I<S> &);
37 template <typename S> friend typename I<S>::difference_type operator - (I<S> &, I<S> &);
38 template <typename S> friend typename I<S>::difference_type operator - (const I<S> &, const I<S> &);
39 template <typename S> friend I<S> operator + (typename I<S>::difference_type , const I<S> &);
40 private:
41 T *p;
42 };
I()43 template <typename T> I<T>::I () : p (0) {}
~I()44 template <typename T> I<T>::~I () { p = (T *) 0; }
I(T * x)45 template <typename T> I<T>::I (T *x) : p (x) {}
I(const I & x)46 template <typename T> I<T>::I (const I &x) : p (x.p) {}
47 template <typename T> T &I<T>::operator * () { return *p; }
48 template <typename T> T *I<T>::operator -> () { return p; }
49 template <typename T> T &I<T>::operator [] (const difference_type &x) const { return p[x]; }
50 template <typename T> I<T> &I<T>::operator = (const I &x) { p = x.p; return *this; }
51 template <typename T> I<T> &I<T>::operator ++ () { ++p; return *this; }
52 template <typename T> I<T> I<T>::operator ++ (int) { return I (p++); }
53 template <typename T> I<T> &I<T>::operator -- () { --p; return *this; }
54 template <typename T> I<T> I<T>::operator -- (int) { return I (p--); }
55 template <typename T> I<T> &I<T>::operator += (const difference_type &x) { p += x; return *this; }
56 template <typename T> I<T> &I<T>::operator -= (const difference_type &x) { p -= x; return *this; }
57 template <typename T> I<T> I<T>::operator + (const difference_type &x) const { return I (p + x); }
58 template <typename T> I<T> I<T>::operator - (const difference_type &x) const { return I (p - x); }
59 template <typename T> bool operator == (I<T> &x, I<T> &y) { return x.p == y.p; }
60 template <typename T> bool operator == (const I<T> &x, const I<T> &y) { return x.p == y.p; }
61 template <typename T> bool operator != (I<T> &x, I<T> &y) { return !(x == y); }
62 template <typename T> bool operator != (const I<T> &x, const I<T> &y) { return !(x == y); }
63 template <typename T> bool operator < (I<T> &x, I<T> &y) { return x.p < y.p; }
64 template <typename T> bool operator < (const I<T> &x, const I<T> &y) { return x.p < y.p; }
65 template <typename T> bool operator <= (I<T> &x, I<T> &y) { return x.p <= y.p; }
66 template <typename T> bool operator <= (const I<T> &x, const I<T> &y) { return x.p <= y.p; }
67 template <typename T> bool operator > (I<T> &x, I<T> &y) { return x.p > y.p; }
68 template <typename T> bool operator > (const I<T> &x, const I<T> &y) { return x.p > y.p; }
69 template <typename T> bool operator >= (I<T> &x, I<T> &y) { return x.p >= y.p; }
70 template <typename T> bool operator >= (const I<T> &x, const I<T> &y) { return x.p >= y.p; }
71 template <typename T> typename I<T>::difference_type operator - (I<T> &x, I<T> &y) { return x.p - y.p; }
72 template <typename T> typename I<T>::difference_type operator - (const I<T> &x, const I<T> &y) { return x.p - y.p; }
73 template <typename T> I<T> operator + (typename I<T>::difference_type x, const I<T> &y) { return I<T> (x + y.p); }
74
75 template <typename T>
76 class J
77 {
78 public:
J(const I<T> & x,const I<T> & y)79 J(const I<T> &x, const I<T> &y) : b (x), e (y) {}
80 const I<T> &begin ();
81 const I<T> &end ();
82 private:
83 I<T> b, e;
84 };
85
begin()86 template <typename T> const I<T> &J<T>::begin () { return b; }
end()87 template <typename T> const I<T> &J<T>::end () { return e; }
88
89 int results[2000];
90
91 template <typename T>
92 void
baz(I<T> & i)93 baz (I<T> &i)
94 {
95 if (*i < 0 || *i >= 2000)
96 abort ();
97 results[*i]++;
98 }
99
100 I<int>
f1(const I<int> & x,const I<int> & y)101 f1 (const I<int> &x, const I<int> &y)
102 {
103 I<int> i;
104 #pragma omp parallel shared (i)
105 {
106 #pragma omp for lastprivate (i) schedule(runtime)
107 for (i = x; i < y - 1; ++i)
108 baz (i);
109 #pragma omp single
110 i += 3;
111 }
112 return I<int> (i);
113 }
114
115 I<int>
f2(const I<int> & x,const I<int> & y)116 f2 (const I<int> &x, const I<int> &y)
117 {
118 I<int> i;
119 #pragma omp parallel for lastprivate (i)
120 for (i = x; i < y - 1; i = 1 - 6 + 7 + i)
121 baz (i);
122 return I<int> (i);
123 }
124
125 template <typename T>
126 I<int>
f3(const I<int> & x,const I<int> & y)127 f3 (const I<int> &x, const I<int> &y)
128 {
129 I<int> i;
130 #pragma omp parallel
131 #pragma omp for lastprivate (i)
132 for (i = x + 1000 - 64; i <= y - 10; i++)
133 baz (i);
134 return i;
135 }
136
137 template <typename T>
138 I<int>
f4(const I<int> & x,const I<int> & y)139 f4 (const I<int> &x, const I<int> &y)
140 {
141 I<int> i;
142 #pragma omp parallel for lastprivate (i)
143 for (i = x + 2000 - 64; i > y + 10; --i)
144 baz (i);
145 return I<int> (i);
146 }
147
148 template <typename T>
149 I<int>
f5(const I<int> & x,const I<int> & y)150 f5 (const I<int> &x, const I<int> &y)
151 {
152 I<int> i;
153 #pragma omp parallel for lastprivate (i)
154 for (i = x; i > y + T (6); i--)
155 baz (i);
156 return i;
157 }
158
159 template <typename T>
160 I<int>
f6(const I<int> & x,const I<int> & y)161 f6 (const I<int> &x, const I<int> &y)
162 {
163 I<int> i;
164 #pragma omp parallel for lastprivate (i)
165 for (i = x - T (7); i > y; i -= T (2))
166 baz (i);
167 return I<int> (i);
168 }
169
170 template <int N>
171 I<int>
f7(I<int> i,const I<int> & x,const I<int> & y)172 f7 (I<int> i, const I<int> &x, const I<int> &y)
173 {
174 #pragma omp parallel for lastprivate (i)
175 for (i = x - 10; i <= y + 10; i += N)
176 baz (i);
177 return I<int> (i);
178 }
179
180 template <int N>
181 I<int>
f8(J<int> j)182 f8 (J<int> j)
183 {
184 I<int> i;
185 #pragma omp parallel shared (i)
186 #pragma omp for lastprivate (i)
187 for (i = j.begin (); i <= j.end () + N; i += 2)
188 baz (i);
189 return i;
190 }
191
192 I<int> i9;
193
194 template <long N>
195 I<int> &
f9(J<int> j)196 f9 (J<int> j)
197 {
198 #pragma omp parallel for lastprivate (i9)
199 for (i9 = j.begin () + N; i9 <= j.end () - N; i9 = i9 - N)
200 baz (i9);
201 return i9;
202 }
203
204 template <typename T, int N>
205 I<T>
f10(const I<T> & x,const I<T> & y)206 f10 (const I<T> &x, const I<T> &y)
207 {
208 I<T> i;
209 #pragma omp parallel for lastprivate (i)
210 for (i = x; i > y; i = i + N)
211 baz (i);
212 return i;
213 }
214
215 template <typename T, typename U>
216 T
f11(T i,const T & x,const T & y)217 f11 (T i, const T &x, const T &y)
218 {
219 #pragma omp parallel
220 #pragma omp for lastprivate (i)
221 for (i = x + U (2); i <= y + U (1); i = U (2) + U (3) + i)
222 baz (i);
223 return T (i);
224 }
225
226 template <typename T>
227 T
f12(const T & x,const T & y)228 f12 (const T &x, const T &y)
229 {
230 T i;
231 #pragma omp parallel for lastprivate (i)
232 for (i = x; i > y; --i)
233 baz (i);
234 return i;
235 }
236
237 #define check(expr) \
238 for (int i = 0; i < 2000; i++) \
239 if (expr) \
240 { \
241 if (results[i] != 1) \
242 abort (); \
243 results[i] = 0; \
244 } \
245 else if (results[i]) \
246 abort ()
247
248 int
main()249 main ()
250 {
251 int a[2000];
252 long b[2000];
253 for (int i = 0; i < 2000; i++)
254 {
255 a[i] = i;
256 b[i] = i;
257 }
258 if (*f1 (&a[10], &a[1873]) != 1875)
259 abort ();
260 check (i >= 10 && i < 1872);
261 if (*f2 (&a[0], &a[1998]) != 1998)
262 abort ();
263 check (i < 1997 && (i & 1) == 0);
264 if (*f3<int> (&a[10], &a[1971]) != 1962)
265 abort ();
266 check (i >= 946 && i <= 1961);
267 if (*f4<int> (&a[0], &a[30]) != 40)
268 abort ();
269 check (i > 40 && i <= 2000 - 64);
270 if (*f5<short> (&a[1931], &a[17]) != 23)
271 abort ();
272 check (i > 23 && i <= 1931);
273 if (*f6<long> (&a[1931], &a[17]) != 16)
274 abort ();
275 check (i > 17 && i <= 1924 && (i & 1) == 0);
276 if (*f7<6> (I<int> (), &a[12], &a[1800]) != 1814)
277 abort ();
278 check (i >= 2 && i <= 1808 && (i - 2) % 6 == 0);
279 if (*f8<121> (J<int> (&a[14], &a[1803])) != 1926)
280 abort ();
281 check (i >= 14 && i <= 1924 && (i & 1) == 0);
282 if (*f9<-3L> (J<int> (&a[27], &a[1761])) != 1767)
283 abort ();
284 check (i >= 24 && i <= 1764 && (i % 3) == 0);
285 if (*f10<int, -7> (&a[1939], &a[17]) != 14)
286 abort ();
287 check (i >= 21 && i <= 1939 && i % 7 == 0);
288 if (*f11<I<int>, short> (I<int> (), &a[71], &a[1941]) != 1943)
289 abort ();
290 check (i >= 73 && i <= 1938 && (i - 73) % 5 == 0);
291 if (*f12<I<int> > (&a[1761], &a[37]) != 37)
292 abort ();
293 check (i > 37 && i <= 1761);
294 if (*f10<long, -7> (&b[1939], &b[17]) != 14)
295 abort ();
296 check (i >= 21 && i <= 1939 && i % 7 == 0);
297 if (*f11<I<long>, short> (I<long> (), &b[71], &b[1941]) != 1943)
298 abort ();
299 check (i >= 73 && i <= 1938 && (i - 73) % 5 == 0);
300 if (*f12<I<long> > (&b[1761], &b[37]) != 37)
301 abort ();
302 check (i > 37 && i <= 1761);
303 }
304