00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00061 #if !defined(INCLUDED_ENDIAN_H)
00062 #define INCLUDED_ENDIAN_H
00063
00064 #if !defined(ES40_LITTLE_ENDIAN) && !defined(ES40_BIG_ENDIAN)
00065 #if defined(_WIN32) || defined(__VMS)
00066 #define ES40_LITTLE_ENDIAN
00067 #else // defined (_WIN32) || defined(__VMS)
00068 #include <sys/param.h>
00069
00070 #if !defined(__BYTE_ORDER) && defined(BYTE_ORDER)
00071 #define __BYTE_ORDER BYTE_ORDER
00072 #if !defined(__BIG_ENDIAN) && defined(BIG_ENDIAN)
00073 #define __BIG_ENDIAN BIG_ENDIAN
00074 #endif
00075 #if !defined(__LITTLE_ENDIAN) && defined(LITTLE_ENDIAN)
00076 #define __LITTLE_ENDIAN LITTLE_ENDIAN
00077 #endif
00078 #endif
00079 #if (defined(__BYTE_ORDER) && (__BYTE_ORDER == __BIG_ENDIAN)) || defined(sparc)
00080 #define ES40_BIG_ENDIAN
00081 #else // assume little endian
00082 #define ES40_LITTLE_ENDIAN
00083 #endif
00084 #endif // defined (_WIN32) || defined(__VMS)
00085 #endif // !defined(ES40_LITTLE_ENDIAN) && !defined(ES40_BIG_ENDIAN)
00086 #define swap_64(x) \
00087 ( \
00088 (((x) & U64(0x00000000000000ff)) << 56) | \
00089 (((x) & U64(0x000000000000ff00)) << 40) | \
00090 (((x) & U64(0x0000000000ff0000)) << 24) | \
00091 (((x) & U64(0x00000000ff000000)) << 8) | \
00092 (((x) & U64(0x000000ff00000000)) >> 8) | \
00093 (((x) & U64(0x0000ff0000000000)) >> 24) | \
00094 (((x) & U64(0x00ff000000000000)) >> 40) | \
00095 (((x) & U64(0xff00000000000000)) >> 56) \
00096 )
00097 #define swap_32(x) \
00098 ( \
00099 (((x) & 0x000000ff) << 24) | (((x) & 0x0000ff00) << 8) | \
00100 (((x) & 0x00ff0000) >> 8) | (((x) & 0xff000000) >> 24) \
00101 )
00102 #define swap_16(x) ((((x) & 0x00ff) << 8) | (((x) & 0xff00) >> 8))
00103 #define swap_8(x) ((x) & 0xff)
00104 #if defined(ES40_BIG_ENDIAN)
00105 #define endian_64(x) swap_64(x)
00106 #define endian_32(x) swap_32(x)
00107 #define endian_16(x) swap_16(x)
00108 #define endian_8(x) swap_8(x)
00109 #else // defined(ES40_BIG_ENDIAN)
00110 #define endian_64(x) (x)
00111 #define endian_32(x) ((x) & 0xffffffff)
00112 #define endian_16(x) ((x) & 0xffff)
00113 #define endian_8(x) ((x) & 0xff)
00114 #endif // defined(ES40_BIG_ENDIAN)
00115 inline u64 endian_bits(u64 x, int numbits)
00116 {
00117 switch(numbits)
00118 {
00119 case 64: return endian_64(x);
00120 case 32: return endian_32(x);
00121 case 16: return endian_16(x);
00122 case 8: return endian_8(x);
00123 default: FAILURE(InvalidArgument, "Weird numbits in endian_bits");
00124 }
00125 }
00126 #endif //INCLUDED_ENDIAN_H