2007-05-30 21:56:52 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
2005-06-23 14:15:49 +00:00
|
|
|
*
|
|
|
|
* This program 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; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2014-02-18 01:34:18 +00:00
|
|
|
*
|
2005-06-23 14:15:49 +00:00
|
|
|
* This program 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.
|
2014-02-18 01:34:18 +00:00
|
|
|
*
|
2005-06-23 14:15:49 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2005-06-23 14:15:49 +00:00
|
|
|
*
|
|
|
|
*/
|
2006-04-15 21:22:33 +00:00
|
|
|
|
2005-06-23 14:15:49 +00:00
|
|
|
#ifndef COMMON_ENDIAN_H
|
|
|
|
#define COMMON_ENDIAN_H
|
|
|
|
|
|
|
|
#include "common/scummsys.h"
|
|
|
|
|
2009-09-11 08:55:47 +00:00
|
|
|
/**
|
|
|
|
* \file endian.h
|
|
|
|
* Endian conversion and byteswap conversion functions or macros
|
|
|
|
*
|
|
|
|
* SWAP_BYTES_??(a) - inverse byte order
|
|
|
|
* SWAP_CONSTANT_??(a) - inverse byte order, implemented as macro.
|
|
|
|
* Use with compiletime-constants only, the result will be a compiletime-constant aswell.
|
|
|
|
* Unlike most other functions these can be used for eg. switch-case labels
|
|
|
|
*
|
|
|
|
* READ_UINT??(a) - read native value from pointer a
|
|
|
|
* READ_??_UINT??(a) - read LE/BE value from pointer a and convert it to native
|
|
|
|
* WRITE_??_UINT??(a, v) - write native value v to pointer a with LE/BE encoding
|
|
|
|
* TO_??_??(a) - convert native value v to LE/BE
|
|
|
|
* FROM_??_??(a) - convert LE/BE value v to native
|
|
|
|
* CONSTANT_??_??(a) - convert LE/BE value v to native, implemented as macro.
|
|
|
|
* Use with compiletime-constants only, the result will be a compiletime-constant aswell.
|
2010-01-25 01:39:44 +00:00
|
|
|
* Unlike most other functions these can be used for eg. switch-case labels
|
2009-09-11 08:55:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
// Sanity check
|
|
|
|
#if !defined(SCUMM_LITTLE_ENDIAN) && !defined(SCUMM_BIG_ENDIAN)
|
|
|
|
# error No endianness defined
|
|
|
|
#endif
|
|
|
|
|
2014-09-16 18:49:27 +00:00
|
|
|
#define SWAP_CONSTANT_64(a) \
|
|
|
|
((uint64)((((a) >> 56) & 0x000000FF) | \
|
|
|
|
(((a) >> 40) & 0x0000FF00) | \
|
|
|
|
(((a) >> 24) & 0x00FF0000) | \
|
|
|
|
(((a) >> 8) & 0xFF000000) | \
|
|
|
|
(((a) & 0xFF000000) << 8) | \
|
|
|
|
(((a) & 0x00FF0000) << 24) | \
|
|
|
|
(((a) & 0x0000FF00) << 40) | \
|
|
|
|
(((a) & 0x000000FF) << 56) ))
|
|
|
|
|
2009-09-11 08:55:47 +00:00
|
|
|
#define SWAP_CONSTANT_32(a) \
|
|
|
|
((uint32)((((a) >> 24) & 0x00FF) | \
|
|
|
|
(((a) >> 8) & 0xFF00) | \
|
|
|
|
(((a) & 0xFF00) << 8) | \
|
|
|
|
(((a) & 0x00FF) << 24) ))
|
|
|
|
|
|
|
|
#define SWAP_CONSTANT_16(a) \
|
|
|
|
((uint16)((((a) >> 8) & 0x00FF) | \
|
|
|
|
(((a) << 8) & 0xFF00) ))
|
2005-06-23 14:15:49 +00:00
|
|
|
|
2015-01-04 20:49:56 +00:00
|
|
|
|
|
|
|
|
2014-09-16 18:49:27 +00:00
|
|
|
/**
|
2015-01-04 20:49:56 +00:00
|
|
|
* Swap the bytes in a 16 bit word in order to convert LE encoded data to BE
|
2014-09-16 18:49:27 +00:00
|
|
|
* and vice versa.
|
|
|
|
*/
|
|
|
|
|
2015-01-04 20:49:56 +00:00
|
|
|
// compilerspecific variants come first, fallback last
|
2014-09-16 18:49:27 +00:00
|
|
|
|
|
|
|
// Test for GCC and if the target has the MIPS rel.2 instructions (we know the psp does)
|
|
|
|
#if defined(__GNUC__) && (defined(__psp__) || defined(_MIPS_ARCH_MIPS32R2) || defined(_MIPS_ARCH_MIPS64R2))
|
|
|
|
|
2015-01-04 20:49:56 +00:00
|
|
|
FORCEINLINE uint16 SWAP_BYTES_16(const uint16 a) {
|
2014-09-16 18:49:27 +00:00
|
|
|
if (__builtin_constant_p(a)) {
|
2015-01-04 20:49:56 +00:00
|
|
|
return SWAP_CONSTANT_16(a);
|
2014-09-16 18:49:27 +00:00
|
|
|
} else {
|
2015-01-04 20:49:56 +00:00
|
|
|
uint16 result;
|
|
|
|
__asm__ ("wsbh %0,%1" : "=r" (result) : "r" (a));
|
2014-09-16 18:49:27 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
2015-01-04 20:49:56 +00:00
|
|
|
inline uint16 SWAP_BYTES_16(const uint16 a) {
|
|
|
|
return (a >> 8) | (a << 8);
|
2014-09-16 18:49:27 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-01-04 20:49:56 +00:00
|
|
|
|
|
|
|
|
2006-02-25 11:45:56 +00:00
|
|
|
/**
|
|
|
|
* Swap the bytes in a 32 bit word in order to convert LE encoded data to BE
|
|
|
|
* and vice versa.
|
|
|
|
*/
|
2009-09-11 08:55:47 +00:00
|
|
|
|
|
|
|
// machine/compiler-specific variants come first, fallback last
|
|
|
|
|
|
|
|
// Test for GCC and if the target has the MIPS rel.2 instructions (we know the psp does)
|
|
|
|
#if defined(__GNUC__) && (defined(__psp__) || defined(_MIPS_ARCH_MIPS32R2) || defined(_MIPS_ARCH_MIPS64R2))
|
|
|
|
|
|
|
|
FORCEINLINE uint32 SWAP_BYTES_32(const uint32 a) {
|
|
|
|
if (__builtin_constant_p(a)) {
|
|
|
|
return SWAP_CONSTANT_32(a);
|
|
|
|
} else {
|
|
|
|
uint32 result;
|
|
|
|
# if defined(__psp__)
|
|
|
|
// use special allegrex instruction
|
|
|
|
__asm__ ("wsbw %0,%1" : "=r" (result) : "r" (a));
|
|
|
|
# else
|
|
|
|
__asm__ ("wsbh %0,%1\n"
|
|
|
|
"rotr %0,%0,16" : "=r" (result) : "r" (a));
|
|
|
|
# endif
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test for GCC >= 4.3.0 as this version added the bswap builtin
|
2012-03-15 09:16:45 +00:00
|
|
|
#elif GCC_ATLEAST(4, 3)
|
2009-09-11 08:55:47 +00:00
|
|
|
|
|
|
|
FORCEINLINE uint32 SWAP_BYTES_32(uint32 a) {
|
|
|
|
return __builtin_bswap32(a);
|
|
|
|
}
|
|
|
|
|
2014-06-20 01:21:14 +00:00
|
|
|
#elif defined(_MSC_VER)
|
2009-09-11 08:55:47 +00:00
|
|
|
|
|
|
|
FORCEINLINE uint32 SWAP_BYTES_32(uint32 a) {
|
|
|
|
return _byteswap_ulong(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
// generic fallback
|
2010-01-25 01:39:44 +00:00
|
|
|
#else
|
2009-09-11 08:55:47 +00:00
|
|
|
|
|
|
|
inline uint32 SWAP_BYTES_32(uint32 a) {
|
|
|
|
const uint16 low = (uint16)a, high = (uint16)(a >> 16);
|
|
|
|
return ((uint32)(uint16)((low >> 8) | (low << 8)) << 16)
|
|
|
|
| (uint16)((high >> 8) | (high << 8));
|
|
|
|
}
|
|
|
|
#endif
|
2005-06-23 14:15:49 +00:00
|
|
|
|
2006-02-25 11:45:56 +00:00
|
|
|
/**
|
2015-01-04 20:49:56 +00:00
|
|
|
* Swap the bytes in a 64 bit word in order to convert LE encoded data to BE
|
2006-02-25 11:45:56 +00:00
|
|
|
* and vice versa.
|
|
|
|
*/
|
2009-09-11 08:55:47 +00:00
|
|
|
|
2015-01-04 20:49:56 +00:00
|
|
|
// machine/compiler-specific variants come first, fallback last
|
2009-09-11 08:55:47 +00:00
|
|
|
|
|
|
|
// Test for GCC and if the target has the MIPS rel.2 instructions (we know the psp does)
|
2015-01-04 20:49:56 +00:00
|
|
|
//
|
2009-09-11 08:55:47 +00:00
|
|
|
#if defined(__GNUC__) && (defined(__psp__) || defined(_MIPS_ARCH_MIPS32R2) || defined(_MIPS_ARCH_MIPS64R2))
|
|
|
|
|
2015-01-04 20:54:11 +00:00
|
|
|
FORCEINLINE uint64 SWAP_BYTES_64(const uint64 a) {
|
2009-09-11 08:55:47 +00:00
|
|
|
if (__builtin_constant_p(a)) {
|
2015-01-04 20:54:11 +00:00
|
|
|
return SWAP_CONSTANT_64(a);
|
2009-09-11 08:55:47 +00:00
|
|
|
} else {
|
2015-01-04 20:54:11 +00:00
|
|
|
uint32 low = (uint32)a, high = (uint32)(a >> 32);
|
|
|
|
low = SWAP_BYTES_32(low);
|
|
|
|
high = SWAP_BYTES_32(high);
|
|
|
|
|
|
|
|
return (((uint64)low) << 32) | high;
|
2009-09-11 08:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-04 20:49:56 +00:00
|
|
|
|
|
|
|
// Test for GCC >= 4.3.0 as this version added the bswap builtin
|
|
|
|
#elif GCC_ATLEAST(4, 3)
|
|
|
|
|
|
|
|
FORCEINLINE uint64 SWAP_BYTES_64(uint64 a) {
|
|
|
|
return __builtin_bswap64(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
|
|
|
|
FORCEINLINE uint64 SWAP_BYTES_64(uint64 a) {
|
|
|
|
return _byteswap_uint64(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
// generic fallback
|
2009-09-11 08:55:47 +00:00
|
|
|
#else
|
|
|
|
|
2015-01-04 20:49:56 +00:00
|
|
|
inline uint64 SWAP_BYTES_64(uint64 a) {
|
|
|
|
uint32 low = (uint32)a, high = (uint32)(a >> 32);
|
|
|
|
uint16 lowLow = (uint16)low, lowHigh = (uint16)(low >> 16),
|
|
|
|
highLow = (uint16)high, highHigh = (uint16)(high >> 16);
|
|
|
|
|
|
|
|
return ((uint64)(((uint32)(uint16)((lowLow >> 8) | (lowLow << 8)) << 16) |
|
|
|
|
(uint16)((lowHigh >> 8) | (lowHigh << 8))) << 32) |
|
|
|
|
(((uint32)(uint16)((highLow >> 8) | (highLow << 8)) << 16) |
|
|
|
|
(uint16)((highHigh >> 8) | (highHigh << 8)));
|
2009-09-11 08:55:47 +00:00
|
|
|
}
|
|
|
|
#endif
|
2005-06-23 14:15:49 +00:00
|
|
|
|
2015-01-04 20:49:56 +00:00
|
|
|
|
2005-06-23 14:15:49 +00:00
|
|
|
|
2006-02-25 11:45:56 +00:00
|
|
|
/**
|
|
|
|
* A wrapper macro used around four character constants, like 'DATA', to
|
2011-04-12 14:53:15 +00:00
|
|
|
* ensure portability. Typical usage: MKTAG('D','A','T','A').
|
2006-02-25 11:45:56 +00:00
|
|
|
*
|
|
|
|
* Why is this necessary? The C/C++ standard does not define the endianess to
|
|
|
|
* be used for character constants. Hence if one uses multi-byte character
|
2008-01-27 19:47:41 +00:00
|
|
|
* constants, a potential portability problem opens up.
|
2006-02-25 11:45:56 +00:00
|
|
|
*/
|
2011-04-12 15:23:21 +00:00
|
|
|
#define MKTAG(a0,a1,a2,a3) ((uint32)((a3) | ((a2) << 8) | ((a1) << 16) | ((a0) << 24)))
|
2005-06-23 14:15:49 +00:00
|
|
|
|
2012-08-26 16:38:35 +00:00
|
|
|
/**
|
|
|
|
* A wrapper macro used around two character constants, like 'wb', to
|
|
|
|
* ensure portability. Typical usage: MKTAG16('w','b').
|
|
|
|
*/
|
|
|
|
#define MKTAG16(a0,a1) ((uint16)((a1) | ((a0) << 8)))
|
|
|
|
|
2011-05-10 13:38:10 +00:00
|
|
|
// Functions for reading/writing native integers.
|
|
|
|
// They also transparently handle the need for alignment.
|
2009-09-11 08:55:47 +00:00
|
|
|
|
2011-05-10 13:38:10 +00:00
|
|
|
// Test for GCC >= 4.0. These implementations will automatically use
|
|
|
|
// CPU-specific instructions for unaligned data when they are available (eg.
|
|
|
|
// MIPS). See also this email thread on scummvm-devel for details:
|
|
|
|
// <http://thread.gmane.org/gmane.games.devel.scummvm/8063>
|
|
|
|
//
|
|
|
|
// Moreover, we activate this code for GCC >= 3.3 but *only* if unaligned access
|
|
|
|
// is allowed.
|
2012-03-15 09:16:45 +00:00
|
|
|
#if GCC_ATLEAST(4, 0) || (GCC_ATLEAST(3, 3) && !defined(SCUMM_NEED_ALIGNMENT))
|
2009-09-11 08:55:47 +00:00
|
|
|
|
|
|
|
FORCEINLINE uint16 READ_UINT16(const void *ptr) {
|
2011-05-10 13:38:10 +00:00
|
|
|
struct Unaligned16 { uint16 val; } __attribute__ ((__packed__, __may_alias__));
|
|
|
|
return ((const Unaligned16 *)ptr)->val;
|
2009-09-11 08:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FORCEINLINE uint32 READ_UINT32(const void *ptr) {
|
2011-05-10 13:38:10 +00:00
|
|
|
struct Unaligned32 { uint32 val; } __attribute__ ((__packed__, __may_alias__));
|
|
|
|
return ((const Unaligned32 *)ptr)->val;
|
2009-09-11 08:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FORCEINLINE void WRITE_UINT16(void *ptr, uint16 value) {
|
2011-05-10 13:38:10 +00:00
|
|
|
struct Unaligned16 { uint16 val; } __attribute__ ((__packed__, __may_alias__));
|
|
|
|
((Unaligned16 *)ptr)->val = value;
|
2009-09-11 08:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FORCEINLINE void WRITE_UINT32(void *ptr, uint32 value) {
|
2011-05-10 13:38:10 +00:00
|
|
|
struct Unaligned32 { uint32 val; } __attribute__ ((__packed__, __may_alias__));
|
|
|
|
((Unaligned32 *)ptr)->val = value;
|
2009-09-11 08:55:47 +00:00
|
|
|
}
|
2005-06-23 14:15:49 +00:00
|
|
|
|
2015-01-04 19:41:50 +00:00
|
|
|
FORCEINLINE uint64 READ_UINT64(const void *ptr) {
|
|
|
|
struct Unaligned64 { uint64 val; } __attribute__ ((__packed__, __may_alias__));
|
|
|
|
return ((const Unaligned64 *)ptr)->val;
|
|
|
|
}
|
|
|
|
|
2014-09-16 18:49:27 +00:00
|
|
|
FORCEINLINE void WRITE_UINT64(void *ptr, uint64 value) {
|
|
|
|
struct Unaligned64 { uint64 val; } __attribute__((__packed__, __may_alias__));
|
|
|
|
((Unaligned64 *)ptr)->val = value;
|
|
|
|
}
|
|
|
|
|
2011-05-10 13:38:10 +00:00
|
|
|
#elif !defined(SCUMM_NEED_ALIGNMENT)
|
2006-02-25 11:45:56 +00:00
|
|
|
|
2009-09-11 08:55:47 +00:00
|
|
|
FORCEINLINE uint16 READ_UINT16(const void *ptr) {
|
2011-05-10 13:38:10 +00:00
|
|
|
return *(const uint16 *)(ptr);
|
2009-09-11 08:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FORCEINLINE uint32 READ_UINT32(const void *ptr) {
|
2011-05-10 13:38:10 +00:00
|
|
|
return *(const uint32 *)(ptr);
|
2009-09-11 08:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FORCEINLINE void WRITE_UINT16(void *ptr, uint16 value) {
|
2011-05-10 13:38:10 +00:00
|
|
|
*(uint16 *)(ptr) = value;
|
2009-09-11 08:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FORCEINLINE void WRITE_UINT32(void *ptr, uint32 value) {
|
2011-05-10 13:38:10 +00:00
|
|
|
*(uint32 *)(ptr) = value;
|
2009-09-11 08:55:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-04 19:41:50 +00:00
|
|
|
FORCEINLINE uint64 READ_UINT64(const void *ptr) {
|
|
|
|
return *(const uint64 *)(ptr);
|
|
|
|
}
|
|
|
|
|
2014-09-16 18:49:27 +00:00
|
|
|
FORCEINLINE void WRITE_UINT64(void *ptr, uint64 value) {
|
|
|
|
*(uint64 *)(ptr) = value;
|
|
|
|
}
|
|
|
|
|
2011-05-10 13:38:10 +00:00
|
|
|
|
2009-09-11 08:55:47 +00:00
|
|
|
// use software fallback by loading each byte explicitely
|
|
|
|
#else
|
|
|
|
|
|
|
|
# if defined(SCUMM_LITTLE_ENDIAN)
|
|
|
|
|
|
|
|
inline uint16 READ_UINT16(const void *ptr) {
|
|
|
|
const uint8 *b = (const uint8 *)ptr;
|
|
|
|
return (b[1] << 8) | b[0];
|
|
|
|
}
|
|
|
|
inline uint32 READ_UINT32(const void *ptr) {
|
|
|
|
const uint8 *b = (const uint8 *)ptr;
|
|
|
|
return (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | (b[0]);
|
|
|
|
}
|
|
|
|
inline void WRITE_UINT16(void *ptr, uint16 value) {
|
|
|
|
uint8 *b = (uint8 *)ptr;
|
|
|
|
b[0] = (uint8)(value >> 0);
|
|
|
|
b[1] = (uint8)(value >> 8);
|
|
|
|
}
|
|
|
|
inline void WRITE_UINT32(void *ptr, uint32 value) {
|
|
|
|
uint8 *b = (uint8 *)ptr;
|
|
|
|
b[0] = (uint8)(value >> 0);
|
|
|
|
b[1] = (uint8)(value >> 8);
|
|
|
|
b[2] = (uint8)(value >> 16);
|
|
|
|
b[3] = (uint8)(value >> 24);
|
|
|
|
}
|
2015-01-04 19:41:50 +00:00
|
|
|
inline uint64 READ_UINT64(const void *ptr) {
|
|
|
|
const uint8 *b = (const uint8 *)ptr;
|
2015-01-14 22:20:33 +00:00
|
|
|
return ((uint64)b[7] << 56) | ((uint64)b[6] << 48) | ((uint64)b[5] << 40) | ((uint64)b[4] << 32) | ((uint64)b[3] << 24) | ((uint64)b[2] << 16) | ((uint64)b[1] << 8) | ((uint64)b[0]);
|
2015-01-04 19:41:50 +00:00
|
|
|
}
|
2014-09-16 18:49:27 +00:00
|
|
|
inline void WRITE_UINT64(void *ptr, uint64 value) {
|
|
|
|
uint8 *b = (uint8 *)ptr;
|
|
|
|
b[0] = (uint8)(value >> 0);
|
|
|
|
b[1] = (uint8)(value >> 8);
|
|
|
|
b[2] = (uint8)(value >> 16);
|
|
|
|
b[3] = (uint8)(value >> 24);
|
|
|
|
b[4] = (uint8)(value >> 32);
|
|
|
|
b[5] = (uint8)(value >> 40);
|
|
|
|
b[6] = (uint8)(value >> 48);
|
|
|
|
b[7] = (uint8)(value >> 56);
|
|
|
|
}
|
2009-09-11 08:55:47 +00:00
|
|
|
|
|
|
|
# elif defined(SCUMM_BIG_ENDIAN)
|
|
|
|
|
|
|
|
inline uint16 READ_UINT16(const void *ptr) {
|
|
|
|
const uint8 *b = (const uint8 *)ptr;
|
|
|
|
return (b[0] << 8) | b[1];
|
|
|
|
}
|
|
|
|
inline uint32 READ_UINT32(const void *ptr) {
|
|
|
|
const uint8 *b = (const uint8 *)ptr;
|
|
|
|
return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | (b[3]);
|
|
|
|
}
|
|
|
|
inline void WRITE_UINT16(void *ptr, uint16 value) {
|
|
|
|
uint8 *b = (uint8 *)ptr;
|
|
|
|
b[0] = (uint8)(value >> 8);
|
|
|
|
b[1] = (uint8)(value >> 0);
|
|
|
|
}
|
|
|
|
inline void WRITE_UINT32(void *ptr, uint32 value) {
|
|
|
|
uint8 *b = (uint8 *)ptr;
|
|
|
|
b[0] = (uint8)(value >> 24);
|
|
|
|
b[1] = (uint8)(value >> 16);
|
|
|
|
b[2] = (uint8)(value >> 8);
|
|
|
|
b[3] = (uint8)(value >> 0);
|
|
|
|
}
|
2015-01-04 19:41:50 +00:00
|
|
|
inline uint64 READ_UINT64(const void *ptr) {
|
|
|
|
const uint8 *b = (const uint8 *)ptr;
|
2015-01-14 22:20:33 +00:00
|
|
|
return ((uint64)b[0] << 56) | ((uint64)b[1] << 48) | ((uint64)b[2] << 40) | ((uint64)b[3] << 32) | ((uint64)b[4] << 24) | ((uint64)b[5] << 16) | ((uint64)b[6] << 8) | ((uint64)b[7]);
|
2015-01-04 19:41:50 +00:00
|
|
|
}
|
2014-09-16 18:49:27 +00:00
|
|
|
inline void WRITE_UINT64(void *ptr, uint64 value) {
|
|
|
|
uint8 *b = (uint8 *)ptr;
|
|
|
|
b[0] = (uint8)(value >> 56);
|
|
|
|
b[1] = (uint8)(value >> 48);
|
|
|
|
b[2] = (uint8)(value >> 40);
|
|
|
|
b[3] = (uint8)(value >> 32);
|
|
|
|
b[4] = (uint8)(value >> 24);
|
|
|
|
b[5] = (uint8)(value >> 16);
|
|
|
|
b[6] = (uint8)(value >> 8);
|
|
|
|
b[7] = (uint8)(value >> 0);
|
|
|
|
}
|
2009-09-11 08:55:47 +00:00
|
|
|
|
|
|
|
# endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// Map Funtions for reading/writing BE/LE integers depending on native endianess
|
2006-02-25 11:45:56 +00:00
|
|
|
#if defined(SCUMM_LITTLE_ENDIAN)
|
2005-06-23 14:15:49 +00:00
|
|
|
|
2009-09-11 08:55:47 +00:00
|
|
|
#define READ_LE_UINT16(a) READ_UINT16(a)
|
|
|
|
#define READ_LE_UINT32(a) READ_UINT32(a)
|
2005-06-23 14:15:49 +00:00
|
|
|
|
2009-09-11 08:55:47 +00:00
|
|
|
#define WRITE_LE_UINT16(a, v) WRITE_UINT16(a, v)
|
|
|
|
#define WRITE_LE_UINT32(a, v) WRITE_UINT32(a, v)
|
2005-10-11 13:40:29 +00:00
|
|
|
|
2005-06-23 14:15:49 +00:00
|
|
|
#define FROM_LE_32(a) ((uint32)(a))
|
|
|
|
#define FROM_LE_16(a) ((uint16)(a))
|
|
|
|
|
2006-11-12 19:05:51 +00:00
|
|
|
#define FROM_BE_32(a) SWAP_BYTES_32(a)
|
|
|
|
#define FROM_BE_16(a) SWAP_BYTES_16(a)
|
|
|
|
|
2005-06-23 14:15:49 +00:00
|
|
|
#define TO_LE_32(a) ((uint32)(a))
|
|
|
|
#define TO_LE_16(a) ((uint16)(a))
|
|
|
|
|
|
|
|
#define TO_BE_32(a) SWAP_BYTES_32(a)
|
|
|
|
#define TO_BE_16(a) SWAP_BYTES_16(a)
|
|
|
|
|
2009-09-11 08:55:47 +00:00
|
|
|
#define CONSTANT_LE_32(a) ((uint32)(a))
|
|
|
|
#define CONSTANT_LE_16(a) ((uint16)(a))
|
|
|
|
|
|
|
|
#define CONSTANT_BE_32(a) SWAP_CONSTANT_32(a)
|
|
|
|
#define CONSTANT_BE_16(a) SWAP_CONSTANT_16(a)
|
|
|
|
|
2015-01-04 19:41:50 +00:00
|
|
|
#define READ_LE_UINT64(a) READ_UINT64(a)
|
|
|
|
#define WRITE_LE_UINT64(a, v) WRITE_UINT64(a, v)
|
|
|
|
#define FROM_LE_64(a) ((uint64)(a))
|
|
|
|
#define FROM_BE_64(a) SWAP_BYTES_64(a)
|
|
|
|
#define TO_LE_64(a) ((uint64)(a))
|
|
|
|
#define TO_BE_64(a) SWAP_BYTES_64(a)
|
|
|
|
#define CONSTANT_LE_64(a) ((uint64)(a))
|
|
|
|
#define CONSTANT_BE_64(a) SWAP_CONSTANT_64(a)
|
|
|
|
|
2009-09-11 08:55:47 +00:00
|
|
|
// if the unaligned load and the byteswap take alot instructions its better to directly read and invert
|
|
|
|
# if defined(SCUMM_NEED_ALIGNMENT) && !defined(__mips__)
|
|
|
|
|
|
|
|
inline uint16 READ_BE_UINT16(const void *ptr) {
|
|
|
|
const uint8 *b = (const uint8 *)ptr;
|
|
|
|
return (b[0] << 8) | b[1];
|
|
|
|
}
|
|
|
|
inline uint32 READ_BE_UINT32(const void *ptr) {
|
|
|
|
const uint8 *b = (const uint8 *)ptr;
|
|
|
|
return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | (b[3]);
|
|
|
|
}
|
|
|
|
inline void WRITE_BE_UINT16(void *ptr, uint16 value) {
|
|
|
|
uint8 *b = (uint8 *)ptr;
|
|
|
|
b[0] = (uint8)(value >> 8);
|
|
|
|
b[1] = (uint8)(value >> 0);
|
|
|
|
}
|
|
|
|
inline void WRITE_BE_UINT32(void *ptr, uint32 value) {
|
|
|
|
uint8 *b = (uint8 *)ptr;
|
|
|
|
b[0] = (uint8)(value >> 24);
|
|
|
|
b[1] = (uint8)(value >> 16);
|
|
|
|
b[2] = (uint8)(value >> 8);
|
|
|
|
b[3] = (uint8)(value >> 0);
|
|
|
|
}
|
2015-01-04 19:41:50 +00:00
|
|
|
inline uint64 READ_BE_UINT64(const void *ptr) {
|
|
|
|
const uint8 *b = (const uint8 *)ptr;
|
2015-01-18 17:50:53 +00:00
|
|
|
return ((uint64)b[0] << 56) | ((uint64)b[1] << 48) | ((uint64)b[2] << 40) | ((uint64)b[3] << 32) | ((uint64)b[4] << 24) | ((uint64)b[5] << 16) | ((uint64)b[6] << 8) | ((uint64)b[7]);
|
2015-01-04 19:41:50 +00:00
|
|
|
}
|
2014-09-16 18:49:27 +00:00
|
|
|
inline void WRITE_BE_UINT64(void *ptr, uint64 value) {
|
|
|
|
uint8 *b = (uint8 *)ptr;
|
|
|
|
b[0] = (uint8)(value >> 56);
|
|
|
|
b[1] = (uint8)(value >> 48);
|
|
|
|
b[2] = (uint8)(value >> 40);
|
|
|
|
b[3] = (uint8)(value >> 32);
|
|
|
|
b[4] = (uint8)(value >> 24);
|
|
|
|
b[5] = (uint8)(value >> 16);
|
|
|
|
b[6] = (uint8)(value >> 8);
|
|
|
|
b[7] = (uint8)(value >> 0);
|
|
|
|
}
|
2015-01-04 19:41:50 +00:00
|
|
|
|
2009-09-11 08:55:47 +00:00
|
|
|
# else
|
|
|
|
|
|
|
|
inline uint16 READ_BE_UINT16(const void *ptr) {
|
|
|
|
return SWAP_BYTES_16(READ_UINT16(ptr));
|
|
|
|
}
|
|
|
|
inline uint32 READ_BE_UINT32(const void *ptr) {
|
|
|
|
return SWAP_BYTES_32(READ_UINT32(ptr));
|
|
|
|
}
|
|
|
|
inline void WRITE_BE_UINT16(void *ptr, uint16 value) {
|
|
|
|
WRITE_UINT16(ptr, SWAP_BYTES_16(value));
|
|
|
|
}
|
|
|
|
inline void WRITE_BE_UINT32(void *ptr, uint32 value) {
|
|
|
|
WRITE_UINT32(ptr, SWAP_BYTES_32(value));
|
|
|
|
}
|
2015-01-04 20:30:05 +00:00
|
|
|
inline uint64 READ_BE_UINT64(const void *ptr) {
|
2015-01-04 19:41:50 +00:00
|
|
|
return SWAP_BYTES_64(READ_UINT64(ptr));
|
|
|
|
}
|
2014-09-16 18:49:27 +00:00
|
|
|
inline void WRITE_BE_UINT64(void *ptr, uint64 value) {
|
|
|
|
WRITE_UINT64(ptr, SWAP_BYTES_64(value));
|
|
|
|
}
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2009-09-11 08:55:47 +00:00
|
|
|
# endif // if defined(SCUMM_NEED_ALIGNMENT)
|
|
|
|
|
2005-06-23 14:15:49 +00:00
|
|
|
#elif defined(SCUMM_BIG_ENDIAN)
|
|
|
|
|
2009-09-11 08:55:47 +00:00
|
|
|
#define READ_BE_UINT16(a) READ_UINT16(a)
|
|
|
|
#define READ_BE_UINT32(a) READ_UINT32(a)
|
2005-06-23 14:15:49 +00:00
|
|
|
|
2009-09-11 08:55:47 +00:00
|
|
|
#define WRITE_BE_UINT16(a, v) WRITE_UINT16(a, v)
|
|
|
|
#define WRITE_BE_UINT32(a, v) WRITE_UINT32(a, v)
|
2005-10-11 13:40:29 +00:00
|
|
|
|
2005-06-23 14:15:49 +00:00
|
|
|
#define FROM_LE_32(a) SWAP_BYTES_32(a)
|
|
|
|
#define FROM_LE_16(a) SWAP_BYTES_16(a)
|
|
|
|
|
2006-11-12 19:05:51 +00:00
|
|
|
#define FROM_BE_32(a) ((uint32)(a))
|
|
|
|
#define FROM_BE_16(a) ((uint16)(a))
|
|
|
|
|
2005-06-23 14:15:49 +00:00
|
|
|
#define TO_LE_32(a) SWAP_BYTES_32(a)
|
|
|
|
#define TO_LE_16(a) SWAP_BYTES_16(a)
|
|
|
|
|
|
|
|
#define TO_BE_32(a) ((uint32)(a))
|
|
|
|
#define TO_BE_16(a) ((uint16)(a))
|
|
|
|
|
2009-09-11 08:55:47 +00:00
|
|
|
#define CONSTANT_LE_32(a) SWAP_CONSTANT_32(a)
|
|
|
|
#define CONSTANT_LE_16(a) SWAP_CONSTANT_16(a)
|
2009-08-24 13:08:21 +00:00
|
|
|
|
2009-09-11 08:55:47 +00:00
|
|
|
#define CONSTANT_BE_32(a) ((uint32)(a))
|
|
|
|
#define CONSTANT_BE_16(a) ((uint16)(a))
|
2009-08-24 13:08:21 +00:00
|
|
|
|
2015-01-04 19:41:50 +00:00
|
|
|
#define READ_BE_UINT64(a) READ_UINT64(a)
|
|
|
|
#define WRITE_BE_UINT64(a, v) WRITE_UINT64(a, v)
|
|
|
|
#define FROM_LE_64(a) SWAP_BYTES_64(a)
|
|
|
|
#define FROM_BE_64(a) ((uint64)(a))
|
|
|
|
#define TO_LE_64(a) SWAP_BYTES_64(a)
|
|
|
|
#define TO_BE_64(a) ((uint64)(a))
|
|
|
|
#define CONSTANT_LE_64(a) SWAP_CONSTANT_64(a)
|
|
|
|
#define CONSTANT_BE_64(a) ((uint64)(a))
|
|
|
|
|
2009-09-11 08:55:47 +00:00
|
|
|
// if the unaligned load and the byteswap take alot instructions its better to directly read and invert
|
|
|
|
# if defined(SCUMM_NEED_ALIGNMENT) && !defined(__mips__)
|
2005-06-23 14:15:49 +00:00
|
|
|
|
2009-09-11 08:55:47 +00:00
|
|
|
inline uint16 READ_LE_UINT16(const void *ptr) {
|
|
|
|
const uint8 *b = (const uint8 *)ptr;
|
|
|
|
return (b[1] << 8) | b[0];
|
2005-06-23 14:15:49 +00:00
|
|
|
}
|
2009-09-11 08:55:47 +00:00
|
|
|
inline uint32 READ_LE_UINT32(const void *ptr) {
|
|
|
|
const uint8 *b = (const uint8 *)ptr;
|
|
|
|
return (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | (b[0]);
|
2005-06-23 14:15:49 +00:00
|
|
|
}
|
2009-09-11 08:55:47 +00:00
|
|
|
inline void WRITE_LE_UINT16(void *ptr, uint16 value) {
|
|
|
|
uint8 *b = (uint8 *)ptr;
|
|
|
|
b[0] = (uint8)(value >> 0);
|
|
|
|
b[1] = (uint8)(value >> 8);
|
2005-06-23 14:15:49 +00:00
|
|
|
}
|
2009-09-11 08:55:47 +00:00
|
|
|
inline void WRITE_LE_UINT32(void *ptr, uint32 value) {
|
|
|
|
uint8 *b = (uint8 *)ptr;
|
|
|
|
b[0] = (uint8)(value >> 0);
|
|
|
|
b[1] = (uint8)(value >> 8);
|
|
|
|
b[2] = (uint8)(value >> 16);
|
|
|
|
b[3] = (uint8)(value >> 24);
|
2005-06-23 14:15:49 +00:00
|
|
|
}
|
2015-01-04 19:41:50 +00:00
|
|
|
|
|
|
|
inline uint64 READ_LE_UINT64(const void *ptr) {
|
|
|
|
const uint8 *b = (const uint8 *)ptr;
|
2015-01-18 17:50:53 +00:00
|
|
|
return ((uint64)b[7] << 56) | ((uint64)b[6] << 48) | ((uint64)b[5] << 40) | ((uint64)b[4] << 32) | ((uint64)b[3] << 24) | ((uint64)b[2] << 16) | ((uint64)b[1] << 8) | ((uint64)b[0]);
|
2015-01-04 19:41:50 +00:00
|
|
|
}
|
2014-09-16 18:49:27 +00:00
|
|
|
inline void WRITE_LE_UINT64(void *ptr, uint64 value) {
|
|
|
|
uint8 *b = (uint8 *)ptr;
|
|
|
|
b[0] = (uint8)(value >> 0);
|
|
|
|
b[1] = (uint8)(value >> 8);
|
|
|
|
b[2] = (uint8)(value >> 16);
|
|
|
|
b[3] = (uint8)(value >> 24);
|
|
|
|
b[4] = (uint8)(value >> 32);
|
|
|
|
b[5] = (uint8)(value >> 40);
|
|
|
|
b[6] = (uint8)(value >> 48);
|
|
|
|
b[7] = (uint8)(value >> 56);
|
|
|
|
}
|
2015-01-04 19:41:50 +00:00
|
|
|
|
2009-09-11 08:55:47 +00:00
|
|
|
# else
|
2005-06-23 14:15:49 +00:00
|
|
|
|
2009-09-11 08:55:47 +00:00
|
|
|
inline uint16 READ_LE_UINT16(const void *ptr) {
|
|
|
|
return SWAP_BYTES_16(READ_UINT16(ptr));
|
2009-08-24 13:08:21 +00:00
|
|
|
}
|
2009-09-11 08:55:47 +00:00
|
|
|
inline uint32 READ_LE_UINT32(const void *ptr) {
|
|
|
|
return SWAP_BYTES_32(READ_UINT32(ptr));
|
2009-08-24 13:08:21 +00:00
|
|
|
}
|
2009-09-11 08:55:47 +00:00
|
|
|
inline void WRITE_LE_UINT16(void *ptr, uint16 value) {
|
|
|
|
WRITE_UINT16(ptr, SWAP_BYTES_16(value));
|
2009-08-24 13:08:21 +00:00
|
|
|
}
|
2009-09-11 08:55:47 +00:00
|
|
|
inline void WRITE_LE_UINT32(void *ptr, uint32 value) {
|
|
|
|
WRITE_UINT32(ptr, SWAP_BYTES_32(value));
|
2009-08-24 13:08:21 +00:00
|
|
|
}
|
2015-01-04 19:41:50 +00:00
|
|
|
inline uint64 READ_LE_UINT64(const void *ptr) {
|
|
|
|
return SWAP_BYTES_64(READ_UINT64(ptr));
|
|
|
|
}
|
2014-09-16 18:49:27 +00:00
|
|
|
inline void WRITE_LE_UINT64(void *ptr, uint64 value) {
|
|
|
|
WRITE_UINT64(ptr, SWAP_BYTES_64(value));
|
|
|
|
}
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2009-09-11 08:55:47 +00:00
|
|
|
# endif // if defined(SCUMM_NEED_ALIGNMENT)
|
2005-06-23 14:15:49 +00:00
|
|
|
|
2009-09-11 08:55:47 +00:00
|
|
|
#endif // if defined(SCUMM_LITTLE_ENDIAN)
|
2005-06-23 14:15:49 +00:00
|
|
|
|
2009-09-11 08:55:47 +00:00
|
|
|
inline uint32 READ_LE_UINT24(const void *ptr) {
|
|
|
|
const uint8 *b = (const uint8 *)ptr;
|
|
|
|
return (b[2] << 16) | (b[1] << 8) | (b[0]);
|
2005-06-23 14:15:49 +00:00
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2019-03-01 00:10:08 +00:00
|
|
|
inline void WRITE_LE_UINT24(void *ptr, uint32 value) {
|
|
|
|
uint8 *b = (uint8 *)ptr;
|
|
|
|
b[0] = (uint8)(value >> 0);
|
|
|
|
b[1] = (uint8)(value >> 8);
|
|
|
|
b[2] = (uint8)(value >> 16);
|
|
|
|
}
|
|
|
|
|
2009-09-11 08:55:47 +00:00
|
|
|
inline uint32 READ_BE_UINT24(const void *ptr) {
|
|
|
|
const uint8 *b = (const uint8 *)ptr;
|
|
|
|
return (b[0] << 16) | (b[1] << 8) | (b[2]);
|
|
|
|
}
|
2009-08-24 13:08:21 +00:00
|
|
|
|
2019-03-01 00:10:08 +00:00
|
|
|
inline void WRITE_BE_UINT24(void *ptr, uint32 value) {
|
|
|
|
uint8 *b = (uint8 *)ptr;
|
|
|
|
b[0] = (uint8)(value >> 16);
|
|
|
|
b[1] = (uint8)(value >> 8);
|
|
|
|
b[2] = (uint8)(value >> 0);
|
|
|
|
}
|
|
|
|
|
2012-03-08 02:58:08 +00:00
|
|
|
#ifdef SCUMM_LITTLE_ENDIAN
|
|
|
|
#define READ_UINT24(a) READ_LE_UINT24(a)
|
2019-03-01 00:10:08 +00:00
|
|
|
#define WRITE_UINT24(a,b) WRITE_LE_UINT24(a,b)
|
2012-03-08 02:58:08 +00:00
|
|
|
#else
|
|
|
|
#define READ_UINT24(a) READ_BE_UINT24(a)
|
2019-03-01 00:10:08 +00:00
|
|
|
#define WRITE_UINT24(a,b) WRITE_BE_UINT24(a,b)
|
2012-03-08 02:58:08 +00:00
|
|
|
#endif
|
|
|
|
|
2017-07-30 16:26:04 +00:00
|
|
|
inline int16 READ_LE_INT16(const void *ptr) {
|
|
|
|
return static_cast<int16>(READ_LE_UINT16(ptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void WRITE_LE_INT16(void *ptr, int16 value) {
|
|
|
|
WRITE_LE_UINT16(ptr, static_cast<uint16>(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int16 READ_BE_INT16(const void *ptr) {
|
|
|
|
return static_cast<int16>(READ_BE_UINT16(ptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void WRITE_BE_INT16(void *ptr, int16 value) {
|
|
|
|
WRITE_BE_UINT16(ptr, static_cast<uint16>(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int32 READ_LE_INT32(const void *ptr) {
|
|
|
|
return static_cast<int32>(READ_LE_UINT32(ptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void WRITE_LE_INT32(void *ptr, int32 value) {
|
|
|
|
WRITE_LE_UINT32(ptr, static_cast<uint32>(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int32 READ_BE_INT32(const void *ptr) {
|
|
|
|
return static_cast<int32>(READ_BE_UINT32(ptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void WRITE_BE_INT32(void *ptr, int32 value) {
|
|
|
|
WRITE_BE_UINT32(ptr, static_cast<uint32>(value));
|
|
|
|
}
|
|
|
|
|
2005-06-23 14:15:49 +00:00
|
|
|
#endif
|