endian.h 5.8 KB

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