Coverage Report

Created: 2025-06-24 07:53

/src/duckdb/third_party/brotli/enc/histogram.cpp
Line
Count
Source (jump to first uncovered line)
1
/* Copyright 2013 Google Inc. All Rights Reserved.
2
3
   Distributed under MIT license.
4
   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5
*/
6
7
/* Build per-context histograms of literals, commands and distance codes. */
8
9
#include "histogram.h"
10
11
#include "../common/context.h"
12
#include "block_splitter.h"
13
#include "command.h"
14
15
using namespace duckdb_brotli;
16
17
typedef struct BlockSplitIterator {
18
  const BlockSplit* split_;  /* Not owned. */
19
  size_t idx_;
20
  size_t type_;
21
  size_t length_;
22
} BlockSplitIterator;
23
24
static void InitBlockSplitIterator(BlockSplitIterator* self,
25
0
    const BlockSplit* split) {
26
0
  self->split_ = split;
27
0
  self->idx_ = 0;
28
0
  self->type_ = 0;
29
0
  self->length_ = split->lengths ? split->lengths[0] : 0;
30
0
}
31
32
0
static void BlockSplitIteratorNext(BlockSplitIterator* self) {
33
0
  if (self->length_ == 0) {
34
0
    ++self->idx_;
35
0
    self->type_ = self->split_->types[self->idx_];
36
0
    self->length_ = self->split_->lengths[self->idx_];
37
0
  }
38
0
  --self->length_;
39
0
}
40
41
void duckdb_brotli::BrotliBuildHistogramsWithContext(
42
    const Command* cmds, const size_t num_commands,
43
    const BlockSplit* literal_split, const BlockSplit* insert_and_copy_split,
44
    const BlockSplit* dist_split, const uint8_t* ringbuffer, size_t start_pos,
45
    size_t mask, uint8_t prev_byte, uint8_t prev_byte2,
46
    const ContextType* context_modes, HistogramLiteral* literal_histograms,
47
    HistogramCommand* insert_and_copy_histograms,
48
0
    HistogramDistance* copy_dist_histograms) {
49
0
  size_t pos = start_pos;
50
0
  BlockSplitIterator literal_it;
51
0
  BlockSplitIterator insert_and_copy_it;
52
0
  BlockSplitIterator dist_it;
53
0
  size_t i;
54
55
0
  InitBlockSplitIterator(&literal_it, literal_split);
56
0
  InitBlockSplitIterator(&insert_and_copy_it, insert_and_copy_split);
57
0
  InitBlockSplitIterator(&dist_it, dist_split);
58
0
  for (i = 0; i < num_commands; ++i) {
59
0
    const Command* cmd = &cmds[i];
60
0
    size_t j;
61
0
    BlockSplitIteratorNext(&insert_and_copy_it);
62
0
    HistogramAddCommand(&insert_and_copy_histograms[insert_and_copy_it.type_],
63
0
        cmd->cmd_prefix_);
64
    /* TODO(eustas): unwrap iterator blocks. */
65
0
    for (j = cmd->insert_len_; j != 0; --j) {
66
0
      size_t context;
67
0
      BlockSplitIteratorNext(&literal_it);
68
0
      context = literal_it.type_;
69
0
      if (context_modes) {
70
0
        ContextLut lut = BROTLI_CONTEXT_LUT(context_modes[context]);
71
0
        context = (context << BROTLI_LITERAL_CONTEXT_BITS) +
72
0
            BROTLI_CONTEXT(prev_byte, prev_byte2, lut);
73
0
      }
74
0
      HistogramAddLiteral(&literal_histograms[context],
75
0
          ringbuffer[pos & mask]);
76
0
      prev_byte2 = prev_byte;
77
0
      prev_byte = ringbuffer[pos & mask];
78
0
      ++pos;
79
0
    }
80
0
    pos += CommandCopyLen(cmd);
81
0
    if (CommandCopyLen(cmd)) {
82
0
      prev_byte2 = ringbuffer[(pos - 2) & mask];
83
0
      prev_byte = ringbuffer[(pos - 1) & mask];
84
0
      if (cmd->cmd_prefix_ >= 128) {
85
0
        size_t context;
86
0
        BlockSplitIteratorNext(&dist_it);
87
0
        context = (dist_it.type_ << BROTLI_DISTANCE_CONTEXT_BITS) +
88
0
            CommandDistanceContext(cmd);
89
0
        HistogramAddDistance(&copy_dist_histograms[context],
90
0
            cmd->dist_prefix_ & 0x3FF);
91
0
      }
92
0
    }
93
0
  }
94
0
}
95
96