forked from makomk/aeskeyfind
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.h
36 lines (28 loc) · 1.23 KB
/
util.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
#ifndef __UTIL_H__
#define __UTIL_H__
// Return bit n of vector.(1bit行の任意の場所のbitを返す)
static inline int bit(uint32_t vector, int n) {
return (vector >> n) & 1;
}
// Set byte n of vector to val.(1bit行のn番目に値をセット)
// return setted vector.
static inline uint32_t set_byte(uint32_t vector, int n, uint8_t val) {
return (vector & ~(0xFF << (8*n))) | (val << (8*n));
}
// Return byte n of vector.
static inline uint8_t get_byte(uint32_t vector, int n) {
return (vector >> (8*n)) & 0xFF;
}
extern const uint64_t m1; // binary: 0101...
extern const uint64_t m2; // binary: 00110011..
extern const uint64_t m4; // binary: 4 zeros, 4 ones ...
extern const uint64_t h01; // the sum of 256 to the power of 0,1,2,3...
// Return the number of bits in x that are 1.(A.K.A. bit count)
static inline int popcount(uint64_t x) {
x -= (x >> 1) & m1; // put count of each 2 bits into those 2 bits
x = (x & m2) + ((x >> 2) & m2); // put count of each 4 bits into those 4 bits
x = (x + (x >> 4)) & m4; // put count of each 8 bits into those 8 bits
return (x * h01) >> 56; // returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24) + ...
}
void print_word(uint32_t word);
#endif//__UTIL_H__