1 /* Copyright 2013 Google Inc. All Rights Reserved.
3 Distributed under MIT license.
4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
7 /* Functions to estimate the bit cost of Huffman trees. */
9 #ifndef BROTLI_ENC_BIT_COST_H_
10 #define BROTLI_ENC_BIT_COST_H_
12 #include "../common/types.h"
13 #include "./fast_log.h"
14 #include "./histogram.h"
17 #if defined(__cplusplus) || defined(c_plusplus)
21 static BROTLI_INLINE
double ShannonEntropy(const uint32_t *population
,
22 size_t size
, size_t *total
) {
25 const uint32_t *population_end
= population
+ size
;
28 goto odd_number_of_elements_left
;
30 while (population
< population_end
) {
33 retval
-= (double)p
* FastLog2(p
);
34 odd_number_of_elements_left
:
37 retval
-= (double)p
* FastLog2(p
);
39 if (sum
) retval
+= (double)sum
* FastLog2(sum
);
44 static BROTLI_INLINE
double BitsEntropy(
45 const uint32_t *population
, size_t size
) {
47 double retval
= ShannonEntropy(population
, size
, &sum
);
49 /* At least one bit per literal is needed. */
55 BROTLI_INTERNAL
double BrotliPopulationCostLiteral(const HistogramLiteral
*);
56 BROTLI_INTERNAL
double BrotliPopulationCostCommand(const HistogramCommand
*);
57 BROTLI_INTERNAL
double BrotliPopulationCostDistance(const HistogramDistance
*);
59 #if defined(__cplusplus) || defined(c_plusplus)
63 #endif /* BROTLI_ENC_BIT_COST_H_ */