[libcxx-commits] [libcxx] [libc++] Use __builtin_bswapg to implement std::byteswap if it's available (PR #168557)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Tue Nov 18 08:00:18 PST 2025


https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/168557

This improves compile times a bit and allows us to remove some code in the future.


>From d4f0c0573ac717e2f05f4c02bea8b87e9a9c9320 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Tue, 18 Nov 2025 16:58:50 +0100
Subject: [PATCH] [libc++] Use __builtin_bswapg to implement std::byteswap if
 it's available

---
 libcxx/include/__bit/byteswap.h | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/libcxx/include/__bit/byteswap.h b/libcxx/include/__bit/byteswap.h
index d761e6a6fdb46..7dd4fc38c1854 100644
--- a/libcxx/include/__bit/byteswap.h
+++ b/libcxx/include/__bit/byteswap.h
@@ -24,6 +24,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 
 template <integral _Tp>
 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp byteswap(_Tp __val) noexcept {
+#if __has_builtin(__builtin_bswapg)
+  return __builtin_bswapg(__val);
+#else
   if constexpr (sizeof(_Tp) == 1) {
     return __val;
   } else if constexpr (sizeof(_Tp) == 2) {
@@ -44,6 +47,7 @@ template <integral _Tp>
   } else {
     static_assert(sizeof(_Tp) == 0, "byteswap is unimplemented for integral types of this size");
   }
+#endif
 }
 
 #endif // _LIBCPP_STD_VER >= 23



More information about the libcxx-commits mailing list