-
Notifications
You must be signed in to change notification settings - Fork 24
/
Range.hpp
239 lines (210 loc) · 5.6 KB
/
Range.hpp
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
/* MCM file compressor
Copyright (C) 2013, Google Inc.
Authors: Mathieu Chartier
LICENSE
This file is part of the MCM file compressor.
MCM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MCM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with MCM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _RANGE_HPP_
#define _RANGE_HPP_
#include "Compressor.hpp"
#include <assert.h>
#include "Model.hpp"
// From 7zip, added single bit functions
class Range7 {
private:
static constexpr uint32_t TopBits = 24, TopValue = 1 << TopBits, Top = 0xFFFFFFFF;
uint32_t Range = Top, Code = 0, _cacheSize = 1;
uint64_t Low = 0;
uint8_t _cache = 0;
template <typename TOut>
ALWAYS_INLINE void shiftLow(TOut& sout) { //Emit the top byte
if (static_cast<uint32_t>(Low) < static_cast<uint32_t>(0xFF << TopBits) || (Low >> 32) != 0) {
uint8_t temp = _cache;
do {
sout.put(uint8_t(temp + uint8_t(Low >> 32)));
temp = 0xFF;
} while (--_cacheSize);
_cache = uint8_t(uint32_t(Low >> 24));
}
++_cacheSize;
Low = static_cast<uint32_t>(Low << 8);
}
public:
template <typename TOut>
ALWAYS_INLINE void IncreaseRange(TOut& out) {
while (Range < TopValue) {
Range <<= 8;
shiftLow(out);
}
}
ALWAYS_INLINE uint32_t getDecodedBit(uint32_t p, uint32_t shift) {
const uint32_t mid = (Range >> shift) * p;
uint32_t bit = Code < mid;
if (bit) {
Range = mid;
} else {
Code -= mid;
Range -= mid;
}
return bit;
}
template <typename TOut>
ALWAYS_INLINE void encode(TOut& out, uint32_t bit, uint32_t p, uint32_t shift) {
assert(p < (1U << shift));
assert(p != 0U);
const uint32_t mid = (Range >> shift) * p;
if (bit) {
Range = mid;
} else {
Low += mid;
Range -= mid;
}
IncreaseRange(out);
}
template <typename TIn>
ALWAYS_INLINE uint32_t decode(TIn& in, uint32_t p, uint32_t shift) {
assert(p < (1U << shift));
assert(p != 0U);
auto ret = getDecodedBit(p, shift);
Normalize(in);
return ret;
}
template <typename TOut>
inline void encodeBit(TOut& out, uint32_t bit) {
Range >>= 1;
if (bit) {
Low += Range;
}
while (Range < TopValue) {
Range <<= 8;
shiftLow(out);
}
}
template <typename TIn>
inline uint32_t decodeBit(TIn& in) {
uint32_t top = Range;
Range >>= 1;
if (Code >= Range) {
Code -= Range;
Normalize(in);
return 1;
} else {
Normalize(in);
return 0;
}
}
template <typename TOut>
void EncodeBits(TOut& Out, uint32_t value, int numBits) {
for (numBits--; numBits >= 0; numBits--) {
Range >>= 1;
Low += Range & (0 - ((value >> numBits) & 1));
while (Range < TopValue) {
Range <<= 8;
shiftLow(Out);
}
}
}
template <typename TIn>
uint32_t DecodeDirectBits(TIn& In, int numTotalBits) {
uint32_t range = Range, code = Code, result = 0;
for (int i = numTotalBits; i != 0; i--) {
range >>= 1;
uint32_t t = (code - range) >> 31;
code -= range & (t - 1);
result = (result << 1) | (1 - t);
if (range < TopValue) {
code = (code << 8) | (In.get() & 0xFF);
range <<= 8;
}
}
Range = range;
Code = code;
return result;
}
template <typename TIn>
uint32_t DecodeDirectBit(TIn& sin) {
Range >>= 1;
uint32_t t = (Code - Range) >> 31;
Code -= Range & (t - 1);
Normalize(sin);
return t ^ 1;
}
template <typename TOut>
inline void Encode(TOut& Out, uint32_t start, uint32_t size, uint32_t total) {
assert(size > 0);
assert(start < total);
assert(start + size <= total);
Range /= total;
Low += start * Range;
Range *= size;
while (Range < TopValue) {
Range <<= 8;
shiftLow(Out);
}
}
template <typename TIn>
inline void Decode(TIn& In, uint32_t start, uint32_t size) {
Code -= start * Range;
Range *= size;
Normalize(In);
}
template <typename TOut>
inline void encodeDirect(TOut& Out, uint32_t start, uint32_t total) {
assert(start < total);
Range /= total;
Low += start * Range;
while (Range < TopValue) {
Range <<= 8;
shiftLow(Out);
}
}
template <typename TIn>
inline uint32_t decodeByte(TIn& In) {
Range >>= 8;
uint32_t start = Code / Range;
Code -= start * Range;
Normalize(In);
return start;
}
template <typename TIn>
inline uint32_t decodeDirect(TIn& In, uint32_t Total) {
uint32_t start = GetThreshold(Total);
Code -= start * Range;
Normalize(In);
return start;
}
template <typename TOut>
void flush(TOut& Out) {
for (uint32_t i = 0; i < 5; i++)
shiftLow(Out);
}
template <typename TIn>
void initDecoder(TIn& In) {
*this = Range7();
Code = 0;
Range = Top;
for (uint32_t i = 0; i < 5; i++)
Code = (Code << 8) | (In.get() & 0xFF);
}
inline uint32_t GetThreshold(uint32_t Total) {
return Code / (Range /= Total);
}
template <typename TIn>
ALWAYS_INLINE void Normalize(TIn& In) {
while (Range < TopValue) {
Code = (Code << 8) | (In.get() & 0xFF);
Range <<= 8;
}
}
};
#endif