1
0

Bits.h 6.5 KB

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