[libcxx-commits] [libcxx] [libc++] Use __builtin_bswapg to implement std::byteswap if it's available (PR #168557)
via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Nov 18 08:03:38 PST 2025
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff origin/main HEAD --extensions h -- libcxx/include/__bit/byteswap.h --diff_from_common_commit
``````````
:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/libcxx/include/__bit/byteswap.h b/libcxx/include/__bit/byteswap.h
index 7dd4fc38c..6103be5a7 100644
--- a/libcxx/include/__bit/byteswap.h
+++ b/libcxx/include/__bit/byteswap.h
@@ -24,9 +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)
+# if __has_builtin(__builtin_bswapg)
return __builtin_bswapg(__val);
-#else
+# else
if constexpr (sizeof(_Tp) == 1) {
return __val;
} else if constexpr (sizeof(_Tp) == 2) {
@@ -35,19 +35,19 @@ template <integral _Tp>
return __builtin_bswap32(__val);
} else if constexpr (sizeof(_Tp) == 8) {
return __builtin_bswap64(__val);
-# if _LIBCPP_HAS_INT128
+# if _LIBCPP_HAS_INT128
} else if constexpr (sizeof(_Tp) == 16) {
-# if __has_builtin(__builtin_bswap128)
+# if __has_builtin(__builtin_bswap128)
return __builtin_bswap128(__val);
-# else
+# else
return static_cast<_Tp>(byteswap(static_cast<uint64_t>(__val))) << 64 |
static_cast<_Tp>(byteswap(static_cast<uint64_t>(__val >> 64)));
-# endif // __has_builtin(__builtin_bswap128)
-# endif // _LIBCPP_HAS_INT128
+# endif // __has_builtin(__builtin_bswap128)
+# endif // _LIBCPP_HAS_INT128
} else {
static_assert(sizeof(_Tp) == 0, "byteswap is unimplemented for integral types of this size");
}
-#endif
+# endif
}
#endif // _LIBCPP_STD_VER >= 23
``````````
</details>
https://github.com/llvm/llvm-project/pull/168557
More information about the libcxx-commits
mailing list