vcmi_endian.h 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. #include <SDL_endian.h>
  2. /* Reading values from memory.
  3. *
  4. * read_le_u16, read_le_u32 : read a little endian value from
  5. * memory. On big endian machines, the value will be byteswapped.
  6. */
  7. #if defined(linux) && defined(sparc)
  8. /* SPARC does not support unaligned memory access. Let gcc know when
  9. * to emit the right code. */
  10. struct unaligned_Uint16 { ui16 val __attribute__(( packed )); };
  11. struct unaligned_Uint32 { ui32 val __attribute__(( packed )); };
  12. static inline ui16 read_unaligned_u16(const void *p)
  13. {
  14. const struct unaligned_Uint16 *v = reinterpret_cast<const struct unaligned_Uint16 *>(p);
  15. return v->val;
  16. }
  17. static inline ui32 read_unaligned_u32(const void *p)
  18. {
  19. const struct unaligned_Uint32 *v = reinterpret_cast<const struct unaligned_Uint32 *>(p);
  20. return v->val;
  21. }
  22. #define read_le_u16(p) (SDL_SwapLE16(read_unaligned_u16(p)))
  23. #define read_le_u32(p) (SDL_SwapLE32(read_unaligned_u32(p)))
  24. #else
  25. #define read_le_u16(p) (SDL_SwapLE16(* reinterpret_cast<const ui16 *>(p)))
  26. #define read_le_u32(p) (SDL_SwapLE32(* reinterpret_cast<const ui32 *>(p)))
  27. #endif