bitops.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * No copyright is claimed. This code is in the public domain; do with
  3. * it what you wish.
  4. *
  5. * Written by Karel Zak <kzak@redhat.com>
  6. */
  7. #ifndef BITOPS_H
  8. #define BITOPS_H
  9. #include <stdint.h>
  10. #include <sys/param.h>
  11. #if defined(HAVE_BYTESWAP_H)
  12. # include <byteswap.h>
  13. #endif
  14. #if defined(HAVE_ENDIAN_H)
  15. # include <endian.h>
  16. #elif defined(HAVE_SYS_ENDIAN_H) /* BSDs have them here */
  17. # include <sys/endian.h>
  18. #endif
  19. #if defined(__OpenBSD__)
  20. # include <sys/types.h>
  21. # define be16toh(x) betoh16(x)
  22. # define be32toh(x) betoh32(x)
  23. # define be64toh(x) betoh64(x)
  24. #endif
  25. /*
  26. * Fallbacks
  27. */
  28. #ifndef bswap_16
  29. # define bswap_16(x) ((((x) & 0x00FF) << 8) | \
  30. (((x) & 0xFF00) >> 8))
  31. #endif
  32. #ifndef bswap_32
  33. # define bswap_32(x) ((((x) & 0x000000FF) << 24) | \
  34. (((x) & 0x0000FF00) << 8) | \
  35. (((x) & 0x00FF0000) >> 8) | \
  36. (((x) & 0xFF000000) >> 24))
  37. #endif
  38. #ifndef bswap_64
  39. # define bswap_64(x) ((((x) & 0x00000000000000FFULL) << 56) | \
  40. (((x) & 0x000000000000FF00ULL) << 40) | \
  41. (((x) & 0x0000000000FF0000ULL) << 24) | \
  42. (((x) & 0x00000000FF000000ULL) << 8) | \
  43. (((x) & 0x000000FF00000000ULL) >> 8) | \
  44. (((x) & 0x0000FF0000000000ULL) >> 24) | \
  45. (((x) & 0x00FF000000000000ULL) >> 40) | \
  46. (((x) & 0xFF00000000000000ULL) >> 56))
  47. #endif
  48. #ifndef htobe16
  49. # if !defined(WORDS_BIGENDIAN)
  50. # define htobe16(x) bswap_16 (x)
  51. # define htole16(x) (x)
  52. # define be16toh(x) bswap_16 (x)
  53. # define le16toh(x) (x)
  54. # define htobe32(x) bswap_32 (x)
  55. # define htole32(x) (x)
  56. # define be32toh(x) bswap_32 (x)
  57. # define le32toh(x) (x)
  58. # define htobe64(x) bswap_64 (x)
  59. # define htole64(x) (x)
  60. # define be64toh(x) bswap_64 (x)
  61. # define le64toh(x) (x)
  62. # else
  63. # define htobe16(x) (x)
  64. # define htole16(x) bswap_16 (x)
  65. # define be16toh(x) (x)
  66. # define le16toh(x) bswap_16 (x)
  67. # define htobe32(x) (x)
  68. # define htole32(x) bswap_32 (x)
  69. # define be32toh(x) (x)
  70. # define le32toh(x) bswap_32 (x)
  71. # define htobe64(x) (x)
  72. # define htole64(x) bswap_64 (x)
  73. # define be64toh(x) (x)
  74. # define le64toh(x) bswap_64 (x)
  75. # endif
  76. #endif
  77. /*
  78. * Byte swab macros (based on linux/byteorder/swab.h)
  79. */
  80. #define swab16(x) bswap_16(x)
  81. #define swab32(x) bswap_32(x)
  82. #define swab64(x) bswap_64(x)
  83. #define cpu_to_le16(x) ((uint16_t) htole16(x))
  84. #define cpu_to_le32(x) ((uint32_t) htole32(x))
  85. #define cpu_to_le64(x) ((uint64_t) htole64(x))
  86. #define cpu_to_be16(x) ((uint16_t) htobe16(x))
  87. #define cpu_to_be32(x) ((uint32_t) htobe32(x))
  88. #define cpu_to_be64(x) ((uint64_t) htobe64(x))
  89. #define le16_to_cpu(x) ((uint16_t) le16toh(x))
  90. #define le32_to_cpu(x) ((uint32_t) le32toh(x))
  91. #define le64_to_cpu(x) ((uint64_t) le64toh(x))
  92. #define be16_to_cpu(x) ((uint16_t) be16toh(x))
  93. #define be32_to_cpu(x) ((uint32_t) be32toh(x))
  94. #define be64_to_cpu(x) ((uint64_t) be64toh(x))
  95. /*
  96. * Bit map related macros. Usually provided by libc.
  97. */
  98. #ifndef NBBY
  99. # define NBBY CHAR_BIT
  100. #endif
  101. #ifndef setbit
  102. # define setbit(a,i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
  103. # define clrbit(a,i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
  104. # define isset(a,i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)))
  105. # define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
  106. #endif
  107. #endif /* BITOPS_H */