From 32c2f49bec26dcd14c395b4c2dc70c2122bcd1b1 Mon Sep 17 00:00:00 2001 From: Yohann Martel Date: Fri, 7 Mar 2025 14:24:17 -0500 Subject: [PATCH] Fix bswap with MSVC compiler --- XenonUtils/byteswap.h | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/XenonUtils/byteswap.h b/XenonUtils/byteswap.h index 33e959f..3b7ebe4 100644 --- a/XenonUtils/byteswap.h +++ b/XenonUtils/byteswap.h @@ -2,17 +2,30 @@ #include +#if defined(_MSC_VER) // MSVC +#include +#define BSWAP16(x) _byteswap_ushort(x) +#define BSWAP32(x) _byteswap_ulong(x) +#define BSWAP64(x) _byteswap_uint64(x) +#elif defined(__GNUC__) || defined(__clang__) // GCC or Clang +#define BSWAP16(x) __builtin_bswap16(x) +#define BSWAP32(x) __builtin_bswap32(x) +#define BSWAP64(x) __builtin_bswap64(x) +#else +#error "Unsupported compiler" +#endif + template inline 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))); + return static_cast(BSWAP16(static_cast(value))); else if constexpr (sizeof(T) == 4) - return static_cast(__builtin_bswap32(static_cast(value))); + return static_cast(BSWAP32(static_cast(value))); else if constexpr (sizeof(T) == 8) - return static_cast(__builtin_bswap64(static_cast(value))); + return static_cast(BSWAP64(static_cast(value))); assert(false && "Unexpected byte size."); return value;