From 9ca4e915baea6956c1cdb9e3a5ca420561286df5 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Mon, 28 May 2018 02:20:30 +0100 Subject: [PATCH 1/4] Add 64 bit byte swapping function --- src/core/bitmath_func.hpp | 19 +++++++++++++++++++ src/core/endian_func.hpp | 8 ++++++++ 2 files changed, 27 insertions(+) diff --git a/src/core/bitmath_func.hpp b/src/core/bitmath_func.hpp index 31e679b005..fdcb310b06 100644 --- a/src/core/bitmath_func.hpp +++ b/src/core/bitmath_func.hpp @@ -365,13 +365,32 @@ static inline T ROR(const T x, const uint8 n) * (since it will use hardware swapping if available). * Even though they should return uint16 and uint32, we get * warnings if we don't cast those (why?) */ + #define BSWAP64(x) ((uint32)CFSwapInt64(x)) #define BSWAP32(x) ((uint32)CFSwapInt32(x)) #define BSWAP16(x) ((uint16)CFSwapInt16(x)) #elif defined(_MSC_VER) /* MSVC has intrinsics for swapping, resulting in faster code */ + #define BSWAP64(x) (_byteswap_uint64(x)) #define BSWAP32(x) (_byteswap_ulong(x)) #define BSWAP16(x) (_byteswap_ushort(x)) #else + /** + * Perform a 64 bits endianness bitswap on x. + * @param x the variable to bitswap + * @return the bitswapped value. + */ + static inline uint64 BSWAP64(uint64 x) + { +#if !defined(__ICC) && (defined(__GNUC__) || defined(__clang__)) + /* GCC >= 4.3 provides a builtin, resulting in faster code */ + return (uint64)__builtin_bswap64((uint64)x); +#else + return ((x >> 56) & 0xFFULL) | ((x >> 40) & 0xFF00ULL) | ((x >> 24) & 0xFF0000ULL) | ((x >> 8) & 0xFF000000ULL) | + ((x << 8) & 0xFF00000000ULL) | ((x << 24) & 0xFF0000000000ULL) | ((x << 40) & 0xFF000000000000ULL) | ((x << 56) & 0xFF000000000000ULL); + ; +#endif /* __GNUC__ || __clang__ */ + } + /** * Perform a 32 bits endianness bitswap on x. * @param x the variable to bitswap diff --git a/src/core/endian_func.hpp b/src/core/endian_func.hpp index ab5b181500..48060bd1ad 100644 --- a/src/core/endian_func.hpp +++ b/src/core/endian_func.hpp @@ -19,25 +19,33 @@ #if TTD_ENDIAN == TTD_BIG_ENDIAN #define FROM_BE16(x) (x) #define FROM_BE32(x) (x) + #define FROM_BE64(x) (x) #define TO_BE16(x) (x) #define TO_BE32(x) (x) #define TO_BE32X(x) (x) + #define TO_BE64(x) (x) #define FROM_LE16(x) BSWAP16(x) #define FROM_LE32(x) BSWAP32(x) + #define FROM_LE64(x) BSWAP64(x) #define TO_LE16(x) BSWAP16(x) #define TO_LE32(x) BSWAP32(x) #define TO_LE32X(x) BSWAP32(x) + #define TO_LE64(x) BSWAP64(x) #else #define FROM_BE16(x) BSWAP16(x) #define FROM_BE32(x) BSWAP32(x) + #define FROM_BE64(x) BSWAP64(x) #define TO_BE16(x) BSWAP16(x) #define TO_BE32(x) BSWAP32(x) #define TO_BE32X(x) BSWAP32(x) + #define TO_BE64(x) BSWAP64(x) #define FROM_LE16(x) (x) #define FROM_LE32(x) (x) + #define FROM_LE64(x) (x) #define TO_LE16(x) (x) #define TO_LE32(x) (x) #define TO_LE32X(x) (x) + #define TO_LE64(x) (x) #endif /* TTD_ENDIAN == TTD_BIG_ENDIAN */ static inline uint16 ReadLE16Aligned(const void *x) From 6862343eb3adcffb49b5158b016873bbac030a81 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Wed, 6 Jun 2018 01:38:05 +0100 Subject: [PATCH 2/4] Fix wrong type cast in Apple BSWAP64 macro --- src/core/bitmath_func.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/bitmath_func.hpp b/src/core/bitmath_func.hpp index fdcb310b06..a40c7dba6e 100644 --- a/src/core/bitmath_func.hpp +++ b/src/core/bitmath_func.hpp @@ -365,7 +365,7 @@ static inline T ROR(const T x, const uint8 n) * (since it will use hardware swapping if available). * Even though they should return uint16 and uint32, we get * warnings if we don't cast those (why?) */ - #define BSWAP64(x) ((uint32)CFSwapInt64(x)) + #define BSWAP64(x) ((uint64)CFSwapInt64(x)) #define BSWAP32(x) ((uint32)CFSwapInt32(x)) #define BSWAP16(x) ((uint16)CFSwapInt16(x)) #elif defined(_MSC_VER) From 183a5cb11435901f1b1c7de26953491cb6b41bf0 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Wed, 6 Jun 2018 01:54:37 +0100 Subject: [PATCH 3/4] Add unaligned uint 16, 32, 64 typedefs --- src/stdafx.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/stdafx.h b/src/stdafx.h index 7fbd61f560..a145d645c2 100644 --- a/src/stdafx.h +++ b/src/stdafx.h @@ -456,6 +456,16 @@ assert_compile(SIZE_MAX >= UINT32_MAX); #define unlikely(x) (x) #endif /* __GNUC__ || __clang__ */ +#if defined(__GNUC__) || defined(__clang__) +__attribute__((aligned(1))) typedef uint16 unaligned_uint16; +__attribute__((aligned(1))) typedef uint32 unaligned_uint32; +__attribute__((aligned(1))) typedef uint64 unaligned_uint64; +#else +typedef uint16 unaligned_uint16; +typedef uint32 unaligned_uint32; +typedef uint64 unaligned_uint64; +#endif /* __GNUC__ || __clang__ */ + void NORETURN CDECL usererror(const char *str, ...) WARN_FORMAT(1, 2); void NORETURN CDECL error(const char *str, ...) WARN_FORMAT(1, 2); void NORETURN CDECL assert_msg_error(int line, const char *file, const char *expr, const char *str, ...) WARN_FORMAT(4, 5); From 6c546a21457fc7c5f1c32dc0d3903d464607e45c Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sun, 26 Aug 2018 04:50:47 +0100 Subject: [PATCH 4/4] Add include for scope header file --- src/scope.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/scope.h b/src/scope.h index d341b50b1f..b546813ac3 100644 --- a/src/scope.h +++ b/src/scope.h @@ -12,6 +12,8 @@ #ifndef SCOPE_H #define SCOPE_H +#include + template class scope_exit_obj { T f;