26#ifndef MYSQL_HARNESS_STDX_BIT_H_
27#define MYSQL_HARNESS_STDX_BIT_H_
51template <std::
integral T>
53 static_assert(std::has_unique_object_representations_v<
decltype(num)>,
54 "T may not have padding bits");
56 if constexpr (
sizeof(T) == 1) {
59 }
else if constexpr (
sizeof(T) == 2) {
60 return __builtin_bswap16(num);
61 }
else if constexpr (
sizeof(T) == 4) {
62 return __builtin_bswap32(num);
63 }
else if constexpr (
sizeof(T) == 8) {
64 return __builtin_bswap64(num);
66 }
else if constexpr (
sizeof(T) == 2) {
67 return (num & UINT16_C(0x00ff)) << (1 * 8) |
68 (num & UINT16_C(0xff00)) >> (1 * 8);
69 }
else if constexpr (
sizeof(T) == 4) {
70 return (num & UINT32_C(0x0000'00ff)) << (3 * 8) |
71 (num & UINT32_C(0x0000'ff00)) << (1 * 8) |
72 (num & UINT32_C(0x00ff'0000)) >> (1 * 8) |
73 (num & UINT32_C(0xff00'0000)) >> (3 * 8);
74 }
else if constexpr (
sizeof(T) == 8) {
75 return (num & UINT64_C(0x0000'0000'0000'00ff)) << (7 * 8) |
76 (num & UINT64_C(0x0000'0000'0000'ff00)) << (5 * 8) |
77 (num & UINT64_C(0x0000'0000'00ff'0000)) << (3 * 8) |
78 (num & UINT64_C(0x0000'0000'ff00'0000)) << (1 * 8) |
79 (num & UINT64_C(0x0000'00ff'0000'0000)) >> (1 * 8) |
80 (num & UINT64_C(0x0000'ff00'0000'0000)) >> (3 * 8) |
81 (num & UINT64_C(0x00ff'0000'0000'0000)) >> (5 * 8) |
82 (num & UINT64_C(0xff00'0000'0000'0000)) >> (7 * 8);
84 static_assert(
sizeof(num) == 0,
85 "byteswap not implemented for integral types of this size");
constexpr T byteswap(T num) noexcept
Definition: bit.h:52