00001 #ifndef __BSWAP_H__
00002 #define __BSWAP_H__
00003
00004 #ifdef HAVE_BYTESWAP_H
00005 #include <byteswap.h>
00006 #else
00007 #include <inttypes.h>
00008
00009
00010 #ifdef ARCH_X86
00011 inline static unsigned short ByteSwap16(unsigned short x)
00012 {
00013 __asm("xchgb %b0,%h0" :
00014 "=q" (x) :
00015 "0" (x));
00016 return x;
00017 }
00018 #define bswap_16(x) ByteSwap16(x)
00019
00020 inline static unsigned int ByteSwap32(unsigned int x)
00021 {
00022 #if __CPU__ > 386
00023 __asm("bswap %0":
00024 "=r" (x) :
00025 #else
00026 __asm("xchgb %b0,%h0\n"
00027 " rorl $16,%0\n"
00028 " xchgb %b0,%h0":
00029 "=q" (x) :
00030 #endif
00031 "0" (x));
00032 return x;
00033 }
00034 #define bswap_32(x) ByteSwap32(x)
00035
00036 inline static unsigned long long int ByteSwap64(unsigned long long int x)
00037 {
00038 register union { __extension__ unsigned long long int __ll;
00039 unsigned long int __l[2]; } __x;
00040 asm("xchgl %0,%1":
00041 "=r"(__x.__l[0]),"=r"(__x.__l[1]):
00042 "0"(bswap_32((unsigned long)x)),"1"(bswap_32((unsigned long)(x>>32))));
00043 return __x.__ll;
00044 }
00045 #define bswap_64(x) ByteSwap64(x)
00046
00047 #else
00048
00049 #define bswap_16(x) (((x) & 0x00ff) << 8 | ((x) & 0xff00) >> 8)
00050
00051 #define bswap_32(x) \
00052 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
00053 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
00054
00055 #ifdef __GNUC__
00056
00057 #define bswap_64(x) \
00058 (__extension__ \
00059 ({ union { __extension__ unsigned long long int __ll; \
00060 unsigned long int __l[2]; } __w, __r; \
00061 __w.__ll = (x); \
00062 __r.__l[0] = bswap_32 (__w.__l[1]); \
00063 __r.__l[1] = bswap_32 (__w.__l[0]); \
00064 __r.__ll; }))
00065 #else
00066 #define bswap_64(x) \
00067 ((((x) & 0xff00000000000000LL) >> 56) | \
00068 (((x) & 0x00ff000000000000LL) >> 40) | \
00069 (((x) & 0x0000ff0000000000LL) >> 24) | \
00070 (((x) & 0x000000ff00000000LL) >> 8) | \
00071 (((x) & 0x00000000ff000000LL) << 8) | \
00072 (((x) & 0x0000000000ff0000LL) << 24) | \
00073 (((x) & 0x000000000000ff00LL) << 40) | \
00074 (((x) & 0x00000000000000ffLL) << 56))
00075 #endif
00076
00077 #endif
00078
00079 #endif
00080
00081
00082
00083
00084 #ifdef WORDS_BIGENDIAN
00085 #define be2me_16(x) (x)
00086 #define be2me_32(x) (x)
00087 #define be2me_64(x) (x)
00088 #define le2me_16(x) bswap_16(x)
00089 #define le2me_32(x) bswap_32(x)
00090 #define le2me_64(x) bswap_64(x)
00091 #else
00092 #define be2me_16(x) bswap_16(x)
00093 #define be2me_32(x) bswap_32(x)
00094 #define be2me_64(x) bswap_64(x)
00095 #define le2me_16(x) (x)
00096 #define le2me_32(x) (x)
00097 #define le2me_64(x) (x)
00098 #endif
00099
00100 #define ABE_16(x) (be2me_16(*(uint16_t*)(x)))
00101 #define ABE_32(x) (be2me_32(*(uint32_t*)(x)))
00102 #define ABE_64(x) (be2me_64(*(uint64_t*)(x)))
00103 #define ALE_16(x) (le2me_16(*(uint16_t*)(x)))
00104 #define ALE_32(x) (le2me_32(*(uint32_t*)(x)))
00105 #define ALE_64(x) (le2me_64(*(uint64_t*)(x)))
00106
00107 #ifdef ARCH_X86
00108
00109 #define BE_16(x) ABE_16(x)
00110 #define BE_32(x) ABE_32(x)
00111 #define BE_64(x) ABE_64(x)
00112 #define LE_16(x) ALE_16(x)
00113 #define LE_32(x) ALE_32(x)
00114 #define LE_64(x) ALE_64(x)
00115
00116 #else
00117
00118 #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
00119 #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
00120 (((uint8_t*)(x))[1] << 16) | \
00121 (((uint8_t*)(x))[2] << 8) | \
00122 ((uint8_t*)(x))[3])
00123 #define BE_64(x) ((uint64_t)(((uint8_t*)(x))[0] << 56) | \
00124 (uint64_t)(((uint8_t*)(x))[1] << 48) | \
00125 (uint64_t)(((uint8_t*)(x))[2] << 40) | \
00126 (uint64_t)(((uint8_t*)(x))[3] << 32) | \
00127 (uint64_t)(((uint8_t*)(x))[4] << 24) | \
00128 (uint64_t)(((uint8_t*)(x))[5] << 16) | \
00129 (uint64_t)(((uint8_t*)(x))[6] << 8) | \
00130 (uint64_t)((uint8_t*)(x))[7])
00131 #define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
00132 #define LE_32(x) ((((uint8_t*)(x))[3] << 24) | \
00133 (((uint8_t*)(x))[2] << 16) | \
00134 (((uint8_t*)(x))[1] << 8) | \
00135 ((uint8_t*)(x))[0])
00136 #define LE_64(x) ((uint64_t)(((uint8_t*)(x))[7] << 56) | \
00137 (uint64_t)(((uint8_t*)(x))[6] << 48) | \
00138 (uint64_t)(((uint8_t*)(x))[5] << 40) | \
00139 (uint64_t)(((uint8_t*)(x))[4] << 32) | \
00140 (uint64_t)(((uint8_t*)(x))[3] << 24) | \
00141 (uint64_t)(((uint8_t*)(x))[2] << 16) | \
00142 (uint64_t)(((uint8_t*)(x))[1] << 8) | \
00143 (uint64_t)((uint8_t*)(x))[0])
00144
00145 #endif
00146
00147 #ifdef WORDS_BIGENDIAN
00148 #define ME_16(x) BE_16(x)
00149 #define ME_32(x) BE_32(x)
00150 #define ME_64(x) BE_64(x)
00151 #define AME_16(x) ABE_16(x)
00152 #define AME_32(x) ABE_32(x)
00153 #define AME_64(x) ABE_64(x)
00154 #else
00155 #define ME_16(x) LE_16(x)
00156 #define ME_32(x) LE_32(x)
00157 #define ME_64(x) LE_64(x)
00158 #define AME_16(x) ALE_16(x)
00159 #define AME_32(x) ALE_32(x)
00160 #define AME_64(x) ALE_64(x)
00161 #endif
00162
00163 #define BE_FOURCC( ch0, ch1, ch2, ch3 ) \
00164 ( (uint32_t)(unsigned char)(ch3) | \
00165 ( (uint32_t)(unsigned char)(ch2) << 8 ) | \
00166 ( (uint32_t)(unsigned char)(ch1) << 16 ) | \
00167 ( (uint32_t)(unsigned char)(ch0) << 24 ) )
00168
00169 #define LE_FOURCC( ch0, ch1, ch2, ch3 ) \
00170 ( (uint32_t)(unsigned char)(ch0) | \
00171 ( (uint32_t)(unsigned char)(ch1) << 8 ) | \
00172 ( (uint32_t)(unsigned char)(ch2) << 16 ) | \
00173 ( (uint32_t)(unsigned char)(ch3) << 24 ) )
00174
00175 #ifdef WORDS_BIGENDIAN
00176 #define ME_FOURCC BE_FOURCC
00177 #else
00178 #define ME_FOURCC LE_FOURCC
00179 #endif
00180
00181 #endif