OpenTTD
endian_func.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #ifndef ENDIAN_FUNC_HPP
11 #define ENDIAN_FUNC_HPP
12 
13 #include "endian_type.hpp"
14 #include "bitmath_func.hpp"
15 
16 /* Setup alignment and conversion macros */
17 #if TTD_ENDIAN == TTD_BIG_ENDIAN
18  #define FROM_BE16(x) (x)
19  #define FROM_BE32(x) (x)
20  #define TO_BE16(x) (x)
21  #define TO_BE32(x) (x)
22  #define TO_BE32X(x) (x)
23  #define FROM_LE16(x) BSWAP16(x)
24  #define FROM_LE32(x) BSWAP32(x)
25  #define TO_LE16(x) BSWAP16(x)
26  #define TO_LE32(x) BSWAP32(x)
27  #define TO_LE32X(x) BSWAP32(x)
28 #else
29  #define FROM_BE16(x) BSWAP16(x)
30  #define FROM_BE32(x) BSWAP32(x)
31  #define TO_BE16(x) BSWAP16(x)
32  #define TO_BE32(x) BSWAP32(x)
33  #define TO_BE32X(x) BSWAP32(x)
34  #define FROM_LE16(x) (x)
35  #define FROM_LE32(x) (x)
36  #define TO_LE16(x) (x)
37  #define TO_LE32(x) (x)
38  #define TO_LE32X(x) (x)
39 #endif /* TTD_ENDIAN == TTD_BIG_ENDIAN */
40 
41 static inline uint16 ReadLE16Aligned(const void *x)
42 {
43  return FROM_LE16(*(const uint16*)x);
44 }
45 
46 static inline uint16 ReadLE16Unaligned(const void *x)
47 {
48 #if OTTD_ALIGNMENT == 1
49  return ((const byte*)x)[0] | ((const byte*)x)[1] << 8;
50 #else
51  return FROM_LE16(*(const uint16*)x);
52 #endif /* OTTD_ALIGNMENT == 1 */
53 }
54 
55 #endif /* ENDIAN_FUNC_HPP */
Definition of various endian-dependant macros.
Functions related to bit mathematics.