brotli 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.gitmodules +3 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/README.md +36 -0
- data/Rakefile +13 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/brotli.gemspec +28 -0
- data/ext/brotli/brotli.cc +67 -0
- data/ext/brotli/brotli.h +9 -0
- data/ext/brotli/extconf.rb +34 -0
- data/lib/brotli.rb +2 -0
- data/lib/brotli/version.rb +3 -0
- data/vendor/brotli/LICENSE +202 -0
- data/vendor/brotli/dec/Makefile +12 -0
- data/vendor/brotli/dec/bit_reader.c +55 -0
- data/vendor/brotli/dec/bit_reader.h +256 -0
- data/vendor/brotli/dec/context.h +260 -0
- data/vendor/brotli/dec/decode.c +1573 -0
- data/vendor/brotli/dec/decode.h +160 -0
- data/vendor/brotli/dec/dictionary.h +9494 -0
- data/vendor/brotli/dec/huffman.c +325 -0
- data/vendor/brotli/dec/huffman.h +77 -0
- data/vendor/brotli/dec/port.h +148 -0
- data/vendor/brotli/dec/prefix.h +756 -0
- data/vendor/brotli/dec/state.c +149 -0
- data/vendor/brotli/dec/state.h +185 -0
- data/vendor/brotli/dec/streams.c +99 -0
- data/vendor/brotli/dec/streams.h +100 -0
- data/vendor/brotli/dec/transform.h +315 -0
- data/vendor/brotli/dec/types.h +36 -0
- data/vendor/brotli/enc/Makefile +11 -0
- data/vendor/brotli/enc/backward_references.cc +769 -0
- data/vendor/brotli/enc/backward_references.h +50 -0
- data/vendor/brotli/enc/bit_cost.h +147 -0
- data/vendor/brotli/enc/block_splitter.cc +418 -0
- data/vendor/brotli/enc/block_splitter.h +78 -0
- data/vendor/brotli/enc/brotli_bit_stream.cc +884 -0
- data/vendor/brotli/enc/brotli_bit_stream.h +149 -0
- data/vendor/brotli/enc/cluster.h +290 -0
- data/vendor/brotli/enc/command.h +140 -0
- data/vendor/brotli/enc/context.h +185 -0
- data/vendor/brotli/enc/dictionary.h +9485 -0
- data/vendor/brotli/enc/dictionary_hash.h +4125 -0
- data/vendor/brotli/enc/encode.cc +715 -0
- data/vendor/brotli/enc/encode.h +196 -0
- data/vendor/brotli/enc/encode_parallel.cc +354 -0
- data/vendor/brotli/enc/encode_parallel.h +37 -0
- data/vendor/brotli/enc/entropy_encode.cc +492 -0
- data/vendor/brotli/enc/entropy_encode.h +88 -0
- data/vendor/brotli/enc/fast_log.h +179 -0
- data/vendor/brotli/enc/find_match_length.h +87 -0
- data/vendor/brotli/enc/hash.h +686 -0
- data/vendor/brotli/enc/histogram.cc +76 -0
- data/vendor/brotli/enc/histogram.h +100 -0
- data/vendor/brotli/enc/literal_cost.cc +172 -0
- data/vendor/brotli/enc/literal_cost.h +38 -0
- data/vendor/brotli/enc/metablock.cc +544 -0
- data/vendor/brotli/enc/metablock.h +88 -0
- data/vendor/brotli/enc/port.h +151 -0
- data/vendor/brotli/enc/prefix.h +85 -0
- data/vendor/brotli/enc/ringbuffer.h +108 -0
- data/vendor/brotli/enc/static_dict.cc +441 -0
- data/vendor/brotli/enc/static_dict.h +40 -0
- data/vendor/brotli/enc/static_dict_lut.h +12063 -0
- data/vendor/brotli/enc/streams.cc +127 -0
- data/vendor/brotli/enc/streams.h +129 -0
- data/vendor/brotli/enc/transform.h +250 -0
- data/vendor/brotli/enc/write_bits.h +91 -0
- metadata +171 -0
@@ -0,0 +1,325 @@
|
|
1
|
+
/* Copyright 2013 Google Inc. All Rights Reserved.
|
2
|
+
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
you may not use this file except in compliance with the License.
|
5
|
+
You may obtain a copy of the License at
|
6
|
+
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
See the License for the specific language governing permissions and
|
13
|
+
limitations under the License.
|
14
|
+
*/
|
15
|
+
|
16
|
+
/* Utilities for building Huffman decoding tables. */
|
17
|
+
|
18
|
+
#include <stdlib.h>
|
19
|
+
#include <stdio.h>
|
20
|
+
#include <string.h>
|
21
|
+
#include "./huffman.h"
|
22
|
+
#include "./port.h"
|
23
|
+
|
24
|
+
#if defined(__cplusplus) || defined(c_plusplus)
|
25
|
+
extern "C" {
|
26
|
+
#endif
|
27
|
+
|
28
|
+
/* Returns reverse(reverse(key, len) + 1, len), where reverse(key, len) is the
|
29
|
+
bit-wise reversal of the len least significant bits of key. */
|
30
|
+
static BROTLI_INLINE uint32_t GetNextKey(uint32_t key, int len) {
|
31
|
+
#ifdef BROTLI_RBIT
|
32
|
+
return BROTLI_RBIT(BROTLI_RBIT(key) + (1 << (8 * sizeof(unsigned) - len)));
|
33
|
+
#else
|
34
|
+
unsigned step = (unsigned)(1 << (len - 1));
|
35
|
+
while (key & step) {
|
36
|
+
step >>= 1;
|
37
|
+
}
|
38
|
+
return (key & (step - 1)) + step;
|
39
|
+
#endif
|
40
|
+
}
|
41
|
+
|
42
|
+
/* Stores code in table[0], table[step], table[2*step], ..., table[end] */
|
43
|
+
/* Assumes that end is an integer multiple of step */
|
44
|
+
static BROTLI_INLINE void ReplicateValue(HuffmanCode* table,
|
45
|
+
int step, int end,
|
46
|
+
HuffmanCode code) {
|
47
|
+
do {
|
48
|
+
end -= step;
|
49
|
+
table[end] = code;
|
50
|
+
} while (end > 0);
|
51
|
+
}
|
52
|
+
|
53
|
+
/* Returns the table width of the next 2nd level table. count is the histogram
|
54
|
+
of bit lengths for the remaining symbols, len is the code length of the next
|
55
|
+
processed symbol */
|
56
|
+
static BROTLI_INLINE int NextTableBitSize(const uint16_t* const count,
|
57
|
+
int len, int root_bits) {
|
58
|
+
int left = 1 << (len - root_bits);
|
59
|
+
while (len < BROTLI_HUFFMAN_MAX_CODE_LENGTH) {
|
60
|
+
left -= count[len];
|
61
|
+
if (left <= 0) break;
|
62
|
+
++len;
|
63
|
+
left <<= 1;
|
64
|
+
}
|
65
|
+
return len - root_bits;
|
66
|
+
}
|
67
|
+
|
68
|
+
|
69
|
+
void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table,
|
70
|
+
const uint8_t* const code_lengths,
|
71
|
+
uint16_t *count) {
|
72
|
+
HuffmanCode code; /* current table entry */
|
73
|
+
int symbol; /* symbol index in original or sorted table */
|
74
|
+
unsigned key; /* reversed prefix code */
|
75
|
+
int step; /* step size to replicate values in current table */
|
76
|
+
int table_size; /* size of current table */
|
77
|
+
int sorted[18]; /* symbols sorted by code length */
|
78
|
+
/* offsets in sorted table for each length */
|
79
|
+
int offset[BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH + 1];
|
80
|
+
int bits;
|
81
|
+
int bits_count;
|
82
|
+
|
83
|
+
/* generate offsets into sorted symbol table by code length */
|
84
|
+
symbol = -1;
|
85
|
+
bits = 1;
|
86
|
+
BROTLI_REPEAT(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH, {
|
87
|
+
symbol += count[bits];
|
88
|
+
offset[bits] = symbol;
|
89
|
+
bits++;
|
90
|
+
});
|
91
|
+
offset[0] = 17;
|
92
|
+
|
93
|
+
/* sort symbols by length, by symbol order within each length */
|
94
|
+
symbol = 18;
|
95
|
+
do {
|
96
|
+
BROTLI_REPEAT(6, {
|
97
|
+
symbol--;
|
98
|
+
sorted[offset[code_lengths[symbol]]--] = symbol;
|
99
|
+
});
|
100
|
+
} while (symbol != 0);
|
101
|
+
|
102
|
+
table_size = 1 << BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH;
|
103
|
+
|
104
|
+
/* special case code with only one value */
|
105
|
+
if (offset[BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH] == 0) {
|
106
|
+
code.bits = 0;
|
107
|
+
code.value = (uint16_t)sorted[0];
|
108
|
+
for (key = 0; key < table_size; ++key) {
|
109
|
+
table[key] = code;
|
110
|
+
}
|
111
|
+
return;
|
112
|
+
}
|
113
|
+
|
114
|
+
/* fill in table */
|
115
|
+
key = 0;
|
116
|
+
symbol = 0;
|
117
|
+
bits = 1;
|
118
|
+
step = 2;
|
119
|
+
do {
|
120
|
+
code.bits = (uint8_t)bits;
|
121
|
+
for (bits_count = count[bits]; bits_count != 0; --bits_count) {
|
122
|
+
code.value = (uint16_t)sorted[symbol++];
|
123
|
+
ReplicateValue(&table[key], step, table_size, code);
|
124
|
+
key = GetNextKey(key, bits);
|
125
|
+
}
|
126
|
+
step <<= 1;
|
127
|
+
} while (++bits <= BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH);
|
128
|
+
}
|
129
|
+
|
130
|
+
int BrotliBuildHuffmanTable(HuffmanCode* root_table,
|
131
|
+
int root_bits,
|
132
|
+
const uint16_t* const symbol_lists,
|
133
|
+
uint16_t *count) {
|
134
|
+
HuffmanCode code; /* current table entry */
|
135
|
+
HuffmanCode* table; /* next available space in table */
|
136
|
+
int len; /* current code length */
|
137
|
+
int symbol; /* symbol index in original or sorted table */
|
138
|
+
unsigned key; /* reversed prefix code */
|
139
|
+
int step; /* step size to replicate values in current table */
|
140
|
+
unsigned low; /* low bits for current root entry */
|
141
|
+
unsigned mask; /* mask for low bits */
|
142
|
+
int table_bits; /* key length of current table */
|
143
|
+
int table_size; /* size of current table */
|
144
|
+
int total_size; /* sum of root table size and 2nd level table sizes */
|
145
|
+
int max_length = -1;
|
146
|
+
int bits;
|
147
|
+
int bits_count;
|
148
|
+
|
149
|
+
while (symbol_lists[max_length] == 0xFFFF) max_length--;
|
150
|
+
max_length += BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1;
|
151
|
+
|
152
|
+
table = root_table;
|
153
|
+
table_bits = root_bits;
|
154
|
+
table_size = 1 << table_bits;
|
155
|
+
total_size = table_size;
|
156
|
+
|
157
|
+
/* fill in root table */
|
158
|
+
/* let's reduce the table size to a smaller size if possible, and */
|
159
|
+
/* create the repetitions by memcpy if possible in the coming loop */
|
160
|
+
if (table_bits > max_length) {
|
161
|
+
table_bits = max_length;
|
162
|
+
table_size = 1 << table_bits;
|
163
|
+
}
|
164
|
+
key = 0;
|
165
|
+
bits = 1;
|
166
|
+
step = 2;
|
167
|
+
do {
|
168
|
+
code.bits = (uint8_t)bits;
|
169
|
+
symbol = bits - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1);
|
170
|
+
for (bits_count = count[bits]; bits_count != 0; --bits_count) {
|
171
|
+
symbol = symbol_lists[symbol];
|
172
|
+
code.value = (uint16_t)symbol;
|
173
|
+
ReplicateValue(&table[key], step, table_size, code);
|
174
|
+
key = GetNextKey(key, bits);
|
175
|
+
}
|
176
|
+
step <<= 1;
|
177
|
+
} while (++bits <= table_bits);
|
178
|
+
|
179
|
+
/* if root_bits != table_bits we only created one fraction of the */
|
180
|
+
/* table, and we need to replicate it now. */
|
181
|
+
while (total_size != table_size) {
|
182
|
+
memcpy(&table[table_size], &table[0],
|
183
|
+
(size_t)table_size * sizeof(table[0]));
|
184
|
+
table_size <<= 1;
|
185
|
+
}
|
186
|
+
|
187
|
+
/* fill in 2nd level tables and add pointers to root table */
|
188
|
+
mask = (unsigned)(total_size - 1);
|
189
|
+
low = (unsigned)-1;
|
190
|
+
for (len = root_bits + 1, step = 2; len <= max_length; ++len, step <<= 1) {
|
191
|
+
symbol = len - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1);
|
192
|
+
for (; count[len] != 0; --count[len]) {
|
193
|
+
if ((key & mask) != low) {
|
194
|
+
table += table_size;
|
195
|
+
table_bits = NextTableBitSize(count, len, root_bits);
|
196
|
+
table_size = 1 << table_bits;
|
197
|
+
total_size += table_size;
|
198
|
+
low = key & mask;
|
199
|
+
root_table[low].bits = (uint8_t)(table_bits + root_bits);
|
200
|
+
root_table[low].value = (uint16_t)(
|
201
|
+
((size_t)(table - root_table)) - low);
|
202
|
+
}
|
203
|
+
code.bits = (uint8_t)(len - root_bits);
|
204
|
+
symbol = symbol_lists[symbol];
|
205
|
+
code.value = (uint16_t)symbol;
|
206
|
+
ReplicateValue(&table[key >> root_bits], step, table_size, code);
|
207
|
+
key = GetNextKey(key, len);
|
208
|
+
}
|
209
|
+
}
|
210
|
+
return total_size;
|
211
|
+
}
|
212
|
+
|
213
|
+
int BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
|
214
|
+
int root_bits,
|
215
|
+
uint16_t *val,
|
216
|
+
uint32_t num_symbols) {
|
217
|
+
int table_size = 1;
|
218
|
+
const int goal_size = 1 << root_bits;
|
219
|
+
switch (num_symbols) {
|
220
|
+
case 0:
|
221
|
+
table[0].bits = 0;
|
222
|
+
table[0].value = val[0];
|
223
|
+
break;
|
224
|
+
case 1:
|
225
|
+
table[0].bits = 1;
|
226
|
+
table[1].bits = 1;
|
227
|
+
if (val[1] > val[0]) {
|
228
|
+
table[0].value = val[0];
|
229
|
+
table[1].value = val[1];
|
230
|
+
} else {
|
231
|
+
table[0].value = val[1];
|
232
|
+
table[1].value = val[0];
|
233
|
+
}
|
234
|
+
table_size = 2;
|
235
|
+
break;
|
236
|
+
case 2:
|
237
|
+
table[0].bits = 1;
|
238
|
+
table[0].value = val[0];
|
239
|
+
table[2].bits = 1;
|
240
|
+
table[2].value = val[0];
|
241
|
+
if (val[2] > val[1]) {
|
242
|
+
table[1].value = val[1];
|
243
|
+
table[3].value = val[2];
|
244
|
+
} else {
|
245
|
+
table[1].value = val[2];
|
246
|
+
table[3].value = val[1];
|
247
|
+
}
|
248
|
+
table[1].bits = 2;
|
249
|
+
table[3].bits = 2;
|
250
|
+
table_size = 4;
|
251
|
+
break;
|
252
|
+
case 3:
|
253
|
+
{
|
254
|
+
int i, k;
|
255
|
+
for (i = 0; i < 3; ++i) {
|
256
|
+
for (k = i + 1; k < 4; ++k) {
|
257
|
+
if (val[k] < val[i]) {
|
258
|
+
uint16_t t = val[k];
|
259
|
+
val[k] = val[i];
|
260
|
+
val[i] = t;
|
261
|
+
}
|
262
|
+
}
|
263
|
+
}
|
264
|
+
for (i = 0; i < 4; ++i) {
|
265
|
+
table[i].bits = 2;
|
266
|
+
}
|
267
|
+
table[0].value = val[0];
|
268
|
+
table[2].value = val[1];
|
269
|
+
table[1].value = val[2];
|
270
|
+
table[3].value = val[3];
|
271
|
+
table_size = 4;
|
272
|
+
}
|
273
|
+
break;
|
274
|
+
case 4:
|
275
|
+
{
|
276
|
+
int i;
|
277
|
+
if (val[3] < val[2]) {
|
278
|
+
uint16_t t = val[3];
|
279
|
+
val[3] = val[2];
|
280
|
+
val[2] = t;
|
281
|
+
}
|
282
|
+
for (i = 0; i < 7; ++i) {
|
283
|
+
table[i].value = val[0];
|
284
|
+
table[i].bits = (uint8_t)(1 + (i & 1));
|
285
|
+
}
|
286
|
+
table[1].value = val[1];
|
287
|
+
table[3].value = val[2];
|
288
|
+
table[5].value = val[1];
|
289
|
+
table[7].value = val[3];
|
290
|
+
table[3].bits = 3;
|
291
|
+
table[7].bits = 3;
|
292
|
+
table_size = 8;
|
293
|
+
}
|
294
|
+
break;
|
295
|
+
}
|
296
|
+
while (table_size != goal_size) {
|
297
|
+
memcpy(&table[table_size], &table[0],
|
298
|
+
(size_t)table_size * sizeof(table[0]));
|
299
|
+
table_size <<= 1;
|
300
|
+
}
|
301
|
+
return goal_size;
|
302
|
+
}
|
303
|
+
|
304
|
+
void BrotliHuffmanTreeGroupInit(HuffmanTreeGroup* group, int alphabet_size,
|
305
|
+
int ntrees) {
|
306
|
+
/* Pack two mallocs into one */
|
307
|
+
const size_t code_size =
|
308
|
+
sizeof(HuffmanCode) * (size_t)(ntrees * BROTLI_HUFFMAN_MAX_TABLE_SIZE);
|
309
|
+
const size_t htree_size = sizeof(HuffmanCode*) * (size_t)ntrees;
|
310
|
+
char *p = (char*)malloc(code_size + htree_size);
|
311
|
+
group->alphabet_size = (int16_t)alphabet_size;
|
312
|
+
group->num_htrees = (int16_t)ntrees;
|
313
|
+
group->codes = (HuffmanCode*)p;
|
314
|
+
group->htrees = (HuffmanCode**)(p + code_size);
|
315
|
+
}
|
316
|
+
|
317
|
+
void BrotliHuffmanTreeGroupRelease(HuffmanTreeGroup* group) {
|
318
|
+
if (group->codes) {
|
319
|
+
free(group->codes);
|
320
|
+
}
|
321
|
+
}
|
322
|
+
|
323
|
+
#if defined(__cplusplus) || defined(c_plusplus)
|
324
|
+
} /* extern "C" */
|
325
|
+
#endif
|
@@ -0,0 +1,77 @@
|
|
1
|
+
/* Copyright 2013 Google Inc. All Rights Reserved.
|
2
|
+
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
you may not use this file except in compliance with the License.
|
5
|
+
You may obtain a copy of the License at
|
6
|
+
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
See the License for the specific language governing permissions and
|
13
|
+
limitations under the License.
|
14
|
+
*/
|
15
|
+
|
16
|
+
/* Utilities for building Huffman decoding tables. */
|
17
|
+
|
18
|
+
#ifndef BROTLI_DEC_HUFFMAN_H_
|
19
|
+
#define BROTLI_DEC_HUFFMAN_H_
|
20
|
+
|
21
|
+
#include "./types.h"
|
22
|
+
|
23
|
+
#if defined(__cplusplus) || defined(c_plusplus)
|
24
|
+
extern "C" {
|
25
|
+
#endif
|
26
|
+
|
27
|
+
#define BROTLI_HUFFMAN_MAX_CODE_LENGTH 15
|
28
|
+
|
29
|
+
/* For current format this constant equals to kNumInsertAndCopyCodes */
|
30
|
+
#define BROTLI_HUFFMAN_MAX_CODE_LENGTHS_SIZE 704
|
31
|
+
|
32
|
+
/* Maximum possible Huffman table size for an alphabet size of 704, max code
|
33
|
+
* length 15 and root table bits 8. */
|
34
|
+
#define BROTLI_HUFFMAN_MAX_TABLE_SIZE 1080
|
35
|
+
|
36
|
+
#define BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH 5
|
37
|
+
|
38
|
+
typedef struct {
|
39
|
+
uint8_t bits; /* number of bits used for this symbol */
|
40
|
+
uint16_t value; /* symbol value or table offset */
|
41
|
+
} HuffmanCode;
|
42
|
+
|
43
|
+
|
44
|
+
/* Builds Huffman lookup table assuming code lengths are in symbol order. */
|
45
|
+
void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* root_table,
|
46
|
+
const uint8_t* const code_lengths,
|
47
|
+
uint16_t *count);
|
48
|
+
|
49
|
+
/* Builds Huffman lookup table assuming code lengths are in symbol order. */
|
50
|
+
/* Returns size of resulting table. */
|
51
|
+
int BrotliBuildHuffmanTable(HuffmanCode* root_table,
|
52
|
+
int root_bits,
|
53
|
+
const uint16_t* const symbol_lists,
|
54
|
+
uint16_t *count_arg);
|
55
|
+
|
56
|
+
int BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
|
57
|
+
int root_bits,
|
58
|
+
uint16_t *symbols,
|
59
|
+
uint32_t num_symbols);
|
60
|
+
|
61
|
+
/* Contains a collection of huffman trees with the same alphabet size. */
|
62
|
+
typedef struct {
|
63
|
+
HuffmanCode** htrees;
|
64
|
+
HuffmanCode* codes;
|
65
|
+
int16_t alphabet_size;
|
66
|
+
int16_t num_htrees;
|
67
|
+
} HuffmanTreeGroup;
|
68
|
+
|
69
|
+
void BrotliHuffmanTreeGroupInit(HuffmanTreeGroup* group,
|
70
|
+
int alphabet_size, int ntrees);
|
71
|
+
void BrotliHuffmanTreeGroupRelease(HuffmanTreeGroup* group);
|
72
|
+
|
73
|
+
#if defined(__cplusplus) || defined(c_plusplus)
|
74
|
+
} /* extern "C" */
|
75
|
+
#endif
|
76
|
+
|
77
|
+
#endif /* BROTLI_DEC_HUFFMAN_H_ */
|
@@ -0,0 +1,148 @@
|
|
1
|
+
/* Copyright 2015 Google Inc. All Rights Reserved.
|
2
|
+
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
you may not use this file except in compliance with the License.
|
5
|
+
You may obtain a copy of the License at
|
6
|
+
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
See the License for the specific language governing permissions and
|
13
|
+
limitations under the License.
|
14
|
+
*/
|
15
|
+
|
16
|
+
/* Macros for branch prediction. */
|
17
|
+
|
18
|
+
#ifndef BROTLI_DEC_PORT_H_
|
19
|
+
#define BROTLI_DEC_PORT_H_
|
20
|
+
|
21
|
+
#include<assert.h>
|
22
|
+
|
23
|
+
/* Compatibility with non-clang compilers. */
|
24
|
+
#ifndef __has_builtin
|
25
|
+
#define __has_builtin(x) 0
|
26
|
+
#endif
|
27
|
+
|
28
|
+
#ifndef __has_attribute
|
29
|
+
#define __has_attribute(x) 0
|
30
|
+
#endif
|
31
|
+
|
32
|
+
#ifndef __has_feature
|
33
|
+
#define __has_feature(x) 0
|
34
|
+
#endif
|
35
|
+
|
36
|
+
#define BROTLI_ASAN_BUILD __has_feature(address_sanitizer)
|
37
|
+
|
38
|
+
/* Define "PREDICT_TRUE" and "PREDICT_FALSE" macros for capable compilers.
|
39
|
+
|
40
|
+
To apply compiler hint, enclose the branching condition into macros, like this:
|
41
|
+
|
42
|
+
if (PREDICT_TRUE(zero == 0)) {
|
43
|
+
// main execution path
|
44
|
+
} else {
|
45
|
+
// compiler should place this code outside of main execution path
|
46
|
+
}
|
47
|
+
|
48
|
+
OR:
|
49
|
+
|
50
|
+
if (PREDICT_FALSE(something_rare_or_unexpected_happens)) {
|
51
|
+
// compiler should place this code outside of main execution path
|
52
|
+
}
|
53
|
+
|
54
|
+
*/
|
55
|
+
#if (__GNUC__ > 2) || (__GNUC__ == 2 && __GNUC_MINOR__ > 95) || \
|
56
|
+
(defined(__llvm__) && __has_builtin(__builtin_expect))
|
57
|
+
#define PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
|
58
|
+
#define PREDICT_FALSE(x) (__builtin_expect(x, 0))
|
59
|
+
#else
|
60
|
+
#define PREDICT_FALSE(x) (x)
|
61
|
+
#define PREDICT_TRUE(x) (x)
|
62
|
+
#endif
|
63
|
+
|
64
|
+
/* IS_CONSTANT macros returns true for compile-time constant expressions. */
|
65
|
+
#if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ > 0) || \
|
66
|
+
(defined(__llvm__) && __has_builtin(__builtin_constant_p))
|
67
|
+
#define IS_CONSTANT(x) __builtin_constant_p(x)
|
68
|
+
#else
|
69
|
+
#define IS_CONSTANT(x) 0
|
70
|
+
#endif
|
71
|
+
|
72
|
+
#if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ > 0) || \
|
73
|
+
(defined(__llvm__) && __has_attribute(always_inline))
|
74
|
+
#define ATTRIBUTE_ALWAYS_INLINE __attribute__ ((always_inline))
|
75
|
+
#else
|
76
|
+
#define ATTRIBUTE_ALWAYS_INLINE
|
77
|
+
#endif
|
78
|
+
|
79
|
+
#ifndef _MSC_VER
|
80
|
+
#if defined(__cplusplus) || !defined(__STRICT_ANSI__) \
|
81
|
+
|| __STDC_VERSION__ >= 199901L
|
82
|
+
#define BROTLI_INLINE inline ATTRIBUTE_ALWAYS_INLINE
|
83
|
+
#else
|
84
|
+
#define BROTLI_INLINE
|
85
|
+
#endif
|
86
|
+
#else /* _MSC_VER */
|
87
|
+
#define BROTLI_INLINE __forceinline
|
88
|
+
#endif /* _MSC_VER */
|
89
|
+
|
90
|
+
#ifdef BROTLI_DECODE_DEBUG
|
91
|
+
#define BROTLI_DCHECK(x) assert(x)
|
92
|
+
#else
|
93
|
+
#define BROTLI_DCHECK(x)
|
94
|
+
#endif
|
95
|
+
|
96
|
+
#if (defined(__x86_64__) || defined(_M_X64) || defined(__aarch64__) || \
|
97
|
+
defined(__PPC64__))
|
98
|
+
#define BROTLI_64_BITS 1
|
99
|
+
#else
|
100
|
+
#define BROTLI_64_BITS 0
|
101
|
+
#endif
|
102
|
+
|
103
|
+
#if (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
|
104
|
+
#define BROTLI_LITTLE_ENDIAN 1
|
105
|
+
#elif defined(_WIN32)
|
106
|
+
/* Win32 can currently always be assumed to be little endian */
|
107
|
+
#define BROTLI_LITTLE_ENDIAN 1
|
108
|
+
#else
|
109
|
+
#define BROTLI_LITTLE_ENDIAN 0
|
110
|
+
#endif
|
111
|
+
|
112
|
+
#if (BROTLI_64_BITS && BROTLI_LITTLE_ENDIAN)
|
113
|
+
#define BROTLI_64_BITS_LITTLE_ENDIAN 1
|
114
|
+
#else
|
115
|
+
#define BROTLI_64_BITS_LITTLE_ENDIAN 0
|
116
|
+
#endif
|
117
|
+
|
118
|
+
#if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1) || \
|
119
|
+
(defined(__llvm__) && __has_attribute(noinline))
|
120
|
+
#define BROTLI_NOINLINE __attribute__ ((noinline))
|
121
|
+
#else
|
122
|
+
#define BROTLI_NOINLINE
|
123
|
+
#endif
|
124
|
+
|
125
|
+
#if BROTLI_ASAN_BUILD
|
126
|
+
#define BROTLI_NO_ASAN __attribute__((no_sanitize("address"))) BROTLI_NOINLINE
|
127
|
+
#else
|
128
|
+
#define BROTLI_NO_ASAN
|
129
|
+
#endif
|
130
|
+
|
131
|
+
#define BROTLI_REPEAT(N, X) { \
|
132
|
+
if ((N & 1) != 0) {X;} \
|
133
|
+
if ((N & 2) != 0) {X; X;} \
|
134
|
+
if ((N & 4) != 0) {X; X; X; X;} \
|
135
|
+
}
|
136
|
+
|
137
|
+
#if (__GNUC__ > 2) || defined(__llvm__)
|
138
|
+
#if (defined(__ARM_ARCH) && (__ARM_ARCH >= 7))
|
139
|
+
static BROTLI_INLINE unsigned BrotliRBit(unsigned input) {
|
140
|
+
unsigned output;
|
141
|
+
__asm__("rbit %0, %1\n" : "=r"(output) : "r"(input));
|
142
|
+
return output;
|
143
|
+
}
|
144
|
+
#define BROTLI_RBIT(x) BrotliRBit(x)
|
145
|
+
#endif /* armv7 */
|
146
|
+
#endif /* gcc || clang */
|
147
|
+
|
148
|
+
#endif /* BROTLI_DEC_PORT_H_ */
|