Bits.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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/Endian.h"
  19. #include "util/Gcc.h"
  20. #include <stdint.h>
  21. #include <stddef.h>
  22. /**
  23. * Find first set bit in a 64 bit integer.
  24. */
  25. static inline int Bits_ffs64(uint64_t number)
  26. {
  27. if (!number) {
  28. return 0;
  29. }
  30. int out = 1;
  31. while (!(number & 1)) {
  32. number >>= 1;
  33. out++;
  34. }
  35. return out;
  36. }
  37. static inline int Bits_popCountx64(uint64_t number)
  38. {
  39. int out = 0;
  40. for (int i = 0; i < 64; i++) {
  41. out += ((number >> i) & 1);
  42. }
  43. return out;
  44. }
  45. static inline int Bits_popCountx32(uint32_t number)
  46. {
  47. int out = 0;
  48. for (int i = 0; i < 32; i++) {
  49. out += ((number >> i) & 1);
  50. }
  51. return out;
  52. }
  53. // TODO(cjd): this is hot, optimize this to use the ASM instruction.
  54. static inline int Bits_log2x64(uint64_t number)
  55. {
  56. int out = 0;
  57. while (number >>= 1) {
  58. out++;
  59. }
  60. return out;
  61. }
  62. /** Largest possible number whose log2 is bitCount. */
  63. static inline uint64_t Bits_maxBits64(uint32_t bitCount)
  64. {
  65. Assert_ifParanoid(bitCount < 64);
  66. return (((uint64_t)1) << bitCount) - 1;
  67. }
  68. static inline int Bits_log2x32(uint32_t number)
  69. {
  70. int out = 0;
  71. while (number >>= 1) {
  72. out++;
  73. }
  74. return out;
  75. }
  76. static inline int Bits_log2x64_be(uint64_t number)
  77. {
  78. return Bits_log2x64(Endian_bigEndianToHost64(number));
  79. }
  80. /**
  81. * Bitwise reversal of the a number.
  82. * This is endian safe.
  83. */
  84. static inline uint64_t Bits_bitReverse64(uint64_t toReverse)
  85. {
  86. #define Bits_rotateAndMask(mask, rotateBits) \
  87. toReverse = ((toReverse >> rotateBits) & mask) | ((toReverse & mask) << rotateBits)
  88. Bits_rotateAndMask(0x5555555555555555ull, 1);
  89. Bits_rotateAndMask(0x3333333333333333ull, 2);
  90. Bits_rotateAndMask(0x0F0F0F0F0F0F0F0Full, 4);
  91. Bits_rotateAndMask(0x00FF00FF00FF00FFull, 8);
  92. Bits_rotateAndMask(0x0000FFFF0000FFFFull, 16);
  93. Bits_rotateAndMask(0x00000000FFFFFFFFull, 32);
  94. return toReverse;
  95. #undef Bits_rotateAndMask
  96. }
  97. /**
  98. * @param buffer the space of check if it's zero.
  99. * @length the nuber of bytes to check for zero'd-ness.
  100. * @return true if all bytes checked are zero.
  101. */
  102. static inline int Bits_isZero(void* buffer, size_t length)
  103. {
  104. uint8_t* buff = (uint8_t*) buffer;
  105. for (size_t i = 0; i < length; i++) {
  106. if (buff[i]) {
  107. return 0;
  108. }
  109. }
  110. return 1;
  111. }
  112. static inline void* Bits_memmove(void* dest, const void* src, size_t length)
  113. {
  114. #ifndef memmove
  115. void* memmove(void* dest, const void* src, size_t length);
  116. #endif
  117. return memmove(dest, src, length);
  118. }
  119. static inline void* Bits_memset(void* location, int byte, size_t count)
  120. {
  121. #ifndef memset
  122. void* memset(void* location, int byte, size_t count);
  123. #endif
  124. return memset(location, byte, count);
  125. }
  126. static inline int Bits_memcmp(const void* loc1, const void* loc2, size_t length)
  127. {
  128. #ifndef memcmp
  129. int memcmp(const void* loc1, const void* loc2, size_t length);
  130. #endif
  131. return memcmp(loc1, loc2, length);
  132. }
  133. static inline void* Bits_memcpyNoDebug(void* restrict out, const void* restrict in, size_t length)
  134. {
  135. #ifndef memcpy
  136. void* memcpy(void* restrict out, const void* restrict in, size_t length);
  137. #endif
  138. return memcpy(out, in, length);
  139. }
  140. /**
  141. * @param out buffer to write to.
  142. * @param in buffer to read from.
  143. * @param length number of bytes to copy.
  144. * @param file name of the file calling this, for logging.
  145. * @param line the line number of the calling file, for logging.
  146. * @param constant true if the length should be checked for being constant.
  147. * @return out
  148. */
  149. static inline void* Bits_memcpyDebug(void* out,
  150. const void* in,
  151. size_t length,
  152. char* file,
  153. int line)
  154. {
  155. const char* inc = in;
  156. const char* outc = out;
  157. // Check that pointers don't alias.
  158. if (outc >= inc && outc < inc + length) {
  159. Assert_failure(file, line, "memcpy() pointers alias each other");
  160. }
  161. return Bits_memcpyNoDebug(out, in, length);
  162. }
  163. /**
  164. * Bits_memcpy()
  165. * Alias to POSIX memcpy(), allows for extra debugging checks.
  166. *
  167. * @param out the buffer to write to.
  168. * @param in the buffer to read from.
  169. * @param length the number of bytes to copy.
  170. */
  171. #ifdef PARANOIA
  172. #define Bits_memcpy(a, b, c) Bits_memcpyDebug(a, b, c, Gcc_SHORT_FILE, Gcc_LINE)
  173. #else
  174. #define Bits_memcpy(a,b,c) Bits_memcpyNoDebug(a,b,c)
  175. #endif
  176. /**
  177. * Bits_memcpyConst()
  178. * Alias to POSIX memcpy(), will not compile unless the number of bytes to be copied
  179. * is known at compile time. This allows for defensive development by declaring intent to copy
  180. * either a static number of bytes of an unknown number of bytes.
  181. *
  182. * @param out the buffer to write to.
  183. * @param in the buffer to read from.
  184. * @param length the number of bytes to copy.
  185. */
  186. #ifdef HAS_BUILTIN_CONSTANT_P
  187. #define Bits_memcpyConst(a, b, c) \
  188. Assert_compileTime(__builtin_constant_p(c) == 1); \
  189. Bits_memcpy(a, b, c)
  190. #define Bits_memmoveConst(a,b,c) \
  191. Assert_compileTime(__builtin_constant_p(c) == 1); \
  192. Bits_memmove(a,b,c)
  193. #else
  194. #define Bits_memcpyConst(a, b, c) Bits_memcpy(a, b, c)
  195. #endif
  196. static inline void* Bits_memmem(const void* haystack,
  197. const void* needle,
  198. size_t haystackLen,
  199. size_t needleLen)
  200. {
  201. uint8_t* needleC = (uint8_t*) needle;
  202. uint8_t* haystackC = (uint8_t*) haystack;
  203. uint8_t* stopAt = haystackC + haystackLen - needleLen;
  204. if (!(haystack && needle && haystackLen && needleLen)) {
  205. return NULL;
  206. }
  207. while (haystackC <= stopAt) {
  208. if (*haystackC == *needleC
  209. && !__builtin_memcmp(haystackC, needleC, needleLen))
  210. {
  211. return haystackC;
  212. }
  213. haystackC++;
  214. }
  215. return NULL;
  216. }
  217. #endif