2007-12-21 19:21:21 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file endian_func.hpp Function to handling different endian machines. */
|
2007-12-21 19:21:21 +00:00
|
|
|
|
|
|
|
#ifndef ENDIAN_FUNC_H
|
|
|
|
#define ENDIAN_FUNC_H
|
|
|
|
|
2008-06-17 19:38:00 +00:00
|
|
|
#include "endian_type.hpp"
|
2007-12-25 13:28:09 +00:00
|
|
|
#include "bitmath_func.hpp"
|
|
|
|
|
|
|
|
/* Setup alignment and conversion macros */
|
2008-06-17 19:38:00 +00:00
|
|
|
#if TTD_ENDIAN == TTD_BIG_ENDIAN
|
2007-12-25 13:28:09 +00:00
|
|
|
#define FROM_BE16(x) (x)
|
2007-12-25 13:59:21 +00:00
|
|
|
#define FROM_BE32(x) (x)
|
2007-12-25 13:28:09 +00:00
|
|
|
#define TO_BE16(x) (x)
|
2007-12-25 13:59:21 +00:00
|
|
|
#define TO_BE32(x) (x)
|
|
|
|
#define TO_BE32X(x) (x)
|
|
|
|
#define FROM_LE16(x) BSWAP16(x)
|
|
|
|
#define FROM_LE32(x) BSWAP32(x)
|
|
|
|
#define TO_LE16(x) BSWAP16(x)
|
|
|
|
#define TO_LE32(x) BSWAP32(x)
|
2007-12-25 13:28:09 +00:00
|
|
|
#define TO_LE32X(x) BSWAP32(x)
|
|
|
|
#else
|
2007-12-25 13:59:21 +00:00
|
|
|
#define FROM_BE16(x) BSWAP16(x)
|
|
|
|
#define FROM_BE32(x) BSWAP32(x)
|
|
|
|
#define TO_BE16(x) BSWAP16(x)
|
|
|
|
#define TO_BE32(x) BSWAP32(x)
|
2007-12-25 13:28:09 +00:00
|
|
|
#define TO_BE32X(x) BSWAP32(x)
|
|
|
|
#define FROM_LE16(x) (x)
|
2007-12-25 13:59:21 +00:00
|
|
|
#define FROM_LE32(x) (x)
|
2007-12-25 13:28:09 +00:00
|
|
|
#define TO_LE16(x) (x)
|
2007-12-25 13:59:21 +00:00
|
|
|
#define TO_LE32(x) (x)
|
|
|
|
#define TO_LE32X(x) (x)
|
2008-06-17 19:38:00 +00:00
|
|
|
#endif /* TTD_ENDIAN == TTD_BIG_ENDIAN */
|
2007-12-25 13:28:09 +00:00
|
|
|
|
2008-06-22 15:21:51 +00:00
|
|
|
static FORCEINLINE uint16 ReadLE16Aligned(const void *x)
|
2007-12-21 19:21:21 +00:00
|
|
|
{
|
|
|
|
return FROM_LE16(*(const uint16*)x);
|
|
|
|
}
|
|
|
|
|
2008-06-22 15:21:51 +00:00
|
|
|
static FORCEINLINE uint16 ReadLE16Unaligned(const void *x)
|
2007-12-21 19:21:21 +00:00
|
|
|
{
|
2008-06-17 19:38:00 +00:00
|
|
|
#if OTTD_ALIGNMENT == 1
|
2007-12-21 19:21:21 +00:00
|
|
|
return ((const byte*)x)[0] | ((const byte*)x)[1] << 8;
|
|
|
|
#else
|
|
|
|
return FROM_LE16(*(const uint16*)x);
|
2008-06-17 19:38:00 +00:00
|
|
|
#endif /* OTTD_ALIGNMENT == 1 */
|
2007-12-21 19:21:21 +00:00
|
|
|
}
|
|
|
|
|
2007-12-25 13:28:09 +00:00
|
|
|
#endif /* ENDIAN_FUNC_HPP */
|