[clang] [Clang][C++20] Implement constexpr std::bit_cast for bit-fields (PR #74775)

via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 4 18:03:17 PST 2024


================
@@ -485,16 +869,16 @@ typedef bool bool17 __attribute__((ext_vector_type(17)));
 typedef bool bool32 __attribute__((ext_vector_type(32)));
 typedef bool bool128 __attribute__((ext_vector_type(128)));
 
-static_assert(bit_cast<unsigned char>(bool8{1,0,1,0,1,0,1,0}) == (LITTLE_END ? 0x55 : 0xAA), "");
-static_assert(round_trip<bool8>(static_cast<unsigned char>(0)), "");
-static_assert(round_trip<bool8>(static_cast<unsigned char>(1)), "");
-static_assert(round_trip<bool8>(static_cast<unsigned char>(0x55)), "");
+static_assert(bit_cast<unsigned char>(bool8{1,0,1,0,1,0,1,0}) == (LITTLE_END ? 0x55 : 0xAA));
+static_assert(round_trip<bool8>('\x00') == 0);
+static_assert(round_trip<bool8>('\x01') == 0x1);
+static_assert(round_trip<bool8>('\x55') == 0x55);
 
-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), "");
+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));
 
-static_assert(round_trip<bool16>(static_cast<short>(0xCAFE)), "");
-static_assert(round_trip<bool32>(static_cast<int>(0xCAFEBABE)), "");
-static_assert(round_trip<bool128>(static_cast<__int128_t>(0xCAFEBABE0C05FEFEULL)), "");
+static_assert(check_round_trip<bool16>(static_cast<short>(0xCAFE)));
+static_assert(check_round_trip<bool32>(static_cast<int>(0xCAFEBABE)));
+static_assert(check_round_trip<bool128>(static_cast<__int128_t>(0xCAFEBABE0C05FEFEULL)));
 
 // expected-error at +2 {{constexpr variable 'bad_bool9_to_short' must be initialized by a constant expression}}
 // expected-note at +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}}
----------------
sethp wrote:

I don't know if it's important or not, but this would be pretty easy to support with the new bit-wise `APInt`-backed BitCastBuffer: I didn't try for it because there's already quite a lot going on here, but I'm happy to follow up on that if it seems worthwhile.

https://github.com/llvm/llvm-project/pull/74775


More information about the cfe-commits mailing list