This repository has been archived by the owner on Sep 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathbitcoder.h
203 lines (175 loc) · 4.56 KB
/
bitcoder.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
#ifndef IZ_BITCODER_H
#define IZ_BITCODER_H 1
#include "intmacros.h"
namespace IZ {
/* 64 bit unsigned datatype. On x86-32, the 64 bit datatypes are slow, so
* use MMX if available.
*/
#if defined(__LP64__) || defined(__L64__)
typedef unsigned long U64;
typedef unsigned int U32;
#elif defined(__LLP64__) || defined(__LL64__) || defined(_WIN64)
typedef unsigned long long U64;
typedef unsigned int U32;
#elif defined(__MMX__)
#include <mmintrin.h>
typedef __m64 U64;
typedef unsigned int U32;
#define USE_MMX
#else
typedef unsigned long long U64;
typedef unsigned int U32;
#endif
template<typename Code = U32>
class BitCoderBase
{
protected:
/**
* Data types the bit coder uses
*
* Code is used to store coding units
* Cache is used for the bit cache
*
* Usually, you are going to write single bytes for codes, so this
* would be "unsigned char".
* You may, however, use larger coding units to speed up the coding.
*
* NOTE size(CacheBits) >= 2 * sizeof(CodeBits) must be true
*/
typedef U64 Cache;
enum Constants {
CodeBits = sizeof(Code) * CHAR_BIT,
CacheBits = sizeof(Cache) * CHAR_BIT
};
protected:
/**
* The number of valid bits in bit cache
*/
unsigned int len;
/**
* The bit cache
*/
Cache bitcache;
};
template<typename Code = U32>
class BitDecoder : public BitCoderBase<Code>
{
using BitCoderBase<Code>::len;
using BitCoderBase<Code>::bitcache;
using BitCoderBase<Code>::CodeBits;
public:
Code fetchCode() {
return /*__builtin_bswap32*/(*p++);
}
void begin(const unsigned char *ptr) {
p = (const Code *) ptr;
#if defined(USE_MMX)
bitcache = _mm_cvtsi32_si64(fetchCode());
#else
bitcache = fetchCode();
#endif
len = CodeBits;
}
void fillCache() {
if (len < CodeBits) {
#if defined(USE_MMX)
bitcache = _mm_slli_si64(bitcache, CodeBits);
bitcache = _mm_or_si64(bitcache, _mm_cvtsi32_si64(fetchCode()));
#else
bitcache <<= CodeBits;
bitcache += fetchCode();
#endif
len += CodeBits;
}
}
unsigned int peekBits(unsigned int count) const {
#if defined(USE_MMX)
return _mm_cvtsi64_si32(_mm_srli_si64(bitcache, len - count)) & bitMask(count);
#else
return (bitcache >> (len - count)) & bitMask(count);
#endif
}
void skipBits(unsigned int count) {
len -= count;
}
unsigned int cachedLength() const {
return len;
}
unsigned int readBits(unsigned int count) {
len -= count;
#if defined(USE_MMX)
return _mm_cvtsi64_si32(_mm_srli_si64(bitcache, len)) & bitMask(count);
#else
return (bitcache >> len) & bitMask(count);
#endif
}
void align() {
len = 0;
}
const unsigned char *end() {
#if defined(USE_MMX)
_mm_empty();
#endif
return (const unsigned char *) (p - (len >= CodeBits));
}
private:
const Code *p;
};
template<typename Code = U32>
class BitEncoder : public BitCoderBase<Code>
{
using BitCoderBase<Code>::len;
using BitCoderBase<Code>::bitcache;
using BitCoderBase<Code>::CodeBits;
public:
void storeCode(Code code) {
*p++ = /*__builtin_bswap32*/(code);
}
void begin(unsigned char *ptr) {
p = (Code *) ptr;
len = 0;
#if defined(USE_MMX)
bitcache = _mm_cvtsi32_si64(0);
#else
bitcache = 0; // silence compiler
#endif
}
void flushCache() {
if (len >= CodeBits) {
len -= CodeBits;
#if defined(USE_MMX)
storeCode(_mm_cvtsi64_si32(_mm_srli_si64(bitcache, len)));
#else
storeCode(bitcache >> len);
#endif
}
}
void writeBits(unsigned int bits, unsigned int count) {
len += count;
#if defined(USE_MMX)
bitcache = _mm_slli_si64(bitcache, count);
bitcache = _mm_or_si64(bitcache, _mm_cvtsi32_si64(bits));
#else
bitcache = (bitcache << count) + bits;
#endif
}
void align() {
if (len > 0) {
#if defined(USE_MMX)
storeCode(_mm_cvtsi64_si32(_mm_slli_si64(bitcache, CodeBits - len)));
_mm_empty();
#else
storeCode(bitcache << (CodeBits - len));
#endif
len = 0;
}
}
unsigned char *end() {
align();
return (unsigned char *) p;
}
private:
Code *p;
};
} // namespace IZ
#endif