11#if HI_COMPILER == HI_CC_MSVC
17namespace hi::inline v1 {
19template<std::
unsigned_
integral T>
20[[nodiscard]] T byte_swap(T x)
noexcept
22#if HI_COMPILER == HI_CC_CLANG || HI_COMPILER == HI_CC_GCC
23 if constexpr (
sizeof(T) ==
sizeof(uint64_t)) {
24 return static_cast<T
>(__builtin_bswap64(
static_cast<uint64_t
>(x)));
25 }
else if constexpr (
sizeof(T) ==
sizeof(uint32_t)) {
26 return static_cast<T
>(__builtin_bswap32(
static_cast<uint32_t
>(x)));
27 }
else if constexpr (
sizeof(T) ==
sizeof(uint16_t)) {
28 return static_cast<T
>(__builtin_bswap16(
static_cast<uint16_t
>(x)));
32#elif HI_COMPILER == HI_CC_MSVC
33 if constexpr (
sizeof(T) ==
sizeof(uint64_t)) {
34 return static_cast<T
>(_byteswap_uint64(
static_cast<uint64_t
>(x)));
35 }
else if constexpr (
sizeof(T) ==
sizeof(
unsigned long)) {
36 return static_cast<T
>(_byteswap_ulong(
static_cast<unsigned long>(x)));
37 }
else if constexpr (
sizeof(T) ==
sizeof(
unsigned short)) {
38 return static_cast<T
>(_byteswap_ushort(
static_cast<unsigned short>(x)));
43#error "Byteswap not implemented for this compiler."
47template<std::
signed_
integral T>
48[[nodiscard]] T byte_swap(T x)
noexcept
50 return static_cast<T
>(byte_swap(
static_cast<std::make_unsigned_t<T>
>(x)));
53template<std::
floating_po
int T>
54[[nodiscard]] T byte_swap(T x)
noexcept
56 if constexpr (std::is_same_v<T, float>) {
57 auto utmp = std::bit_cast<uint32_t>(x);
58 utmp = byte_swap(utmp);
59 return std::bit_cast<float>(x);
60 }
else if constexpr (std::is_same_v<T, double>) {
61 auto utmp = std::bit_cast<uint64_t>(x);
62 utmp = byte_swap(utmp);
63 return std::bit_cast<double>(x);
69template<std::
integral T>
70[[nodiscard]] T little_to_native(T x)
72 if constexpr (std::endian::native == std::endian::little) {
79template<std::
integral T>
80[[nodiscard]] T big_to_native(T x)
82 if constexpr (std::endian::native == std::endian::big) {
89template<std::
integral T>
90[[nodiscard]] T native_to_little(T x)
92 if constexpr (std::endian::native == std::endian::little) {
99template<std::
integral T>
100[[nodiscard]] T native_to_big(T x)
102 if constexpr (std::endian::native == std::endian::big) {
109template<
typename T, std::endian E, std::
size_t A = alignof(T)>
111 alignas(A) std::byte _value[
sizeof(T)];
113 [[nodiscard]] T value()
const noexcept
118 return E == std::endian::native ? x : byte_swap(x);
123 if constexpr (E != std::endian::native) {
136 operator T()
const noexcept
Functions and macros for handling architectural difference between compilers, CPUs and operating syst...
Definition endian.hpp:110