Pixie
Loading...
Searching...
No Matches
byte_histogram.h
Go to the documentation of this file.
1#pragma once
2
7
8#include <array>
9#include <cstddef>
10#include <cstdint>
11#include <span>
12
13namespace pixie::detail {
14
17 public:
19 void add(std::span<const std::byte> bytes) {
20 std::size_t index = 0;
21 for (; index + kLaneCount <= bytes.size(); index += kLaneCount) {
22 ++partial_[0][std::to_integer<std::uint8_t>(bytes[index])];
23 ++partial_[1][std::to_integer<std::uint8_t>(bytes[index + 1])];
24 ++partial_[2][std::to_integer<std::uint8_t>(bytes[index + 2])];
25 ++partial_[3][std::to_integer<std::uint8_t>(bytes[index + 3])];
26 }
27 for (; index < bytes.size(); ++index) {
28 ++partial_[index & (kLaneCount - 1)]
29 [std::to_integer<std::uint8_t>(bytes[index])];
30 }
31 }
32
34 std::array<std::size_t, 256> counts() const {
35 std::array<std::size_t, 256> result{};
36 for (std::size_t symbol = 0; symbol < result.size(); ++symbol) {
37 for (const auto& lane : partial_) {
38 result[symbol] += lane[symbol];
39 }
40 }
41 return result;
42 }
43
44 private:
45 static constexpr std::size_t kLaneCount = 4;
46 static_assert((kLaneCount & (kLaneCount - 1)) == 0);
47
48 alignas(64) std::array<std::array<std::size_t, 256>, kLaneCount> partial_{};
49};
50
51} // namespace pixie::detail
Incremental 256-bin byte histogram with four update lanes.
Definition byte_histogram.h:16
std::array< std::size_t, 256 > counts() const
Return the combined symbol counts.
Definition byte_histogram.h:34
void add(std::span< const std::byte > bytes)
Add one contiguous chunk to the histogram.
Definition byte_histogram.h:19