endian.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * All the code below is a rework of
  3. * https://github.com/freebsd/freebsd/blob/master/sys/sys/endian.h
  4. * to import symbols defining non-standard endian handling functions.
  5. * The aforementioned source code license terms are included here.
  6. * For further license info, please look at https://github.com/freebsd/freebsd
  7. */
  8. /*-
  9. * SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD
  10. * SPDX-FileCopyrightText: 2020 Francesco Giancane <francesco.giancane@accenture.com>
  11. * SPDX-FileCopyrightText: 2002 Thomas Moestl <tmm@FreeBSD.org>
  12. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND Apache-2.0
  13. *
  14. * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org>
  15. * All rights reserved.
  16. *
  17. * Redistribution and use in source and binary forms, with or without
  18. * modification, are permitted provided that the following conditions
  19. * are met:
  20. * 1. Redistributions of source code must retain the above copyright
  21. * notice, this list of conditions and the following disclaimer.
  22. * 2. Redistributions in binary form must reproduce the above copyright
  23. * notice, this list of conditions and the following disclaimer in the
  24. * documentation and/or other materials provided with the distribution.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  27. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  35. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  36. * SUCH DAMAGE.
  37. *
  38. * $FreeBSD$
  39. */
  40. #pragma once
  41. #include <stdint.h>
  42. /*
  43. * This is a compatibility header for <endian.h>.
  44. * In xtensa-newlib distribution it is located in <machine/endian.h>
  45. * but most program expect to be plain <endian.h>.
  46. */
  47. #ifndef __has_include
  48. #define __has_include(x) 0
  49. #endif
  50. #if __has_include(<machine/endian.h>)
  51. #include <machine/endian.h>
  52. #else
  53. /* for platforms that do not have machine/endian.h */
  54. #define _LITTLE_ENDIAN 1234
  55. #define _BIG_ENDIAN 4321
  56. #define _PDP_ENDIAN 3412
  57. /* little-endian architecture */
  58. #define _BYTE_ORDER _LITTLE_ENDIAN
  59. static __inline uint16_t
  60. __bswap16(uint16_t _x)
  61. {
  62. return ((uint16_t)((_x >> 8) | ((_x << 8) & 0xff00)));
  63. }
  64. static __inline uint32_t
  65. __bswap32(uint32_t _x)
  66. {
  67. return ((uint32_t)((_x >> 24) | ((_x >> 8) & 0xff00) |
  68. ((_x << 8) & 0xff0000) | ((_x << 24) & 0xff000000)));
  69. }
  70. static __inline uint64_t
  71. __bswap64(uint64_t _x)
  72. {
  73. return ((uint64_t)((_x >> 56) | ((_x >> 40) & 0xff00) |
  74. ((_x >> 24) & 0xff0000) | ((_x >> 8) & 0xff000000) |
  75. ((_x << 8) & ((uint64_t)0xff << 32)) |
  76. ((_x << 24) & ((uint64_t)0xff << 40)) |
  77. ((_x << 40) & ((uint64_t)0xff << 48)) | ((_x << 56))));
  78. }
  79. #endif
  80. #ifdef __cplusplus
  81. extern "C" {
  82. #endif
  83. /*
  84. * General byte order swapping functions.
  85. */
  86. #define bswap16(x) __bswap16(x)
  87. #define bswap32(x) __bswap32(x)
  88. #define bswap64(x) __bswap64(x)
  89. /*
  90. * Host to big endian, host to little endian, big endian to host, and little
  91. * endian to host byte order functions as detailed in byteorder(9).
  92. */
  93. #if _BYTE_ORDER == _LITTLE_ENDIAN
  94. #define htobe16(x) bswap16((x))
  95. #define htobe32(x) bswap32((x))
  96. #define htobe64(x) bswap64((x))
  97. #define htole16(x) ((uint16_t)(x))
  98. #define htole32(x) ((uint32_t)(x))
  99. #define htole64(x) ((uint64_t)(x))
  100. #define be16toh(x) bswap16((x))
  101. #define be32toh(x) bswap32((x))
  102. #define be64toh(x) bswap64((x))
  103. #define le16toh(x) ((uint16_t)(x))
  104. #define le32toh(x) ((uint32_t)(x))
  105. #define le64toh(x) ((uint64_t)(x))
  106. #else /* _BYTE_ORDER != _LITTLE_ENDIAN */
  107. #define htobe16(x) ((uint16_t)(x))
  108. #define htobe32(x) ((uint32_t)(x))
  109. #define htobe64(x) ((uint64_t)(x))
  110. #define htole16(x) bswap16((x))
  111. #define htole32(x) bswap32((x))
  112. #define htole64(x) bswap64((x))
  113. #define be16toh(x) ((uint16_t)(x))
  114. #define be32toh(x) ((uint32_t)(x))
  115. #define be64toh(x) ((uint64_t)(x))
  116. #define le16toh(x) bswap16((x))
  117. #define le32toh(x) bswap32((x))
  118. #define le64toh(x) bswap64((x))
  119. #endif /* _BYTE_ORDER == _LITTLE_ENDIAN */
  120. /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
  121. static __inline uint16_t
  122. be16dec(const void *pp)
  123. {
  124. uint8_t const *p = (uint8_t const *)pp;
  125. return ((p[0] << 8) | p[1]);
  126. }
  127. static __inline uint32_t
  128. be32dec(const void *pp)
  129. {
  130. uint8_t const *p = (uint8_t const *)pp;
  131. return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
  132. }
  133. static __inline uint64_t
  134. be64dec(const void *pp)
  135. {
  136. uint8_t const *p = (uint8_t const *)pp;
  137. return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
  138. }
  139. static __inline uint16_t
  140. le16dec(const void *pp)
  141. {
  142. uint8_t const *p = (uint8_t const *)pp;
  143. return ((p[1] << 8) | p[0]);
  144. }
  145. static __inline uint32_t
  146. le32dec(const void *pp)
  147. {
  148. uint8_t const *p = (uint8_t const *)pp;
  149. return (((unsigned)p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
  150. }
  151. static __inline uint64_t
  152. le64dec(const void *pp)
  153. {
  154. uint8_t const *p = (uint8_t const *)pp;
  155. return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
  156. }
  157. static __inline void
  158. be16enc(void *pp, uint16_t u)
  159. {
  160. uint8_t *p = (uint8_t *)pp;
  161. p[0] = (u >> 8) & 0xff;
  162. p[1] = u & 0xff;
  163. }
  164. static __inline void
  165. be32enc(void *pp, uint32_t u)
  166. {
  167. uint8_t *p = (uint8_t *)pp;
  168. p[0] = (u >> 24) & 0xff;
  169. p[1] = (u >> 16) & 0xff;
  170. p[2] = (u >> 8) & 0xff;
  171. p[3] = u & 0xff;
  172. }
  173. static __inline void
  174. be64enc(void *pp, uint64_t u)
  175. {
  176. uint8_t *p = (uint8_t *)pp;
  177. be32enc(p, (uint32_t)(u >> 32));
  178. be32enc(p + 4, (uint32_t)(u & 0xffffffffU));
  179. }
  180. static __inline void
  181. le16enc(void *pp, uint16_t u)
  182. {
  183. uint8_t *p = (uint8_t *)pp;
  184. p[0] = u & 0xff;
  185. p[1] = (u >> 8) & 0xff;
  186. }
  187. static __inline void
  188. le32enc(void *pp, uint32_t u)
  189. {
  190. uint8_t *p = (uint8_t *)pp;
  191. p[0] = u & 0xff;
  192. p[1] = (u >> 8) & 0xff;
  193. p[2] = (u >> 16) & 0xff;
  194. p[3] = (u >> 24) & 0xff;
  195. }
  196. static __inline void
  197. le64enc(void *pp, uint64_t u)
  198. {
  199. uint8_t *p = (uint8_t *)pp;
  200. le32enc(p, (uint32_t)(u & 0xffffffffU));
  201. le32enc(p + 4, (uint32_t)(u >> 32));
  202. }
  203. #ifdef __cplusplus
  204. }
  205. #endif