You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.4KB

  1. #ifndef INTREADWRITE_H
  2. #define INTREADWRITE_H
  3. #ifdef __GNUC__
  4. struct unaligned_64 { uint64_t l; } __attribute__((packed));
  5. struct unaligned_32 { uint32_t l; } __attribute__((packed));
  6. struct unaligned_16 { uint16_t l; } __attribute__((packed));
  7. #define LD16(a) (((const struct unaligned_16 *) (a))->l)
  8. #define LD32(a) (((const struct unaligned_32 *) (a))->l)
  9. #define LD64(a) (((const struct unaligned_64 *) (a))->l)
  10. #define ST16(a, b) (((struct unaligned_16 *) (a))->l) = (b)
  11. #define ST32(a, b) (((struct unaligned_32 *) (a))->l) = (b)
  12. #else /* __GNUC__ */
  13. #define LD16(a) (*((uint16_t*)(a)))
  14. #define LD32(a) (*((uint32_t*)(a)))
  15. #define LD64(a) (*((uint64_t*)(a)))
  16. #define ST16(a, b) *((uint16_t*)(a)) = (b)
  17. #define ST32(a, b) *((uint32_t*)(a)) = (b)
  18. #endif /* !__GNUC__ */
  19. /* endian macros */
  20. #if !defined(BE_16) || !defined(BE_32) || !defined(LE_16) || !defined(LE_32)
  21. #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
  22. #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
  23. (((uint8_t*)(x))[1] << 16) | \
  24. (((uint8_t*)(x))[2] << 8) | \
  25. ((uint8_t*)(x))[3])
  26. #define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
  27. #define LE_32(x) ((((uint8_t*)(x))[3] << 24) | \
  28. (((uint8_t*)(x))[2] << 16) | \
  29. (((uint8_t*)(x))[1] << 8) | \
  30. ((uint8_t*)(x))[0])
  31. #endif
  32. #endif /* INTREADWRITE_H */