Skip to content

Commit f24767d

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

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

XenonUtils/byteswap.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,27 @@
22

33
#include <cassert>
44

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+
515
template<typename T>
616
inline T ByteSwap(T value)
717
{
818
if constexpr (sizeof(T) == 1)
919
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)));
20+
if constexpr (sizeof(T) == 2)
21+
return static_cast<T>(_byte_swap16(value));
22+
if constexpr (sizeof(T) == 4)
23+
return static_cast<T>(_byte_swap32(value));
24+
if constexpr (sizeof(T) == 8)
25+
return static_cast<T>(_byte_swap64(value));
1626

1727
assert(false && "Unexpected byte size.");
1828
return value;

0 commit comments

Comments
 (0)