[libc++] Implement C++20's P0476R2: std::bit_cast
Thanks to Arthur O'Dwyer for fixing up some of the tests.
Differential Revision: https://reviews.llvm.org/D75960
NOKEYCHECK=True
GitOrigin-RevId: b1fb3d75c953fa2e02ebddb6ebbf100f99786f0c
diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt
index 4217e19..34cc9a5 100644
--- a/include/CMakeLists.txt
+++ b/include/CMakeLists.txt
@@ -94,6 +94,7 @@
__algorithm/upper_bound.h
__availability
__bit_reference
+ __bit/bit_cast.h
__bits
__bsd_locale_defaults.h
__bsd_locale_fallbacks.h
diff --git a/include/__bit/bit_cast.h b/include/__bit/bit_cast.h
new file mode 100644
index 0000000..6cfe4d7
--- /dev/null
+++ b/include/__bit/bit_cast.h
@@ -0,0 +1,38 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___BIT_BIT_CAST_H
+#define _LIBCPP___BIT_BIT_CAST_H
+
+#include <__config>
+#include <type_traits>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER > 17
+
+template<class _ToType, class _FromType, class = enable_if_t<
+ sizeof(_ToType) == sizeof(_FromType) &&
+ is_trivially_copyable_v<_ToType> &&
+ is_trivially_copyable_v<_FromType>
+>>
+_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI
+constexpr _ToType bit_cast(_FromType const& __from) noexcept {
+ return __builtin_bit_cast(_ToType, __from);
+}
+
+#endif // _LIBCPP_STD_VER > 17
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___BIT_BIT_CAST_H
diff --git a/include/bit b/include/bit
index 168cdd9..eab6797 100644
--- a/include/bit
+++ b/include/bit
@@ -14,6 +14,9 @@
bit synopsis
namespace std {
+ // [bit.cast], bit_cast
+ template<class To, class From>
+ constexpr To bit_cast(const From& from) noexcept; // C++20
// [bit.pow.two], integral powers of 2
template <class T>
@@ -54,8 +57,9 @@
*/
-#include <__config>
+#include <__bit/bit_cast.h>
#include <__bits> // __libcpp_clz
+#include <__config>
#include <__debug>
#include <limits>
#include <type_traits>
diff --git a/include/module.modulemap b/include/module.modulemap
index 4986fd6..e49d78a 100644
--- a/include/module.modulemap
+++ b/include/module.modulemap
@@ -335,6 +335,10 @@
module bit {
header "bit"
export *
+
+ module __bit {
+ module bit_cast { private header "__bit/bit_cast.h" }
+ }
}
module bitset {
header "bitset"
diff --git a/include/version b/include/version
index 8db08ef..8aa5fd4 100644
--- a/include/version
+++ b/include/version
@@ -286,7 +286,7 @@
# define __cpp_lib_barrier 201907L
# endif
# define __cpp_lib_bind_front 201907L
-// # define __cpp_lib_bit_cast 201806L
+# define __cpp_lib_bit_cast 201806L
// # define __cpp_lib_bitops 201907L
# define __cpp_lib_bounded_array_traits 201902L
# if !defined(_LIBCPP_HAS_NO_CHAR8_T)