src/core/endian_func.hpp
author terom@terom-desktop
Thu, 24 Jul 2008 16:18:29 +0300
changeset 11185 5036deb1fcaf
parent 11049 f8bbc9635251
permissions -rw-r--r--
fix check_available_write_cb buffer-growing logic
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
10429
1b99254f9607 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 8631
diff changeset
     3
/** @file endian_func.hpp Function to handling different endian machines. */
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
10998
04f58fa3dbb0 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 10429
diff changeset
     8
#include "endian_type.hpp"
8628
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
     9
#include "bitmath_func.hpp"
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    10
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    11
/* Setup alignment and conversion macros */
10998
04f58fa3dbb0 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 10429
diff changeset
    12
#if TTD_ENDIAN == TTD_BIG_ENDIAN
8629
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    13
	#define FROM_BE16(x) (x)
8628
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    14
	#define FROM_BE32(x) (x)
8629
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    15
	#define TO_BE16(x)   (x)
8628
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    16
	#define TO_BE32(x)   (x)
8629
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    17
	#define TO_BE32X(x)  (x)
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    18
	#define FROM_LE16(x) BSWAP16(x)
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    19
	#define FROM_LE32(x) BSWAP32(x)
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    20
	#define TO_LE16(x)   BSWAP16(x)
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    21
	#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
    22
	#define TO_LE32X(x)  BSWAP32(x)
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    23
#else
8629
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    24
	#define FROM_BE16(x) BSWAP16(x)
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    25
	#define FROM_BE32(x) BSWAP32(x)
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    26
	#define TO_BE16(x)   BSWAP16(x)
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    27
	#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
    28
	#define TO_BE32X(x)  BSWAP32(x)
8629
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    29
	#define FROM_LE16(x) (x)
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    30
	#define FROM_LE32(x) (x)
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    31
	#define TO_LE16(x)   (x)
c764b6319d60 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8628
diff changeset
    32
	#define TO_LE32(x)   (x)
8628
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    33
	#define TO_LE32X(x)  (x)
10998
04f58fa3dbb0 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 10429
diff changeset
    34
#endif /* TTD_ENDIAN == TTD_BIG_ENDIAN */
8628
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    35
11049
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 10998
diff changeset
    36
static FORCEINLINE 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
    37
{
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
    38
	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
    39
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    40
11049
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 10998
diff changeset
    41
static FORCEINLINE 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
    42
{
10998
04f58fa3dbb0 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 10429
diff changeset
    43
#if OTTD_ALIGNMENT == 1
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
    44
	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
    45
#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
    46
	return FROM_LE16(*(const uint16*)x);
10998
04f58fa3dbb0 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 10429
diff changeset
    47
#endif /* OTTD_ALIGNMENT == 1 */
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
    48
}
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
    49
8628
4e316518420a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8609
diff changeset
    50
#endif /* ENDIAN_FUNC_HPP */