Add support for big-endian architectures.

master
Konstantin Isakov 2014-01-15 21:48:05 +04:00
parent ccdeab23b6
commit 7c2205a3e0
1 changed files with 21 additions and 3 deletions

View File

@ -12,9 +12,7 @@
#include <endian.h>
#endif
#if __BYTE_ORDER != __LITTLE_ENDIAN
#error Please add support for architectures different from little-endian.
#endif
#if __BYTE_ORDER == __LITTLE_ENDIAN
/// Converts the given host-order value to big-endian value
inline uint32_t toBigEndian( uint32_t v ) { return htonl( v ); }
@ -25,4 +23,24 @@ inline uint64_t toLittleEndian( uint64_t v ) { return v; }
inline uint32_t fromLittleEndian( uint32_t v ) { return v; }
inline uint64_t fromLittleEndian( uint64_t v ) { return v; }
#elif __BYTE_ORDER == __BIG_ENDIAN
// Note: the functions used are non-standard. Add more ifdefs if needed
/// Converts the given host-order value to big-endian value
inline uint32_t toBigEndian( uint32_t v ) { return v; }
/// Converts the given host-order value to little-endian value
inline uint32_t toLittleEndian( uint32_t v ) { return htole32( v ); }
inline uint64_t toLittleEndian( uint64_t v ) { return htole64( v ); }
/// Converts the given little-endian value to host-order value
inline uint32_t fromLittleEndian( uint32_t v ) { return le32toh( v ); }
inline uint64_t fromLittleEndian( uint64_t v ) { return le64toh( v ); }
#else
#error Please add support for architectures different from little-endian and\
big-endian.
#endif
#endif