[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 10:21:24 PST 2023


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGdd8ecde00b3b: [ADT] llvm::bit_cast - use __builtin_bit_cast if available (authored by RKSimon).

Changed prior to commit:
  https://reviews.llvm.org/D142305?vs=491160&id=491189#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D142305/new/

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
+
 #if defined(_MSC_VER) && !defined(_DEBUG)
 #include <cstdlib>  // for _byteswap_{ushort,ulong,uint64}
 #endif
@@ -48,9 +51,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
 }
 
 /// Reverses the bytes in the given integer value V.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D142305.491189.patch
Type: text/x-patch
Size: 938 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230122/11589da7/attachment.bin>


More information about the llvm-commits mailing list