Use CPU optimized versions of endian swap functions when available

This commit is contained in:
Erik Hofman 2018-07-04 09:35:45 +02:00
parent 4e8df9dcc8
commit 3e081ae869

View File

@ -56,21 +56,42 @@ typedef int ssize_t;
inline uint16_t sg_bswap_16(uint16_t x) { inline uint16_t sg_bswap_16(uint16_t x) {
#if defined(__llvm__) || \
(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) && !defined(__ICC)
return __builtin_bswap16(x);
#elif defined(_MSC_VER) && !defined(_DEBUG)
return _byteswap_ushort(x);
#else
x = (x >> 8) | (x << 8); x = (x >> 8) | (x << 8);
return x; return x;
#endif
} }
inline uint32_t sg_bswap_32(uint32_t x) { inline uint32_t sg_bswap_32(uint32_t x) {
#if defined(__llvm__) || \
(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) && !defined(__ICC)
return __builtin_bswap32(x);
#elif defined(_MSC_VER) && !defined(_DEBUG)
return _byteswap_ulong(x);
#else
x = ((x >> 8) & 0x00FF00FFL) | ((x << 8) & 0xFF00FF00L); x = ((x >> 8) & 0x00FF00FFL) | ((x << 8) & 0xFF00FF00L);
x = (x >> 16) | (x << 16); x = (x >> 16) | (x << 16);
return x; return x;
#endif
} }
inline uint64_t sg_bswap_64(uint64_t x) { inline uint64_t sg_bswap_64(uint64_t x) {
#if defined(__llvm__) || \
(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) && !defined(__ICC)
return __builtin_bswap64(x);
#elif defined(_MSC_VER) && !defined(_DEBUG)
return _byteswap_uint64(x);
#else
x = ((x >> 8) & 0x00FF00FF00FF00FFLL) | ((x << 8) & 0xFF00FF00FF00FF00LL); x = ((x >> 8) & 0x00FF00FF00FF00FFLL) | ((x << 8) & 0xFF00FF00FF00FF00LL);
x = ((x >> 16) & 0x0000FFFF0000FFFFLL) | ((x << 16) & 0xFFFF0000FFFF0000LL); x = ((x >> 16) & 0x0000FFFF0000FFFFLL) | ((x << 16) & 0xFFFF0000FFFF0000LL);
x = (x >> 32) | (x << 32); x = (x >> 32) | (x << 32);
return x; return x;
#endif
} }