00001 #ifndef __UPF_ENDIAN_H__
00002 #define __UPF_ENDIAN_H__
00003
00004
00005
00006
00007
00008 #ifdef HAVE_ENDIAN_H
00009 #include <endian.h>
00010
00011 #if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN)
00012 #if __BYTE_ORDER == __LITTLE_ENDIAN
00013 #define _UPF_LITTLE_ENDIAN
00014 #endif
00015 #if __BYTE_ORDER == __BIG_ENDIAN
00016 #define _UPF_BIG_ENDIAN
00017 #endif
00018 #endif
00019 #endif
00020
00021 #if !defined(_UPF_LITTLE_ENDIAN) && !defined(_UPF_BIG_ENDIAN)
00022
00023 #if defined(__i386__) || defined(__ia64__) || defined(__x86_64__) || \
00024 defined(WIN32) || defined(__alpha__) || defined(__alpha) || \
00025 defined(__arm__) || (defined(__mips__) && defined(__MIPSEL__)) || \
00026 defined(__SYMBIAN32__) || defined(__LITTLE_ENDIAN__)
00027 #define _UPF_LITTLE_ENDIAN
00028 #elif defined(__sparc) || defined(__sparc__) || defined(__powerpc__) || \
00029 defined(__ppc__) || defined(__hppa) || defined(_MIPSEB) || \
00030 defined(__BIG_ENDIAN__)
00031 #define _UPF_BIG_ENDIAN
00032 #else
00033 #error "Please define endianess of your machine!"
00034 #endif
00035 #endif
00036
00037
00038
00039
00040
00041 #define _UPF_SWAP_16(x) \
00042 ((((x) & 0x00FFU) << 8) | (((x) & 0xFF00U) >> 8))
00043
00044 #define _UPF_SWAP_32(x) \
00045 ((((x) & 0x000000FFU) << 24) | \
00046 (((x) & 0x0000FF00U) << 8) | \
00047 (((x) & 0x00FF0000U) >> 8) | \
00048 (((x) & 0xFF000000U) >> 24))
00049
00050
00051 #ifdef _UPF_LITTLE_ENDIAN
00052 #define _UPF_SWAP_LE16(x) (x)
00053 #define _UPF_SWAP_LE32(x) (x)
00054 #define _UPF_SWAP_BE16(x) _UPF_SWAP_16(x)
00055 #define _UPF_SWAP_BE32(x) _UPF_SWAP_32(x)
00056 #else
00057 #define _UPF_SWAP_LE16(x) _UPF_SWAP_16(x)
00058 #define _UPF_SWAP_LE32(x) _UPF_SWAP_32(x)
00059 #define _UPF_SWAP_BE16(x) (x)
00060 #define _UPF_SWAP_BE32(x) (x)
00061 #endif
00062
00063 #define _UPF_SWAP_NET16(x) _UPF_SWAP_BE16(x)
00064 #define _UPF_SWAP_NET32(x) _UPF_SWAP_BE32(x)
00065
00066
00067 #endif