Skip to content

Commit 68543c5

Browse files
committed
Address #17 (only)
1 parent 2991a3b commit 68543c5

1 file changed

Lines changed: 18 additions & 11 deletions

File tree

XenonUtils/byteswap.h

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,31 @@
22

33
#include <cassert>
44

5-
template<typename T>
6-
inline T ByteSwap(T value)
5+
#ifdef __clang__
6+
#define _byte_swap16(value) __builtin_bswap16(static_cast<uint16_t>(value))
7+
#define _byte_swap32(value) __builtin_bswap32(static_cast<uint32_t>(value))
8+
#define _byte_swap64(value) __builtin_bswap64(static_cast<uint64_t>(value))
9+
#elif defined(_MSC_VER)
10+
#define _byte_swap16(value) _byteswap_ushort(static_cast<uint16_t>(value))
11+
#define _byte_swap32(value) _byteswap_ulong(static_cast<uint32_t>(value))
12+
#define _byte_swap64(value) _byteswap_uint64(static_cast<uint64_t>(value))
13+
#endif
14+
15+
template<typename T> T ByteSwap(T value)
716
{
817
if constexpr (sizeof(T) == 1)
918
return value;
10-
else if constexpr (sizeof(T) == 2)
11-
return static_cast<T>(__builtin_bswap16(static_cast<uint16_t>(value)));
12-
else if constexpr (sizeof(T) == 4)
13-
return static_cast<T>(__builtin_bswap32(static_cast<uint32_t>(value)));
14-
else if constexpr (sizeof(T) == 8)
15-
return static_cast<T>(__builtin_bswap64(static_cast<uint64_t>(value)));
19+
if constexpr (sizeof(T) == 2)
20+
return static_cast<T>(_byte_swap16(value));
21+
if constexpr (sizeof(T) == 4)
22+
return static_cast<T>(_byte_swap32(value));
23+
if constexpr (sizeof(T) == 8)
24+
return static_cast<T>(_byte_swap64(value));
1625

1726
assert(false && "Unexpected byte size.");
18-
return value;
1927
}
2028

21-
template<typename T>
22-
inline void ByteSwapInplace(T& value)
29+
template<typename T> void ByteSwapInplace(T& value)
2330
{
2431
value = ByteSwap(value);
2532
}

0 commit comments

Comments
 (0)