From 727865dcc538bac378331e15ff2b1b89b2ca47e7 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Fri, 3 May 2019 07:39:01 +0000 Subject: [PATCH] enhanced hcl_bswap16/32/64() --- lib/hcl-cmn.h | 24 ++++++++++++++++++++++++ lib/hcl-utl.h | 22 ++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/lib/hcl-cmn.h b/lib/hcl-cmn.h index 8e72ff1..3ea7208 100644 --- a/lib/hcl-cmn.h +++ b/lib/hcl-cmn.h @@ -912,6 +912,19 @@ typedef struct hcl_t hcl_t; #define HCL_HAVE_SYNC_VAL_COMPARE_AND_SWAP #endif + #if __has_builtin(__builtin_bswap16) + #define HCL_HAVE_BUILTIN_BSWAP16 + #endif + #if __has_builtin(__builtin_bswap32) + #define HCL_HAVE_BUILTIN_BSWAP32 + #endif + #if __has_builtin(__builtin_bswap64) + #define HCL_HAVE_BUILTIN_BSWAP64 + #endif + #if __has_builtin(__builtin_bswap128) + #define HCL_HAVE_BUILTIN_BSWAP128 + #endif + #elif defined(__GNUC__) && defined(__GNUC_MINOR__) #if (__GNUC__ >= 4) @@ -949,6 +962,17 @@ typedef struct hcl_t hcl_t; #define HCL_HAVE_BUILTIN_SMULLL_OVERFLOW #endif + #if (__GNUC__ >= 5) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8) + /* 4.8.0 or later */ + #define HCL_HAVE_BUILTIN_BSWAP16 + #endif + #if (__GNUC__ >= 5) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) + /* 4.3.0 or later */ + #define HCL_HAVE_BUILTIN_BSWAP32 + #define HCL_HAVE_BUILTIN_BSWAP64 + /*#define HCL_HAVE_BUILTIN_BSWAP128*/ + #endif + #endif #if defined(HCL_HAVE_BUILTIN_EXPECT) diff --git a/lib/hcl-utl.h b/lib/hcl-utl.h index c427fba..8fd60f9 100644 --- a/lib/hcl-utl.h +++ b/lib/hcl-utl.h @@ -692,23 +692,43 @@ HCL_EXPORT int hcl_ucwidth ( #if defined(HCL_HAVE_UINT16_T) static HCL_INLINE hcl_uint16_t hcl_bswap16 (hcl_uint16_t x) { +#if defined(HCL_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(HCL_HAVE_UINT32_T) static HCL_INLINE hcl_uint32_t hcl_bswap32 (hcl_uint32_t x) { +#if defined(HCL_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) & ((hcl_uint32_t)0xff << 8)) | ((x << 8) & ((hcl_uint32_t)0xff << 16)) | ((x << 24)); +#endif } #endif #if defined(HCL_HAVE_UINT64_T) static HCL_INLINE hcl_uint64_t hcl_bswap64 (hcl_uint64_t x) { +#if defined(HCL_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) & ((hcl_uint64_t)0xff << 8)) | ((x >> 24) & ((hcl_uint64_t)0xff << 16)) | @@ -717,9 +737,11 @@ static HCL_INLINE hcl_uint64_t hcl_bswap64 (hcl_uint64_t x) ((x << 24) & ((hcl_uint64_t)0xff << 40)) | ((x << 40) & ((hcl_uint64_t)0xff << 48)) | ((x << 56)); +#endif } #endif + #if defined(HCL_HAVE_UINT128_T) static HCL_INLINE hcl_uint128_t hcl_bswap128 (hcl_uint128_t x) {