1 #ifndef KBIT_H
2 #define KBIT_H
3 
4 #include <stdint.h>
5 
kbi_popcount64(uint64_t y)6 static inline uint64_t kbi_popcount64(uint64_t y) // standard popcount; from wikipedia
7 {
8 	y -= ((y >> 1) & 0x5555555555555555ull);
9 	y = (y & 0x3333333333333333ull) + (y >> 2 & 0x3333333333333333ull);
10 	return ((y + (y >> 4)) & 0xf0f0f0f0f0f0f0full) * 0x101010101010101ull >> 56;
11 }
12 
kbi_DNAcount64(uint64_t y,int c)13 static inline uint64_t kbi_DNAcount64(uint64_t y, int c) // count #A/C/G/T from a 2-bit encoded integer; from BWA
14 {
15 	// reduce nucleotide counting to bits counting
16 	y = ((c&2)? y : ~y) >> 1 & ((c&1)? y : ~y) & 0x5555555555555555ull;
17 	// count the number of 1s in y
18 	y = (y & 0x3333333333333333ull) + (y >> 2 & 0x3333333333333333ull);
19 	return ((y + (y >> 4)) & 0xf0f0f0f0f0f0f0full) * 0x101010101010101ull >> 56;
20 }
21 
22 #ifndef kroundup32 // round a 32-bit integer to the next closet integer; from "bit twiddling hacks"
23 #define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
24 #endif
25 
26 #ifndef kbi_swap
27 #define kbi_swap(a, b) (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b))) // from "bit twiddling hacks"
28 #endif
29 
30 #endif
31