# endian_func.hpp

Go to the documentation of this file.

    00001 /* $Id: endian_func.hpp 11697 2007-12-25 14:08:56Z rubidium $ */
    00002
    00005 #ifndef ENDIAN_FUNC_H
    00006 #define ENDIAN_FUNC_H
    00007
    00008 #include "bitmath_func.hpp"
    00009
    00010 #if defined(ARM) || defined(__arm__) || defined(__alpha__)
    00011   #define OTTD_ALIGNMENT
    00012 #endif
    00013
    00014 /* Windows has always LITTLE_ENDIAN */
    00015 #if defined(WIN32) || defined(__OS2__) || defined(WIN64)
    00016   #define TTD_LITTLE_ENDIAN
    00017 #elif !defined(TESTING)
    00018   /* Else include endian[target/host].h, which has the endian-type, autodetected by the Makefile */
    00019   #if defined(STRGEN)
    00020     #include "endian_host.h"
    00021   #else
    00022     #include "endian_target.h"
    00023   #endif
    00024 #endif /* WIN32 || __OS2__ || WIN64 */
    00025
    00026 /* Setup alignment and conversion macros */
    00027 #if defined(TTD_BIG_ENDIAN)
    00028   #define FROM_BE16(x) (x)
    00029   #define FROM_BE32(x) (x)
    00030   #define TO_BE16(x)   (x)
    00031   #define TO_BE32(x)   (x)
    00032   #define TO_BE32X(x)  (x)
    00033   #define FROM_LE16(x) BSWAP16(x)
    00034   #define FROM_LE32(x) BSWAP32(x)
    00035   #define TO_LE16(x)   BSWAP16(x)
    00036   #define TO_LE32(x)   BSWAP32(x)
    00037   #define TO_LE32X(x)  BSWAP32(x)
    00038 #else
    00039   #define FROM_BE16(x) BSWAP16(x)
    00040   #define FROM_BE32(x) BSWAP32(x)
    00041   #define TO_BE16(x)   BSWAP16(x)
    00042   #define TO_BE32(x)   BSWAP32(x)
    00043   #define TO_BE32X(x)  BSWAP32(x)
    00044   #define FROM_LE16(x) (x)
    00045   #define FROM_LE32(x) (x)
    00046   #define TO_LE16(x)   (x)
    00047   #define TO_LE32(x)   (x)
    00048   #define TO_LE32X(x)  (x)
    00049 #endif /* TTD_BIG_ENDIAN */
    00050
    00051 static inline uint16 ReadLE16Aligned(const void *x)
    00052 {
    00053   return FROM_LE16(*(const uint16*)x);
    00054 }
    00055
    00056 static inline uint16 ReadLE16Unaligned(const void *x)
    00057 {
    00058 #ifdef OTTD_ALIGNMENT
    00059   return ((const byte*)x)[0] | ((const byte*)x)[1] << 8;
    00060 #else
    00061   return FROM_LE16(*(const uint16*)x);
    00062 #endif
    00063 }
    00064
    00065 #endif /* ENDIAN_FUNC_HPP */