Bits.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifndef Bits_H
  16. #define Bits_H
  17. #include "util/Assert.h"
  18. #include "util/Gcc.h"
  19. #include "util/Linker.h"
  20. Linker_require("util/Bits.c")
  21. #include <stdint.h>
  22. #include <stddef.h>
  23. /**
  24. * Find first set bit in a 64 bit integer.
  25. */
  26. static inline int Bits_ffs64(uint64_t number)
  27. {
  28. if (!number) {
  29. return 0;
  30. }
  31. int out = 1;
  32. while (!(number & 1)) {
  33. number >>= 1;
  34. out++;
  35. }
  36. return out;
  37. }
  38. static inline int Bits_popCountx64(uint64_t number)
  39. {
  40. int out = 0;
  41. for (int i = 0; i < 64; i++) {
  42. out += ((number >> i) & 1);
  43. }
  44. return out;
  45. }
  46. static inline int Bits_popCountx32(uint32_t number)
  47. {
  48. int out = 0;
  49. for (int i = 0; i < 32; i++) {
  50. out += ((number >> i) & 1);
  51. }
  52. return out;
  53. }
  54. static inline int Bits_log2x64(uint64_t number)
  55. {
  56. if (!number) { return 0; }
  57. return 63 - __builtin_clzll(number);
  58. }
  59. int Bits_log2x64_stupid(uint64_t number);
  60. /** Largest possible number whose log2 is bitCount. */
  61. static inline uint64_t Bits_maxBits64(uint32_t bitCount)
  62. {
  63. Assert_ifParanoid(bitCount < 64);
  64. return (((uint64_t)1) << bitCount) - 1;
  65. }
  66. static inline int Bits_log2x32(uint32_t number)
  67. {
  68. int out = 0;
  69. while (number >>= 1) {
  70. out++;
  71. }
  72. return out;
  73. }
  74. /**
  75. * Bitwise reversal of the a number.
  76. * This is endian safe.
  77. */
  78. static inline uint64_t Bits_bitReverse64(uint64_t toReverse)
  79. {
  80. #define Bits_rotateAndMask(mask, rotateBits) \
  81. toReverse = ((toReverse >> rotateBits) & mask) | ((toReverse & mask) << rotateBits)
  82. Bits_rotateAndMask(0x5555555555555555ull, 1);
  83. Bits_rotateAndMask(0x3333333333333333ull, 2);
  84. Bits_rotateAndMask(0x0F0F0F0F0F0F0F0Full, 4);
  85. return __builtin_bswap64(toReverse);
  86. #undef Bits_rotateAndMask
  87. }
  88. /**
  89. * @param buffer the space of check if it's zero.
  90. * @length the nuber of bytes to check for zero'd-ness.
  91. * @return true if all bytes checked are zero.
  92. */
  93. static inline int Bits_isZero(void* buffer, size_t length)
  94. {
  95. uint8_t* buff = (uint8_t*) buffer;
  96. for (size_t i = 0; i < length; i++) {
  97. if (buff[i]) {
  98. return 0;
  99. }
  100. }
  101. return 1;
  102. }
  103. #define Bits_memmove(a,b,c) __builtin_memmove(a,b,c)
  104. #define Bits_memset(a,b,c) __builtin_memset(a,b,c)
  105. #define Bits_memcmp(a,b,c) __builtin_memcmp(a,b,c)
  106. /**
  107. * Bits_memcpy()
  108. * Alias to POSIX memcpy(), allows for extra debugging checks.
  109. *
  110. * @param out buffer to write to.
  111. * @param in buffer to read from.
  112. * @param length number of bytes to copy.
  113. * @param file name of the file calling this, for logging.
  114. * @param line the line number of the calling file, for logging.
  115. * @param constant true if the length should be checked for being constant.
  116. * @return out
  117. */
  118. #define Bits_memcpy(a,b,c) Bits__memcpy(a,b,c,Gcc_SHORT_FILE,Gcc_LINE)
  119. static inline void* Bits__memcpy(void* out,
  120. const void* in,
  121. size_t length,
  122. char* file,
  123. int line)
  124. {
  125. const char* inc = in;
  126. const char* outc = out;
  127. // Check that pointers don't alias.
  128. if (outc >= inc && outc < inc + length) {
  129. Assert_failure(file, line, "memcpy() pointers alias each other");
  130. }
  131. return __builtin_memcpy(out, in, length);
  132. }
  133. /**
  134. * Bits_memcpyConst()
  135. * Alias to POSIX memcpy(), will not compile unless the number of bytes to be copied
  136. * is known at compile time. This allows for defensive development by declaring intent to copy
  137. * either a static number of bytes of an unknown number of bytes.
  138. *
  139. * @param out the buffer to write to.
  140. * @param in the buffer to read from.
  141. * @param length the number of bytes to copy.
  142. */
  143. #define Bits_memcpyConst(a,b,c) \
  144. do { \
  145. Assert_true(__builtin_constant_p(c)); \
  146. Bits_memcpy(a,b,c); \
  147. } while (0)
  148. // CHECKFILES_IGNORE
  149. #define Bits_memmoveConst(a,b,c) \
  150. do { \
  151. Assert_true(__builtin_constant_p(c)); \
  152. Bits_memmove(a,b,c); \
  153. } while (0)
  154. // CHECKFILES_IGNORE
  155. void* Bits_memmem(const void* haystack, size_t haystackLen, const void* needle, size_t needleLen);
  156. #endif