1 /* CpuArch.h 2 2008-08-05 3 Igor Pavlov 4 Public domain */ 5 6 #ifndef __CPUARCH_H 7 #define __CPUARCH_H 8 9 /* 10 LITTLE_ENDIAN_UNALIGN means: 11 1) CPU is LITTLE_ENDIAN 12 2) it's allowed to make unaligned memory accesses 13 if LITTLE_ENDIAN_UNALIGN is not defined, it means that we don't know 14 about these properties of platform. 15 */ 16 17 #if defined(_M_IX86) || defined(_M_X64) || defined(_M_AMD64) || defined(__i386__) || defined(__x86_64__) 18 #define LITTLE_ENDIAN_UNALIGN 19 #endif 20 21 #ifdef LITTLE_ENDIAN_UNALIGN 22 23 #define GetUi16(p) (*(const UInt16 *)(p)) 24 #define GetUi32(p) (*(const UInt32 *)(p)) 25 #define GetUi64(p) (*(const UInt64 *)(p)) 26 #define SetUi32(p, d) *(UInt32 *)(p) = (d); 27 28 #else 29 30 #define GetUi16(p) (((const Byte *)(p))[0] | ((UInt16)((const Byte *)(p))[1] << 8)) 31 32 #define GetUi32(p) ( \ 33 ((const Byte *)(p))[0] | \ 34 ((UInt32)((const Byte *)(p))[1] << 8) | \ 35 ((UInt32)((const Byte *)(p))[2] << 16) | \ 36 ((UInt32)((const Byte *)(p))[3] << 24)) 37 38 #define GetUi64(p) (GetUi32(p) | ((UInt64)GetUi32(((const Byte *)(p)) + 4) << 32)) 39 40 #define SetUi32(p, d) { UInt32 _x_ = (d); \ 41 ((Byte *)(p))[0] = (Byte)_x_; \ 42 ((Byte *)(p))[1] = (Byte)(_x_ >> 8); \ 43 ((Byte *)(p))[2] = (Byte)(_x_ >> 16); \ 44 ((Byte *)(p))[3] = (Byte)(_x_ >> 24); } 45 46 #endif 47 48 #if defined(LITTLE_ENDIAN_UNALIGN) && defined(_WIN64) && (_MSC_VER >= 1300) 49 50 #pragma intrinsic(_byteswap_ulong) 51 #pragma intrinsic(_byteswap_uint64) 52 #define GetBe32(p) _byteswap_ulong(*(const UInt32 *)(const Byte *)(p)) 53 #define GetBe64(p) _byteswap_uint64(*(const UInt64 *)(const Byte *)(p)) 54 55 #else 56 57 #define GetBe32(p) ( \ 58 ((UInt32)((const Byte *)(p))[0] << 24) | \ 59 ((UInt32)((const Byte *)(p))[1] << 16) | \ 60 ((UInt32)((const Byte *)(p))[2] << 8) | \ 61 ((const Byte *)(p))[3] ) 62 63 #define GetBe64(p) (((UInt64)GetBe32(p) << 32) | GetBe32(((const Byte *)(p)) + 4)) 64 65 #endif 66 67 #define GetBe16(p) (((UInt16)((const Byte *)(p))[0] << 8) | ((const Byte *)(p))[1]) 68 69 #endif 70