30 #ifndef Pt_Byteorder_h
31 #define Pt_Byteorder_h
40 #if defined(PT_LE) || defined(PT_BE)
42 #elif defined (_BYTE_ORDER)
43 # if (_BYTE_ORDER == _LITTLE_ENDIAN)
45 # elif (_BYTE_ORDER == _BIG_ENDIAN)
48 # error: unknown _BYTE_ORDER
50 #elif defined (__BYTE_ORDER)
51 # if (__BYTE_ORDER == __LITTLE_ENDIAN)
53 # elif (__BYTE_ORDER == __BIG_ENDIAN)
56 # error: unknown __BYTE_ORDER
58 #elif defined (__LITTLE_ENDIAN__) || \
59 defined (i386) || defined(__i386) || defined (__i386__) || \
60 defined(_X86_) || defined(sun386) || defined (_M_IX86) || \
61 defined (_M_IA64) || defined (__ia64__) || \
62 defined(__IA64__) || defined(_IA64) || \
63 defined (_M_AMD64) || defined (__amd64) || \
64 defined(MIPSEL) || defined(_MIPSEL) || \
65 defined (ARM) || defined(__arm__) || defined(_M_ARM) || defined(_M_ARMT) || \
66 defined (vax) || defined (__alpha) || defined(__THW_INTEL)
68 #elif defined(__BIG_ENDIAN__) || \
69 defined(__hppa__) || defined(__hppa) || defined(__hp9000) || \
70 defined(__hp9000s300) || defined(hp9000s300) || \
71 defined(__hp9000s700) || defined(hp9000s700) || \
72 defined(__hp9000s800) || defined(hp9000s800) || defined(hp9000s820) || \
73 defined(__sparc__) || defined(sparc) || defined(__sparc) || \
74 defined(ibm032) || defined(ibm370) || defined(_IBMR2) || \
75 defined(MIPSEB) || defined(_MIPSEB) || \
76 defined(mc68000) || defined(is68k) || defined(macII) || defined(m68k) || \
77 defined(apollo) || defined(__convex__) || defined(_CRAY) || defined(sel)
80 #error: PT_LE or PT_BE is not defined
88 inline T swab16(T value)
101 return ( (value & 0x00FF) << 8 ) |
102 ( (value & 0xFF00) >> 8 );
107 template <
typename T>
108 inline T swab32(T value)
110 return ( (value & 0x000000FF) << 24 ) |
111 ( (value & 0x0000FF00) << 8 ) |
112 ( (value & 0x00FF0000) >> 8 ) |
113 ( (value & 0xFF000000) >> 24 );
119 template <
typename T>
120 inline T swab64(T value)
122 return ( (value & 0x00000000000000FFULL) << 56 ) |
123 ( (value & 0x000000000000FF00ULL) << 40 ) |
124 ( (value & 0x0000000000FF0000ULL) << 24 ) |
125 ( (value & 0x00000000FF000000ULL) << 8 ) |
126 ( (value & 0x000000FF00000000ULL) >> 8 ) |
127 ( (value & 0x0000FF0000000000ULL) >> 24 ) |
128 ( (value & 0x00FF000000000000ULL) >> 40 ) |
129 ( (value & 0xFF00000000000000ULL) >> 56 );
140 {
return swab16(value); }
143 {
return swab16(value); }
150 {
return swab32(value); }
157 {
return swab32(value); }
161 {
return swab64(value); }
164 {
return swab64(value); }
174 return *
reinterpret_cast<const int8_t*
>(&i) == 0;
184 return *
reinterpret_cast<const int8_t*
>(&i) == 1;
196 template <
typename T>
215 template <
typename T>
234 template <
typename T>
253 template <
typename T>
bool isLittleEndian()
Returns true, if the cpu is little-endian (low-byte first).
Definition: Byteorder.h:181
uint_type uint16_t
Unsigned 16-bit integer type.
Definition: Types.h:30
T beToHost(const T &value)
Converts a value from big-endian to host-byteorder.
Definition: Byteorder.h:254
int_type int16_t
Signed 16-bit integer type.
Definition: Types.h:24
int8_t swab(int8_t value)
Swaps the byteorder of an int32_t.
Definition: Byteorder.h:133
int_type int32_t
Signed 32-bit integer type.
Definition: Types.h:36
int_type int64_t
Signed 64-bit integer type.
Definition: Types.h:48
T hostToBe(const T &value)
Converts a value from the host-byteorder to big-endian.
Definition: Byteorder.h:235
T hostToLe(const T &value)
Converts a value from host-byteorder to little-endian.
Definition: Byteorder.h:197
int_type int8_t
Signed 8-bit integer type.
Definition: Types.h:12
uint_type uint64_t
Unsigned 64-bit integer type.
Definition: Types.h:54
uint_type uint32_t
Unsigned 32-bit integer type.
Definition: Types.h:42
uint_type uint8_t
Unsigned 8-bit integer type.
Definition: Types.h:18
T leToHost(const T &value)
Converts a value from little-endian to host-byteorder.
Definition: Byteorder.h:216
bool isBigEndian()
Returns true, if the cpu is big-endian (high-byte first).
Definition: Byteorder.h:171