author | glx |
Mon, 26 May 2008 17:40:33 +0000 | |
branch | noai |
changeset 10718 | 7e9d9e40e16f |
parent 10455 | 22c441f5adf9 |
child 11044 | 097ea3e7ec56 |
permissions | -rw-r--r-- |
9723 | 1 |
/* $Id$ */ |
2 |
||
10455
22c441f5adf9
(svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents:
9723
diff
changeset
|
3 |
/** @file endian_func.hpp Function to handling different endian machines. */ |
9723 | 4 |
|
5 |
#ifndef ENDIAN_FUNC_H |
|
6 |
#define ENDIAN_FUNC_H |
|
7 |
||
8 |
#include "bitmath_func.hpp" |
|
9 |
||
10 |
#if defined(ARM) || defined(__arm__) || defined(__alpha__) |
|
11 |
#define OTTD_ALIGNMENT |
|
12 |
#endif |
|
13 |
||
14 |
/* Windows has always LITTLE_ENDIAN */ |
|
15 |
#if defined(WIN32) || defined(__OS2__) || defined(WIN64) |
|
16 |
#define TTD_LITTLE_ENDIAN |
|
17 |
#elif !defined(TESTING) |
|
18 |
/* Else include endian[target/host].h, which has the endian-type, autodetected by the Makefile */ |
|
19 |
#if defined(STRGEN) |
|
20 |
#include "endian_host.h" |
|
21 |
#else |
|
22 |
#include "endian_target.h" |
|
23 |
#endif |
|
24 |
#endif /* WIN32 || __OS2__ || WIN64 */ |
|
25 |
||
26 |
/* Setup alignment and conversion macros */ |
|
27 |
#if defined(TTD_BIG_ENDIAN) |
|
28 |
#define FROM_BE16(x) (x) |
|
29 |
#define FROM_BE32(x) (x) |
|
30 |
#define TO_BE16(x) (x) |
|
31 |
#define TO_BE32(x) (x) |
|
32 |
#define TO_BE32X(x) (x) |
|
33 |
#define FROM_LE16(x) BSWAP16(x) |
|
34 |
#define FROM_LE32(x) BSWAP32(x) |
|
35 |
#define TO_LE16(x) BSWAP16(x) |
|
36 |
#define TO_LE32(x) BSWAP32(x) |
|
37 |
#define TO_LE32X(x) BSWAP32(x) |
|
38 |
#else |
|
39 |
#define FROM_BE16(x) BSWAP16(x) |
|
40 |
#define FROM_BE32(x) BSWAP32(x) |
|
41 |
#define TO_BE16(x) BSWAP16(x) |
|
42 |
#define TO_BE32(x) BSWAP32(x) |
|
43 |
#define TO_BE32X(x) BSWAP32(x) |
|
44 |
#define FROM_LE16(x) (x) |
|
45 |
#define FROM_LE32(x) (x) |
|
46 |
#define TO_LE16(x) (x) |
|
47 |
#define TO_LE32(x) (x) |
|
48 |
#define TO_LE32X(x) (x) |
|
49 |
#endif /* TTD_BIG_ENDIAN */ |
|
50 |
||
51 |
static inline uint16 ReadLE16Aligned(const void *x) |
|
52 |
{ |
|
53 |
return FROM_LE16(*(const uint16*)x); |
|
54 |
} |
|
55 |
||
56 |
static inline uint16 ReadLE16Unaligned(const void *x) |
|
57 |
{ |
|
58 |
#ifdef OTTD_ALIGNMENT |
|
59 |
return ((const byte*)x)[0] | ((const byte*)x)[1] << 8; |
|
60 |
#else |
|
61 |
return FROM_LE16(*(const uint16*)x); |
|
62 |
#endif |
|
63 |
} |
|
64 |
||
65 |
#endif /* ENDIAN_FUNC_HPP */ |