From 92121319976d96081428ec58e1164e6e1d6f116f Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Fri, 3 May 2019 07:36:53 +0000 Subject: [PATCH] enhanced mio_bswap16/32/64() --- mio/lib/mio-cmn.h | 23 +++++++++++++++++++++++ mio/lib/mio-utl.h | 21 +++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/mio/lib/mio-cmn.h b/mio/lib/mio-cmn.h index faaf3ef..94d2fcc 100644 --- a/mio/lib/mio-cmn.h +++ b/mio/lib/mio-cmn.h @@ -802,6 +802,19 @@ struct mio_cmgr_t #define MIO_HAVE_SYNC_VAL_COMPARE_AND_SWAP #endif + #if __has_builtin(__builtin_bswap16) + #define MIO_HAVE_BUILTIN_BSWAP16 + #endif + #if __has_builtin(__builtin_bswap32) + #define MIO_HAVE_BUILTIN_BSWAP32 + #endif + #if __has_builtin(__builtin_bswap64) + #define MIO_HAVE_BUILTIN_BSWAP64 + #endif + #if __has_builtin(__builtin_bswap128) + #define MIO_HAVE_BUILTIN_BSWAP128 + #endif + #elif defined(__GNUC__) && defined(__GNUC_MINOR__) #if (__GNUC__ >= 4) @@ -836,6 +849,16 @@ struct mio_cmgr_t #define MIO_HAVE_BUILTIN_SMULLL_OVERFLOW #endif + #if (__GNUC__ >= 5) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8) + /* 4.8.0 or later */ + #define MIO_HAVE_BUILTIN_BSWAP16 + #endif + #if (__GNUC__ >= 5) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) + /* 4.3.0 or later */ + #define MIO_HAVE_BUILTIN_BSWAP32 + #define MIO_HAVE_BUILTIN_BSWAP64 + /*#define MIO_HAVE_BUILTIN_BSWAP128*/ + #endif #endif #if defined(MIO_HAVE_BUILTIN_EXPECT) diff --git a/mio/lib/mio-utl.h b/mio/lib/mio-utl.h index b1e6a5d..897f8ac 100644 --- a/mio/lib/mio-utl.h +++ b/mio/lib/mio-utl.h @@ -517,23 +517,43 @@ MIO_EXPORT mio_oow_t mio_utf8_to_uc ( #if defined(MIO_HAVE_UINT16_T) static MIO_INLINE mio_uint16_t mio_bswap16 (mio_uint16_t x) { +#if defined(MIO_HAVE_BUILTIN_BSWAP16) + return __builtin_bswap16(x); +#elif defined(__GNUC__) && (defined(__x86_64) || defined(__amd64) || defined(__i386) || defined(i386)) + __asm__ volatile ("xchgb %b0, %h0" : "=Q"(x): "0"(x)); + return x; +#else return (x << 8) | (x >> 8); +#endif } #endif #if defined(MIO_HAVE_UINT32_T) static MIO_INLINE mio_uint32_t mio_bswap32 (mio_uint32_t x) { +#if defined(MIO_HAVE_BUILTIN_BSWAP32) + return __builtin_bswap32(x); +#elif defined(__GNUC__) && (defined(__x86_64) || defined(__amd64) || defined(__i386) || defined(i386)) + __asm__ volatile ("bswapl %0" : "=r"(x) : "0"(x)); + return x; +#else return ((x >> 24)) | ((x >> 8) & ((mio_uint32_t)0xff << 8)) | ((x << 8) & ((mio_uint32_t)0xff << 16)) | ((x << 24)); +#endif } #endif #if defined(MIO_HAVE_UINT64_T) static MIO_INLINE mio_uint64_t mio_bswap64 (mio_uint64_t x) { +#if defined(MIO_HAVE_BUILTIN_BSWAP64) + return __builtin_bswap64(x); +#elif defined(__GNUC__) && (defined(__x86_64) || defined(__amd64)) + __asm__ volatile ("bswapq %0" : "=r"(x) : "0"(x)); + return x; +#else return ((x >> 56)) | ((x >> 40) & ((mio_uint64_t)0xff << 8)) | ((x >> 24) & ((mio_uint64_t)0xff << 16)) | @@ -542,6 +562,7 @@ static MIO_INLINE mio_uint64_t mio_bswap64 (mio_uint64_t x) ((x << 24) & ((mio_uint64_t)0xff << 40)) | ((x << 40) & ((mio_uint64_t)0xff << 48)) | ((x << 56)); +#endif } #endif