forked from voutcn/megahit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bit_operation.h
127 lines (103 loc) · 4.14 KB
/
bit_operation.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* @file bit_operation.h
* @brief Bit Operations.
* @author Yu Peng ([email protected])
* @version 1.0.0
* @date 2011-08-02
* @modified by Dinghua Li
* @date 2014-10-02
*/
#ifndef __BASIC_BIT_OPERATION_H_
#define __BASIC_BIT_OPERATION_H_
#include <stdint.h>
namespace bit_operation {
const uint64_t kSwap64Mask32 = 0x00000000FFFFFFFFULL;
const uint64_t kSwap64Mask16 = 0x0000FFFF0000FFFFULL;
const uint64_t kSwap64Mask8 = 0x00FF00FF00FF00FFULL;
const uint64_t kSwap64Mask4 = 0x0F0F0F0F0F0F0F0FULL;
const uint64_t kSwap64Mask2 = 0x3333333333333333ULL;
const uint64_t kSwap64Mask1 = 0x5555555555555555ULL;
const uint8_t kSwap8Mask4 = 0x0FU;
const uint8_t kSwap8Mask2 = 0x33U;
const uint8_t kSwap8Mask1 = 0x55U;
inline void ReverseComplement(uint64_t &value) {
value = ((value & kSwap64Mask32) << 32) | ((value & ~kSwap64Mask32) >> 32);
value = ((value & kSwap64Mask16) << 16) | ((value & ~kSwap64Mask16) >> 16);
value = ((value & kSwap64Mask8) << 8) | ((value & ~kSwap64Mask8) >> 8);
value = ((value & kSwap64Mask4) << 4) | ((value & ~kSwap64Mask4) >> 4);
value = ((value & kSwap64Mask2) << 2) | ((value & ~kSwap64Mask2) >> 2);
value = ~value;
}
inline void ReverseComplement(uint32_t &value) {
value = ((value & kSwap64Mask16) << 16) | ((value & ~kSwap64Mask16) >> 16);
value = ((value & kSwap64Mask8) << 8) | ((value & ~kSwap64Mask8) >> 8);
value = ((value & kSwap64Mask4) << 4) | ((value & ~kSwap64Mask4) >> 4);
value = ((value & kSwap64Mask2) << 2) | ((value & ~kSwap64Mask2) >> 2);
value = ~value;
}
inline void ReverseComplement(uint16_t &value) {
value = ((value & kSwap64Mask8) << 8) | ((value & ~kSwap64Mask8) >> 8);
value = ((value & kSwap64Mask4) << 4) | ((value & ~kSwap64Mask4) >> 4);
value = ((value & kSwap64Mask2) << 2) | ((value & ~kSwap64Mask2) >> 2);
value = ~value;
}
inline void ReverseComplement(uint8_t &value) {
value = ((value & kSwap64Mask4) << 4) | ((value & ~kSwap64Mask4) >> 4);
value = ((value & kSwap64Mask2) << 2) | ((value & ~kSwap64Mask2) >> 2);
value = ~value;
}
inline uint8_t ReverseComplement(uint8_t value) {
value = ((value & kSwap8Mask4) << 4) | ((value & ~kSwap8Mask4) >> 4);
value = ((value & kSwap8Mask2) << 2) | ((value & ~kSwap8Mask2) >> 2);
return ~value;
}
inline uint32_t Reverse(uint32_t value) {
value = ((value & kSwap64Mask16) << 16) | ((value & ~kSwap64Mask16) >> 16);
value = ((value & kSwap64Mask8) << 8) | ((value & ~kSwap64Mask8) >> 8);
value = ((value & kSwap64Mask4) << 4) | ((value & ~kSwap64Mask4) >> 4);
value = ((value & kSwap64Mask2) << 2) | ((value & ~kSwap64Mask2) >> 2);
return value;
}
inline uint8_t Reverse(uint8_t value) {
value = ((value & kSwap8Mask4) << 4) | ((value & ~kSwap8Mask4) >> 4);
value = ((value & kSwap8Mask2) << 2) | ((value & ~kSwap8Mask2) >> 2);
return value;
}
inline int BitCount(uint8_t x) {
x = (x & kSwap8Mask1) + ((x >> 1) & kSwap8Mask1);
x = (x & kSwap8Mask2) + ((x >> 2) & kSwap8Mask2);
x = (x & kSwap8Mask4) + ((x >> 4) & kSwap8Mask4);
return x;
}
inline int BitCount(uint64_t x) {
x = (x & kSwap64Mask1) + ((x >> 1) & kSwap64Mask1);
x = (x & kSwap64Mask2) + ((x >> 2) & kSwap64Mask2);
x = (x & kSwap64Mask4) + ((x >> 4) & kSwap64Mask4);
x = (x & kSwap64Mask8) + ((x >> 8) & kSwap64Mask8);
x = (x & kSwap64Mask16) + ((x >> 16) & kSwap64Mask16);
x = (x & kSwap64Mask32) + ((x >> 32) & kSwap64Mask32);
return x;
}
inline int BaseCount(uint64_t x) {
x = (x & kSwap64Mask1) | ((x >> 1) & kSwap64Mask1);
x = (x & kSwap64Mask2) + ((x >> 2) & kSwap64Mask2);
x = (x & kSwap64Mask4) + ((x >> 4) & kSwap64Mask4);
x = (x & kSwap64Mask8) + ((x >> 8) & kSwap64Mask8);
x = (x & kSwap64Mask16) + ((x >> 16) & kSwap64Mask16);
x = (x & kSwap64Mask32) + ((x >> 32) & kSwap64Mask32);
return x;
}
inline int BitToIndex(uint8_t x) {
static int bit_to_index[] = {0, 0, 1, 0, 2, 0, 0, 0, 3,};
return bit_to_index[x];
}
template<uint32_t number>
struct MostSignificantBit {
static const int value = 1 + MostSignificantBit < (number >> 1) >::value;
};
template<>
struct MostSignificantBit<0> {
static const int value = 0;
};
}
#endif