10#define HistogramType FN(Histogram)
12double FN(BrotliPopulationCost)(
const HistogramType* histogram) {
13 static const double kOneSymbolHistogramCost = 12;
14 static const double kTwoSymbolHistogramCost = 20;
15 static const double kThreeSymbolHistogramCost = 28;
16 static const double kFourSymbolHistogramCost = 37;
17 const size_t data_size = FN(HistogramDataSize)();
22 if (histogram->total_count_ == 0) {
23 return kOneSymbolHistogramCost;
25 for (i = 0; i < data_size; ++i) {
26 if (histogram->data_[i] > 0) {
33 return kOneSymbolHistogramCost;
36 return (kTwoSymbolHistogramCost + (
double)histogram->total_count_);
39 const uint32_t histo0 = histogram->data_[s[0]];
40 const uint32_t histo1 = histogram->data_[s[1]];
41 const uint32_t histo2 = histogram->data_[s[2]];
42 const uint32_t histomax =
43 BROTLI_MAX(uint32_t, histo0, BROTLI_MAX(uint32_t, histo1, histo2));
44 return (kThreeSymbolHistogramCost +
45 2 * (histo0 + histo1 + histo2) - histomax);
51 for (i = 0; i < 4; ++i) {
52 histo[i] = histogram->data_[s[i]];
55 for (i = 0; i < 4; ++i) {
57 for (j = i + 1; j < 4; ++j) {
58 if (histo[j] > histo[i]) {
59 BROTLI_SWAP(uint32_t, histo, j, i);
63 h23 = histo[2] + histo[3];
64 histomax = BROTLI_MAX(uint32_t, h23, histo[0]);
65 return (kFourSymbolHistogramCost +
66 3 * h23 + 2 * (histo[0] + histo[1]) - histomax);
74 uint32_t depth_histo[BROTLI_CODE_LENGTH_CODES] = { 0 };
75 const double log2total = FastLog2(histogram->total_count_);
76 for (i = 0; i < data_size;) {
77 if (histogram->data_[i] > 0) {
80 double log2p = log2total - FastLog2(histogram->data_[i]);
82 size_t depth = (size_t)(log2p + 0.5);
83 bits += histogram->data_[i] * log2p;
87 if (depth > max_depth) {
97 for (k = i + 1; k < data_size && histogram->data_[k] == 0; ++k) {
101 if (i == data_size) {
107 depth_histo[0] += reps;
111 ++depth_histo[BROTLI_REPEAT_ZERO_CODE_LENGTH];
120 bits += (double)(18 + 2 * max_depth);
122 bits += BitsEntropy(depth_histo, BROTLI_CODE_LENGTH_CODES);
@ count
Not a check: the number of checks.