[PATCH] D142305: [ADT] llvm::bit_cast - use __builtin_bit_cast if available
Simon Pilgrim via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sun Jan 22 07:52:45 PST 2023
RKSimon created this revision.
RKSimon added reviewers: kazu, dblaikie, MaskRay, barannikov88.
Herald added a project: All.
RKSimon requested review of this revision.
Herald added a project: LLVM.
If the compiler supports __builtin_bit_cast we should try to use it instead of std::memcpy (and avoid including the cstring header).
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D142305
Files:
llvm/include/llvm/ADT/bit.h
Index: llvm/include/llvm/ADT/bit.h
===================================================================
--- llvm/include/llvm/ADT/bit.h
+++ llvm/include/llvm/ADT/bit.h
@@ -16,10 +16,13 @@
#include "llvm/Support/Compiler.h"
#include <cstdint>
-#include <cstring>
#include <limits>
#include <type_traits>
+#if !__has_builtin(__builtin_bit_cast)
+#include <cstring>
+#endif
+
#ifdef _MSC_VER
// Declare these intrinsics manually rather including intrin.h. It's very
// expensive, and bit.h is popular via MathExtras.h.
@@ -44,9 +47,13 @@
typename = std::enable_if_t<std::is_trivially_copyable<To>::value>,
typename = std::enable_if_t<std::is_trivially_copyable<From>::value>>
[[nodiscard]] inline To bit_cast(const From &from) noexcept {
+#if __has_builtin(__builtin_bit_cast)
+ return __builtin_bit_cast(To, from);
+#else
To to;
std::memcpy(&to, &from, sizeof(To));
return to;
+#endif
}
template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D142305.491160.patch
Type: text/x-patch
Size: 994 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230122/93bc182d/attachment.bin>
More information about the llvm-commits
mailing list