1 // RUN: %clang_cc1 -verify=ref,both -std=c++2a -fsyntax-only %s
2 // RUN: %clang_cc1 -verify=ref,both -std=c++2a -fsyntax-only -triple aarch64_be-linux-gnu %s
3 // RUN: %clang_cc1 -verify=ref,both -std=c++2a -fsyntax-only -triple powerpc64le-unknown-unknown -mabi=ieeelongdouble %s
4 // RUN: %clang_cc1 -verify=ref,both -std=c++2a -fsyntax-only -triple powerpc64-unknown-unknown -mabi=ieeelongdouble %s
6 // RUN: %clang_cc1 -verify=expected,both -std=c++2a -fsyntax-only -fexperimental-new-constant-interpreter %s
7 // RUN: %clang_cc1 -verify=expected,both -std=c++2a -fsyntax-only -triple aarch64_be-linux-gnu -fexperimental-new-constant-interpreter %s
8 // RUN: %clang_cc1 -verify=expected,both -std=c++2a -fsyntax-only -fexperimental-new-constant-interpreter -triple powerpc64le-unknown-unknown -mabi=ieeelongdouble %s
9 // RUN: %clang_cc1 -verify=expected,both -std=c++2a -fsyntax-only -fexperimental-new-constant-interpreter -triple powerpc64-unknown-unknown -mabi=ieeelongdouble %s
11 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
13 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
19 typedef decltype(nullptr) nullptr_t
;
20 typedef __INTPTR_TYPE__
intptr_t;
22 static_assert(sizeof(int) == 4);
23 static_assert(sizeof(long long) == 8);
25 template <class To
, class From
>
26 constexpr To
bit_cast(const From
&from
) {
27 static_assert(sizeof(To
) == sizeof(From
));
28 return __builtin_bit_cast(To
, from
);
31 template <class Intermediate
, class Init
>
32 constexpr bool check_round_trip(const Init
&init
) {
33 return bit_cast
<Init
>(bit_cast
<Intermediate
>(init
)) == init
;
36 template <class Intermediate
, class Init
>
37 constexpr Init
round_trip(const Init
&init
) {
38 return bit_cast
<Init
>(bit_cast
<Intermediate
>(init
));
42 namespace Discarding
{
44 constexpr int f
= (__builtin_bit_cast(int, 2), 0);
45 constexpr int f2
= (__builtin_bit_cast(S
, 2), 0);
49 enum byte
: unsigned char {};
52 using uint8_t = unsigned char;
56 using size_t = unsigned int;
59 constexpr unsigned char &operator[](size_t index
) {
66 template <int N
, typename T
= unsigned char, int Pad
= 0>
71 constexpr bool operator==(const T
& rhs
) const {
76 template <int N
, typename T
, int P
>
77 constexpr bool operator==(const struct bits
<N
, T
, P
>& lhs
, const struct bits
<N
, T
, P
>& rhs
) {
78 return lhs
.bits
== rhs
.bits
;
81 #ifdef __SIZEOF_INT128__
82 static_assert(check_round_trip
<__int128_t
>((__int128_t
)34));
83 static_assert(check_round_trip
<__int128_t
>((__int128_t
)-34));
85 constexpr unsigned char OneBit
[] = {
91 constexpr __int128_t One
= 1;
92 constexpr __int128_t Expected
= One
<< 120;
93 static_assert(__builtin_bit_cast(__int128_t
, OneBit
) == (LITTLE_END
? 1 : Expected
));
97 static_assert(check_round_trip
<double>(17.0));
101 constexpr int A
= __builtin_bit_cast(int, 10);
102 static_assert(A
== 10);
104 static_assert(__builtin_bit_cast(unsigned, 1.0F
) == 1065353216);
109 constexpr unsigned B
= __builtin_bit_cast(unsigned, Bytes
{10, 12, 13, 14});
110 static_assert(B
== (LITTLE_END
? 235736074 : 168561934));
113 constexpr unsigned C
= __builtin_bit_cast(unsigned, (_BitInt(32))12);
114 static_assert(C
== 12);
120 constexpr unsigned D
= __builtin_bit_cast(unsigned, BitInts
{12, 13});
121 static_assert(D
== (LITTLE_END
? 851980 : 786445));
125 static_assert(__builtin_bit_cast(char, true) == 1);
127 static_assert(check_round_trip
<unsigned>((int)-1));
128 static_assert(check_round_trip
<unsigned>((int)0x12345678));
129 static_assert(check_round_trip
<unsigned>((int)0x87654321));
130 static_assert(check_round_trip
<unsigned>((int)0x0C05FEFE));
131 static_assert(round_trip
<float>((int)0x0C05FEFE));
134 /// This works in GCC and in the bytecode interpreter, but the current interpreter
136 static_assert(__builtin_bit_cast(intptr_t, nullptr) == 0); // ref-error {{not an integral constant expression}} \
137 // ref-note {{indeterminate value can only initialize an object}}
141 constexpr int a
= 1/0; // both-error {{must be initialized by a constant expression}} \
142 // both-note {{division by zero}} \
143 // both-note {{declared here}}
144 constexpr int b
= __builtin_bit_cast(int, a
); // both-error {{must be initialized by a constant expression}} \
145 // both-note {{initializer of 'a' is not a constant expression}}
149 constexpr nullptr_t N
= __builtin_bit_cast(nullptr_t
, (intptr_t)1u);
150 static_assert(N
== nullptr);
151 static_assert(__builtin_bit_cast(nullptr_t
, (_BitInt(sizeof(void*) * 8))12) == __builtin_bit_cast(nullptr_t
, (unsigned _BitInt(sizeof(void*) * 8))0));
152 static_assert(__builtin_bit_cast(nullptr_t
, nullptr) == nullptr);
156 constexpr _BitInt(sizeof(int) * 8) BI
= ~0;
157 constexpr unsigned int I
= __builtin_bit_cast(unsigned int, BI
);
158 static_assert(I
== ~0u, "");
160 constexpr _BitInt(sizeof(int) * 8) IB
= __builtin_bit_cast(_BitInt(sizeof(int) * 8), I
); // ref-error {{must be initialized by a constant expression}} \
161 // ref-note {{constexpr bit cast involving type '_BitInt(32)' is not yet supported}} \
162 // ref-note {{declared here}}
163 static_assert(IB
== ~0u, ""); // ref-error {{not an integral constant expression}} \
164 // ref-note {{initializer of 'IB' is not a constant expression}}
167 namespace BitFields
{
173 constexpr unsigned A
= __builtin_bit_cast(unsigned, BitFields
{3, 16}); // ref-error {{must be initialized by a constant expression}} \
174 // ref-note {{not yet supported}} \
175 // ref-note {{declared here}}
176 static_assert(A
== (LITTLE_END
? 67 : 3221225488)); // ref-error {{not an integral constant expression}} \
177 // ref-note {{initializer of 'A'}}
180 void bitfield_indeterminate() {
181 struct BF
{ unsigned char z
: 2; };
182 enum byte
: unsigned char {};
184 constexpr BF bf
= {0x3};
185 /// Requires bitcasts to composite types.
186 // static_assert(bit_cast<bits<2>>(bf).bits == bf.z);
187 // static_assert(bit_cast<unsigned char>(bf));
190 // static_assert(__builtin_bit_cast(byte, bf));
193 // expected-note@+1 {{subobject declared here}}
194 unsigned char mem
[sizeof(BF
)];
196 // expected-error@+2 {{initialized by a constant expression}}
197 // expected-note@+1 {{not initialized}}
198 constexpr M m
= bit_cast
<M
>(bf
);
200 constexpr auto f
= []() constexpr {
201 // bits<24, unsigned int, LITTLE_END ? 0 : 8> B = {0xc0ffee};
202 constexpr struct { unsigned short b1
; unsigned char b0
; } B
= {0xc0ff, 0xee};
203 return bit_cast
<bytes
<4>>(B
);
206 static_assert(f()[0] + f()[1] + f()[2] == 0xc0 + 0xff + 0xee);
208 // expected-error@+2 {{initialized by a constant expression}}
209 // expected-note@+1 {{read of uninitialized object is not allowed in a constant expression}}
210 constexpr auto _bad
= f()[3];
214 unsigned short s0
: 8;
215 unsigned short s1
: 8;
220 constexpr auto g
= [f
]() constexpr {
221 return bit_cast
<B
>(f());
223 static_assert(g().s0
+ g().s1
+ g().b0
+ g().b1
== 0xc0 + 0xff + 0xe + 0xe);
225 // expected-error@+2 {{initialized by a constant expression}}
226 // expected-note@+1 {{read of uninitialized object is not allowed in a constant expression}}
227 constexpr auto _bad
= g().b2
;
242 static_assert(__builtin_bit_cast(int, B
{{0, 0},{0, 0}}) == 0);
243 static_assert(__builtin_bit_cast(int, B
{{13, 0},{0, 0}}) == (LITTLE_END
? 13 : 218103808));
244 static_assert(__builtin_bit_cast(int, B
{{13, 7},{12, 20}}) == (LITTLE_END
? 336332557 : 218565652));
249 constexpr Ref(const int &a
) : a(a
) {}
251 constexpr int I
= 12;
253 typedef __INTPTR_TYPE__
intptr_t;
254 static_assert(__builtin_bit_cast(intptr_t, Ref
{I
}) == 0); // both-error {{not an integral constant expression}} \
255 // both-note {{bit_cast from a type with a reference member is not allowed in a constant expression}}
259 constexpr C() : A
{1,2} {}
260 virtual constexpr int get() {
264 static_assert(__builtin_bit_cast(_BitInt(sizeof(C
) * 8), C()) == 0); // both-error {{source type must be trivially copyable}}
267 class D
: virtual A
{};
268 static_assert(__builtin_bit_cast(_BitInt(sizeof(D
) * 8), D()) == 0); // both-error {{source type must be trivially copyable}}
275 class E
: public A
, public F
{
277 constexpr E() : A
{1,2}, F
{3,4}, e
{5,6,7,8} {}
280 static_assert(__builtin_bit_cast(long long, E()) == (LITTLE_END
? 578437695752307201 : 72623859790382856));
287 constexpr int_splicer() : x(1), y(2) {}
288 constexpr int_splicer(unsigned x
, unsigned y
) : x(x
), y(y
) {}
290 constexpr bool operator==(const int_splicer
&other
) const {
291 return other
.x
== x
&& other
.y
== y
;
295 constexpr int_splicer
splice(0x0C05FEFE, 0xCAFEBABE);
298 static_assert(bit_cast
<unsigned long long>(splice
) == (LITTLE_END
300 : 0x0C05FEFECAFEBABE));
302 constexpr int_splicer IS
= bit_cast
<int_splicer
>(0xCAFEBABE0C05FEFE);
303 static_assert(bit_cast
<int_splicer
>(0xCAFEBABE0C05FEFE).x
== (LITTLE_END
307 static_assert(check_round_trip
<unsigned long long>(splice
));
308 static_assert(check_round_trip
<long long>(splice
));
313 /// ---------------------------------------------------------------------------
314 /// From here on, it's things copied from test/SemaCXX/constexpr-builtin-bit.cast.cpp
317 static_assert(round_trip
<unsigned>((int)-1));
318 static_assert(round_trip
<unsigned>((int)0x12345678));
319 static_assert(round_trip
<unsigned>((int)0x87654321));
320 static_assert(round_trip
<unsigned>((int)0x0C05FEFE));
324 constexpr unsigned char input
[] = {0xCA, 0xFE, 0xBA, 0xBE};
325 constexpr unsigned expected
= LITTLE_END
? 0xBEBAFECA : 0xCAFEBABE;
326 static_assert(bit_cast
<unsigned>(input
) == expected
);
328 /// Same things but with a composite array.
329 struct US
{ unsigned char I
; };
330 constexpr US input2
[] = {{0xCA}, {0xFE}, {0xBA}, {0xBE}};
331 static_assert(bit_cast
<unsigned>(input2
) == expected
);
339 constexpr bool operator==(const int_splicer
&other
) const {
340 return other
.x
== x
&& other
.y
== y
;
344 constexpr int_splicer splice
{0x0C05FEFE, 0xCAFEBABE};
346 static_assert(bit_cast
<unsigned long long>(splice
) == (LITTLE_END
348 : 0x0C05FEFECAFEBABE));
350 static_assert(bit_cast
<int_splicer
>(0xCAFEBABE0C05FEFE).x
== (LITTLE_END
354 static_assert(check_round_trip
<unsigned long long>(splice
));
355 static_assert(check_round_trip
<long long>(splice
));
364 struct bases
: int_splicer
, base2
, base3
{
369 unsigned x
, y
, z
, doublez
;
371 bool operator==(tuple4
const &other
) const = default;
372 constexpr bool operator==(bases
const &other
) const {
373 return x
== other
.x
&& y
== other
.y
&&
374 z
== other
.z
&& doublez
== other
.doublez
;
377 constexpr bases b
= {{1, 2}, {}, {3}, 4};
378 constexpr tuple4 t4
= bit_cast
<tuple4
>(b
);
379 static_assert(t4
== tuple4
{1, 2, 3, 4});
380 static_assert(check_round_trip
<tuple4
>(b
));
382 /// FIXME: We need to initialize the base pointers in the pointer we're bitcasting to.
383 // constexpr auto b2 = bit_cast<bases>(t4);
384 // static_assert(t4 == b2);
387 void test_partially_initialized() {
395 signed char p1
, p2
, p3
;
399 static_assert(sizeof(pad
) == sizeof(no_pad
));
402 constexpr pad pir
{4, 4};
403 constexpr int piw
= bit_cast
<no_pad
>(pir
).x
; // both-error {{constexpr variable 'piw' must be initialized by a constant expression}} \
404 // both-note {{in call to 'bit_cast<no_pad, pad>(pir)'}}
407 constexpr no_pad bad
= bit_cast
<no_pad
>(pir
); // both-error {{constexpr variable 'bad' must be initialized by a constant expression}} \
408 // both-note {{in call to 'bit_cast<no_pad, pad>(pir)'}}
409 // constexpr pad fine = bit_cast<pad>(no_pad{1, 2, 3, 4, 5});
410 // static_assert(fine.x == 1 && fine.y == 5);
419 static_assert(__builtin_bit_cast(int, X
{0}) == 0); // both-error {{not an integral constant expression}} \
420 // both-note {{bit_cast from a union type is not allowed in a constant expression}}
426 // both-error@+2 {{constexpr variable 'g' must be initialized by a constant expression}}
427 // both-note@+1 {{bit_cast from a union type is not allowed in a constant expression}}
428 constexpr G g
= __builtin_bit_cast(G
, X
{0});
429 // both-error@+2 {{constexpr variable 'x' must be initialized by a constant expression}}
430 // both-note@+1 {{bit_cast to a union type is not allowed in a constant expression}}
431 constexpr X x
= __builtin_bit_cast(X
, G
{0});
434 int *ptr
; // both-note {{invalid type 'int *' is a member of 'has_pointer'}}
437 constexpr intptr_t ptr
= __builtin_bit_cast(intptr_t, has_pointer
{0}); // both-error {{constexpr variable 'ptr' must be initialized by a constant expression}} \
438 // both-note {{bit_cast from a pointer type is not allowed in a constant expression}}
441 // expected-error@+2 {{constexpr variable 'hptr' must be initialized by a constant expression}}
442 // expected-note@+1 {{bit_cast to a pointer type is not allowed in a constant expression}}
443 constexpr has_pointer hptr
= __builtin_bit_cast(has_pointer
, 0ul);
447 void test_array_fill() {
448 constexpr unsigned char a
[4] = {1, 2};
449 constexpr unsigned int i
= bit_cast
<unsigned int>(a
);
450 static_assert(i
== (LITTLE_END
? 0x00000201 : 0x01020000));
457 // both-error@+2 {{constexpr variable 'run_vol_mem' must be initialized by a constant expression}}
458 // both-note@+1 {{non-literal type 'vol_mem' cannot be used in a constant expression}}
459 constexpr int run_vol_mem
= __builtin_bit_cast(int, vol_mem
{43});
462 int vol_mem::*x
; // both-note{{invalid type 'int vol_mem::*' is a member of 'mem_ptr'}}
465 // both-error@+2 {{constexpr variable 'run_mem_ptr' must be initialized by a constant expression}}
466 // both-note@+1 {{bit_cast from a member pointer type is not allowed in a constant expression}}
467 constexpr _BitInt(sizeof(mem_ptr
) * 8) run_mem_ptr
= __builtin_bit_cast(_BitInt(sizeof(mem_ptr
) * 8), mem_ptr
{nullptr});
469 constexpr int global_int
= 0;
474 // both-error@+2 {{constexpr variable 'run_ref_mem' must be initialized by a constant expression}}
475 // both-note@+1 {{bit_cast from a type with a reference member is not allowed in a constant expression}}
476 constexpr intptr_t run_ref_mem
= __builtin_bit_cast(intptr_t, ref_mem
{global_int
});
478 namespace test_vector
{
480 typedef unsigned uint2
__attribute__((vector_size(2 * sizeof(unsigned))));
481 typedef char byte8
__attribute__((vector_size(sizeof(unsigned long long))));
483 constexpr uint2 test_vector
= { 0x0C05FEFE, 0xCAFEBABE };
485 static_assert(bit_cast
<unsigned long long>(test_vector
) == (LITTLE_END
487 : 0x0C05FEFECAFEBABE), "");
488 static_assert(check_round_trip
<uint2
>(0xCAFEBABE0C05FEFEULL
), "");
489 static_assert(check_round_trip
<byte8
>(0xCAFEBABE0C05FEFEULL
), "");
491 typedef bool bool8
__attribute__((ext_vector_type(8)));
492 typedef bool bool9
__attribute__((ext_vector_type(9)));
493 typedef bool bool16
__attribute__((ext_vector_type(16)));
494 typedef bool bool17
__attribute__((ext_vector_type(17)));
495 typedef bool bool32
__attribute__((ext_vector_type(32)));
496 typedef bool bool128
__attribute__((ext_vector_type(128)));
498 static_assert(bit_cast
<unsigned char>(bool8
{1,0,1,0,1,0,1,0}) == (LITTLE_END
? 0x55 : 0xAA), "");
499 constexpr bool8 b8
= __builtin_bit_cast(bool8
, 0x55); // both-error {{'__builtin_bit_cast' source type 'int' does not match destination type 'bool8' (vector of 8 'bool' values) (4 vs 1 bytes)}}
501 static_assert(check_round_trip
<bool8
>(static_cast<unsigned char>(0)), "");
502 static_assert(check_round_trip
<bool8
>(static_cast<unsigned char>(1)), "");
503 static_assert(check_round_trip
<bool8
>(static_cast<unsigned char>(0x55)), "");
505 static_assert(bit_cast
<unsigned short>(bool16
{1,1,1,1,1,0,0,0, 1,1,1,1,0,1,0,0}) == (LITTLE_END
? 0x2F1F : 0xF8F4), "");
507 static_assert(check_round_trip
<bool16
>(static_cast<short>(0xCAFE)), "");
508 static_assert(check_round_trip
<bool32
>(static_cast<int>(0xCAFEBABE)), "");
509 static_assert(check_round_trip
<bool128
>(static_cast<__int128_t
>(0xCAFEBABE0C05FEFEULL
)), "");
513 // expected-error@+2 {{constexpr variable 'bad_bool9_to_short' must be initialized by a constant expression}}
514 // expected-note@+1 {{bit_cast involving type 'bool __attribute__((ext_vector_type(9)))' (vector of 9 'bool' values) is not allowed in a constant expression; element size 1 * element count 9 is not a multiple of the byte size 8}}
515 constexpr unsigned short bad_bool9_to_short
= __builtin_bit_cast(unsigned short, bool9
{1,1,0,1,0,1,0,1,0});
516 // expected-error@+2 {{constexpr variable 'bad_short_to_bool9' must be initialized by a constant expression}}
517 // expected-note@+1 {{bit_cast involving type 'bool __attribute__((ext_vector_type(9)))' (vector of 9 'bool' values) is not allowed in a constant expression; element size 1 * element count 9 is not a multiple of the byte size 8}}
518 constexpr bool9 bad_short_to_bool9
= __builtin_bit_cast(bool9
, static_cast<unsigned short>(0));
519 // expected-error@+2 {{constexpr variable 'bad_int_to_bool17' must be initialized by a constant expression}}
520 // expected-note@+1 {{bit_cast involving type 'bool __attribute__((ext_vector_type(17)))' (vector of 17 'bool' values) is not allowed in a constant expression; element size 1 * element count 17 is not a multiple of the byte size 8}}
521 constexpr bool17 bad_int_to_bool17
= __builtin_bit_cast(bool17
, 0x0001CAFEU
);
525 namespace test_complex
{
526 constexpr _Complex
unsigned test_int_complex
= { 0x0C05FEFE, 0xCAFEBABE };
527 static_assert(round_trip
<_Complex
unsigned>(0xCAFEBABE0C05FEFEULL
), "");
528 static_assert(bit_cast
<unsigned long long>(test_int_complex
) == (LITTLE_END
530 : 0x0C05FEFECAFEBABE), "");
531 static_assert(sizeof(double) == 2 * sizeof(float));
532 struct TwoFloats
{ float A
; float B
; };
533 constexpr _Complex
float test_float_complex
= {1.0f
, 2.0f
};
534 constexpr TwoFloats TF
= __builtin_bit_cast(TwoFloats
, test_float_complex
);
535 static_assert(TF
.A
== 1.0f
&& TF
.B
== 2.0f
);
537 constexpr double D
= __builtin_bit_cast(double, test_float_complex
);
538 constexpr int M
= __builtin_bit_cast(int, test_int_complex
); // both-error {{size of '__builtin_bit_cast' source type 'const _Complex unsigned int' does not match destination type 'int' (8 vs 4 bytes)}}