summaryrefslogtreecommitdiff
path: root/gcc/testsuite/g++.dg/opt/pr14029.C
blob: 1673edfd615e309872386c8d997b4c5b4a355d73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// { dg-do run }
// { dg-options "-O2" }
// We used to mis-compile this testcase as we did not know that
// &a+offsetof(b,a) was the same as &a.b

struct Iterator {
    int * ptr;

    Iterator(int * i) : ptr(i) { }
    void operator++() { ++ptr; }
    int *const & base() const { return ptr; }
};


Iterator find_7(Iterator first, Iterator last)
{
  int trip_count  = (last.base() - first.base()) >> 1;

  for ( ; trip_count > 0 ; --trip_count) {
    if (*first.ptr == 7) return first;
    ++first;

    if (*first.ptr == 7) return first;
    ++first;
  }

  switch(last.base() - first.base()) {
    case 1:
          if (*first.ptr == 7) return first;
          ++first;
    case 0:
    default:
          return last;
  }
}

int main() {
  int as[5] = {4,4,4,4,7};
  return (find_7(Iterator(&as[0]), Iterator(&as[5])).ptr == &as[5]);
};