1
1
#pragma once
2
2
3
3
#include " fly/concepts/concepts.hpp"
4
- #include " fly/fly .hpp"
4
+ #include " fly/types/numeric/detail/byte_swap .hpp"
5
5
#include " fly/types/numeric/detail/endian_concepts.hpp"
6
6
7
7
#include < bit>
8
8
#include < cstdint>
9
9
10
- #if defined(FLY_LINUX)
11
- # include < byteswap.h>
12
- #elif defined(FLY_MACOS)
13
- # include < libkern/OSByteOrder.h>
14
- #elif defined(FLY_WINDOWS)
15
- # include " fly/types/numeric/literals.hpp"
16
- #else
17
- # error Unknown byte swapping includes.
18
- #endif
19
-
20
- #if defined(FLY_LINUX)
21
- # define byte_swap_16 (b ) __builtin_bswap16(b)
22
- # define byte_swap_32 (b ) __builtin_bswap32(b)
23
- # define byte_swap_64 (b ) __builtin_bswap64(b)
24
- #elif defined(FLY_MACOS)
25
- # define byte_swap_16 (b ) OSSwapInt16(b)
26
- # define byte_swap_32 (b ) OSSwapInt32(b)
27
- # define byte_swap_64 (b ) OSSwapInt64(b)
28
- #elif defined(FLY_WINDOWS)
29
-
30
- // Windows has _byteswap_ushort, _byteswap_ulong, and _byteswap_uint64, but they are non-constexpr.
31
- // So to allow endian swapping to be used at compile time, use custom byte swapping methods.
32
-
33
- constexpr std::uint16_t byte_swap_16 (std::uint16_t value)
34
- {
35
- using namespace fly ::literals::numeric_literals;
36
-
37
- return ((value & 0xff00_u16) >> 8 ) | ((value & 0x00ff_u16) << 8 );
38
- }
39
-
40
- constexpr std::uint32_t byte_swap_32 (std::uint32_t value)
41
- {
42
- using namespace fly ::literals::numeric_literals;
43
-
44
- return (
45
- ((value & 0xff00' 0000_u32) >> 24 ) | ((value & 0x00ff' 0000_u32) >> 8 ) |
46
- ((value & 0x0000' ff00_u32) << 8 ) | ((value & 0x0000' 00ff_u32) << 24 ));
47
- }
48
-
49
- constexpr std::uint64_t byte_swap_64 (std::uint64_t value)
50
- {
51
- using namespace fly ::literals::numeric_literals;
52
-
53
- return (
54
- ((value & 0xff00'0000'0000' 0000_u64) >> 56 ) | ((value & 0x00ff'0000'0000' 0000_u64) >> 40 ) |
55
- ((value & 0x0000'ff00'0000' 0000_u64) >> 24 ) | ((value & 0x0000'00ff'0000' 0000_u64) >> 8 ) |
56
- ((value & 0x0000'0000'ff00' 0000_u64) << 8 ) | ((value & 0x0000'0000'00ff' 0000_u64) << 24 ) |
57
- ((value & 0x0000'0000'0000' ff00_u64) << 40 ) | ((value & 0x0000'0000'0000' 00ff_u64) << 56 ));
58
- }
59
-
60
- #else
61
- # error Unknown byte swapping methods.
62
- #endif
63
-
64
10
namespace fly {
65
11
66
12
/* *
@@ -81,15 +27,15 @@ constexpr T endian_swap(T value)
81
27
}
82
28
else if constexpr (fly::SizeOfTypeIs<T, 2 >)
83
29
{
84
- return static_cast <T>(byte_swap_16 (static_cast <std::uint16_t >(value)));
30
+ return static_cast <T>(detail::byte_swap (static_cast <std::uint16_t >(value)));
85
31
}
86
32
else if constexpr (fly::SizeOfTypeIs<T, 4 >)
87
33
{
88
- return static_cast <T>(byte_swap_32 (static_cast <std::uint32_t >(value)));
34
+ return static_cast <T>(detail::byte_swap (static_cast <std::uint32_t >(value)));
89
35
}
90
36
else if constexpr (fly::SizeOfTypeIs<T, 8 >)
91
37
{
92
- return static_cast <T>(byte_swap_64 (static_cast <std::uint64_t >(value)));
38
+ return static_cast <T>(detail::byte_swap (static_cast <std::uint64_t >(value)));
93
39
}
94
40
}
95
41
0 commit comments