00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef LO_ENDIAN_H
00018 #define LO_ENDIAN_H
00019
00020 #include <sys/types.h>
00021 #include <stdint.h>
00022
00023 #ifdef WIN32
00024 #include <winsock2.h>
00025 #include <ws2tcpip.h>
00026 #else
00027 #include <netinet/in.h>
00028 #endif
00029
00030 #ifdef __cplusplus
00031 extern "C" {
00032 #endif
00033
00034 #define lo_swap16(x) htons(x)
00035
00036 #define lo_swap32(x) htonl(x)
00037
00038
00039
00040 #ifndef lo_swap16
00041 #define lo_swap16(x) \
00042 ({ \
00043 uint16_t __x = (x); \
00044 ((uint16_t)( \
00045 (((uint16_t)(__x) & (uint16_t)0x00ffU) << 8) | \
00046 (((uint16_t)(__x) & (uint16_t)0xff00U) >> 8) )); \
00047 })
00048 #warning USING UNOPTIMISED ENDIAN STUFF
00049 #endif
00050
00051 #ifndef lo_swap32
00052 #define lo_swap32(x) \
00053 ({ \
00054 uint32_t __x = (x); \
00055 ((uint32_t)( \
00056 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
00057 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
00058 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
00059 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) )); \
00060 })
00061 #endif
00062
00063 #if 0
00064 #ifndef lo_swap64
00065 #define lo_swap64(x) \
00066 ({ \
00067 uint64_t __x = (x); \
00068 ((uint64_t)( \
00069 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000000000ffULL) << 56) | \
00070 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
00071 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
00072 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000ff000000ULL) << 8) | \
00073 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
00074 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
00075 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
00076 (uint64_t)(((uint64_t)(__x) & (uint64_t)0xff00000000000000ULL) >> 56) )); \
00077 })
00078 #endif
00079 #else
00080
00081 typedef union {
00082 uint64_t all;
00083 struct {
00084 uint32_t a;
00085 uint32_t b;
00086 } part;
00087 } lo_split64;
00088
00089 static inline uint64_t lo_swap64(uint64_t x)
00090 {
00091 lo_split64 in, out;
00092
00093 in.all = x;
00094 out.part.a = lo_swap32(in.part.b);
00095 out.part.b = lo_swap32(in.part.a);
00096
00097 return out.all;
00098 }
00099 #endif
00100
00101
00102
00103 #if 0
00104 #define lo_htoo16(x) (x)
00105 #define lo_htoo32(x) (x)
00106 #define lo_htoo64(x) (x)
00107 #define lo_otoh16(x) (x)
00108 #define lo_otoh32(x) (x)
00109 #define lo_otoh64(x) (x)
00110 #else
00111 #define lo_htoo16 lo_swap16
00112 #define lo_htoo32 lo_swap32
00113 #define lo_htoo64 lo_swap64
00114 #define lo_otoh16 lo_swap16
00115 #define lo_otoh32 lo_swap32
00116 #define lo_otoh64 lo_swap64
00117 #endif
00118
00119 #ifdef __cplusplus
00120 }
00121 #endif
00122
00123 #endif
00124
00125