xref: /aosp_15_r20/external/brotli/c/enc/bit_cost.h (revision f4ee7fba7774faf2a30f13154332c0a06550dbc4)
1*f4ee7fbaSAndroid Build Coastguard Worker /* Copyright 2013 Google Inc. All Rights Reserved.
2*f4ee7fbaSAndroid Build Coastguard Worker 
3*f4ee7fbaSAndroid Build Coastguard Worker    Distributed under MIT license.
4*f4ee7fbaSAndroid Build Coastguard Worker    See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5*f4ee7fbaSAndroid Build Coastguard Worker */
6*f4ee7fbaSAndroid Build Coastguard Worker 
7*f4ee7fbaSAndroid Build Coastguard Worker /* Functions to estimate the bit cost of Huffman trees. */
8*f4ee7fbaSAndroid Build Coastguard Worker 
9*f4ee7fbaSAndroid Build Coastguard Worker #ifndef BROTLI_ENC_BIT_COST_H_
10*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_ENC_BIT_COST_H_
11*f4ee7fbaSAndroid Build Coastguard Worker 
12*f4ee7fbaSAndroid Build Coastguard Worker #include "../common/platform.h"
13*f4ee7fbaSAndroid Build Coastguard Worker #include <brotli/types.h>
14*f4ee7fbaSAndroid Build Coastguard Worker #include "./fast_log.h"
15*f4ee7fbaSAndroid Build Coastguard Worker #include "./histogram.h"
16*f4ee7fbaSAndroid Build Coastguard Worker 
17*f4ee7fbaSAndroid Build Coastguard Worker #if defined(__cplusplus) || defined(c_plusplus)
18*f4ee7fbaSAndroid Build Coastguard Worker extern "C" {
19*f4ee7fbaSAndroid Build Coastguard Worker #endif
20*f4ee7fbaSAndroid Build Coastguard Worker 
ShannonEntropy(const uint32_t * population,size_t size,size_t * total)21*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE double ShannonEntropy(
22*f4ee7fbaSAndroid Build Coastguard Worker     const uint32_t* population, size_t size, size_t* total) {
23*f4ee7fbaSAndroid Build Coastguard Worker   size_t sum = 0;
24*f4ee7fbaSAndroid Build Coastguard Worker   double retval = 0;
25*f4ee7fbaSAndroid Build Coastguard Worker   const uint32_t* population_end = population + size;
26*f4ee7fbaSAndroid Build Coastguard Worker   size_t p;
27*f4ee7fbaSAndroid Build Coastguard Worker   if (size & 1) {
28*f4ee7fbaSAndroid Build Coastguard Worker     goto odd_number_of_elements_left;
29*f4ee7fbaSAndroid Build Coastguard Worker   }
30*f4ee7fbaSAndroid Build Coastguard Worker   while (population < population_end) {
31*f4ee7fbaSAndroid Build Coastguard Worker     p = *population++;
32*f4ee7fbaSAndroid Build Coastguard Worker     sum += p;
33*f4ee7fbaSAndroid Build Coastguard Worker     retval -= (double)p * FastLog2(p);
34*f4ee7fbaSAndroid Build Coastguard Worker  odd_number_of_elements_left:
35*f4ee7fbaSAndroid Build Coastguard Worker     p = *population++;
36*f4ee7fbaSAndroid Build Coastguard Worker     sum += p;
37*f4ee7fbaSAndroid Build Coastguard Worker     retval -= (double)p * FastLog2(p);
38*f4ee7fbaSAndroid Build Coastguard Worker   }
39*f4ee7fbaSAndroid Build Coastguard Worker   if (sum) retval += (double)sum * FastLog2(sum);
40*f4ee7fbaSAndroid Build Coastguard Worker   *total = sum;
41*f4ee7fbaSAndroid Build Coastguard Worker   return retval;
42*f4ee7fbaSAndroid Build Coastguard Worker }
43*f4ee7fbaSAndroid Build Coastguard Worker 
BitsEntropy(const uint32_t * population,size_t size)44*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE double BitsEntropy(
45*f4ee7fbaSAndroid Build Coastguard Worker     const uint32_t* population, size_t size) {
46*f4ee7fbaSAndroid Build Coastguard Worker   size_t sum;
47*f4ee7fbaSAndroid Build Coastguard Worker   double retval = ShannonEntropy(population, size, &sum);
48*f4ee7fbaSAndroid Build Coastguard Worker   if (retval < sum) {
49*f4ee7fbaSAndroid Build Coastguard Worker     /* At least one bit per literal is needed. */
50*f4ee7fbaSAndroid Build Coastguard Worker     retval = (double)sum;
51*f4ee7fbaSAndroid Build Coastguard Worker   }
52*f4ee7fbaSAndroid Build Coastguard Worker   return retval;
53*f4ee7fbaSAndroid Build Coastguard Worker }
54*f4ee7fbaSAndroid Build Coastguard Worker 
55*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_INTERNAL double BrotliPopulationCostLiteral(const HistogramLiteral*);
56*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_INTERNAL double BrotliPopulationCostCommand(const HistogramCommand*);
57*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_INTERNAL double BrotliPopulationCostDistance(const HistogramDistance*);
58*f4ee7fbaSAndroid Build Coastguard Worker 
59*f4ee7fbaSAndroid Build Coastguard Worker #if defined(__cplusplus) || defined(c_plusplus)
60*f4ee7fbaSAndroid Build Coastguard Worker }  /* extern "C" */
61*f4ee7fbaSAndroid Build Coastguard Worker #endif
62*f4ee7fbaSAndroid Build Coastguard Worker 
63*f4ee7fbaSAndroid Build Coastguard Worker #endif  /* BROTLI_ENC_BIT_COST_H_ */
64