diff options
author | peter <peter@FreeBSD.org> | 2003-09-22 22:37:49 +0000 |
---|---|---|
committer | peter <peter@FreeBSD.org> | 2003-09-22 22:37:49 +0000 |
commit | e75370c7757ea900e24b94c843172586bff23dbe (patch) | |
tree | c89cb05205ca1558f61a08633e5714ef29d12bed /sys/amd64/include/endian.h | |
parent | 005d12e9b38ed99e81fc18208fc002a0c24ec74a (diff) | |
download | FreeBSD-src-e75370c7757ea900e24b94c843172586bff23dbe.zip FreeBSD-src-e75370c7757ea900e24b94c843172586bff23dbe.tar.gz |
MFi386 rev 1.37: constant-friendly bswap macros
Diffstat (limited to 'sys/amd64/include/endian.h')
-rw-r--r-- | sys/amd64/include/endian.h | 74 |
1 files changed, 70 insertions, 4 deletions
diff --git a/sys/amd64/include/endian.h b/sys/amd64/include/endian.h index 1766866..cdab6c0 100644 --- a/sys/amd64/include/endian.h +++ b/sys/amd64/include/endian.h @@ -69,26 +69,92 @@ #ifdef __GNUC__ -#define __word_swap_int(x) \ +#define __word_swap_int_var(x) \ __extension__ ({ register __uint32_t __X = (x); \ __asm ("rorl $16, %0" : "+r" (__X)); \ __X; }) -#define __byte_swap_int(x) \ +#ifdef __OPTIMIZE__ + +#define __word_swap_int_const(x) \ + ((((x) & 0xffff0000) >> 16) | \ + (((x) & 0x0000ffff) << 16)) +#define __word_swap_int(x) (__builtin_constant_p(x) ? \ + __word_swap_int_const(x) : __word_swap_int_var(x)) + +#else /* __OPTIMIZE__ */ + +#define __word_swap_int(x) __word_swap_int_var(x) + +#endif /* __OPTIMIZE__ */ + +#define __byte_swap_int_var(x) \ __extension__ ({ register __uint32_t __X = (x); \ __asm ("bswap %0" : "+r" (__X)); \ __X; }) -#define __byte_swap_long(x) \ +#ifdef __OPTIMIZE__ + +#define __byte_swap_int_const(x) \ + ((((x) & 0xff000000) >> 24) | \ + (((x) & 0x00ff0000) >> 8) | \ + (((x) & 0x0000ff00) << 8) | \ + (((x) & 0x000000ff) << 24)) +#define __byte_swap_int(x) (__builtin_constant_p(x) ? \ + __byte_swap_int_const(x) : __byte_swap_int_var(x)) + +#else /* __OPTIMIZE__ */ + +#define __byte_swap_int(x) __byte_swap_int_var(x) + +#endif /* __OPTIMIZE__ */ + +#define __byte_swap_long_var(x) \ __extension__ ({ register __uint64_t __X = (x); \ __asm ("bswap %0" : "+r" (__X)); \ __X; }) -#define __byte_swap_word(x) \ +#ifdef __OPTIMIZE__ + +#define __byte_swap_long_const(x) \ + (((x >> 56) | \ + ((x >> 40) & 0xff00) | \ + ((x >> 24) & 0xff0000) | \ + ((x >> 8) & 0xff000000) | \ + ((x << 8) & (0xfful << 32)) | \ + ((x << 24) & (0xfful << 40)) | \ + ((x << 40) & (0xfful << 48)) | \ + ((x << 56)))) + +#define __byte_swap_long(x) (__builtin_constant_p(x) ? \ + __byte_swap_long_const(x) : __byte_swap_long_var(x)) + +#else /* __OPTIMIZE__ */ + +#define __byte_swap_long(x) __byte_swap_long_var(x) + +#endif /* __OPTIMIZE__ */ + +#define __byte_swap_word_var(x) \ __extension__ ({ register __uint16_t __X = (x); \ __asm ("xchgb %h0, %b0" : "+Q" (__X)); \ __X; }) +#ifdef __OPTIMIZE__ + +#define __byte_swap_word_const(x) \ + ((((x) & 0xff00) >> 8) | \ + (((x) & 0x00ff) << 8)) + +#define __byte_swap_word(x) (__builtin_constant_p(x) ? \ + __byte_swap_word_const(x) : __byte_swap_word_var(x)) + +#else /* __OPTIMIZE__ */ + +#define __byte_swap_word(x) __byte_swap_word_var(x) + +#endif /* __OPTIMIZE__ */ + static __inline __uint64_t __bswap64(__uint64_t _x) { |