1
0

Bits.h 6.6 KB

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