| 1 |
// { dg-do run } |
| 2 |
// { dg-options "-O2" } |
| 3 |
|
| 4 |
struct A { int i : 1; }; |
| 5 |
struct B { int j : 3; }; |
| 6 |
|
| 7 |
template <typename S> |
| 8 |
int |
| 9 |
foo (int x, int y) |
| 10 |
{ |
| 11 |
A a = {}; |
| 12 |
S s = {}; |
| 13 |
return __builtin_add_overflow_p (x, y, a.i) + 2 * __builtin_mul_overflow_p (x, y, s.j); |
| 14 |
} |
| 15 |
|
| 16 |
__attribute__((noinline, noclone)) int |
| 17 |
bar (int x, int y) |
| 18 |
{ |
| 19 |
return foo <B> (x, y); |
| 20 |
} |
| 21 |
|
| 22 |
#if __cplusplus >= 201402L |
| 23 |
template <typename S> |
| 24 |
constexpr int |
| 25 |
baz (int x, int y) |
| 26 |
{ |
| 27 |
A a = {}; |
| 28 |
S s = {}; |
| 29 |
return __builtin_add_overflow_p (x, y, a.i) + 2 * __builtin_mul_overflow_p (x, y, s.j); |
| 30 |
} |
| 31 |
|
| 32 |
constexpr int t1 = baz <B> (0, 0); |
| 33 |
constexpr int t2 = baz <B> (1, -1); |
| 34 |
constexpr int t3 = baz <B> (1, -4); |
| 35 |
constexpr int t4 = baz <B> (-4, 4); |
| 36 |
constexpr int t5 = baz <B> (4, 2); |
| 37 |
static_assert (t1 == 0, ""); |
| 38 |
static_assert (t2 == 0, ""); |
| 39 |
static_assert (t3 == 1, ""); |
| 40 |
static_assert (t4 == 2, ""); |
| 41 |
static_assert (t5 == 3, ""); |
| 42 |
#endif |
| 43 |
|
| 44 |
int |
| 45 |
main () |
| 46 |
{ |
| 47 |
if (bar (0, 0) != 0 |
| 48 |
|| bar (-1, 1) != 0 |
| 49 |
|| bar (-4, 1) != 1 |
| 50 |
|| bar (4, -4) != 2 |
| 51 |
|| bar (2, 4) != 3) |
| 52 |
__builtin_abort (); |
| 53 |
} |