x86: use compiler intrinsics for bswap*

This commit is contained in:
Mateusz Guzik 2021-02-01 04:00:13 +01:00
parent aae89f6f09
commit e6ff6154d2

View File

@ -65,65 +65,9 @@
#define BYTE_ORDER _BYTE_ORDER
#endif
#define __bswap16_gen(x) (__uint16_t)((x) << 8 | (x) >> 8)
#define __bswap32_gen(x) \
(((__uint32_t)__bswap16((x) & 0xffff) << 16) | __bswap16((x) >> 16))
#define __bswap64_gen(x) \
(((__uint64_t)__bswap32((x) & 0xffffffff) << 32) | __bswap32((x) >> 32))
#ifdef __GNUCLIKE_BUILTIN_CONSTANT_P
#define __bswap16(x) \
((__uint16_t)(__builtin_constant_p(x) ? \
__bswap16_gen((__uint16_t)(x)) : __bswap16_var(x)))
#define __bswap32(x) \
(__builtin_constant_p(x) ? \
__bswap32_gen((__uint32_t)(x)) : __bswap32_var(x))
#define __bswap64(x) \
(__builtin_constant_p(x) ? \
__bswap64_gen((__uint64_t)(x)) : __bswap64_var(x))
#else
/* XXX these are broken for use in static initializers. */
#define __bswap16(x) __bswap16_var(x)
#define __bswap32(x) __bswap32_var(x)
#define __bswap64(x) __bswap64_var(x)
#endif
/* These are defined as functions to avoid multiple evaluation of x. */
static __inline __uint16_t
__bswap16_var(__uint16_t _x)
{
return (__bswap16_gen(_x));
}
static __inline __uint32_t
__bswap32_var(__uint32_t _x)
{
#ifdef __GNUCLIKE_ASM
__asm("bswap %0" : "+r" (_x));
return (_x);
#else
return (__bswap32_gen(_x));
#endif
}
static __inline __uint64_t
__bswap64_var(__uint64_t _x)
{
#if defined(__amd64__) && defined(__GNUCLIKE_ASM)
__asm("bswap %0" : "+r" (_x));
return (_x);
#else
/*
* It is important for the optimizations that the following is not
* really generic, but expands to 2 __bswap32_var()'s.
*/
return (__bswap64_gen(_x));
#endif
}
#define __bswap16(x) __builtin_bswap16(x)
#define __bswap32(x) __builtin_bswap32(x)
#define __bswap64(x) __builtin_bswap64(x)
#define __htonl(x) __bswap32(x)
#define __htons(x) __bswap16(x)