-
Notifications
You must be signed in to change notification settings - Fork 12
/
Bitvec.h
245 lines (201 loc) · 6 KB
/
Bitvec.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#pragma once
#include "Platform.h"
#include <vector>
//-----------------------------------------------------------------------------
void printbits ( const void * blob, int len );
void printhex32 ( const void * blob, int len );
void printbytes ( const void * blob, int len );
void printbytes2 ( const void * blob, int len );
uint32_t popcount ( uint32_t v );
uint32_t parity ( uint32_t v );
uint32_t getbit ( const void * blob, int len, uint32_t bit );
uint32_t getbit_wrap ( const void * blob, int len, uint32_t bit );
void setbit ( void * blob, int len, uint32_t bit );
void setbit ( void * blob, int len, uint32_t bit, uint32_t val );
void clearbit ( void * blob, int len, uint32_t bit );
void flipbit ( void * blob, int len, uint32_t bit );
int countbits ( uint32_t v );
int countbits ( std::vector<uint32_t> & v );
int countbits ( const void * blob, int len );
void invert ( std::vector<uint32_t> & v );
//----------
template< typename T >
inline uint32_t getbit ( T & blob, uint32_t bit )
{
return getbit(&blob,sizeof(blob),bit);
}
template<> inline uint32_t getbit ( uint32_t & blob, uint32_t bit ) { return (blob >> (bit & 31)) & 1; }
template<> inline uint32_t getbit ( uint64_t & blob, uint32_t bit ) { return (blob >> (bit & 63)) & 1; }
//----------
template< typename T >
inline void setbit ( T & blob, uint32_t bit )
{
return setbit(&blob,sizeof(blob),bit);
}
template<> inline void setbit ( uint32_t & blob, uint32_t bit ) { blob |= uint32_t(1) << (bit & 31); }
template<> inline void setbit ( uint64_t & blob, uint32_t bit ) { blob |= uint64_t(1) << (bit & 63); }
//----------
template< typename T >
inline void flipbit ( T & blob, uint32_t bit )
{
flipbit(&blob,sizeof(blob),bit);
}
template<> inline void flipbit ( uint32_t & blob, uint32_t bit ) { bit &= 31; blob ^= (uint32_t(1) << bit); }
template<> inline void flipbit ( uint64_t & blob, uint32_t bit ) { bit &= 63; blob ^= (uint64_t(1) << bit); }
//-----------------------------------------------------------------------------
// Left and right shift of blobs. The shift(N) versions work on chunks of N
// bits at a time (faster)
void lshift1 ( void * blob, int len, int c );
void lshift8 ( void * blob, int len, int c );
void lshift32 ( void * blob, int len, int c );
void rshift1 ( void * blob, int len, int c );
void rshift8 ( void * blob, int len, int c );
void rshift32 ( void * blob, int len, int c );
inline void lshift ( void * blob, int len, int c )
{
if((len & 3) == 0)
{
lshift32(blob,len,c);
}
else
{
lshift8(blob,len,c);
}
}
inline void rshift ( void * blob, int len, int c )
{
if((len & 3) == 0)
{
rshift32(blob,len,c);
}
else
{
rshift8(blob,len,c);
}
}
template < typename T >
inline void lshift ( T & blob, int c )
{
if((sizeof(T) & 3) == 0)
{
lshift32(&blob,sizeof(T),c);
}
else
{
lshift8(&blob,sizeof(T),c);
}
}
template < typename T >
inline void rshift ( T & blob, int c )
{
if((sizeof(T) & 3) == 0)
{
lshift32(&blob,sizeof(T),c);
}
else
{
lshift8(&blob,sizeof(T),c);
}
}
template<> inline void lshift ( uint32_t & blob, int c ) { blob <<= c; }
template<> inline void lshift ( uint64_t & blob, int c ) { blob <<= c; }
template<> inline void rshift ( uint32_t & blob, int c ) { blob >>= c; }
template<> inline void rshift ( uint64_t & blob, int c ) { blob >>= c; }
//-----------------------------------------------------------------------------
// Left and right rotate of blobs. The rot(N) versions work on chunks of N
// bits at a time (faster)
void lrot1 ( void * blob, int len, int c );
void lrot8 ( void * blob, int len, int c );
void lrot32 ( void * blob, int len, int c );
void rrot1 ( void * blob, int len, int c );
void rrot8 ( void * blob, int len, int c );
void rrot32 ( void * blob, int len, int c );
inline void lrot ( void * blob, int len, int c )
{
if((len & 3) == 0)
{
return lrot32(blob,len,c);
}
else
{
return lrot8(blob,len,c);
}
}
inline void rrot ( void * blob, int len, int c )
{
if((len & 3) == 0)
{
return rrot32(blob,len,c);
}
else
{
return rrot8(blob,len,c);
}
}
template < typename T >
inline void lrot ( T & blob, int c )
{
if((sizeof(T) & 3) == 0)
{
return lrot32(&blob,sizeof(T),c);
}
else
{
return lrot8(&blob,sizeof(T),c);
}
}
template < typename T >
inline void rrot ( T & blob, int c )
{
if((sizeof(T) & 3) == 0)
{
return rrot32(&blob,sizeof(T),c);
}
else
{
return rrot8(&blob,sizeof(T),c);
}
}
template<> inline void lrot ( uint32_t & blob, int c ) { if (c) blob = ROTL32(blob,c); }
template<> inline void lrot ( uint64_t & blob, int c ) { if (c) blob = ROTL64(blob,c); }
template<> inline void rrot ( uint32_t & blob, int c ) { if (c) blob = ROTR32(blob,c); }
template<> inline void rrot ( uint64_t & blob, int c ) { if (c) blob = ROTR64(blob,c); }
//-----------------------------------------------------------------------------
// Bit-windowing functions - select some N-bit subset of the input blob
uint32_t window1 ( void * blob, int len, int start, int count );
uint32_t window8 ( void * blob, int len, int start, int count );
uint32_t window32 ( void * blob, int len, int start, int count );
inline uint32_t window ( void * blob, int len, int start, int count )
{
if(len & 3)
{
return window8(blob,len,start,count);
}
else
{
return window32(blob,len,start,count);
}
}
template < typename T >
inline uint32_t window ( T & blob, int start, int count )
{
if((sizeof(T) & 3) == 0)
{
return window32(&blob,sizeof(T),start,count);
}
else
{
return window8(&blob,sizeof(T),start,count);
}
}
template<>
inline uint32_t window ( uint32_t & blob, int start, int count )
{
return ( blob >> start ) & ( ( 1 << count ) - 1 );
}
template<>
inline uint32_t window ( uint64_t & blob, int start, int count )
{
return (uint32_t)( blob >> start ) & ( ( 1 << count ) - 1 );
}
//-----------------------------------------------------------------------------