From 68543c53c0448f8faced51afbe3d1b78592dee35 Mon Sep 17 00:00:00 2001 From: Caroline Joy Bell Date: Tue, 4 Mar 2025 02:00:50 -0800 Subject: [PATCH] Address #17 (only) --- XenonUtils/byteswap.h | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/XenonUtils/byteswap.h b/XenonUtils/byteswap.h index 33e959f..46d1734 100644 --- a/XenonUtils/byteswap.h +++ b/XenonUtils/byteswap.h @@ -2,24 +2,31 @@ #include -template -inline T ByteSwap(T value) +#ifdef __clang__ +#define _byte_swap16(value) __builtin_bswap16(static_cast(value)) +#define _byte_swap32(value) __builtin_bswap32(static_cast(value)) +#define _byte_swap64(value) __builtin_bswap64(static_cast(value)) +#elif defined(_MSC_VER) +#define _byte_swap16(value) _byteswap_ushort(static_cast(value)) +#define _byte_swap32(value) _byteswap_ulong(static_cast(value)) +#define _byte_swap64(value) _byteswap_uint64(static_cast(value)) +#endif + +template T ByteSwap(T value) { if constexpr (sizeof(T) == 1) return value; - else if constexpr (sizeof(T) == 2) - return static_cast(__builtin_bswap16(static_cast(value))); - else if constexpr (sizeof(T) == 4) - return static_cast(__builtin_bswap32(static_cast(value))); - else if constexpr (sizeof(T) == 8) - return static_cast(__builtin_bswap64(static_cast(value))); + if constexpr (sizeof(T) == 2) + return static_cast(_byte_swap16(value)); + if constexpr (sizeof(T) == 4) + return static_cast(_byte_swap32(value)); + if constexpr (sizeof(T) == 8) + return static_cast(_byte_swap64(value)); assert(false && "Unexpected byte size."); - return value; } -template -inline void ByteSwapInplace(T& value) +template void ByteSwapInplace(T& value) { value = ByteSwap(value); }