2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file endian_func.hpp Function to handling different endian machines. */
|
2007-12-21 19:21:21 +00:00
|
|
|
|
2009-02-17 02:08:53 +00:00
|
|
|
#ifndef ENDIAN_FUNC_HPP
|
|
|
|
#define ENDIAN_FUNC_HPP
|
2007-12-21 19:21:21 +00:00
|
|
|
|
2008-06-17 19:38:00 +00:00
|
|
|
#include "endian_type.hpp"
|
2007-12-25 13:28:09 +00:00
|
|
|
#include "bitmath_func.hpp"
|
|
|
|
|
|
|
|
/* Setup alignment and conversion macros */
|
2008-06-17 19:38:00 +00:00
|
|
|
#if TTD_ENDIAN == TTD_BIG_ENDIAN
|
2007-12-25 13:28:09 +00:00
|
|
|
#define FROM_BE16(x) (x)
|
2007-12-25 13:59:21 +00:00
|
|
|
#define FROM_BE32(x) (x)
|
2018-05-28 01:20:30 +00:00
|
|
|
#define FROM_BE64(x) (x)
|
2007-12-25 13:28:09 +00:00
|
|
|
#define TO_BE16(x) (x)
|
2007-12-25 13:59:21 +00:00
|
|
|
#define TO_BE32(x) (x)
|
|
|
|
#define TO_BE32X(x) (x)
|
2018-05-28 01:20:30 +00:00
|
|
|
#define TO_BE64(x) (x)
|
2007-12-25 13:59:21 +00:00
|
|
|
#define FROM_LE16(x) BSWAP16(x)
|
|
|
|
#define FROM_LE32(x) BSWAP32(x)
|
2018-05-28 01:20:30 +00:00
|
|
|
#define FROM_LE64(x) BSWAP64(x)
|
2007-12-25 13:59:21 +00:00
|
|
|
#define TO_LE16(x) BSWAP16(x)
|
|
|
|
#define TO_LE32(x) BSWAP32(x)
|
2007-12-25 13:28:09 +00:00
|
|
|
#define TO_LE32X(x) BSWAP32(x)
|
2018-05-28 01:20:30 +00:00
|
|
|
#define TO_LE64(x) BSWAP64(x)
|
2007-12-25 13:28:09 +00:00
|
|
|
#else
|
2007-12-25 13:59:21 +00:00
|
|
|
#define FROM_BE16(x) BSWAP16(x)
|
|
|
|
#define FROM_BE32(x) BSWAP32(x)
|
2018-05-28 01:20:30 +00:00
|
|
|
#define FROM_BE64(x) BSWAP64(x)
|
2007-12-25 13:59:21 +00:00
|
|
|
#define TO_BE16(x) BSWAP16(x)
|
|
|
|
#define TO_BE32(x) BSWAP32(x)
|
2007-12-25 13:28:09 +00:00
|
|
|
#define TO_BE32X(x) BSWAP32(x)
|
2018-05-28 01:20:30 +00:00
|
|
|
#define TO_BE64(x) BSWAP64(x)
|
2007-12-25 13:28:09 +00:00
|
|
|
#define FROM_LE16(x) (x)
|
2007-12-25 13:59:21 +00:00
|
|
|
#define FROM_LE32(x) (x)
|
2018-05-28 01:20:30 +00:00
|
|
|
#define FROM_LE64(x) (x)
|
2007-12-25 13:28:09 +00:00
|
|
|
#define TO_LE16(x) (x)
|
2007-12-25 13:59:21 +00:00
|
|
|
#define TO_LE32(x) (x)
|
|
|
|
#define TO_LE32X(x) (x)
|
2018-05-28 01:20:30 +00:00
|
|
|
#define TO_LE64(x) (x)
|
2008-06-17 19:38:00 +00:00
|
|
|
#endif /* TTD_ENDIAN == TTD_BIG_ENDIAN */
|
2007-12-25 13:28:09 +00:00
|
|
|
|
2024-01-06 11:19:27 +00:00
|
|
|
inline uint16_t ReadLE16Aligned(const void *x)
|
2007-12-21 19:21:21 +00:00
|
|
|
{
|
2024-01-07 16:41:53 +00:00
|
|
|
return FROM_LE16(*(const uint16_t*)x);
|
2007-12-21 19:21:21 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 11:19:27 +00:00
|
|
|
inline uint16_t ReadLE16Unaligned(const void *x)
|
2007-12-21 19:21:21 +00:00
|
|
|
{
|
2008-06-17 19:38:00 +00:00
|
|
|
#if OTTD_ALIGNMENT == 1
|
2007-12-21 19:21:21 +00:00
|
|
|
return ((const byte*)x)[0] | ((const byte*)x)[1] << 8;
|
|
|
|
#else
|
2024-01-07 16:41:53 +00:00
|
|
|
return FROM_LE16(*(const uint16_t*)x);
|
2008-06-17 19:38:00 +00:00
|
|
|
#endif /* OTTD_ALIGNMENT == 1 */
|
2007-12-21 19:21:21 +00:00
|
|
|
}
|
|
|
|
|
2007-12-25 13:28:09 +00:00
|
|
|
#endif /* ENDIAN_FUNC_HPP */
|