diff --git a/libc/src/__support/CPP/bit.h b/libc/src/__support/CPP/bit.h --- a/libc/src/__support/CPP/bit.h +++ b/libc/src/__support/CPP/bit.h @@ -11,6 +11,8 @@ #include "src/__support/macros/config.h" // LIBC_HAS_BUILTIN +#include + namespace __llvm_libc::cpp { #if LIBC_HAS_BUILTIN(__builtin_bit_cast) @@ -26,8 +28,10 @@ template constexpr To bit_cast(const From &from) { static_assert(sizeof(To) == sizeof(From), "To and From must be of same size"); #if defined(LLVM_LIBC_HAS_BUILTIN_BIT_CAST) - return __builtin_bit_cast(To, from); -#else + if constexpr (std::is_trivially_copyable_v && + std::is_trivially_copyable_v) + return __builtin_bit_cast(To, from); +#endif // defined(LLVM_LIBC_HAS_BUILTIN_BIT_CAST) To to; char *dst = reinterpret_cast(&to); const char *src = reinterpret_cast(&from); @@ -38,7 +42,6 @@ dst[i] = src[i]; #endif // defined(LLVM_LIBC_HAS_BUILTIN_MEMCPY_INLINE) return to; -#endif // defined(LLVM_LIBC_HAS_BUILTIN_BIT_CAST) } } // namespace __llvm_libc::cpp