104 lines · cpp
1// RUN: %clang_cc1 -verify=ref,both -std=c++2a -fsyntax-only -triple x86_64-apple-macosx10.14.0 %s2// RUN: %clang_cc1 -verify=ref,both -std=c++2a -fsyntax-only -triple x86_64-apple-macosx10.14.0 %s -fno-signed-char3// RUN: %clang_cc1 -verify=ref,both -std=c++2a -fsyntax-only -triple aarch64_be-linux-gnu %s4 5// RUN: %clang_cc1 -verify=expected,both -std=c++2a -fsyntax-only -triple x86_64-apple-macosx10.14.0 %s -fexperimental-new-constant-interpreter6// 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-interpreter7// RUN: %clang_cc1 -verify=expected,both -std=c++2a -fsyntax-only -triple aarch64_be-linux-gnu %s -fexperimental-new-constant-interpreter8 9#if !__x86_6410// both-no-diagnostics11#endif12 13 14typedef decltype(nullptr) nullptr_t;15typedef __INTPTR_TYPE__ intptr_t;16 17static_assert(sizeof(int) == 4);18static_assert(sizeof(long long) == 8);19 20template <class To, class From>21constexpr To bit_cast(const From &from) {22 static_assert(sizeof(To) == sizeof(From));23 return __builtin_bit_cast(To, from);24#if __x86_6425 // both-note@-2 {{indeterminate value can only initialize an object of type}}26#endif27}28 29template <class Intermediate, class Init>30constexpr bool check_round_trip(const Init &init) {31 return bit_cast<Init>(bit_cast<Intermediate>(init)) == init;32}33 34template <class Intermediate, class Init>35constexpr Init round_trip(const Init &init) {36 return bit_cast<Init>(bit_cast<Intermediate>(init));37}38 39 40 41 42namespace test_long_double {43#if __x86_6444constexpr __int128_t test_cast_to_int128 = bit_cast<__int128_t>((long double)0); // both-error{{must be initialized by a constant expression}}\45 // both-note{{in call}}46constexpr long double ld = 3.1425926539;47 48struct bytes {49 unsigned char d[16];50};51 52static_assert(round_trip<bytes>(ld), "");53 54static_assert(round_trip<long double>(10.0L));55 56constexpr long double foo() {57 bytes A = __builtin_bit_cast(bytes, ld);58 long double ld = __builtin_bit_cast(long double, A);59 return ld;60}61static_assert(foo() == ld);62 63constexpr bool f(bool read_uninit) {64 bytes b = bit_cast<bytes>(ld);65 unsigned char ld_bytes[10] = {66 0x0, 0x48, 0x9f, 0x49, 0xf0,67 0x3c, 0x20, 0xc9, 0x0, 0x40,68 };69 70 for (int i = 0; i != 10; ++i)71 if (ld_bytes[i] != b.d[i])72 return false;73 74 if (read_uninit && b.d[10]) // both-note{{read of uninitialized object is not allowed in a constant expression}}75 return false;76 77 return true;78}79 80static_assert(f(/*read_uninit=*/false), "");81static_assert(f(/*read_uninit=*/true), ""); // both-error{{static assertion expression is not an integral constant expression}} \82 // both-note{{in call to 'f(true)'}}83constexpr bytes ld539 = {84 0x0, 0x0, 0x0, 0x0,85 0x0, 0x0, 0xc0, 0x86,86 0x8, 0x40, 0x0, 0x0,87 0x0, 0x0, 0x0, 0x0,88};89constexpr long double fivehundredandthirtynine = 539.0;90static_assert(bit_cast<long double>(ld539) == fivehundredandthirtynine, "");91 92struct LD {93 long double v;94};95 96constexpr LD ld2 = __builtin_bit_cast(LD, ld539.d);97constexpr long double five39 = __builtin_bit_cast(long double, ld539.d);98static_assert(ld2.v == five39);99 100#else101static_assert(round_trip<__int128_t>(34.0L));102#endif103}104