modified: src1/input.c
[GalaxyCodeBases.git] / c_cpp / lib / klib / kbit.h
blob3793cf837301fe8866a89f79a0ba0ecdf161402f
1 #ifndef KBIT_H
2 #define KBIT_H
4 #include <stdint.h>
6 static inline uint64_t kbi_popcount64(uint64_t y) // standard popcount; from wikipedia
8 y -= ((y >> 1) & 0x5555555555555555ull);
9 y = (y & 0x3333333333333333ull) + (y >> 2 & 0x3333333333333333ull);
10 return ((y + (y >> 4)) & 0xf0f0f0f0f0f0f0full) * 0x101010101010101ull >> 56;
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
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;
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
26 #ifndef kbi_swap
27 #define kbi_swap(a, b) (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b))) // from "bit twiddling hacks"
28 #endif
30 #endif