aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/pack_num.h
blob: d5a8a6bb592e4d19f02943ecc91210220b844f22 (plain) (blame)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#pragma once

#include <util/system/compiler.h>
#include <util/system/defaults.h>
#include <util/system/unaligned_mem.h>

namespace NKikimr {

constexpr size_t MAX_PACKED32_SIZE = 5;
constexpr size_t MAX_PACKED64_SIZE = 9;

Y_FORCE_INLINE size_t GetPack32Length(ui32 value) {
    if (value < 0x80) {
        return 1;
    }
    else if (value < 0x4000) {
        return 2;
    }
    else if (value < 0x200000) {
        return 3;
    }
    else {
        return 5;
    }
}

[[nodiscard]] Y_FORCE_INLINE size_t Pack32(ui32 value, char buffer[MAX_PACKED32_SIZE]) {
    if (value < 0x80) {
        buffer[0] = value << 0x1;
        return 1;
    }
    else if (value < 0x4000) {
        WriteUnaligned<ui16>(((ui16*)(buffer)), ui16((value << 2) | 0x1));
        return 2;
    }
    else if (value < 0x200000) {
        WriteUnaligned<ui32>(((ui32*)(buffer)), ui32((value << 3) | 0x3));
        return 3;
    }
    else {
        buffer[0] = 0x7;
        WriteUnaligned<ui32>((ui32*)(buffer + 1), ui32(value));
        return 5;
    }
}

[[nodiscard]] Y_FORCE_INLINE size_t Unpack32(const char* buffer, size_t length, ui32& value) {
    if (Y_UNLIKELY(!length)) {
        return 0;
    }

    size_t count;
    if (0 == (buffer[0] & 0x1)) {
        count = 1;
        value = ((const ui8*)buffer)[0] >> 1;
    } else if (0 == (buffer[0] & 0x2)) {
        count = 2;
        if (Y_UNLIKELY(length < count)) {
            return 0;
        }
        value = ReadUnaligned<ui16>((const ui16*)buffer) >> 2;
    } else if (0 == (buffer[0] & 0x4)) {
        count = 3;
        if (Y_UNLIKELY(length < count)) {
            return 0;
        }
        value = (ReadUnaligned<ui16>((const ui16*)buffer) >> 3) | (((const ui8*)buffer)[2] << 13);
    } else {
        count = 5;
        if (Y_UNLIKELY(length < count || buffer[0] != 0x7)) {
            return 0;
        }
        value = ReadUnaligned<ui32>((const ui32*)(buffer + 1));
    }

    return count;
}

Y_FORCE_INLINE size_t GetPack64Length(ui64 value) {
    if (value < 0x80) {
        return 1;
    }
    else if (value < 0x4000) {
        return 2;
    }
    else if (value < 0x200000) {
        return 3;
    }
    else if (value < 0x10000000) {
        return 4;
    }
    else if (value < 0x800000000) {
        return 5;
    }
    else if (value < 0x40000000000) {
        return 6;
    }
    else if (value < 0x2000000000000) {
        return 7;
    }
    else {
        return 9;
    }
}

[[nodiscard]] Y_FORCE_INLINE size_t Pack64(ui64 value, char buffer[MAX_PACKED64_SIZE]) {
    if (value < 0x80) {
        buffer[0] = value << 1;
        return 1;
    }
    else if (value < 0x4000) {
        WriteUnaligned<ui16>((ui16*)(buffer), ui16((value << 2) | 0x1));
        return 2;
    }
    else if (value < 0x200000) {
        WriteUnaligned<ui32>((ui32*)(buffer), ui32((value << 3) | 0x3));
        return 3;
    }
    else if (value < 0x10000000) {
        WriteUnaligned<ui32>((ui32*)(buffer), ui32((value << 4) | 0x7));
        return 4;
    }
    else if (value < 0x800000000) {
        WriteUnaligned<ui64>((ui64*)(buffer), ui64((value << 5) | 0xF));
        return 5;
    }
    else if (value < 0x40000000000) {
        WriteUnaligned<ui64>((ui64*)(buffer), ui64((value << 6) | 0x1F));
        return 6;
    }
    else if (value < 0x2000000000000) {
        WriteUnaligned<ui64>((ui64*)(buffer), ui64((value << 7) | 0x3F));
        return 7;
    }
    else {
        buffer[0] = 0x7F;
        WriteUnaligned<ui64>((ui64*)(buffer + 1), value);
        return 9;
    }
}

[[nodiscard]] Y_FORCE_INLINE size_t Unpack64(const char* buffer, size_t length, ui64& value) {
    if (Y_UNLIKELY(!length)) {
        return 0;
    }

    size_t count;
    if (0 == (buffer[0] & 0x1)) {
        count = 1;
        value = ((const ui8*)buffer)[0] >> 1;
    } else if (0 == (buffer[0] & 0x2)) {
        count = 2;
        if (Y_UNLIKELY(length < count)) {
            return 0;
        }
        value = ReadUnaligned<ui16>((const ui16*)buffer) >> 2;
    } else if (0 == (buffer[0] & 0x4)) {
        count = 3;
        if (Y_UNLIKELY(length < count)) {
            return 0;
        }
        value = (ui64(ReadUnaligned<ui16>((const ui16*)buffer)) >> 3) | (ui64(((const ui8*)buffer)[2]) << 13);
    } else if (0 == (buffer[0] & 0x8)) {
        count = 4;
        if (Y_UNLIKELY(length < count)) {
            return 0;
        }
        value = ReadUnaligned<ui32>((const ui32*)buffer) >> 4;
    } else if (0 == (buffer[0] & 0x10)) {
        count = 5;
        if (Y_UNLIKELY(length < count)) {
            return 0;
        }
        value = (ui64(ReadUnaligned<ui32>((const ui32*)buffer)) >> 5) | (ui64(((const ui8*)buffer)[4]) << 27);
    } else if (0 == (buffer[0] & 0x20)) {
        count = 6;
        if (Y_UNLIKELY(length < count)) {
            return 0;
        }
        value = (ui64(ReadUnaligned<ui32>((const ui32*)buffer)) >> 6) | (ui64(ReadUnaligned<ui16>(((const ui16*)buffer) + 2)) << 26);
    } else if (0 == (buffer[0] & 0x40)) {
        count = 7;
        if (Y_UNLIKELY(length < count)) {
            return 0;
        }
        value = (ui64(ReadUnaligned<ui32>((const ui32*)buffer)) >> 7) | (ui64(ReadUnaligned<ui16>(((const ui16*)buffer) + 2)) << 25) | (ui64(((const ui8*)buffer)[6]) << 41);
    } else {
        count = 9;
        if (Y_UNLIKELY(length < count || buffer[0] != 0x7F)) {
            return 0;
        }
        value = ReadUnaligned<ui64>((const ui64*)(buffer + 1));
    }

    return count;
}

} // NKikimr