endian.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #pragma once
  2. #include <stdint.h>
  3. #include "compiler.h"
  4. #if defined(__MINGW32__) || defined(_WIN32)
  5. # define LITTLE_ENDIAN 1234
  6. # define BYTE_ORDER LITTLE_ENDIAN
  7. #elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__)
  8. # include <sys/endian.h>
  9. #elif defined(__OpenBSD__) || defined(__SVR4)
  10. # include <sys/types.h>
  11. #elif defined(__APPLE__)
  12. # include <machine/endian.h>
  13. #elif defined( BSD ) && (BSD >= 199103)
  14. # include <machine/endian.h>
  15. #elif defined( __QNXNTO__ ) && defined( __LITTLEENDIAN__ )
  16. # define LITTLE_ENDIAN 1234
  17. # define BYTE_ORDER LITTLE_ENDIAN
  18. #elif defined( __QNXNTO__ ) && defined( __BIGENDIAN__ )
  19. # define BIG_ENDIAN 1234
  20. # define BYTE_ORDER BIG_ENDIAN
  21. #else
  22. # include <endian.h>
  23. #endif
  24. #if defined(_WIN32)
  25. #include <stdlib.h>
  26. #define ethash_swap_u32(input_) _byteswap_ulong(input_)
  27. #define ethash_swap_u64(input_) _byteswap_uint64(input_)
  28. #elif defined(__APPLE__)
  29. #include <libkern/OSByteOrder.h>
  30. #define ethash_swap_u32(input_) OSSwapInt32(input_)
  31. #define ethash_swap_u64(input_) OSSwapInt64(input_)
  32. #elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__)
  33. #define ethash_swap_u32(input_) bswap32(input_)
  34. #define ethash_swap_u64(input_) bswap64(input_)
  35. #else // posix
  36. #include <byteswap.h>
  37. #define ethash_swap_u32(input_) __bswap_32(input_)
  38. #define ethash_swap_u64(input_) __bswap_64(input_)
  39. #endif
  40. #if LITTLE_ENDIAN == BYTE_ORDER
  41. #define fix_endian32(dst_ ,src_) dst_ = src_
  42. #define fix_endian32_same(val_)
  43. #define fix_endian64(dst_, src_) dst_ = src_
  44. #define fix_endian64_same(val_)
  45. #define fix_endian_arr32(arr_, size_)
  46. #define fix_endian_arr64(arr_, size_)
  47. #elif BIG_ENDIAN == BYTE_ORDER
  48. #define fix_endian32(dst_, src_) dst_ = ethash_swap_u32(src_)
  49. #define fix_endian32_same(val_) val_ = ethash_swap_u32(val_)
  50. #define fix_endian64(dst_, src_) dst_ = ethash_swap_u64(src_
  51. #define fix_endian64_same(val_) val_ = ethash_swap_u64(val_)
  52. #define fix_endian_arr32(arr_, size_) \
  53. do { \
  54. for (unsigned i_ = 0; i_ < (size_), ++i_) { \
  55. arr_[i_] = ethash_swap_u32(arr_[i_]); \
  56. } \
  57. while (0)
  58. #define fix_endian_arr64(arr_, size_) \
  59. do { \
  60. for (unsigned i_ = 0; i_ < (size_), ++i_) { \
  61. arr_[i_] = ethash_swap_u64(arr_[i_]); \
  62. } \
  63. while (0) \
  64. #else
  65. # error "endian not supported"
  66. #endif // BYTE_ORDER