11cb0ef41Sopenharmony_ci/* NOLINT(build/header_guard) */
21cb0ef41Sopenharmony_ci/* Copyright 2013 Google Inc. All Rights Reserved.
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci   Distributed under MIT license.
51cb0ef41Sopenharmony_ci   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
61cb0ef41Sopenharmony_ci*/
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci/* template parameters: Histogram, DATA_SIZE, DataType */
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci/* A simple container for histograms of data in blocks. */
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_citypedef struct FN(Histogram) {
131cb0ef41Sopenharmony_ci  uint32_t data_[DATA_SIZE];
141cb0ef41Sopenharmony_ci  size_t total_count_;
151cb0ef41Sopenharmony_ci  double bit_cost_;
161cb0ef41Sopenharmony_ci} FN(Histogram);
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_cistatic BROTLI_INLINE void FN(HistogramClear)(FN(Histogram)* self) {
191cb0ef41Sopenharmony_ci  memset(self->data_, 0, sizeof(self->data_));
201cb0ef41Sopenharmony_ci  self->total_count_ = 0;
211cb0ef41Sopenharmony_ci  self->bit_cost_ = HUGE_VAL;
221cb0ef41Sopenharmony_ci}
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_cistatic BROTLI_INLINE void FN(ClearHistograms)(
251cb0ef41Sopenharmony_ci    FN(Histogram)* array, size_t length) {
261cb0ef41Sopenharmony_ci  size_t i;
271cb0ef41Sopenharmony_ci  for (i = 0; i < length; ++i) FN(HistogramClear)(array + i);
281cb0ef41Sopenharmony_ci}
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_cistatic BROTLI_INLINE void FN(HistogramAdd)(FN(Histogram)* self, size_t val) {
311cb0ef41Sopenharmony_ci  ++self->data_[val];
321cb0ef41Sopenharmony_ci  ++self->total_count_;
331cb0ef41Sopenharmony_ci}
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_cistatic BROTLI_INLINE void FN(HistogramAddVector)(FN(Histogram)* self,
361cb0ef41Sopenharmony_ci    const DataType* p, size_t n) {
371cb0ef41Sopenharmony_ci  self->total_count_ += n;
381cb0ef41Sopenharmony_ci  n += 1;
391cb0ef41Sopenharmony_ci  while (--n) ++self->data_[*p++];
401cb0ef41Sopenharmony_ci}
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_cistatic BROTLI_INLINE void FN(HistogramAddHistogram)(FN(Histogram)* self,
431cb0ef41Sopenharmony_ci    const FN(Histogram)* v) {
441cb0ef41Sopenharmony_ci  size_t i;
451cb0ef41Sopenharmony_ci  self->total_count_ += v->total_count_;
461cb0ef41Sopenharmony_ci  for (i = 0; i < DATA_SIZE; ++i) {
471cb0ef41Sopenharmony_ci    self->data_[i] += v->data_[i];
481cb0ef41Sopenharmony_ci  }
491cb0ef41Sopenharmony_ci}
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_cistatic BROTLI_INLINE size_t FN(HistogramDataSize)(void) { return DATA_SIZE; }
52