endian.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /****************************************************************************
  2. * include/endian.h
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. #ifndef __INCLUDE_ENDIAN_H
  21. #define __INCLUDE_ENDIAN_H
  22. /****************************************************************************
  23. * Included Files
  24. ****************************************************************************/
  25. #include <nuttx/config.h>
  26. #include <nuttx/compiler.h>
  27. #include <stdint.h>
  28. /****************************************************************************
  29. * Preprocessor definitions
  30. ****************************************************************************/
  31. /* The endian.h header must define at least the following macros for use in
  32. * determining host byte order for integer types:
  33. *
  34. * BYTE_ORDER This macro will have a value equal to one of the *_ENDIAN
  35. * macros in this header.
  36. * LITTLE_ENDIAN If BYTE_ORDER == LITTLE_ENDIAN, the host byte order is
  37. * from least significant to most significant.
  38. * BIG_ENDIAN If BYTE_ORDER == BIG_ENDIAN, the host byte order is from
  39. * most significant to least significant.
  40. */
  41. #define LITTLE_ENDIAN 1234
  42. #define BIG_ENDIAN 4321
  43. /* Common byte swapping macros */
  44. #define __SWAP_UINT16_ISMACRO 1
  45. #undef __SWAP_UINT32_ISMACRO
  46. #ifdef __SWAP_UINT16_ISMACRO
  47. # define __swap_uint16(n) \
  48. (uint16_t)(((((uint16_t)(n)) & 0x00ff) << 8) | \
  49. ((((uint16_t)(n)) >> 8) & 0x00ff))
  50. #endif
  51. #ifdef __SWAP_UINT32_ISMACRO
  52. # define __swap_uint32(n) \
  53. (uint32_t)(((((uint32_t)(n)) & 0x000000ffUL) << 24) | \
  54. ((((uint32_t)(n)) & 0x0000ff00UL) << 8) | \
  55. ((((uint32_t)(n)) & 0x00ff0000UL) >> 8) | \
  56. ((((uint32_t)(n)) & 0xff000000UL) >> 24))
  57. #endif
  58. /* Endian-specific definitions */
  59. #ifdef CONFIG_ENDIAN_BIG
  60. /* Big-endian byte order */
  61. # define BYTE_ORDER BIG_ENDIAN
  62. /* Big-endian byte order macros */
  63. # define htobe16(n) (n)
  64. # define htole16(n) __swap_uint16((uint16_t)n)
  65. # define be16toh(n) (n)
  66. # define le16toh(n) __swap_uint16((uint16_t)n)
  67. # define htobe32(n) (n)
  68. # define htole32(n) __swap_uint32((uint32_t)n)
  69. # define be32toh(n) (n)
  70. # define le32toh(n) __swap_uint32(n)
  71. # ifdef CONFIG_HAVE_LONG_LONG
  72. # define htobe64(n) (n)
  73. # define htole64(n) __swap_uint64((uint64_t)n)
  74. # define be64toh(n) (n)
  75. # define le64toh(n) __swap_uint64((uint64_t)n)
  76. # endif
  77. #else
  78. /* Little-endian byte order */
  79. # define BYTE_ORDER LITTLE_ENDIAN
  80. /* Little-endian byte order macros */
  81. # define htobe16(n) __swap_uint16((uint16_t)n)
  82. # define htole16(n) (n)
  83. # define be16toh(n) __swap_uint16((uint16_t)n)
  84. # define le16toh(n) (n)
  85. # define htobe32(n) __swap_uint32((uint32_t)n)
  86. # define htole32(n) (n)
  87. # define be32toh(n) __swap_uint32((uint32_t)n)
  88. # define le32toh(n) (n)
  89. # ifdef CONFIG_HAVE_LONG_LONG
  90. # define htobe64(n) __swap_uint64((uint64_t)n)
  91. # define htole64(n) (n)
  92. # define be64toh(n) __swap_uint64((uint64_t)n)
  93. # define le64toh(n) (n)
  94. # endif
  95. #endif
  96. /****************************************************************************
  97. * Public Function Prototypes
  98. ****************************************************************************/
  99. #ifndef __SWAP_UINT16_ISMACRO
  100. uint16_t __swap_uint16(uint16_t n);
  101. #endif
  102. #ifndef __SWAP_UINT32_ISMACRO
  103. uint32_t __swap_uint32(uint32_t n);
  104. #endif
  105. #if CONFIG_HAVE_LONG_LONG
  106. uint64_t __swap_uint64(uint64_t n);
  107. #endif
  108. #endif /* __INCLUDE_ENDIAN_H */