-
Notifications
You must be signed in to change notification settings - Fork 2
/
bits.h
201 lines (179 loc) · 7.01 KB
/
bits.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
/**
Copyright (c) 2019 Vicente Romero Calero. All rights reserved.
Licensed under the MIT License.
See LICENSE file in the project root for full license information.
*/
#ifndef VTENC_BITS_H_
#define VTENC_BITS_H_
#include <stdint.h>
#include "compiler.h"
static const uint64_t BITS_SIZE_MASK[] = {
0x0ULL,
0x1ULL, 0x3ULL, 0x7ULL, 0xfULL,
0x1fULL, 0x3fULL, 0x7fULL, 0xffULL,
0x1ffULL, 0x3ffULL, 0x7ffULL, 0xfffULL,
0x1fffULL, 0x3fffULL, 0x7fffULL, 0xffffULL,
0x1ffffULL, 0x3ffffULL, 0x7ffffULL, 0xfffffULL,
0x1fffffULL, 0x3fffffULL, 0x7fffffULL, 0xffffffULL,
0x1ffffffULL, 0x3ffffffULL, 0x7ffffffULL, 0xfffffffULL,
0x1fffffffULL, 0x3fffffffULL, 0x7fffffffULL, 0xffffffffULL,
0x1ffffffffULL, 0x3ffffffffULL, 0x7ffffffffULL, 0xfffffffffULL,
0x1fffffffffULL, 0x3fffffffffULL, 0x7fffffffffULL, 0xffffffffffULL,
0x1ffffffffffULL, 0x3ffffffffffULL, 0x7ffffffffffULL, 0xfffffffffffULL,
0x1fffffffffffULL, 0x3fffffffffffULL, 0x7fffffffffffULL, 0xffffffffffffULL,
0x1ffffffffffffULL, 0x3ffffffffffffULL, 0x7ffffffffffffULL, 0xfffffffffffffULL,
0x1fffffffffffffULL, 0x3fffffffffffffULL, 0x7fffffffffffffULL, 0xffffffffffffffULL,
0x1ffffffffffffffULL, 0x3ffffffffffffffULL, 0x7ffffffffffffffULL, 0xfffffffffffffffULL,
0x1fffffffffffffffULL, 0x3fffffffffffffffULL, 0x7fffffffffffffffULL, 0xffffffffffffffffULL
};
#define BITS_SIZE_MASK_LEN (sizeof(BITS_SIZE_MASK) / sizeof(BITS_SIZE_MASK[0]))
static const uint8_t BITS_POS_MASK8[] = {
0x1, 0x2, 0x4, 0x8,
0x10, 0x20, 0x40, 0x80
};
static const uint16_t BITS_POS_MASK16[] = {
0x1, 0x2, 0x4, 0x8,
0x10, 0x20, 0x40, 0x80,
0x100, 0x200, 0x400, 0x800,
0x1000, 0x2000, 0x4000, 0x8000,
};
static const uint32_t BITS_POS_MASK32[] = {
0x1, 0x2, 0x4, 0x8,
0x10, 0x20, 0x40, 0x80,
0x100, 0x200, 0x400, 0x800,
0x1000, 0x2000, 0x4000, 0x8000,
0x10000, 0x20000, 0x40000, 0x80000,
0x100000, 0x200000, 0x400000, 0x800000,
0x1000000, 0x2000000, 0x4000000, 0x8000000,
0x10000000, 0x20000000, 0x40000000, 0x80000000
};
static const uint64_t BITS_POS_MASK64[] = {
0x1ULL, 0x2ULL, 0x4ULL, 0x8ULL,
0x10ULL, 0x20ULL, 0x40ULL, 0x80ULL,
0x100ULL, 0x200ULL, 0x400ULL, 0x800ULL,
0x1000ULL, 0x2000ULL, 0x4000ULL, 0x8000ULL,
0x10000ULL, 0x20000ULL, 0x40000ULL, 0x80000ULL,
0x100000ULL, 0x200000ULL, 0x400000ULL, 0x800000ULL,
0x1000000ULL, 0x2000000ULL, 0x4000000ULL, 0x8000000ULL,
0x10000000ULL, 0x20000000ULL, 0x40000000ULL, 0x80000000ULL,
0x100000000ULL, 0x200000000ULL, 0x400000000ULL, 0x800000000ULL,
0x1000000000ULL, 0x2000000000ULL, 0x4000000000ULL, 0x8000000000ULL,
0x10000000000ULL, 0x20000000000ULL, 0x40000000000ULL, 0x80000000000ULL,
0x100000000000ULL, 0x200000000000ULL, 0x400000000000ULL, 0x800000000000ULL,
0x1000000000000ULL, 0x2000000000000ULL, 0x4000000000000ULL, 0x8000000000000ULL,
0x10000000000000ULL, 0x20000000000000ULL, 0x40000000000000ULL, 0x80000000000000ULL,
0x100000000000000ULL, 0x200000000000000ULL, 0x400000000000000ULL, 0x800000000000000ULL,
0x1000000000000000ULL, 0x2000000000000000ULL, 0x4000000000000000ULL, 0x8000000000000000ULL
};
static const unsigned int BITS_LEN8[] = {
1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8
};
static inline uint16_t bits_swap_u16(uint16_t value)
{
#ifdef __HAVE_BUILTIN_BSWAP16__
return __builtin_bswap16(value);
#else
return (value << 8) | (value >> 8);
#endif
}
static inline uint32_t bits_swap_u32(uint32_t value)
{
#ifdef __HAVE_BUILTIN_BSWAP32__
return __builtin_bswap32(value);
#else
return ((value << 24) & 0xff000000 ) |
((value << 8) & 0x00ff0000 ) |
((value >> 8) & 0x0000ff00 ) |
((value >> 24) & 0x000000ff );
#endif
}
static inline uint64_t bits_swap_u64(uint64_t value)
{
#ifdef __HAVE_BUILTIN_BSWAP64__
return __builtin_bswap64(value);
#else
return ((value << 56) & 0xff00000000000000ULL) |
((value << 40) & 0x00ff000000000000ULL) |
((value << 24) & 0x0000ff0000000000ULL) |
((value << 8) & 0x000000ff00000000ULL) |
((value >> 8) & 0x00000000ff000000ULL) |
((value >> 24) & 0x0000000000ff0000ULL) |
((value >> 40) & 0x000000000000ff00ULL) |
((value >> 56) & 0x00000000000000ffULL);
#endif
}
static inline unsigned int bits_len_u8(uint8_t value)
{
return BITS_LEN8[value];
}
static inline unsigned int bits_len_u16(uint16_t value)
{
unsigned int n = 0;
if (value > 0x00ff) {
value >>= 8;
n = 8;
}
return n + BITS_LEN8[value];
}
static inline unsigned int bits_len_u32(uint32_t value)
{
#ifdef __HAVE_BUILTIN_CLZ__
if (value == 0) {
return 1;
} else {
return 32 - __builtin_clz(value);
}
#else
unsigned int n = 0;
if (value > 0x0000ffff) {
value >>= 16;
n = 16;
}
if (value > 0x000000ff) {
value >>= 8;
n += 8;
}
return n + BITS_LEN8[value];
#endif
}
static inline unsigned int bits_len_u64(uint64_t value)
{
#ifdef __HAVE_BUILTIN_CLZLL__
if (value == 0) {
return 1;
} else {
return 64 - __builtin_clzll(value);
}
#else
unsigned int n = 0;
if (value > 0x00000000ffffffffULL) {
value >>= 32;
n = 32;
}
if (value > 0x000000000000ffffULL) {
value >>= 16;
n += 16;
}
if (value > 0x00000000000000ffULL) {
value >>= 8;
n += 8;
}
return n + BITS_LEN8[value];
#endif
}
#endif /* VTENC_BITS_H_ */