endian.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*-
  2. * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. *
  26. * $FreeBSD: releng/10.1/sys/sys/endian.h 208331 2010-05-20 06:16:13Z phk $
  27. */
  28. #ifndef _SYS_ENDIAN_H_
  29. #define _SYS_ENDIAN_H_
  30. #include <rtthread.h>
  31. #include "sunxi_hal_common.h"
  32. #ifdef CONFIG_RTTKERNEL
  33. #ifndef _LITTLE_ENDIAN
  34. #define _LITTLE_ENDIAN 1
  35. #endif
  36. #define _BYTE_ORDER _LITTLE_ENDIAN
  37. #ifndef __bswap32
  38. #if ((defined(__GNUC__) && !defined(__CC_ARM)))
  39. #define __bswap16(x) ((uint16_t)__builtin_bswap16(x))
  40. #define __bswap32(x) ((uint32_t)__builtin_bswap32(x))
  41. #define __bswap64(x) ((uint64_t)__builtin_bswap64(x))
  42. #else /* ((defined(__GNUC__) && !defined(__CC_ARM))) */
  43. #define __bswap16(x) ((uint16_t)( \
  44. (((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | \
  45. (((uint16_t)(x) & (uint16_t)0xff00U) >> 8)))
  46. #define __bswap32(x) ((uint32_t)( \
  47. (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \
  48. (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | \
  49. (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \
  50. (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24)))
  51. #define __bswap64(x) ((uint64_t)( \
  52. (((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \
  53. (((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
  54. (((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
  55. (((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) << 8) | \
  56. (((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
  57. (((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
  58. (((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
  59. (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56)))
  60. #endif /* ((defined(__GNUC__) && !defined(__CC_ARM))) */
  61. #else /* __bswap32 */
  62. #undef __bswap32
  63. #define __bswap32(x) (uint32_t)__builtin_bswap32(x)
  64. #endif /* __bswap32 */
  65. /*
  66. * General byte order swapping functions.
  67. */
  68. #define bswap16(x) __bswap16(x)
  69. #define bswap32(x) __bswap32(x)
  70. #define bswap64(x) __bswap64(x)
  71. /*
  72. * Host to big endian, host to little endian, big endian to host, and little
  73. * endian to host byte order functions as detailed in byteorder(9).
  74. */
  75. #if _BYTE_ORDER == _LITTLE_ENDIAN
  76. #define htobe16(x) bswap16((x))
  77. #define htobe32(x) bswap32((x))
  78. #define htobe64(x) bswap64((x))
  79. #define htole16(x) ((uint16_t)(x))
  80. #define htole32(x) ((uint32_t)(x))
  81. #define htole64(x) ((uint64_t)(x))
  82. #define be16toh(x) bswap16((x))
  83. #define be32toh(x) bswap32((x))
  84. #define be64toh(x) bswap64((x))
  85. #define le16toh(x) ((uint16_t)(x))
  86. #define le32toh(x) ((uint32_t)(x))
  87. #define le64toh(x) ((uint64_t)(x))
  88. #elif (_BYTE_ORDER == _BIG_ENDIAN)
  89. #define htobe16(x) ((uint16_t)(x))
  90. #define htobe32(x) ((uint32_t)(x))
  91. #define htobe64(x) ((uint64_t)(x))
  92. #define htole16(x) bswap16((x))
  93. #define htole32(x) bswap32((x))
  94. #define htole64(x) bswap64((x))
  95. #define be16toh(x) ((uint16_t)(x))
  96. #define be32toh(x) ((uint32_t)(x))
  97. #define be64toh(x) ((uint64_t)(x))
  98. #define le16toh(x) bswap16((x))
  99. #define le32toh(x) bswap32((x))
  100. #define le64toh(x) bswap64((x))
  101. #else
  102. #error "Endian not defined!"
  103. #endif /* _BYTE_ORDER == _LITTLE_ENDIAN */
  104. #endif
  105. #define __cpu_to_le64(x) htole64(x)
  106. #define __le64_to_cpu(x) le64toh(x)
  107. #define __cpu_to_le32(x) htole32(x)
  108. #define __le32_to_cpu(x) le32toh(x)
  109. #define __cpu_to_le16(x) htole16(x)
  110. #define __le16_to_cpu(x) le16toh(x)
  111. #define __cpu_to_be64(x) htobe64(x)
  112. #define __be64_to_cpu(x) be64toh(x)
  113. #define __cpu_to_be32(x) htobe32(x)
  114. #define __be32_to_cpu(x) be32toh(x)
  115. #define __cpu_to_be16(x) htobe16(x)
  116. #define __be16_to_cpu(x) be16toh(x)
  117. #define cpu_to_le64 __cpu_to_le64
  118. #define le64_to_cpu __le64_to_cpu
  119. #define cpu_to_le32 __cpu_to_le32
  120. #define le32_to_cpu __le32_to_cpu
  121. #define cpu_to_le16 __cpu_to_le16
  122. #define le16_to_cpu __le16_to_cpu
  123. #define cpu_to_be64 __cpu_to_be64
  124. #define be64_to_cpu __be64_to_cpu
  125. #define cpu_to_be32 __cpu_to_be32
  126. #define be32_to_cpu __be32_to_cpu
  127. #define cpu_to_be16 __cpu_to_be16
  128. #define be16_to_cpu __be16_to_cpu
  129. /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
  130. static __inline uint16_t
  131. be16dec(const void *pp)
  132. {
  133. uint8_t const *p = (uint8_t const *)pp;
  134. return ((p[0] << 8) | p[1]);
  135. }
  136. static __inline uint32_t
  137. be32dec(const void *pp)
  138. {
  139. uint8_t const *p = (uint8_t const *)pp;
  140. return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
  141. }
  142. static __inline uint64_t
  143. be64dec(const void *pp)
  144. {
  145. uint8_t const *p = (uint8_t const *)pp;
  146. return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
  147. }
  148. static __inline uint16_t
  149. le16dec(const void *pp)
  150. {
  151. uint8_t const *p = (uint8_t const *)pp;
  152. return ((p[1] << 8) | p[0]);
  153. }
  154. static __inline uint32_t
  155. le32dec(const void *pp)
  156. {
  157. uint8_t const *p = (uint8_t const *)pp;
  158. return (((unsigned)p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
  159. }
  160. static __inline uint64_t
  161. le64dec(const void *pp)
  162. {
  163. uint8_t const *p = (uint8_t const *)pp;
  164. return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
  165. }
  166. static __inline void
  167. be16enc(void *pp, uint16_t u)
  168. {
  169. uint8_t *p = (uint8_t *)pp;
  170. p[0] = (u >> 8) & 0xff;
  171. p[1] = u & 0xff;
  172. }
  173. static __inline void
  174. be32enc(void *pp, uint32_t u)
  175. {
  176. uint8_t *p = (uint8_t *)pp;
  177. p[0] = (u >> 24) & 0xff;
  178. p[1] = (u >> 16) & 0xff;
  179. p[2] = (u >> 8) & 0xff;
  180. p[3] = u & 0xff;
  181. }
  182. static __inline void
  183. be64enc(void *pp, uint64_t u)
  184. {
  185. uint8_t *p = (uint8_t *)pp;
  186. be32enc(p, (uint32_t)(u >> 32));
  187. be32enc(p + 4, (uint32_t)(u & 0xffffffffU));
  188. }
  189. static __inline void
  190. le16enc(void *pp, uint16_t u)
  191. {
  192. uint8_t *p = (uint8_t *)pp;
  193. p[0] = u & 0xff;
  194. p[1] = (u >> 8) & 0xff;
  195. }
  196. static __inline void
  197. le32enc(void *pp, uint32_t u)
  198. {
  199. uint8_t *p = (uint8_t *)pp;
  200. p[0] = u & 0xff;
  201. p[1] = (u >> 8) & 0xff;
  202. p[2] = (u >> 16) & 0xff;
  203. p[3] = (u >> 24) & 0xff;
  204. }
  205. static __inline void
  206. le64enc(void *pp, uint64_t u)
  207. {
  208. uint8_t *p = (uint8_t *)pp;
  209. le32enc(p, (uint32_t)(u & 0xffffffffU));
  210. le32enc(p + 4, (uint32_t)(u >> 32));
  211. }
  212. #endif /* _SYS_ENDIAN_H_ */