1 // RUN: %clang_cc1 -verify=ref,both -std=c++2a -fsyntax-only -triple x86_64-apple-macosx10.14.0 %s
2 // RUN: %clang_cc1 -verify=ref,both -std=c++2a -fsyntax-only -triple x86_64-apple-macosx10.14.0 %s -fno-signed-char
3 // RUN: %clang_cc1 -verify=ref,both -std=c++2a -fsyntax-only -triple aarch64_be-linux-gnu %s
5 // RUN: %clang_cc1 -verify=expected,both -std=c++2a -fsyntax-only -triple x86_64-apple-macosx10.14.0 %s -fexperimental-new-constant-interpreter
6 // RUN: %clang_cc1 -verify=expected,both -std=c++2a -fsyntax-only -triple x86_64-apple-macosx10.14.0 %s -fno-signed-char -fexperimental-new-constant-interpreter
7 // RUN: %clang_cc1 -verify=expected,both -std=c++2a -fsyntax-only -triple aarch64_be-linux-gnu %s -fexperimental-new-constant-interpreter
11 typedef decltype(nullptr) nullptr_t
;
12 typedef __INTPTR_TYPE__
intptr_t;
14 static_assert(sizeof(int) == 4);
15 static_assert(sizeof(long long) == 8);
17 template <class To
, class From
>
18 constexpr To
bit_cast(const From
&from
) {
19 static_assert(sizeof(To
) == sizeof(From
));
20 return __builtin_bit_cast(To
, from
);
23 template <class Intermediate
, class Init
>
24 constexpr bool check_round_trip(const Init
&init
) {
25 return bit_cast
<Init
>(bit_cast
<Intermediate
>(init
)) == init
;
28 template <class Intermediate
, class Init
>
29 constexpr Init
round_trip(const Init
&init
) {
30 return bit_cast
<Init
>(bit_cast
<Intermediate
>(init
));
36 namespace test_long_double
{
39 constexpr __int128_t test_cast_to_int128
= bit_cast
<__int128_t
>((long double)0); // expected-error{{must be initialized by a constant expression}}\
40 // expected-note{{in call}}
42 constexpr long double ld
= 3.1425926539;
48 static_assert(round_trip
<bytes
>(ld
), "");
50 static_assert(round_trip
<long double>(10.0L));
53 constexpr bool f(bool read_uninit
) {
54 bytes b
= bit_cast
<bytes
>(ld
);
55 unsigned char ld_bytes
[10] = {
56 0x0, 0x48, 0x9f, 0x49, 0xf0,
57 0x3c, 0x20, 0xc9, 0x0, 0x40,
60 for (int i
= 0; i
!= 10; ++i
)
61 if (ld_bytes
[i
] != b
.d
[i
])
64 if (read_uninit
&& b
.d
[10]) // expected-note{{read of uninitialized object is not allowed in a constant expression}}
70 static_assert(f(/*read_uninit=*/false), "");
71 static_assert(f(/*read_uninit=*/true), ""); // expected-error{{static assertion expression is not an integral constant expression}} \
72 // expected-note{{in call to 'f(true)'}}
74 constexpr bytes ld539
= {
80 constexpr long double fivehundredandthirtynine
= 539.0;
81 static_assert(bit_cast
<long double>(ld539
) == fivehundredandthirtynine
, "");
87 constexpr LD ld2
= __builtin_bit_cast(LD
, ld539
.d
);
88 constexpr long double five39
= __builtin_bit_cast(long double, ld539
.d
);
89 static_assert(ld2
.v
== five39
);
92 static_assert(round_trip
<__int128_t
>(34.0L));