enhanced hcl_bswap16/32/64()

This commit is contained in:
hyung-hwan 2019-05-03 07:39:01 +00:00
parent 882ca9358c
commit 727865dcc5
2 changed files with 46 additions and 0 deletions

View File

@ -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)

View File

@ -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)
{