| 1 |
// { dg-do compile { target c++11 } } |
| 2 |
|
| 3 |
template <typename To, typename From> |
| 4 |
constexpr To |
| 5 |
bit_cast (const From &from) |
| 6 |
{ |
| 7 |
return __builtin_bit_cast (To, from); |
| 8 |
} |
| 9 |
|
| 10 |
template <typename To, typename From> |
| 11 |
constexpr bool |
| 12 |
check (const From &from) |
| 13 |
{ |
| 14 |
return bit_cast <From> (bit_cast <To> (from)) == from; |
| 15 |
} |
| 16 |
|
| 17 |
struct A |
| 18 |
{ |
| 19 |
int a, b, c; |
| 20 |
constexpr bool operator == (const A &x) const |
| 21 |
{ |
| 22 |
return x.a == a && x.b == b && x.c == c; |
| 23 |
} |
| 24 |
}; |
| 25 |
|
| 26 |
struct B |
| 27 |
{ |
| 28 |
unsigned a[3]; |
| 29 |
constexpr bool operator == (const B &x) const |
| 30 |
{ |
| 31 |
return x.a[0] == a[0] && x.a[1] == a[1] && x.a[2] == a[2]; |
| 32 |
} |
| 33 |
}; |
| 34 |
|
| 35 |
struct C |
| 36 |
{ |
| 37 |
char a[2][3][2]; |
| 38 |
constexpr bool operator == (const C &x) const |
| 39 |
{ |
| 40 |
return x.a[0][0][0] == a[0][0][0] |
| 41 |
&& x.a[0][0][1] == a[0][0][1] |
| 42 |
&& x.a[0][1][0] == a[0][1][0] |
| 43 |
&& x.a[0][1][1] == a[0][1][1] |
| 44 |
&& x.a[0][2][0] == a[0][2][0] |
| 45 |
&& x.a[0][2][1] == a[0][2][1] |
| 46 |
&& x.a[1][0][0] == a[1][0][0] |
| 47 |
&& x.a[1][0][1] == a[1][0][1] |
| 48 |
&& x.a[1][1][0] == a[1][1][0] |
| 49 |
&& x.a[1][1][1] == a[1][1][1] |
| 50 |
&& x.a[1][2][0] == a[1][2][0] |
| 51 |
&& x.a[1][2][1] == a[1][2][1]; |
| 52 |
} |
| 53 |
}; |
| 54 |
|
| 55 |
struct D |
| 56 |
{ |
| 57 |
int a, b; |
| 58 |
constexpr bool operator == (const D &x) const |
| 59 |
{ |
| 60 |
return x.a == a && x.b == b; |
| 61 |
} |
| 62 |
}; |
| 63 |
|
| 64 |
struct E {}; |
| 65 |
struct F { char c, d, e, f; }; |
| 66 |
struct G : public D, E, F |
| 67 |
{ |
| 68 |
int g; |
| 69 |
constexpr bool operator == (const G &x) const |
| 70 |
{ |
| 71 |
return x.a == a && x.b == b && x.c == c && x.d == d |
| 72 |
&& x.e == e && x.f == f && x.g == g; |
| 73 |
} |
| 74 |
}; |
| 75 |
|
| 76 |
struct H |
| 77 |
{ |
| 78 |
int a, b[2], c; |
| 79 |
constexpr bool operator == (const H &x) const |
| 80 |
{ |
| 81 |
return x.a == a && x.b[0] == b[0] && x.b[1] == b[1] && x.c == c; |
| 82 |
} |
| 83 |
}; |
| 84 |
|
| 85 |
#if __SIZEOF_INT__ == 4 |
| 86 |
struct I |
| 87 |
{ |
| 88 |
int a; |
| 89 |
int b : 3; |
| 90 |
int c : 24; |
| 91 |
int d : 5; |
| 92 |
int e; |
| 93 |
constexpr bool operator == (const I &x) const |
| 94 |
{ |
| 95 |
return x.a == a && x.b == b && x.c == c && x.d == d && x.e == e; |
| 96 |
} |
| 97 |
}; |
| 98 |
#endif |
| 99 |
|
| 100 |
#if __SIZEOF_INT__ == 4 && __SIZEOF_LONG_LONG__ == 8 |
| 101 |
struct J |
| 102 |
{ |
| 103 |
long long int a, b : 11, c : 3, d : 37, e : 1, f : 10, g : 2, h; |
| 104 |
constexpr bool operator == (const J &x) const |
| 105 |
{ |
| 106 |
return x.a == a && x.b == b && x.c == c && x.d == d && x.e == e |
| 107 |
&& x.f == f && x.g == g && x.h == h; |
| 108 |
} |
| 109 |
}; |
| 110 |
|
| 111 |
struct K |
| 112 |
{ |
| 113 |
long long int a, b, c; |
| 114 |
constexpr bool operator == (const K &x) const |
| 115 |
{ |
| 116 |
return x.a == a && x.b == b && x.c == c; |
| 117 |
} |
| 118 |
}; |
| 119 |
|
| 120 |
struct M |
| 121 |
{ |
| 122 |
signed a : 6, b : 7, c : 6, d : 5; |
| 123 |
unsigned char e; |
| 124 |
unsigned int f; |
| 125 |
long long int g; |
| 126 |
constexpr bool operator == (const M &x) const |
| 127 |
{ |
| 128 |
return x.a == a && x.b == b && x.c == c && x.d == d && x.e == e |
| 129 |
&& x.f == f && x.g == g; |
| 130 |
} |
| 131 |
}; |
| 132 |
|
| 133 |
struct N |
| 134 |
{ |
| 135 |
unsigned long long int a, b; |
| 136 |
constexpr bool operator == (const N &x) const |
| 137 |
{ |
| 138 |
return x.a == a && x.b == b; |
| 139 |
} |
| 140 |
}; |
| 141 |
#endif |
| 142 |
|
| 143 |
static_assert (check <unsigned int> (0), ""); |
| 144 |
static_assert (check <long long int> (0xdeadbeeffeedbac1ULL), ""); |
| 145 |
static_assert (check <signed char> ((unsigned char) 42), ""); |
| 146 |
static_assert (check <char> ((unsigned char) 42), ""); |
| 147 |
static_assert (check <unsigned char> ((unsigned char) 42), ""); |
| 148 |
static_assert (check <signed char> ((signed char) 42), ""); |
| 149 |
static_assert (check <char> ((signed char) 42), ""); |
| 150 |
static_assert (check <unsigned char> ((signed char) 42), ""); |
| 151 |
static_assert (check <signed char> ((char) 42), ""); |
| 152 |
static_assert (check <char> ((char) 42), ""); |
| 153 |
static_assert (check <unsigned char> ((char) 42), ""); |
| 154 |
#if __SIZEOF_INT__ == __SIZEOF_FLOAT__ |
| 155 |
static_assert (check <int> (2.5f), ""); |
| 156 |
static_assert (check <unsigned int> (136.5f), ""); |
| 157 |
#endif |
| 158 |
#if __SIZEOF_LONG_LONG__ == __SIZEOF_DOUBLE__ |
| 159 |
static_assert (check <long long> (2.5), ""); |
| 160 |
static_assert (check <long long unsigned> (123456.75), ""); |
| 161 |
#endif |
| 162 |
|
| 163 |
static_assert (check <B> (A{ 1, 2, 3 }), ""); |
| 164 |
static_assert (check <A> (B{ 4, 5, 6 }), ""); |
| 165 |
|
| 166 |
#if __SIZEOF_INT__ == 4 |
| 167 |
static_assert (check <C> (A{ 7, 8, 9 }), ""); |
| 168 |
static_assert (check <C> (B{ 10, 11, 12 }), ""); |
| 169 |
static_assert (check <A> (C{ { { { 13, 14 }, { 15, 16 }, { 17, 18 } }, |
| 170 |
{ { 19, 20 }, { 21, 22 }, { 23, 24 } } } }), ""); |
| 171 |
constexpr unsigned char c[] = { 1, 2, 3, 4 }; |
| 172 |
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 173 |
static_assert (bit_cast <unsigned int> (c) == 0x04030201U, ""); |
| 174 |
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 175 |
static_assert (bit_cast <unsigned int> (c) == 0x01020304U, ""); |
| 176 |
#endif |
| 177 |
|
| 178 |
#if __cplusplus >= 201703L |
| 179 |
static_assert (check <G> (H { 0x12345678, { 0x23456789, 0x5a876543 }, 0x3ba78654 }), ""); |
| 180 |
#endif |
| 181 |
constexpr int d[] = { 0x12345678, 0x23456789, 0x5a876543, 0x3ba78654 }; |
| 182 |
static_assert (bit_cast <G> (d) == bit_cast <G> (H { 0x12345678, { 0x23456789, 0x5a876543 }, 0x3ba78654 }), ""); |
| 183 |
|
| 184 |
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 185 |
static_assert (bit_cast <I> (A { 0x7efa3412, 0x5a876543, 0x1eeffeed }) |
| 186 |
== I { 0x7efa3412, 3, 0x50eca8, 0xb, 0x1eeffeed }, ""); |
| 187 |
static_assert (bit_cast <A> (I { 0x7efa3412, 3, 0x50eca8, 0xb, 0x1eeffeed }) |
| 188 |
== A { 0x7efa3412, 0x5a876543, 0x1eeffeed }, ""); |
| 189 |
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 190 |
static_assert (bit_cast <I> (A { 0x7efa3412, 0x5a876543, 0x1eeffeed }) |
| 191 |
== I { 0x7efa3412, 2, -0x2bc4d6, 0x3, 0x1eeffeed }, ""); |
| 192 |
static_assert (bit_cast <A> (I { 0x7efa3412, 2, -0x2bc4d6, 0x3, 0x1eeffeed }) |
| 193 |
== A { 0x7efa3412, 0x5a876543, 0x1eeffeed }, ""); |
| 194 |
#endif |
| 195 |
#endif |
| 196 |
|
| 197 |
#if 2 * __SIZEOF_INT__ == __SIZEOF_LONG_LONG__ && __SIZEOF_INT__ >= 4 |
| 198 |
constexpr unsigned long long a = 0xdeadbeeffee1deadULL; |
| 199 |
constexpr unsigned b[] = { 0xfeedbacU, 0xbeeffeedU }; |
| 200 |
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 201 |
static_assert (bit_cast <D> (a) == D { int (0xfee1deadU), int (0xdeadbeefU) }, ""); |
| 202 |
static_assert (bit_cast <unsigned long long> (b) == 0xbeeffeed0feedbacULL, ""); |
| 203 |
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 204 |
static_assert (bit_cast <D> (a) == D { int (0xdeadbeefU), int (0xfee1deadU) }, ""); |
| 205 |
static_assert (bit_cast <unsigned long long> (b) == 0x0feedbacbeeffeedULL, ""); |
| 206 |
#endif |
| 207 |
#endif |
| 208 |
|
| 209 |
#if __SIZEOF_INT__ == 4 && __SIZEOF_LONG_LONG__ == 8 |
| 210 |
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 211 |
static_assert (bit_cast <J> (K { 0x0feedbacdeadbeefLL, 7862463375103529997LL, 0x0feedbacdeadbeefLL }) |
| 212 |
== J { 0x0feedbacdeadbeefLL, -1011, 2, -0xbacdeadbeLL, -1, -303, 1, 0x0feedbacdeadbeefLL }, ""); |
| 213 |
static_assert (bit_cast <K> (J { 0x0feedbacdeadbeefLL, -1011, 2, -0xbacdeadbeLL, -1, -303, 1, 0x0feedbacdeadbeefLL }) |
| 214 |
== K { 0x0feedbacdeadbeefLL, 7862463375103529997LL, 0x0feedbacdeadbeefLL }, ""); |
| 215 |
static_assert (bit_cast <M> (N { 0xfeedbacdeadbeef8ULL, 0x123456789abcde42ULL }) |
| 216 |
== M { -8, 59, 31, -5, 234, 0xfeedbacdU, 0x123456789abcde42ULL }, ""); |
| 217 |
static_assert (bit_cast <N> (M { -8, 59, 31, -5, 234, 0xfeedbacdU, 0x123456789abcde42ULL }) |
| 218 |
== N { 0xfeedbacdeadbeef8ULL, 0x123456789abcde42ULL }, ""); |
| 219 |
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 220 |
static_assert (bit_cast <J> (K { 0x0feedbacdeadbeefLL, -9103311533965288635LL, 0x0feedbacdeadbeefLL }) |
| 221 |
== J { 0x0feedbacdeadbeefLL, -1011, 2, -0xbacdeadbeLL, -1, -303, 1, 0x0feedbacdeadbeefLL }, ""); |
| 222 |
static_assert (bit_cast <K> (J { 0x0feedbacdeadbeefLL, -1011, 2, -0xbacdeadbeLL, -1, -303, 1, 0x0feedbacdeadbeefLL }) |
| 223 |
== K { 0x0feedbacdeadbeefLL, -9103311533965288635LL, 0x0feedbacdeadbeefLL }, ""); |
| 224 |
static_assert (bit_cast <M> (N { 0xfeedbacdeadbeef8ULL, 0x123456789abcde42ULL }) |
| 225 |
== M { -1, -35, -19, -6, 205, 0xeadbeef8U, 0x123456789abcde42ULL }, ""); |
| 226 |
static_assert (bit_cast <N> (M { -1, -35, -19, -6, 205, 0xeadbeef8U, 0x123456789abcde42ULL }) |
| 227 |
== N { 0xfeedbacdeadbeef8ULL, 0x123456789abcde42ULL }, ""); |
| 228 |
#endif |
| 229 |
#endif |