| 1 |
#ifndef __BSWAP_H__ |
| 2 |
#define __BSWAP_H__ |
| 3 |
|
| 4 |
#ifdef HAVE_CONFIG_H |
| 5 |
#include "config.h" |
| 6 |
#endif |
| 7 |
|
| 8 |
#ifdef HAVE_BYTESWAP_H |
| 9 |
#include <byteswap.h> |
| 10 |
#else |
| 11 |
|
| 12 |
#include <inttypes.h> /* for __WORDSIZE */ |
| 13 |
|
| 14 |
#ifdef ARCH_X86 |
| 15 |
inline static unsigned short ByteSwap16(unsigned short x) |
| 16 |
{ |
| 17 |
__asm("xchgb %b0,%h0" : |
| 18 |
"=q" (x) : |
| 19 |
"0" (x)); |
| 20 |
return x; |
| 21 |
} |
| 22 |
#define bswap_16(x) ByteSwap16(x) |
| 23 |
|
| 24 |
inline static unsigned int ByteSwap32(unsigned int x) |
| 25 |
{ |
| 26 |
#if __CPU__ > 386 |
| 27 |
__asm("bswap %0": |
| 28 |
"=r" (x) : |
| 29 |
#else |
| 30 |
__asm("xchgb %b0,%h0\n" |
| 31 |
" rorl $16,%0\n" |
| 32 |
" xchgb %b0,%h0": |
| 33 |
"=q" (x) : |
| 34 |
#endif |
| 35 |
"0" (x)); |
| 36 |
return x; |
| 37 |
} |
| 38 |
#define bswap_32(x) ByteSwap32(x) |
| 39 |
|
| 40 |
inline static unsigned long long int ByteSwap64(unsigned long long int x) |
| 41 |
{ |
| 42 |
register union { __extension__ unsigned long long int __ll; |
| 43 |
unsigned long int __l[2]; } __x; |
| 44 |
asm("xchgl %0,%1": |
| 45 |
"=r"(__x.__l[0]),"=r"(__x.__l[1]): |
| 46 |
"0"(bswap_32((unsigned long)x)),"1"(bswap_32((unsigned long)(x>>32)))); |
| 47 |
return __x.__ll; |
| 48 |
} |
| 49 |
#define bswap_64(x) ByteSwap64(x) |
| 50 |
|
| 51 |
#else |
| 52 |
|
| 53 |
#define bswap_16(x) (((x) & 0x00ff) << 8 | ((x) & 0xff00) >> 8) |
| 54 |
|
| 55 |
|
| 56 |
// code from bits/byteswap.h (C) 1997, 1998 Free Software Foundation, Inc. |
| 57 |
#define bswap_32(x) \ |
| 58 |
((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \ |
| 59 |
(((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24)) |
| 60 |
|
| 61 |
#if __WORDSIZE >= 64 |
| 62 |
# define bswap_64(x) \ |
| 63 |
((((x) & 0xff00000000000000ull) >> 56) \ |
| 64 |
| (((x) & 0x00ff000000000000ull) >> 40) \ |
| 65 |
| (((x) & 0x0000ff0000000000ull) >> 24) \ |
| 66 |
| (((x) & 0x000000ff00000000ull) >> 8) \ |
| 67 |
| (((x) & 0x00000000ff000000ull) << 8) \ |
| 68 |
| (((x) & 0x0000000000ff0000ull) << 24) \ |
| 69 |
| (((x) & 0x000000000000ff00ull) << 40) \ |
| 70 |
| (((x) & 0x00000000000000ffull) << 56)) |
| 71 |
#else |
| 72 |
#define bswap_64(x) \ |
| 73 |
(__extension__ \ |
| 74 |
({ union { __extension__ unsigned long long int __ll; \ |
| 75 |
unsigned long int __l[2]; } __w, __r; \ |
| 76 |
__w.__ll = (x); \ |
| 77 |
__r.__l[0] = bswap_32 (__w.__l[1]); \ |
| 78 |
__r.__l[1] = bswap_32 (__w.__l[0]); \ |
| 79 |
__r.__ll; })) |
| 80 |
#endif |
| 81 |
#endif /* !ARCH_X86 */ |
| 82 |
|
| 83 |
#endif /* !HAVE_BYTESWAP_H */ |
| 84 |
|
| 85 |
// be2me ... BigEndian to MachineEndian |
| 86 |
// le2me ... LittleEndian to MachineEndian |
| 87 |
|
| 88 |
#ifdef WORDS_BIGENDIAN |
| 89 |
#define be2me_16(x) (x) |
| 90 |
#define be2me_32(x) (x) |
| 91 |
#define be2me_64(x) (x) |
| 92 |
#define me2be_16(x) (x) |
| 93 |
#define me2be_32(x) (x) |
| 94 |
#define me2be_64(x) (x) |
| 95 |
#define le2me_16(x) bswap_16(x) |
| 96 |
#define le2me_32(x) bswap_32(x) |
| 97 |
#define le2me_64(x) bswap_64(x) |
| 98 |
#define me2le_16(x) bswap_16(x) |
| 99 |
#define me2le_32(x) bswap_32(x) |
| 100 |
#define me2le_64(x) bswap_64(x) |
| 101 |
#else |
| 102 |
#define be2me_16(x) bswap_16(x) |
| 103 |
#define be2me_32(x) bswap_32(x) |
| 104 |
#define be2me_64(x) bswap_64(x) |
| 105 |
#define me2be_16(x) bswap_16(x) |
| 106 |
#define me2be_32(x) bswap_32(x) |
| 107 |
#define me2be_64(x) bswap_64(x) |
| 108 |
#define le2me_16(x) (x) |
| 109 |
#define le2me_32(x) (x) |
| 110 |
#define le2me_64(x) (x) |
| 111 |
#define me2le_16(x) (x) |
| 112 |
#define me2le_32(x) (x) |
| 113 |
#define me2le_64(x) (x) |
| 114 |
#endif |
| 115 |
|
| 116 |
#endif |