From 141c867810da87056b2b178207d6b54b2158415c Mon Sep 17 00:00:00 2001 From: Lev Date: Wed, 25 Jan 2017 17:11:11 +0500 Subject: [PATCH] Declare and use __builtin_bswapXX. --- rehlds/common/mathlib.h | 27 +++++++++++++-------------- rehlds/public/rehlds/osconfig.h | 11 +++++++++++ 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/rehlds/common/mathlib.h b/rehlds/common/mathlib.h index 6d19b3f..83affe8 100644 --- a/rehlds/common/mathlib.h +++ b/rehlds/common/mathlib.h @@ -65,32 +65,31 @@ typedef union DLONG_u #endif template -inline T min(T a, T b) { +inline T min(T a, T b) +{ return (a < b) ? a : b; } template -inline T max(T a, T b) { +inline T max(T a, T b) +{ return (a < b) ? b : a; } template -inline T clamp(T a, T min, T max) { +inline T clamp(T a, T min, T max) +{ return (a > max) ? max : (a < min) ? min : a; } template -inline T bswap(T s) { - switch (sizeof(T)) { -#ifdef _WIN32 - case 2: {auto res = _byteswap_ushort(*(uint16 *)&s); return *(T *)&res;} - case 4: {auto res = _byteswap_ulong(*(uint32 *)(&s)); return *(T *)&res;} - case 8: {auto res = _byteswap_uint64(*(uint64 *)&s); return *(T *)&res;} -#else - case 2: {auto res = _bswap16(*(uint16 *)&s); return *(T *)&res;} - case 4: {auto res = _bswap(*(uint32 *)&s); return *(T *)&res;} - case 8: {auto res = _bswap64(*(uint64 *)&s); return *(T *)&res;} -#endif +inline T bswap(T s) +{ + switch (sizeof(T)) + { + case 2: {auto res = __builtin_bswap16(*(uint16 *)&s); return *(T *)&res; } + case 4: {auto res = __builtin_bswap32(*(uint32 *)&s); return *(T *)&res; } + case 8: {auto res = __builtin_bswap64(*(uint64 *)&s); return *(T *)&res; } default: return s; } } diff --git a/rehlds/public/rehlds/osconfig.h b/rehlds/public/rehlds/osconfig.h index cf6a551..66a1d89 100644 --- a/rehlds/public/rehlds/osconfig.h +++ b/rehlds/public/rehlds/osconfig.h @@ -92,6 +92,7 @@ #include #include + #ifdef _WIN32 // WINDOWS #define _CRT_SECURE_NO_WARNINGS #define WIN32_LEAN_AND_MEAN @@ -106,6 +107,10 @@ #define NORETURN __declspec(noreturn) #define FORCE_STACK_ALIGN + #define __builtin_bswap16 _byteswap_ushort + #define __builtin_bswap32 _byteswap_ulong + #define __builtin_bswap64 _byteswap_uint64 + //inline bool SOCKET_FIONBIO(SOCKET s, int m) { return (ioctlsocket(s, FIONBIO, (u_long*)&m) == 0); } //inline int SOCKET_MSGLEN(SOCKET s, u_long& r) { return ioctlsocket(s, FIONREAD, (u_long*)&r); } typedef int socklen_t; @@ -152,6 +157,12 @@ #define NORETURN __attribute__((noreturn)) #define FORCE_STACK_ALIGN __attribute__((force_align_arg_pointer)) +#if defined __INTEL_COMPILER + #define __builtin_bswap16 _bswap16 + #define __builtin_bswap32 _bswap + #define __builtin_bswap64 _bswap64 +#endif // __INTEL_COMPILER + //inline bool SOCKET_FIONBIO(SOCKET s, int m) { return (ioctl(s, FIONBIO, (int*)&m) == 0); } //inline int SOCKET_MSGLEN(SOCKET s, u_long& r) { return ioctl(s, FIONREAD, (int*)&r); } typedef int SOCKET;