sysendian.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #ifndef SYSENDIAN_H_
  2. #define SYSENDIAN_H_
  3. #include <stdint.h>
  4. /* Avoid namespace collisions with BSD <sys/endian.h>. */
  5. #define be16dec libcperciva_be16dec
  6. #define be16enc libcperciva_be16enc
  7. #define be32dec libcperciva_be32dec
  8. #define be32enc libcperciva_be32enc
  9. #define be64dec libcperciva_be64dec
  10. #define be64enc libcperciva_be64enc
  11. #define le16dec libcperciva_le16dec
  12. #define le16enc libcperciva_le16enc
  13. #define le32dec libcperciva_le32dec
  14. #define le32enc libcperciva_le32enc
  15. #define le64dec libcperciva_le64dec
  16. #define le64enc libcperciva_le64enc
  17. static inline uint16_t
  18. be16dec(const void * pp)
  19. {
  20. const uint8_t * p = (uint8_t const *)pp;
  21. return (uint16_t)((uint16_t)(p[1]) | ((uint16_t)(p[0]) << 8));
  22. }
  23. static inline void
  24. be16enc(void * pp, uint16_t x)
  25. {
  26. uint8_t * p = (uint8_t *)pp;
  27. p[1] = x & 0xff;
  28. p[0] = (x >> 8) & 0xff;
  29. }
  30. static inline uint32_t
  31. be32dec(const void * pp)
  32. {
  33. const uint8_t * p = (uint8_t const *)pp;
  34. return ((uint32_t)(p[3]) | ((uint32_t)(p[2]) << 8) |
  35. ((uint32_t)(p[1]) << 16) | ((uint32_t)(p[0]) << 24));
  36. }
  37. static inline void
  38. be32enc(void * pp, uint32_t x)
  39. {
  40. uint8_t * p = (uint8_t *)pp;
  41. p[3] = x & 0xff;
  42. p[2] = (x >> 8) & 0xff;
  43. p[1] = (x >> 16) & 0xff;
  44. p[0] = (x >> 24) & 0xff;
  45. }
  46. static inline uint64_t
  47. be64dec(const void * pp)
  48. {
  49. const uint8_t * p = (uint8_t const *)pp;
  50. return ((uint64_t)(p[7]) | ((uint64_t)(p[6]) << 8) |
  51. ((uint64_t)(p[5]) << 16) | ((uint64_t)(p[4]) << 24) |
  52. ((uint64_t)(p[3]) << 32) | ((uint64_t)(p[2]) << 40) |
  53. ((uint64_t)(p[1]) << 48) | ((uint64_t)(p[0]) << 56));
  54. }
  55. static inline void
  56. be64enc(void * pp, uint64_t x)
  57. {
  58. uint8_t * p = (uint8_t *)pp;
  59. p[7] = x & 0xff;
  60. p[6] = (x >> 8) & 0xff;
  61. p[5] = (x >> 16) & 0xff;
  62. p[4] = (x >> 24) & 0xff;
  63. p[3] = (x >> 32) & 0xff;
  64. p[2] = (x >> 40) & 0xff;
  65. p[1] = (x >> 48) & 0xff;
  66. p[0] = (x >> 56) & 0xff;
  67. }
  68. static inline uint16_t
  69. le16dec(const void * pp)
  70. {
  71. const uint8_t * p = (uint8_t const *)pp;
  72. return (uint16_t)((uint16_t)(p[0]) | ((uint16_t)(p[1]) << 8));
  73. }
  74. static inline void
  75. le16enc(void * pp, uint16_t x)
  76. {
  77. uint8_t * p = (uint8_t *)pp;
  78. p[0] = x & 0xff;
  79. p[1] = (x >> 8) & 0xff;
  80. }
  81. static inline uint32_t
  82. le32dec(const void * pp)
  83. {
  84. const uint8_t * p = (uint8_t const *)pp;
  85. return ((uint32_t)(p[0]) | ((uint32_t)(p[1]) << 8) |
  86. ((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24));
  87. }
  88. static inline void
  89. le32enc(void * pp, uint32_t x)
  90. {
  91. uint8_t * p = (uint8_t *)pp;
  92. p[0] = x & 0xff;
  93. p[1] = (x >> 8) & 0xff;
  94. p[2] = (x >> 16) & 0xff;
  95. p[3] = (x >> 24) & 0xff;
  96. }
  97. static inline uint64_t
  98. le64dec(const void * pp)
  99. {
  100. const uint8_t * p = (uint8_t const *)pp;
  101. return ((uint64_t)(p[0]) | ((uint64_t)(p[1]) << 8) |
  102. ((uint64_t)(p[2]) << 16) | ((uint64_t)(p[3]) << 24) |
  103. ((uint64_t)(p[4]) << 32) | ((uint64_t)(p[5]) << 40) |
  104. ((uint64_t)(p[6]) << 48) | ((uint64_t)(p[7]) << 56));
  105. }
  106. static inline void
  107. le64enc(void * pp, uint64_t x)
  108. {
  109. uint8_t * p = (uint8_t *)pp;
  110. p[0] = x & 0xff;
  111. p[1] = (x >> 8) & 0xff;
  112. p[2] = (x >> 16) & 0xff;
  113. p[3] = (x >> 24) & 0xff;
  114. p[4] = (x >> 32) & 0xff;
  115. p[5] = (x >> 40) & 0xff;
  116. p[6] = (x >> 48) & 0xff;
  117. p[7] = (x >> 56) & 0xff;
  118. }
  119. #endif /* !SYSENDIAN_H_ */