endian_func.hpp

Go to the documentation of this file.
00001 /* $Id: endian_func.hpp 17248 2009-08-21 20:21:05Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #ifndef ENDIAN_FUNC_HPP
00013 #define ENDIAN_FUNC_HPP
00014 
00015 #include "endian_type.hpp"
00016 #include "bitmath_func.hpp"
00017 
00018 /* Setup alignment and conversion macros */
00019 #if TTD_ENDIAN == TTD_BIG_ENDIAN
00020   #define FROM_BE16(x) (x)
00021   #define FROM_BE32(x) (x)
00022   #define TO_BE16(x)   (x)
00023   #define TO_BE32(x)   (x)
00024   #define TO_BE32X(x)  (x)
00025   #define FROM_LE16(x) BSWAP16(x)
00026   #define FROM_LE32(x) BSWAP32(x)
00027   #define TO_LE16(x)   BSWAP16(x)
00028   #define TO_LE32(x)   BSWAP32(x)
00029   #define TO_LE32X(x)  BSWAP32(x)
00030 #else
00031   #define FROM_BE16(x) BSWAP16(x)
00032   #define FROM_BE32(x) BSWAP32(x)
00033   #define TO_BE16(x)   BSWAP16(x)
00034   #define TO_BE32(x)   BSWAP32(x)
00035   #define TO_BE32X(x)  BSWAP32(x)
00036   #define FROM_LE16(x) (x)
00037   #define FROM_LE32(x) (x)
00038   #define TO_LE16(x)   (x)
00039   #define TO_LE32(x)   (x)
00040   #define TO_LE32X(x)  (x)
00041 #endif /* TTD_ENDIAN == TTD_BIG_ENDIAN */
00042 
00043 static FORCEINLINE uint16 ReadLE16Aligned(const void *x)
00044 {
00045   return FROM_LE16(*(const uint16*)x);
00046 }
00047 
00048 static FORCEINLINE uint16 ReadLE16Unaligned(const void *x)
00049 {
00050 #if OTTD_ALIGNMENT == 1
00051   return ((const byte*)x)[0] | ((const byte*)x)[1] << 8;
00052 #else
00053   return FROM_LE16(*(const uint16*)x);
00054 #endif /* OTTD_ALIGNMENT == 1 */
00055 }
00056 
00057 #endif /* ENDIAN_FUNC_HPP */