diff options
-rw-r--r-- | common/endian.h | 126 |
1 files changed, 77 insertions, 49 deletions
diff --git a/common/endian.h b/common/endian.h index a18e6f088b..70cc8b434f 100644 --- a/common/endian.h +++ b/common/endian.h @@ -49,7 +49,7 @@ # error No endianness defined #endif - +#ifdef HAVE_INT64 #define SWAP_CONSTANT_64(a) \ ((uint64)((((a) >> 56) & 0x000000FF) | \ (((a) >> 40) & 0x0000FF00) | \ @@ -59,6 +59,7 @@ (((a) & 0x00FF0000) << 24) | \ (((a) & 0x0000FF00) << 40) | \ (((a) & 0x000000FF) << 56) )) +#endif #define SWAP_CONSTANT_32(a) \ ((uint32)((((a) >> 24) & 0x00FF) | \ @@ -70,6 +71,7 @@ ((uint16)((((a) >> 8) & 0x00FF) | \ (((a) << 8) & 0xFF00) )) +#ifdef HAVE_INT64 /** * Swap the bytes in a 64 bit word in order to convert LE encoded data to BE * and vice versa. @@ -122,10 +124,11 @@ 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))) + (uint16)((highHigh >> 8) | (highHigh << 8))); } #endif +#endif // HAVE_INT64 /** * Swap the bytes in a 32 bit word in order to convert LE encoded data to BE * and vice versa. @@ -240,11 +243,6 @@ return ((const Unaligned32 *)ptr)->val; } - FORCEINLINE uint64 READ_UINT64(const void *ptr) { - struct Unaligned64 { uint64 val; } __attribute__ ((__packed__, __may_alias__)); - return ((const Unaligned64 *)ptr)->val; - } - FORCEINLINE void WRITE_UINT16(void *ptr, uint16 value) { struct Unaligned16 { uint16 val; } __attribute__ ((__packed__, __may_alias__)); ((Unaligned16 *)ptr)->val = value; @@ -255,10 +253,17 @@ ((Unaligned32 *)ptr)->val = value; } +#ifdef HAVE_INT64 + FORCEINLINE uint64 READ_UINT64(const void *ptr) { + struct Unaligned64 { uint64 val; } __attribute__ ((__packed__, __may_alias__)); + return ((const Unaligned64 *)ptr)->val; + } + FORCEINLINE void WRITE_UINT64(void *ptr, uint64 value) { struct Unaligned64 { uint64 val; } __attribute__((__packed__, __may_alias__)); ((Unaligned64 *)ptr)->val = value; } +#endif #elif !defined(SCUMM_NEED_ALIGNMENT) @@ -270,10 +275,6 @@ return *(const uint32 *)(ptr); } - FORCEINLINE uint64 READ_UINT64(const void *ptr) { - return *(const uint64 *)(ptr); - } - FORCEINLINE void WRITE_UINT16(void *ptr, uint16 value) { *(uint16 *)(ptr) = value; } @@ -282,9 +283,15 @@ *(uint32 *)(ptr) = value; } +#ifdef HAVE_INT64 + FORCEINLINE uint64 READ_UINT64(const void *ptr) { + return *(const uint64 *)(ptr); + } + FORCEINLINE void WRITE_UINT64(void *ptr, uint64 value) { *(uint64 *)(ptr) = value; } +#endif // use software fallback by loading each byte explicitely @@ -300,10 +307,6 @@ const uint8 *b = (const uint8 *)ptr; return (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | (b[0]); } - inline uint64 READ_UINT64(const void *ptr) { - const uint8 *b = (const uint8 *)ptr; - return (b[7] << 56) | (b[6] << 48) | (b[5] << 40) | (b[4] << 32) | (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); @@ -316,6 +319,11 @@ b[2] = (uint8)(value >> 16); b[3] = (uint8)(value >> 24); } +#ifdef HAVE_INT64 + inline uint64 READ_UINT64(const void *ptr) { + const uint8 *b = (const uint8 *)ptr; + return (b[7] << 56) | (b[6] << 48) | (b[5] << 40) | (b[4] << 32) | (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | (b[0]); + } inline void WRITE_UINT64(void *ptr, uint64 value) { uint8 *b = (uint8 *)ptr; b[0] = (uint8)(value >> 0); @@ -327,6 +335,7 @@ b[6] = (uint8)(value >> 48); b[7] = (uint8)(value >> 56); } +#endif # elif defined(SCUMM_BIG_ENDIAN) @@ -338,10 +347,6 @@ const uint8 *b = (const uint8 *)ptr; return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | (b[3]); } - inline uint64 READ_UINT64(const void *ptr) { - const uint8 *b = (const uint8 *)ptr; - return (b[0] << 56) | (b[1] << 48) | (b[2] << 40) | (b[3] << 32) | (b[4] << 24) | (b[5] << 16) | (b[6] << 8) | (b[7]); - } inline void WRITE_UINT16(void *ptr, uint16 value) { uint8 *b = (uint8 *)ptr; b[0] = (uint8)(value >> 8); @@ -354,6 +359,11 @@ b[2] = (uint8)(value >> 8); b[3] = (uint8)(value >> 0); } +#ifdef HAVE_INT64 + inline uint64 READ_UINT64(const void *ptr) { + const uint8 *b = (const uint8 *)ptr; + return (b[0] << 56) | (b[1] << 48) | (b[2] << 40) | (b[3] << 32) | (b[4] << 24) | (b[5] << 16) | (b[6] << 8) | (b[7]); + } inline void WRITE_UINT64(void *ptr, uint64 value) { uint8 *b = (uint8 *)ptr; b[0] = (uint8)(value >> 56); @@ -365,6 +375,7 @@ b[6] = (uint8)(value >> 8); b[7] = (uint8)(value >> 0); } +#endif # endif @@ -376,36 +387,39 @@ #define READ_LE_UINT16(a) READ_UINT16(a) #define READ_LE_UINT32(a) READ_UINT32(a) - #define READ_LE_UINT64(a) READ_UINT64(a) #define WRITE_LE_UINT16(a, v) WRITE_UINT16(a, v) #define WRITE_LE_UINT32(a, v) WRITE_UINT32(a, v) - #define WRITE_LE_UINT64(a, v) WRITE_UINT64(a, v) - #define FROM_LE_64(a) ((uint64)(a)) #define FROM_LE_32(a) ((uint32)(a)) #define FROM_LE_16(a) ((uint16)(a)) - #define FROM_BE_64(a) SWAP_BYTES_64(a) #define FROM_BE_32(a) SWAP_BYTES_32(a) #define FROM_BE_16(a) SWAP_BYTES_16(a) - #define TO_LE_64(a) ((uint64)(a)) #define TO_LE_32(a) ((uint32)(a)) #define TO_LE_16(a) ((uint16)(a)) - #define TO_BE_64(a) SWAP_BYTES_64(a) #define TO_BE_32(a) SWAP_BYTES_32(a) #define TO_BE_16(a) SWAP_BYTES_16(a) - #define CONSTANT_LE_64(a) ((uint64)(a)) #define CONSTANT_LE_32(a) ((uint32)(a)) #define CONSTANT_LE_16(a) ((uint16)(a)) - #define CONSTANT_BE_64(a) SWAP_CONSTANT_64(a) #define CONSTANT_BE_32(a) SWAP_CONSTANT_32(a) #define CONSTANT_BE_16(a) SWAP_CONSTANT_16(a) +#ifdef HAVE_INT64 + #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) +#endif + // if the unaligned load and the byteswap take alot instructions its better to directly read and invert # if defined(SCUMM_NEED_ALIGNMENT) && !defined(__mips__) @@ -417,10 +431,6 @@ const uint8 *b = (const uint8 *)ptr; return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | (b[3]); } - inline uint64 READ_BE_UINT64(const void *ptr) { - const uint8 *b = (const uint8 *)ptr; - return (b[0] << 56) | b[1] << 48) | b[2] << 40) | b[3] << 32) | b[4] << 24) | (b[5] << 16) | (b[6] << 8) | (b[7]); - } inline void WRITE_BE_UINT16(void *ptr, uint16 value) { uint8 *b = (uint8 *)ptr; b[0] = (uint8)(value >> 8); @@ -433,6 +443,11 @@ b[2] = (uint8)(value >> 8); b[3] = (uint8)(value >> 0); } +#ifdef HAVE_INT64 + inline uint64 READ_BE_UINT64(const void *ptr) { + const uint8 *b = (const uint8 *)ptr; + return (b[0] << 56) | b[1] << 48) | b[2] << 40) | b[3] << 32) | b[4] << 24) | (b[5] << 16) | (b[6] << 8) | (b[7]); + } inline void WRITE_BE_UINT64(void *ptr, uint64 value) { uint8 *b = (uint8 *)ptr; b[0] = (uint8)(value >> 56); @@ -444,6 +459,8 @@ b[6] = (uint8)(value >> 8); b[7] = (uint8)(value >> 0); } +#endif + # else inline uint16 READ_BE_UINT16(const void *ptr) { @@ -452,18 +469,20 @@ inline uint32 READ_BE_UINT32(const void *ptr) { return SWAP_BYTES_32(READ_UINT32(ptr)); } - inline uint32 READ_BE_UINT64(const void *ptr) { - return SWAP_BYTES_64(READ_UINT64(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)); } +#ifdef HAVE_INT64 + inline uint32 READ_BE_UINT64(const void *ptr) { + return SWAP_BYTES_64(READ_UINT64(ptr)); + } inline void WRITE_BE_UINT64(void *ptr, uint64 value) { WRITE_UINT64(ptr, SWAP_BYTES_64(value)); } +#endif # endif // if defined(SCUMM_NEED_ALIGNMENT) @@ -471,36 +490,39 @@ #define READ_BE_UINT16(a) READ_UINT16(a) #define READ_BE_UINT32(a) READ_UINT32(a) - #define READ_BE_UINT64(a) READ_UINT64(a) #define WRITE_BE_UINT16(a, v) WRITE_UINT16(a, v) #define WRITE_BE_UINT32(a, v) WRITE_UINT32(a, v) - #define WRITE_BE_UINT64(a, v) WRITE_UINT64(a, v) - #define FROM_LE_64(a) SWAP_BYTES_64(a) #define FROM_LE_32(a) SWAP_BYTES_32(a) #define FROM_LE_16(a) SWAP_BYTES_16(a) - #define FROM_BE_64(a) ((uint64)(a)) #define FROM_BE_32(a) ((uint32)(a)) #define FROM_BE_16(a) ((uint16)(a)) - #define TO_LE_64(a) SWAP_BYTES_64(a) #define TO_LE_32(a) SWAP_BYTES_32(a) #define TO_LE_16(a) SWAP_BYTES_16(a) - #define TO_BE_64(a) ((uint64)(a)) #define TO_BE_32(a) ((uint32)(a)) #define TO_BE_16(a) ((uint16)(a)) - #define CONSTANT_LE_64(a) SWAP_CONSTANT_64(a) #define CONSTANT_LE_32(a) SWAP_CONSTANT_32(a) #define CONSTANT_LE_16(a) SWAP_CONSTANT_16(a) - #define CONSTANT_BE_64(a) ((uint64)(a)) #define CONSTANT_BE_32(a) ((uint32)(a)) #define CONSTANT_BE_16(a) ((uint16)(a)) +#ifdef HAVE_INT64 + #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)) +#endif + // if the unaligned load and the byteswap take alot instructions its better to directly read and invert # if defined(SCUMM_NEED_ALIGNMENT) && !defined(__mips__) @@ -512,10 +534,6 @@ const uint8 *b = (const uint8 *)ptr; return (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | (b[0]); } - inline uint64 READ_LE_UINT64(const void *ptr) { - const uint8 *b = (const uint8 *)ptr; - return (b[7] << 56) | (b[6] << 48) | (b[5] << 40) | (b[4] << 32) | (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | (b[0]); - } inline void WRITE_LE_UINT16(void *ptr, uint16 value) { uint8 *b = (uint8 *)ptr; b[0] = (uint8)(value >> 0); @@ -528,6 +546,12 @@ b[2] = (uint8)(value >> 16); b[3] = (uint8)(value >> 24); } + +#ifdef HAVE_INT64 + inline uint64 READ_LE_UINT64(const void *ptr) { + const uint8 *b = (const uint8 *)ptr; + return (b[7] << 56) | (b[6] << 48) | (b[5] << 40) | (b[4] << 32) | (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | (b[0]); + } inline void WRITE_LE_UINT64(void *ptr, uint64 value) { uint8 *b = (uint8 *)ptr; b[0] = (uint8)(value >> 0); @@ -539,6 +563,8 @@ b[6] = (uint8)(value >> 48); b[7] = (uint8)(value >> 56); } +#endif + # else inline uint16 READ_LE_UINT16(const void *ptr) { @@ -547,18 +573,20 @@ inline uint32 READ_LE_UINT32(const void *ptr) { return SWAP_BYTES_32(READ_UINT32(ptr)); } - inline uint64 READ_LE_UINT64(const void *ptr) { - return SWAP_BYTES_64(READ_UINT64(ptr)); - } inline void WRITE_LE_UINT16(void *ptr, uint16 value) { WRITE_UINT16(ptr, SWAP_BYTES_16(value)); } inline void WRITE_LE_UINT32(void *ptr, uint32 value) { WRITE_UINT32(ptr, SWAP_BYTES_32(value)); } +#ifdef HAVE_INT64 + inline uint64 READ_LE_UINT64(const void *ptr) { + return SWAP_BYTES_64(READ_UINT64(ptr)); + } inline void WRITE_LE_UINT64(void *ptr, uint64 value) { WRITE_UINT64(ptr, SWAP_BYTES_64(value)); } +#endif # endif // if defined(SCUMM_NEED_ALIGNMENT) |