# endian_func.hpp

Go to the documentation of this file.

    00001 /* $Id: endian_func.hpp 15508 2009-02-17 02:08:53Z smatz $ */
    00002
    00005 #ifndef ENDIAN_FUNC_HPP
    00006 #define ENDIAN_FUNC_HPP
    00007
    00008 #include "endian_type.hpp"
    00009 #include "bitmath_func.hpp"
    00010
    00011 /* Setup alignment and conversion macros */
    00012 #if TTD_ENDIAN == TTD_BIG_ENDIAN
    00013   #define FROM_BE16(x) (x)
    00014   #define FROM_BE32(x) (x)
    00015   #define TO_BE16(x)   (x)
    00016   #define TO_BE32(x)   (x)
    00017   #define TO_BE32X(x)  (x)
    00018   #define FROM_LE16(x) BSWAP16(x)
    00019   #define FROM_LE32(x) BSWAP32(x)
    00020   #define TO_LE16(x)   BSWAP16(x)
    00021   #define TO_LE32(x)   BSWAP32(x)
    00022   #define TO_LE32X(x)  BSWAP32(x)
    00023 #else
    00024   #define FROM_BE16(x) BSWAP16(x)
    00025   #define FROM_BE32(x) BSWAP32(x)
    00026   #define TO_BE16(x)   BSWAP16(x)
    00027   #define TO_BE32(x)   BSWAP32(x)
    00028   #define TO_BE32X(x)  BSWAP32(x)
    00029   #define FROM_LE16(x) (x)
    00030   #define FROM_LE32(x) (x)
    00031   #define TO_LE16(x)   (x)
    00032   #define TO_LE32(x)   (x)
    00033   #define TO_LE32X(x)  (x)
    00034 #endif /* TTD_ENDIAN == TTD_BIG_ENDIAN */
    00035
    00036 static FORCEINLINE uint16 ReadLE16Aligned(const void *x)
    00037 {
    00038   return FROM_LE16(*(const uint16*)x);
    00039 }
    00040
    00041 static FORCEINLINE uint16 ReadLE16Unaligned(const void *x)
    00042 {
    00043 #if OTTD_ALIGNMENT == 1
    00044   return ((const byte*)x)[0] | ((const byte*)x)[1] << 8;
    00045 #else
    00046   return FROM_LE16(*(const uint16*)x);
    00047 #endif /* OTTD_ALIGNMENT == 1 */
    00048 }
    00049
    00050 #endif /* ENDIAN_FUNC_HPP */