sparse-endian.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* Copyright (c) 2012 Josh Triplett <josh@joshtriplett.org>
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #pragma once
  22. #include <byteswap.h>
  23. #include <endian.h>
  24. #include <stdint.h>
  25. #ifdef __CHECKER__
  26. #define __ap_bitwise __attribute__((bitwise))
  27. #define __ap_force __attribute__((force))
  28. #else
  29. #define __ap_bitwise
  30. #define __ap_force
  31. #endif
  32. typedef uint16_t __ap_bitwise le16_t;
  33. typedef uint16_t __ap_bitwise be16_t;
  34. typedef uint32_t __ap_bitwise le32_t;
  35. typedef uint32_t __ap_bitwise be32_t;
  36. typedef uint64_t __ap_bitwise le64_t;
  37. typedef uint64_t __ap_bitwise be64_t;
  38. #undef htobe16
  39. #undef htole16
  40. #undef be16toh
  41. #undef le16toh
  42. #undef htobe32
  43. #undef htole32
  44. #undef be32toh
  45. #undef le32toh
  46. #undef htobe64
  47. #undef htole64
  48. #undef be64toh
  49. #undef le64toh
  50. #if __BYTE_ORDER == __LITTLE_ENDIAN
  51. #define bswap_16_on_le(x) __bswap_16(x)
  52. #define bswap_32_on_le(x) __bswap_32(x)
  53. #define bswap_64_on_le(x) __bswap_64(x)
  54. #define bswap_16_on_be(x) (x)
  55. #define bswap_32_on_be(x) (x)
  56. #define bswap_64_on_be(x) (x)
  57. #elif __BYTE_ORDER == __BIG_ENDIAN
  58. #define bswap_16_on_le(x) (x)
  59. #define bswap_32_on_le(x) (x)
  60. #define bswap_64_on_le(x) (x)
  61. #define bswap_16_on_be(x) __bswap_16(x)
  62. #define bswap_32_on_be(x) __bswap_32(x)
  63. #define bswap_64_on_be(x) __bswap_64(x)
  64. #endif
  65. static inline le16_t htole16(uint16_t value) { return (le16_t __ap_force) bswap_16_on_be(value); }
  66. static inline le32_t htole32(uint32_t value) { return (le32_t __ap_force) bswap_32_on_be(value); }
  67. static inline le64_t htole64(uint64_t value) { return (le64_t __ap_force) bswap_64_on_be(value); }
  68. static inline be16_t htobe16(uint16_t value) { return (be16_t __ap_force) bswap_16_on_le(value); }
  69. static inline be32_t htobe32(uint32_t value) { return (be32_t __ap_force) bswap_32_on_le(value); }
  70. static inline be64_t htobe64(uint64_t value) { return (be64_t __ap_force) bswap_64_on_le(value); }
  71. static inline uint16_t le16toh(le16_t value) { return bswap_16_on_be((uint16_t __ap_force)value); }
  72. static inline uint32_t le32toh(le32_t value) { return bswap_32_on_be((uint32_t __ap_force)value); }
  73. static inline uint64_t le64toh(le64_t value) { return bswap_64_on_be((uint64_t __ap_force)value); }
  74. static inline uint16_t be16toh(be16_t value) { return bswap_16_on_le((uint16_t __ap_force)value); }
  75. static inline uint32_t be32toh(be32_t value) { return bswap_32_on_le((uint32_t __ap_force)value); }
  76. static inline uint64_t be64toh(be64_t value) { return bswap_64_on_le((uint64_t __ap_force)value); }
  77. #undef __ap_bitwise
  78. #undef __ap_force