for-5.C 8.6 KB

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