summaryrefslogtreecommitdiffstats
path: root/modules/brotli/enc/bit_cost.h
diff options
context:
space:
mode:
authorMatt A. Tobin <email@mattatobin.com>2019-11-14 21:08:43 -0500
committerMatt A. Tobin <email@mattatobin.com>2019-11-14 21:08:43 -0500
commit1d30f6fa8413746ddc408f93710d701493af273d (patch)
treeabe84e83d704e13c60c90db7ac4b9e363d8a81fc /modules/brotli/enc/bit_cost.h
parent9308ec68e863e4c6e650680370a5d7baa9f0d1f3 (diff)
parent00573571a226a0c59dd744da67483864a22911aa (diff)
downloadUXP-1d30f6fa8413746ddc408f93710d701493af273d.tar
UXP-1d30f6fa8413746ddc408f93710d701493af273d.tar.gz
UXP-1d30f6fa8413746ddc408f93710d701493af273d.tar.lz
UXP-1d30f6fa8413746ddc408f93710d701493af273d.tar.xz
UXP-1d30f6fa8413746ddc408f93710d701493af273d.zip
Merge branch 'master' into mailnews-work
Diffstat (limited to 'modules/brotli/enc/bit_cost.h')
-rw-r--r--modules/brotli/enc/bit_cost.h63
1 files changed, 63 insertions, 0 deletions
diff --git a/modules/brotli/enc/bit_cost.h b/modules/brotli/enc/bit_cost.h
new file mode 100644
index 000000000..6586469e6
--- /dev/null
+++ b/modules/brotli/enc/bit_cost.h
@@ -0,0 +1,63 @@
+/* Copyright 2013 Google Inc. All Rights Reserved.
+
+ Distributed under MIT license.
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+/* Functions to estimate the bit cost of Huffman trees. */
+
+#ifndef BROTLI_ENC_BIT_COST_H_
+#define BROTLI_ENC_BIT_COST_H_
+
+#include "../common/platform.h"
+#include <brotli/types.h>
+#include "./fast_log.h"
+#include "./histogram.h"
+
+#if defined(__cplusplus) || defined(c_plusplus)
+extern "C" {
+#endif
+
+static BROTLI_INLINE double ShannonEntropy(
+ const uint32_t* population, size_t size, size_t* total) {
+ size_t sum = 0;
+ double retval = 0;
+ const uint32_t* population_end = population + size;
+ size_t p;
+ if (size & 1) {
+ goto odd_number_of_elements_left;
+ }
+ while (population < population_end) {
+ p = *population++;
+ sum += p;
+ retval -= (double)p * FastLog2(p);
+ odd_number_of_elements_left:
+ p = *population++;
+ sum += p;
+ retval -= (double)p * FastLog2(p);
+ }
+ if (sum) retval += (double)sum * FastLog2(sum);
+ *total = sum;
+ return retval;
+}
+
+static BROTLI_INLINE double BitsEntropy(
+ const uint32_t* population, size_t size) {
+ size_t sum;
+ double retval = ShannonEntropy(population, size, &sum);
+ if (retval < sum) {
+ /* At least one bit per literal is needed. */
+ retval = (double)sum;
+ }
+ return retval;
+}
+
+BROTLI_INTERNAL double BrotliPopulationCostLiteral(const HistogramLiteral*);
+BROTLI_INTERNAL double BrotliPopulationCostCommand(const HistogramCommand*);
+BROTLI_INTERNAL double BrotliPopulationCostDistance(const HistogramDistance*);
+
+#if defined(__cplusplus) || defined(c_plusplus)
+} /* extern "C" */
+#endif
+
+#endif /* BROTLI_ENC_BIT_COST_H_ */