src/core/endian_func.hpp
author terom@frrb.lan
Fri, 19 Dec 2008 01:32:07 +0200
changeset 10438 51bff16a04c9
parent 9575 58d55b1a70c9
permissions -rw-r--r--
initial mini-map stuff
8113
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents:
diff changeset
     1
/* $Id$ */
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents:
diff changeset
     2
9111
48ce04029fe4 (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: 8135
diff changeset
     3
/** @file endian_func.hpp Function to handling different endian machines. */
8113
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents:
diff changeset
     4
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents:
diff changeset
     5
#ifndef ENDIAN_FUNC_H
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents:
diff changeset
     6
#define ENDIAN_FUNC_H
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents:
diff changeset
     7
9543
a60fb4bacc66 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 9111
diff changeset
     8
#include "endian_type.hpp"
8132
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
     9
#include "bitmath_func.hpp"
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
    10
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
    11
/* Setup alignment and conversion macros */
9543
a60fb4bacc66 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 9111
diff changeset
    12
#if TTD_ENDIAN == TTD_BIG_ENDIAN
8133
3ab8eaf85ca0 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8132
diff changeset
    13
	#define FROM_BE16(x) (x)
8132
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
    14
	#define FROM_BE32(x) (x)
8133
3ab8eaf85ca0 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8132
diff changeset
    15
	#define TO_BE16(x)   (x)
8132
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
    16
	#define TO_BE32(x)   (x)
8133
3ab8eaf85ca0 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8132
diff changeset
    17
	#define TO_BE32X(x)  (x)
3ab8eaf85ca0 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8132
diff changeset
    18
	#define FROM_LE16(x) BSWAP16(x)
3ab8eaf85ca0 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8132
diff changeset
    19
	#define FROM_LE32(x) BSWAP32(x)
3ab8eaf85ca0 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8132
diff changeset
    20
	#define TO_LE16(x)   BSWAP16(x)
3ab8eaf85ca0 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8132
diff changeset
    21
	#define TO_LE32(x)   BSWAP32(x)
8132
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
    22
	#define TO_LE32X(x)  BSWAP32(x)
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
    23
#else
8133
3ab8eaf85ca0 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8132
diff changeset
    24
	#define FROM_BE16(x) BSWAP16(x)
3ab8eaf85ca0 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8132
diff changeset
    25
	#define FROM_BE32(x) BSWAP32(x)
3ab8eaf85ca0 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8132
diff changeset
    26
	#define TO_BE16(x)   BSWAP16(x)
3ab8eaf85ca0 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8132
diff changeset
    27
	#define TO_BE32(x)   BSWAP32(x)
8132
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
    28
	#define TO_BE32X(x)  BSWAP32(x)
8133
3ab8eaf85ca0 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8132
diff changeset
    29
	#define FROM_LE16(x) (x)
3ab8eaf85ca0 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8132
diff changeset
    30
	#define FROM_LE32(x) (x)
3ab8eaf85ca0 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8132
diff changeset
    31
	#define TO_LE16(x)   (x)
3ab8eaf85ca0 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8132
diff changeset
    32
	#define TO_LE32(x)   (x)
8132
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
    33
	#define TO_LE32X(x)  (x)
9543
a60fb4bacc66 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 9111
diff changeset
    34
#endif /* TTD_ENDIAN == TTD_BIG_ENDIAN */
8132
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
    35
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9543
diff changeset
    36
static FORCEINLINE uint16 ReadLE16Aligned(const void *x)
8113
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents:
diff changeset
    37
{
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents:
diff changeset
    38
	return FROM_LE16(*(const uint16*)x);
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents:
diff changeset
    39
}
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents:
diff changeset
    40
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9543
diff changeset
    41
static FORCEINLINE uint16 ReadLE16Unaligned(const void *x)
8113
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents:
diff changeset
    42
{
9543
a60fb4bacc66 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 9111
diff changeset
    43
#if OTTD_ALIGNMENT == 1
8113
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents:
diff changeset
    44
	return ((const byte*)x)[0] | ((const byte*)x)[1] << 8;
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents:
diff changeset
    45
#else
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents:
diff changeset
    46
	return FROM_LE16(*(const uint16*)x);
9543
a60fb4bacc66 (svn r13552) -Codechange: use TTD_ENDIAN comparations instead of tests if TTD_[BIG/LITTLE]_ENDIAN is defined
smatz
parents: 9111
diff changeset
    47
#endif /* OTTD_ALIGNMENT == 1 */
8113
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents:
diff changeset
    48
}
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents:
diff changeset
    49
8132
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
    50
#endif /* ENDIAN_FUNC_HPP */