File indexing completed on 2026-05-03 08:13:13
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___BIT_BYTESWAP_H
0011 #define _LIBCPP___BIT_BYTESWAP_H
0012
0013 #include <__concepts/arithmetic.h>
0014 #include <__config>
0015 #include <cstdint>
0016
0017 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0018 # pragma GCC system_header
0019 #endif
0020
0021 _LIBCPP_BEGIN_NAMESPACE_STD
0022
0023 #if _LIBCPP_STD_VER >= 23
0024
0025 template <integral _Tp>
0026 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp byteswap(_Tp __val) noexcept {
0027 if constexpr (sizeof(_Tp) == 1) {
0028 return __val;
0029 } else if constexpr (sizeof(_Tp) == 2) {
0030 return __builtin_bswap16(__val);
0031 } else if constexpr (sizeof(_Tp) == 4) {
0032 return __builtin_bswap32(__val);
0033 } else if constexpr (sizeof(_Tp) == 8) {
0034 return __builtin_bswap64(__val);
0035 # if _LIBCPP_HAS_INT128
0036 } else if constexpr (sizeof(_Tp) == 16) {
0037 # if __has_builtin(__builtin_bswap128)
0038 return __builtin_bswap128(__val);
0039 # else
0040 return static_cast<_Tp>(byteswap(static_cast<uint64_t>(__val))) << 64 |
0041 static_cast<_Tp>(byteswap(static_cast<uint64_t>(__val >> 64)));
0042 # endif
0043 # endif
0044 } else {
0045 static_assert(sizeof(_Tp) == 0, "byteswap is unimplemented for integral types of this size");
0046 }
0047 }
0048
0049 #endif
0050
0051 _LIBCPP_END_NAMESPACE_STD
0052
0053 #endif