/src/ghostpdl/brotli/c/enc/bit_cost.c
Line | Count | Source |
1 | | /* Copyright 2013 Google Inc. All Rights Reserved. |
2 | | |
3 | | Distributed under MIT license. |
4 | | See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
5 | | */ |
6 | | |
7 | | /* Functions to estimate the bit cost of Huffman trees. */ |
8 | | |
9 | | #include "bit_cost.h" |
10 | | |
11 | | #include "../common/platform.h" |
12 | | #include "fast_log.h" |
13 | | |
14 | | #if defined(__cplusplus) || defined(c_plusplus) |
15 | | extern "C" { |
16 | | #endif |
17 | | |
18 | 0 | double BrotliBitsEntropy(const uint32_t* population, size_t size) { |
19 | 0 | size_t sum = 0; |
20 | 0 | double retval = 0; |
21 | 0 | const uint32_t* population_end = population + size; |
22 | 0 | size_t p; |
23 | 0 | if (size & 1) { |
24 | 0 | goto odd_number_of_elements_left; |
25 | 0 | } |
26 | 0 | while (population < population_end) { |
27 | 0 | p = *population++; |
28 | 0 | sum += p; |
29 | 0 | retval -= (double)p * FastLog2(p); |
30 | 0 | odd_number_of_elements_left: |
31 | 0 | p = *population++; |
32 | 0 | sum += p; |
33 | 0 | retval -= (double)p * FastLog2(p); |
34 | 0 | } |
35 | 0 | if (sum) retval += (double)sum * FastLog2(sum); |
36 | |
|
37 | 0 | if (retval < (double)sum) { |
38 | | /* TODO(eustas): consider doing that per-symbol? */ |
39 | | /* At least one bit per literal is needed. */ |
40 | 0 | retval = (double)sum; |
41 | 0 | } |
42 | |
|
43 | 0 | return retval; |
44 | 0 | } |
45 | | |
46 | 0 | #define FN(X) X ## Literal |
47 | | #include "bit_cost_inc.h" /* NOLINT(build/include) */ |
48 | | #undef FN |
49 | | |
50 | 0 | #define FN(X) X ## Command |
51 | | #include "bit_cost_inc.h" /* NOLINT(build/include) */ |
52 | | #undef FN |
53 | | |
54 | 0 | #define FN(X) X ## Distance |
55 | | #include "bit_cost_inc.h" /* NOLINT(build/include) */ |
56 | | #undef FN |
57 | | |
58 | | #if defined(__cplusplus) || defined(c_plusplus) |
59 | | } /* extern "C" */ |
60 | | #endif |