OLD | NEW |
1 /* Copyright 2013 Google Inc. All Rights Reserved. | 1 /* Copyright 2013 Google Inc. All Rights Reserved. |
2 | 2 |
3 Distributed under MIT license. | 3 Distributed under MIT license. |
4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT | 4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
5 */ | 5 */ |
6 | 6 |
7 /* Bit reading helpers */ | 7 /* Functions for clustering similar histograms together. */ |
8 | 8 |
9 #include "./bit_reader.h" | 9 #include "./cluster.h" |
10 | 10 |
| 11 #include <brotli/types.h> |
| 12 #include "./bit_cost.h" /* BrotliPopulationCost */ |
| 13 #include "./fast_log.h" |
| 14 #include "./histogram.h" |
| 15 #include "./memory.h" |
11 #include "./port.h" | 16 #include "./port.h" |
12 #include "./types.h" | |
13 | 17 |
14 #if defined(__cplusplus) || defined(c_plusplus) | 18 #if defined(__cplusplus) || defined(c_plusplus) |
15 extern "C" { | 19 extern "C" { |
16 #endif | 20 #endif |
17 | 21 |
18 void BrotliInitBitReader(BrotliBitReader* const br) { | 22 static BROTLI_INLINE BROTLI_BOOL HistogramPairIsLess( |
19 br->val_ = 0; | 23 const HistogramPair* p1, const HistogramPair* p2) { |
20 br->bit_pos_ = sizeof(br->val_) << 3; | 24 if (p1->cost_diff != p2->cost_diff) { |
| 25 return TO_BROTLI_BOOL(p1->cost_diff > p2->cost_diff); |
| 26 } |
| 27 return TO_BROTLI_BOOL((p1->idx2 - p1->idx1) > (p2->idx2 - p2->idx1)); |
21 } | 28 } |
22 | 29 |
23 int BrotliWarmupBitReader(BrotliBitReader* const br) { | 30 /* Returns entropy reduction of the context map when we combine two clusters. */ |
24 size_t aligned_read_mask = (sizeof(br->val_) >> 1) - 1; | 31 static BROTLI_INLINE double ClusterCostDiff(size_t size_a, size_t size_b) { |
25 /* Fixing alignment after unaligned BrotliFillWindow would result accumulator | 32 size_t size_c = size_a + size_b; |
26 overflow. If unalignment is caused by BrotliSafeReadBits, then there is | 33 return (double)size_a * FastLog2(size_a) + |
27 enough space in accumulator to fix aligment. */ | 34 (double)size_b * FastLog2(size_b) - |
28 if (!BROTLI_ALIGNED_READ) { | 35 (double)size_c * FastLog2(size_c); |
29 aligned_read_mask = 0; | 36 } |
30 } | |
31 if (BrotliGetAvailableBits(br) == 0) { | |
32 if (!BrotliPullByte(br)) { | |
33 return 0; | |
34 } | |
35 } | |
36 | 37 |
37 while ((((size_t)br->next_in) & aligned_read_mask) != 0) { | 38 #define CODE(X) X |
38 if (!BrotliPullByte(br)) { | 39 |
39 /* If we consumed all the input, we don't care about the alignment. */ | 40 #define FN(X) X ## Literal |
40 return 1; | 41 #include "./cluster_inc.h" /* NOLINT(build/include) */ |
41 } | 42 #undef FN |
42 } | 43 |
43 return 1; | 44 #define FN(X) X ## Command |
44 } | 45 #include "./cluster_inc.h" /* NOLINT(build/include) */ |
| 46 #undef FN |
| 47 |
| 48 #define FN(X) X ## Distance |
| 49 #include "./cluster_inc.h" /* NOLINT(build/include) */ |
| 50 #undef FN |
| 51 |
| 52 #undef CODE |
45 | 53 |
46 #if defined(__cplusplus) || defined(c_plusplus) | 54 #if defined(__cplusplus) || defined(c_plusplus) |
47 } /* extern "C" */ | 55 } /* extern "C" */ |
48 #endif | 56 #endif |
OLD | NEW |