2019-08-09 07:02:02 -04:00
|
|
|
#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
|
2013-03-17 15:02:33 -04:00
|
|
|
#else
|
2019-08-09 07:02:02 -04:00
|
|
|
#error "unknown architecture"
|
2013-03-17 15:02:33 -04:00
|
|
|
#endif
|
|
|
|
|
2009-03-13 18:26:44 -04:00
|
|
|
char *uint2str(char *dest, size_t len, uint64_t num, uint32_t base);
|
2009-02-01 00:41:43 -05:00
|
|
|
int str2int(char *str, size_t len, int64_t *res, uint32_t base);
|
|
|
|
int isdigit_base(char c, int base);
|
2008-12-30 23:45:08 -05:00
|
|
|
|
2008-06-30 21:53:51 -04:00
|
|
|
#ifdef ARCH_X86_64
|
2019-08-09 07:02:02 -04:00
|
|
|
#define LEGACY_REGS "=Q"
|
2008-06-30 21:53:51 -04:00
|
|
|
#else
|
2019-08-09 07:02:02 -04:00
|
|
|
#define LEGACY_REGS "=q"
|
2008-06-30 21:53:51 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(__INTEL_COMPILER) && (defined(ARCH_X86) || defined(ARCH_X86_64))
|
2019-08-09 14:35:20 -04:00
|
|
|
static uint16_t ByteSwap16(uint16_t x)
|
2008-06-30 21:53:51 -04:00
|
|
|
{
|
2019-08-09 07:02:02 -04:00
|
|
|
__asm("xchgb %b0,%h0" : LEGACY_REGS(x) : "0"(x));
|
2008-06-30 21:53:51 -04:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
#define bswap_16(x) ByteSwap16(x)
|
|
|
|
|
2019-08-09 14:35:20 -04:00
|
|
|
static uint32_t ByteSwap32(uint32_t x)
|
2008-06-30 21:53:51 -04:00
|
|
|
{
|
|
|
|
#if __CPU__ > 386
|
2019-08-09 10:18:36 -04:00
|
|
|
__asm("bswap %0"
|
2019-08-09 07:02:02 -04:00
|
|
|
: "=r"(x)
|
|
|
|
:
|
2008-06-30 21:53:51 -04:00
|
|
|
#else
|
2019-08-09 10:18:36 -04:00
|
|
|
__asm("xchgb %b0,%h0\n"
|
|
|
|
" rorl $16,%0\n"
|
|
|
|
" xchgb %b0,%h0"
|
2019-08-09 07:02:02 -04:00
|
|
|
: LEGACY_REGS(x)
|
|
|
|
:
|
2008-06-30 21:53:51 -04:00
|
|
|
#endif
|
2019-08-09 07:02:02 -04:00
|
|
|
"0"(x));
|
|
|
|
return x;
|
2008-06-30 21:53:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#define bswap_32(x) ByteSwap32(x)
|
|
|
|
|
2019-08-09 14:35:20 -04:00
|
|
|
static uint64_t ByteSwap64(uint64_t x)
|
2008-06-30 21:53:51 -04:00
|
|
|
{
|
|
|
|
#ifdef ARCH_X86_64
|
2019-08-09 10:18:36 -04:00
|
|
|
__asm("bswap %0" : "=r"(x) : "0"(x));
|
2019-08-09 07:02:02 -04:00
|
|
|
return x;
|
2008-06-30 21:53:51 -04:00
|
|
|
#else
|
2019-08-09 07:02:02 -04:00
|
|
|
register union {
|
2019-08-09 14:00:03 -04:00
|
|
|
__extension__ uint64_t __ll;
|
|
|
|
uint32_t __l[2];
|
2019-08-09 07:02:02 -04:00
|
|
|
} __x;
|
2019-08-09 10:18:36 -04:00
|
|
|
asm("xchgl %0,%1"
|
2019-08-09 07:02:02 -04:00
|
|
|
: "=r"(__x.__l[0]), "=r"(__x.__l[1])
|
|
|
|
: "0"(bswap_32((unsigned long)x)),
|
|
|
|
"1"(bswap_32((unsigned long)(x >> 32))));
|
|
|
|
return __x.__ll;
|
2008-06-30 21:53:51 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#define bswap_64(x) ByteSwap64(x)
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
#define bswap_16(x) (((x)&0x00ff) << 8 | ((x)&0xff00) >> 8)
|
2008-06-30 21:53:51 -04:00
|
|
|
|
|
|
|
#ifdef __INTEL_COMPILER
|
|
|
|
#define bswap_32(x) _bswap(x)
|
|
|
|
#else
|
2019-08-09 07:02:02 -04:00
|
|
|
#define bswap_32(x) \
|
|
|
|
((((x)&0xff000000) >> 24) | (((x)&0x00ff0000) >> 8) | \
|
|
|
|
(((x)&0x0000ff00) << 8) | (((x)&0x000000ff) << 24))
|
2008-06-30 21:53:51 -04:00
|
|
|
#endif
|
|
|
|
|
2019-08-09 14:35:20 -04:00
|
|
|
static uint64_t ByteSwap64(uint64_t x)
|
2008-06-30 21:53:51 -04:00
|
|
|
{
|
2019-08-09 07:02:02 -04:00
|
|
|
union {
|
2019-08-09 14:00:03 -04:00
|
|
|
uint64_t ll;
|
|
|
|
uint32_t l[2];
|
2008-06-30 21:53:51 -04:00
|
|
|
} w, r;
|
|
|
|
w.ll = x;
|
2019-08-09 07:02:02 -04:00
|
|
|
r.l[0] = bswap_32(w.l[1]);
|
|
|
|
r.l[1] = bswap_32(w.l[0]);
|
2008-06-30 21:53:51 -04:00
|
|
|
return r.ll;
|
|
|
|
}
|
|
|
|
#define bswap_64(x) ByteSwap64(x)
|
|
|
|
|
|
|
|
#endif
|