src/core/endian_func.hpp
author belugas
Tue, 04 Mar 2008 17:07:33 +0000
changeset 9168 a35d94d8501c
parent 8631 ab2ac7e7e047
child 10429 1b99254f9607
permissions -rw-r--r--
(svn r12337) -Change: update some documentation.
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2159
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2159
diff changeset
     2
8609
8c0c3e9dd6a0 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8501
diff changeset
     3
/** @file endian_func.hpp */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     4
8609
8c0c3e9dd6a0 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8501
diff changeset
     5
#ifndef ENDIAN_FUNC_H
8c0c3e9dd6a0 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8501
diff changeset
     6
#define ENDIAN_FUNC_H
8115
95deea94d1f5 (svn r11149) -Fix [FS#1225]: Draw building stages for new house ground sprites.
maedhros
parents: 8012
diff changeset
     7
8628
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
     8
#include "bitmath_func.hpp"
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
     9
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    10
#if defined(ARM) || defined(__arm__) || defined(__alpha__)
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    11
	#define OTTD_ALIGNMENT
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    12
#endif
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    13
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    14
/* Windows has always LITTLE_ENDIAN */
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    15
#if defined(WIN32) || defined(__OS2__) || defined(WIN64)
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    16
	#define TTD_LITTLE_ENDIAN
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    17
#elif !defined(TESTING)
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    18
	/* Else include endian[target/host].h, which has the endian-type, autodetected by the Makefile */
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    19
	#if defined(STRGEN)
8631
ab2ac7e7e047 (svn r11697) -Fix [FS#1585]: strgen did not (always) compile properly.
rubidium
parents: 8629
diff changeset
    20
		#include "endian_host.h"
8628
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    21
	#else
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    22
		#include "endian_target.h"
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    23
	#endif
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    24
#endif /* WIN32 || __OS2__ || WIN64 */
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    25
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    26
/* Setup alignment and conversion macros */
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    27
#if defined(TTD_BIG_ENDIAN)
8629
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    28
	#define FROM_BE16(x) (x)
8628
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    29
	#define FROM_BE32(x) (x)
8629
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    30
	#define TO_BE16(x)   (x)
8628
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    31
	#define TO_BE32(x)   (x)
8629
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    32
	#define TO_BE32X(x)  (x)
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    33
	#define FROM_LE16(x) BSWAP16(x)
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    34
	#define FROM_LE32(x) BSWAP32(x)
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    35
	#define TO_LE16(x)   BSWAP16(x)
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    36
	#define TO_LE32(x)   BSWAP32(x)
8628
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    37
	#define TO_LE32X(x)  BSWAP32(x)
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    38
#else
8629
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    39
	#define FROM_BE16(x) BSWAP16(x)
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    40
	#define FROM_BE32(x) BSWAP32(x)
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    41
	#define TO_BE16(x)   BSWAP16(x)
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    42
	#define TO_BE32(x)   BSWAP32(x)
8628
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    43
	#define TO_BE32X(x)  BSWAP32(x)
8629
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    44
	#define FROM_LE16(x) (x)
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    45
	#define FROM_LE32(x) (x)
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    46
	#define TO_LE16(x)   (x)
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    47
	#define TO_LE32(x)   (x)
8628
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    48
	#define TO_LE32X(x)  (x)
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    49
#endif /* TTD_BIG_ENDIAN */
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    50
8609
8c0c3e9dd6a0 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8501
diff changeset
    51
static inline uint16 ReadLE16Aligned(const void *x)
2966
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
    52
{
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
    53
	return FROM_LE16(*(const uint16*)x);
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
    54
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    55
8609
8c0c3e9dd6a0 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8501
diff changeset
    56
static inline uint16 ReadLE16Unaligned(const void *x)
2966
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
    57
{
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
    58
#ifdef OTTD_ALIGNMENT
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
    59
	return ((const byte*)x)[0] | ((const byte*)x)[1] << 8;
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
    60
#else
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
    61
	return FROM_LE16(*(const uint16*)x);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    62
#endif
2966
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
    63
}
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
    64
8628
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    65
#endif /* ENDIAN_FUNC_HPP */