1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
#ifndef INTERPOLATIVE_INL_H_
#error "Direct inclusion of this file is not allowed, include interpolative.h"
// For the sake of sane code completion.
#include "interpolative.h"
#endif
#include "bit_io.h"
#include <library/cpp/yt/assert/assert.h>
#include <library/cpp/yt/memory/range.h>
#include <util/system/compiler.h>
#include <array>
#include <bit>
#include <concepts>
namespace NYT {
////////////////////////////////////////////////////////////////////////////////
namespace NInterpolativeCodingDetail {
// Truncated-binary (minimal) code for a value in [0, rangeSize): the first
// Cutoff values take LowWidth bits, the rest one more.
struct TTruncatedBinaryParams
{
int LowWidth; // floor(log2(rangeSize))
ui32 Cutoff; // 2^(LowWidth + 1) - rangeSize
};
Y_FORCE_INLINE TTruncatedBinaryParams GetTruncatedBinaryParams(ui32 rangeSize)
{
int lowWidth = std::bit_width(rangeSize) - 1;
ui32 cutoff = (2u << lowWidth) - rangeSize;
return {lowWidth, cutoff};
}
// Writes #value in [0, #rangeSize) with the entropy-optimal integer code for a
// uniform value. rangeSize == 1 yields lowWidth 0 / cutoff 1 and emits a
// zero-width code (a no-op), so the singleton case needs no branch.
Y_FORCE_INLINE void WriteTruncatedBinary(TBitWriter* writer, ui32 value, ui32 rangeSize)
{
auto [lowWidth, cutoff] = GetTruncatedBinaryParams(rangeSize);
// Branchless: the (value >= cutoff) predicate would mispredict on nearly
// every element, so fold it into the emitted codeword and width instead.
ui32 isLong = value >= cutoff ? 1 : 0;
writer->WriteBits(value + (cutoff & (0u - isLong)), lowWidth + static_cast<int>(isLong));
}
Y_FORCE_INLINE ui32 ReadTruncatedBinary(TBitReader* reader, ui32 rangeSize)
{
auto [lowWidth, cutoff] = GetTruncatedBinaryParams(rangeSize);
ui32 high = reader->ReadBits(lowWidth);
if (high < cutoff) {
return high;
}
ui32 low = reader->ReadBits(1);
return ((high << 1) | low) - cutoff;
}
// A subrange [BeginIndex, EndIndex) of the value array together with the
// inclusive value bounds [Lo, Hi] that its elements must fall in. The median
// values[m] is the only array element touched per node, which matters because
// the traversal walks a large sequence in a cache-unfriendly tree order.
struct TInterpolativeFrame
{
int BeginIndex;
int EndIndex;
ui32 Lo;
ui32 Hi;
};
} // namespace NInterpolativeCodingDetail
////////////////////////////////////////////////////////////////////////////////
inline size_t GetInterpolativeMaxByteSize(int count, ui32 lo, ui32 hi)
{
// Each element is coded in at most ceil(log2(hi - lo + 1)) bits; the trailing
// word covers the writer's 4-byte flush store.
int maxBitWidth = std::bit_width(hi - lo);
return (static_cast<size_t>(count) * maxBitWidth + 7) / 8 + sizeof(ui32);
}
////////////////////////////////////////////////////////////////////////////////
// Both traversals descend the left child in place and stack only the (non-empty)
// right child, so the stack sees ~count/2 pushes instead of ~count. Left-first
// order matches between encoder and decoder.
template <std::unsigned_integral T>
void InterpolativeEncode(TBitWriter* writer, TRange<T> values, ui32 lo, ui32 hi)
{
using namespace NInterpolativeCodingDetail;
int count = std::ssize(values);
if (count == 0) {
return;
}
// Right children stack up along the leftmost path => depth <= ceil(log2(count)).
std::array<TInterpolativeFrame, 48> stack;
int top = 0;
int beginIndex = 0;
int endIndex = count;
ui32 l = lo;
ui32 h = hi;
for (;;) {
// beginIndex is invariant while descending left; only endIndex/l/h change.
while (beginIndex < endIndex) {
int half = (endIndex - beginIndex) / 2;
int m = beginIndex + half;
// The next descent step reads values[beginIndex + half/2]; prefetch it
// to hide the cache-scattered tree walk on large sequences.
Y_PREFETCH_READ(values.data() + beginIndex + (half >> 1), 0);
ui32 value = static_cast<ui32>(values[m]);
// rangeSize = (upperBound - lowerBound + 1) simplifies to this;
// lowerBound = l + half.
ui32 rangeSize = (h - l) - static_cast<ui32>(endIndex - beginIndex) + 2;
WriteTruncatedBinary(writer, value - l - static_cast<ui32>(half), rangeSize);
if (m + 1 < endIndex) {
YT_ASSERT(top < std::ssize(stack));
stack[top++] = {m + 1, endIndex, value + 1, h};
}
endIndex = m;
h = value - 1;
}
if (top == 0) {
break;
}
auto f = stack[--top];
beginIndex = f.BeginIndex;
endIndex = f.EndIndex;
l = f.Lo;
h = f.Hi;
}
}
template <std::unsigned_integral T>
void InterpolativeDecode(TBitReader* reader, TMutableRange<T> values, ui32 lo, ui32 hi)
{
using namespace NInterpolativeCodingDetail;
int count = std::ssize(values);
if (count == 0) {
return;
}
std::array<TInterpolativeFrame, 48> stack;
int top = 0;
int beginIndex = 0;
int endIndex = count;
ui32 l = lo;
ui32 h = hi;
for (;;) {
while (beginIndex < endIndex) {
int half = (endIndex - beginIndex) / 2;
int m = beginIndex + half;
ui32 rangeSize = (h - l) - static_cast<ui32>(endIndex - beginIndex) + 2;
ui32 value = l + static_cast<ui32>(half) + ReadTruncatedBinary(reader, rangeSize);
values[m] = static_cast<T>(value);
if (m + 1 < endIndex) {
stack[top++] = {m + 1, endIndex, value + 1, h};
}
endIndex = m;
h = value - 1;
}
if (top == 0) {
break;
}
auto f = stack[--top];
beginIndex = f.BeginIndex;
endIndex = f.EndIndex;
l = f.Lo;
h = f.Hi;
}
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT
|