1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/storage/compression/chimp/chimp.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/storage/compression/chimp/algorithm/chimp128.hpp"
12#include "duckdb/common/assert.hpp"
13#include "duckdb/common/exception.hpp"
14#include "duckdb/common/helper.hpp"
15#include "duckdb/common/limits.hpp"
16#include "duckdb/common/types/validity_mask.hpp"
17#include "duckdb/function/compression_function.hpp"
18
19namespace duckdb {
20
21using byte_index_t = uint32_t;
22
23template <class T>
24struct ChimpType {};
25
26template <>
27struct ChimpType<double> {
28 typedef uint64_t type;
29};
30
31template <>
32struct ChimpType<float> {
33 typedef uint32_t type;
34};
35
36class ChimpPrimitives {
37public:
38 static constexpr uint32_t CHIMP_SEQUENCE_SIZE = 1024;
39 static constexpr uint8_t MAX_BYTES_PER_VALUE = sizeof(double) + 1; // extra wiggle room
40 static constexpr uint8_t HEADER_SIZE = sizeof(uint32_t);
41 static constexpr uint8_t FLAG_BIT_SIZE = 2;
42 static constexpr uint32_t LEADING_ZERO_BLOCK_BUFFERSIZE = 1 + (CHIMP_SEQUENCE_SIZE / 8) * 3;
43};
44
45//! Where all the magic happens
46template <class T, bool EMPTY>
47struct ChimpState {
48public:
49 using CHIMP_TYPE = typename ChimpType<T>::type;
50
51 ChimpState() : chimp() {
52 }
53 Chimp128CompressionState<CHIMP_TYPE, EMPTY> chimp;
54
55public:
56 void AssignDataBuffer(uint8_t *data_out) {
57 chimp.output.SetStream(data_out);
58 }
59
60 void AssignFlagBuffer(uint8_t *flag_out) {
61 chimp.flag_buffer.SetBuffer(flag_out);
62 }
63
64 void AssignPackedDataBuffer(uint16_t *packed_data_out) {
65 chimp.packed_data_buffer.SetBuffer(packed_data_out);
66 }
67
68 void AssignLeadingZeroBuffer(uint8_t *leading_zero_out) {
69 chimp.leading_zero_buffer.SetBuffer(leading_zero_out);
70 }
71
72 void Flush() {
73 chimp.output.Flush();
74 }
75};
76
77} // namespace duckdb
78