From 2610213b42e7a685c8efcbfa4b7323162ba1fd51 Mon Sep 17 00:00:00 2001 From: Lassi Kortela Date: Fri, 9 Aug 2019 22:04:55 +0300 Subject: [PATCH] Do not include bswap* all over the place --- c/flisp.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ c/utils.h | 72 ------------------------------------------------------ 2 files changed, 73 insertions(+), 72 deletions(-) diff --git a/c/flisp.c b/c/flisp.c index fa1d782..6f74317 100644 --- a/c/flisp.c +++ b/c/flisp.c @@ -924,6 +924,79 @@ no_kw: return nargs; } + +#if defined(__amd64__) || defined(_M_AMD64) +#define ARCH_X86_64 +#define __CPU__ 686 +#elif defined(_M_IX86) // msvs, intel, digital mars, watcom +#if !defined(__386__) +#error "unsupported target: 16-bit x86" +#endif +#define ARCH_X86 +#define __CPU__ (_M_IX86 + 86) +#elif defined(__i686__) // gnu c +#define ARCH_X86 +#define __CPU__ 686 +#elif defined(__i586__) // gnu c +#define ARCH_X86 +#define __CPU__ 586 +#elif defined(__i486__) // gnu c +#define ARCH_X86 +#define __CPU__ 486 +#elif defined(__i386__) // gnu c +#define ARCH_X86 +#define __CPU__ 386 +#else +#error "unknown architecture" +#endif + +#ifdef ARCH_X86_64 +#define LEGACY_REGS "=Q" +#else +#define LEGACY_REGS "=q" +#endif + +#if !defined(__INTEL_COMPILER) && (defined(ARCH_X86) || defined(ARCH_X86_64)) +static uint16_t ByteSwap16(uint16_t x) +{ + __asm("xchgb %b0,%h0" : LEGACY_REGS(x) : "0"(x)); + return x; +} +#define bswap_16(x) ByteSwap16(x) + +static uint32_t ByteSwap32(uint32_t x) +{ +#if __CPU__ > 386 + __asm("bswap %0" + : "=r"(x) + : +#else + __asm("xchgb %b0,%h0\n" + " rorl $16,%0\n" + " xchgb %b0,%h0" + : LEGACY_REGS(x) + : +#endif + "0"(x)); + return x; +} + +#define bswap_32(x) ByteSwap32(x) + +#else + +#define bswap_16(x) (((x)&0x00ff) << 8 | ((x)&0xff00) >> 8) + +#ifdef __INTEL_COMPILER +#define bswap_32(x) _bswap(x) +#else +#define bswap_32(x) \ + ((((x)&0xff000000) >> 24) | (((x)&0x00ff0000) >> 8) | \ + (((x)&0x0000ff00) << 8) | (((x)&0x000000ff) << 24)) +#endif + +#endif + #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ #define GET_INT32(a) \ ((int32_t)((((int32_t)a[0]) << 0) | (((int32_t)a[1]) << 8) | \ diff --git a/c/utils.h b/c/utils.h index 639e6ae..df7ded2 100644 --- a/c/utils.h +++ b/c/utils.h @@ -1,75 +1,3 @@ -#if defined(__amd64__) || defined(_M_AMD64) -#define ARCH_X86_64 -#define __CPU__ 686 -#elif defined(_M_IX86) // msvs, intel, digital mars, watcom -#if !defined(__386__) -#error "unsupported target: 16-bit x86" -#endif -#define ARCH_X86 -#define __CPU__ (_M_IX86 + 86) -#elif defined(__i686__) // gnu c -#define ARCH_X86 -#define __CPU__ 686 -#elif defined(__i586__) // gnu c -#define ARCH_X86 -#define __CPU__ 586 -#elif defined(__i486__) // gnu c -#define ARCH_X86 -#define __CPU__ 486 -#elif defined(__i386__) // gnu c -#define ARCH_X86 -#define __CPU__ 386 -#else -#error "unknown architecture" -#endif - char *uint2str(char *dest, size_t len, uint64_t num, uint32_t base); int str2int(char *str, size_t len, int64_t *res, uint32_t base); int isdigit_base(char c, int base); - -#ifdef ARCH_X86_64 -#define LEGACY_REGS "=Q" -#else -#define LEGACY_REGS "=q" -#endif - -#if !defined(__INTEL_COMPILER) && (defined(ARCH_X86) || defined(ARCH_X86_64)) -static uint16_t ByteSwap16(uint16_t x) -{ - __asm("xchgb %b0,%h0" : LEGACY_REGS(x) : "0"(x)); - return x; -} -#define bswap_16(x) ByteSwap16(x) - -static uint32_t ByteSwap32(uint32_t x) -{ -#if __CPU__ > 386 - __asm("bswap %0" - : "=r"(x) - : -#else - __asm("xchgb %b0,%h0\n" - " rorl $16,%0\n" - " xchgb %b0,%h0" - : LEGACY_REGS(x) - : -#endif - "0"(x)); - return x; -} - -#define bswap_32(x) ByteSwap32(x) - -#else - -#define bswap_16(x) (((x)&0x00ff) << 8 | ((x)&0xff00) >> 8) - -#ifdef __INTEL_COMPILER -#define bswap_32(x) _bswap(x) -#else -#define bswap_32(x) \ - ((((x)&0xff000000) >> 24) | (((x)&0x00ff0000) >> 8) | \ - (((x)&0x0000ff00) << 8) | (((x)&0x000000ff) << 24)) -#endif - -#endif