CpuArch.h 2.0 KB

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