aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/packedtypes/longs.h
blob: e23b9416b6c37a002363918cd05f367c4eb5b3bf (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#pragma once

#include <util/system/defaults.h> // _BIDSCLASS _EXPCLASS
#include <util/system/yassert.h>
#include <util/system/unaligned_mem.h>

#define PUT_8(x, buf, shift) WriteUnaligned<ui8>((buf)++, (x) >> (shift))
#define GET_8_OR(x, buf, type, shift) (x) |= (type) * (buf)++ << (shift)

#if defined(_big_endian_)
#define LO_SHIFT 1
#define HI_SHIFT 0
#elif defined(_little_endian_)
#define LO_SHIFT 0
#define HI_SHIFT 1
#endif

#if !defined(_must_align2_)
#define PUT_16(x, buf, shift) WriteUnaligned<ui16>(buf, (x) >> (shift)), (buf) += 2
#define GET_16_OR(x, buf, type, shift) (x) |= (type)ReadUnaligned<ui16>(buf) << (shift), (buf) += 2
#else
#define PUT_16(x, buf, shift) PUT_8(x, buf, shift + 8 * LO_SHIFT), PUT_8(x, buf, shift + 8 * HI_SHIFT)
#define GET_16_OR(x, buf, type, shift) GET_8_OR(x, buf, type, shift + 8 * LO_SHIFT), GET_8_OR(x, buf, type, shift + 8 * HI_SHIFT)
#endif

#if !defined(_must_align4_)
#define PUT_32(x, buf, shift) WriteUnaligned<ui32>(buf, (x) >> (shift)), (buf) += 4
#define GET_32_OR(x, buf, type, shift) (x) |= (type)ReadUnaligned<ui32>(buf) << (shift), (buf) += 4
#else
#define PUT_32(x, buf, shift) PUT_16(x, buf, shift + 16 * LO_SHIFT), PUT_16(x, buf, shift + 16 * HI_SHIFT)
#define GET_32_OR(x, buf, type, shift) GET_16_OR(x, buf, type, shift + 16 * LO_SHIFT), GET_16_OR(x, buf, type, shift + 16 * HI_SHIFT)
#endif

#if !defined(_must_align8_)
#define PUT_64(x, buf, shift) WriteUnaligned<ui64>(buf, (x) >> (shift)), (buf) += 8
#define GET_64_OR(x, buf, type, shift) (x) |= (type)ReadUnaligned<ui64>(buf) << (shift), (buf) += 8
#else
#define PUT_64(x, buf, shift) PUT_32(x, buf, shift + 32 * LO_SHIFT), PUT_32(x, buf, shift + 32 * HI_SHIFT)
#define GET_64_OR(x, buf, type, shift) GET_32_OR(x, buf, type, shift + 32 * LO_SHIFT), GET_32_OR(x, buf, type, shift + 32 * HI_SHIFT)
#endif

struct mem_traits {
    static ui8 get_8(const char*& mem) {
        ui8 x = 0;
        GET_8_OR(x, mem, ui8, 0);
        return x;
    }
    static ui16 get_16(const char*& mem) {
        ui16 x = 0;
        GET_16_OR(x, mem, ui16, 0);
        return x;
    }
    static ui32 get_32(const char*& mem) {
        ui32 x = 0;
        GET_32_OR(x, mem, ui32, 0);
        return x;
    }
    static void put_8(ui8 x, char*& mem) {
        PUT_8(x, mem, 0);
    }
    static void put_16(ui16 x, char*& mem) {
        PUT_16(x, mem, 0);
    }
    static void put_32(ui32 x, char*& mem) {
        PUT_32(x, mem, 0);
    }
    static int is_good(char*&) {
        return 1;
    }
};

/*
|____|____|____|____|____|____|____|____|____|____|____|____|____|____|8***|****
|____|____|____|____|____|____|____|____|____|____|____|____|i4**|****|****|****
|____|____|____|____|____|____|____|____|____|____|ii2*|****|****|****|****|****
|____|____|____|____|____|____|____|____|iii1|****|****|****|****|****|****|****
|____|____|____|____|____|____|iiii|8***|****|****|****|****|****|****|****|****
|____|____|____|____|iiii|i4**|****|****|****|****|****|****|****|****|****|****
|____|____|iiii|ii2*|****|****|****|****|****|****|****|****|****|****|****|****
|iiii|iii1|****|****|****|****|****|****|****|****|****|****|****|****|****|****
*/

#define PACK1LIM 0x80u
#define PACK2LIM 0x4000u
#define PACK3LIM 0x200000u
#define PACK4LIM 0x10000000u
#define PACK5LIM 0x800000000ull
#define PACK6LIM 0x40000000000ull
#define PACK7LIM 0x2000000000000ull
#define PACK8LIM 0x100000000000000ull

#define MY_14(x) ((ui16)(x) < PACK1LIM ? 1 : 2)
#define MY_28(x) ((ui32)(x) < PACK2LIM ? MY_14(x) : ((ui32)(x) < PACK3LIM ? 3 : 4))

#define MY_32(x) ((ui32)(x) < PACK4LIM ? MY_28(x) : 5)
#define MY_64(x) ((ui64)(x) < PACK4LIM ? MY_28(x) : ((ui64)(x) < PACK6LIM ? ((ui64)(x) < PACK5LIM ? 5 : 6) : ((ui64)(x) < PACK7LIM ? 7 : ((ui64)(x) < PACK8LIM ? 8 : 9))))

#if !defined(MACRO_BEGIN)
#define MACRO_BEGIN do {
#define MACRO_END \
    }             \
    while (0)
#endif

#define PACK_14(x, buf, how, ret)                  \
    MACRO_BEGIN                                    \
    if ((ui16)(x) < PACK1LIM) {                    \
        how::put_8((ui8)(x), (buf));               \
        (ret) = 1;                                 \
    } else {                                       \
        how::put_8((ui8)(0x80 | (x) >> 8), (buf)); \
        how::put_8((ui8)(x), (buf));               \
        (ret) = 2;                                 \
    }                                              \
    MACRO_END

#define PACK_28(x, buf, how, ret)                     \
    MACRO_BEGIN                                       \
    if ((ui32)(x) < PACK2LIM) {                       \
        PACK_14(x, buf, how, ret);                    \
    } else {                                          \
        if ((ui32)(x) < PACK3LIM) {                   \
            how::put_8((ui8)(0xC0 | (x) >> 16), buf); \
            (ret) = 3;                                \
        } else {                                      \
            how::put_8((ui8)(0xE0 | (x) >> 24), buf); \
            how::put_8((ui8)((x) >> 16), buf);        \
            (ret) = 4;                                \
        }                                             \
        how::put_16((ui16)(x), (buf));                \
    }                                                 \
    MACRO_END

#define PACK_32(x, buf, how, ret)     \
    MACRO_BEGIN                       \
    if ((ui32)(x) < PACK4LIM) {       \
        PACK_28(x, buf, how, ret);    \
    } else {                          \
        how::put_8((ui8)(0xF0), buf); \
        how::put_32((ui32)(x), buf);  \
        (ret) = 5;                    \
    }                                 \
    MACRO_END

#define PACK_64(x, buf, how, ret)                             \
    MACRO_BEGIN                                               \
    if ((ui64)(x) < PACK4LIM) {                               \
        PACK_28((ui32)(x), buf, how, ret);                    \
    } else {                                                  \
        if ((ui64)(x) < PACK6LIM) {                           \
            if ((ui64)(x) < PACK5LIM) {                       \
                how::put_8((ui8)(0xF0 | (x) >> 32), buf);     \
                (ret) = 5;                                    \
            } else {                                          \
                how::put_8((ui8)(0xF8 | (x) >> 40), buf);     \
                how::put_8((ui8)((x) >> 32), buf);            \
                (ret) = 6;                                    \
            }                                                 \
        } else {                                              \
            if ((ui64)(x) < PACK7LIM) {                       \
                how::put_8((ui8)(0xFC | (x) >> 48), buf);     \
                (ret) = 7;                                    \
            } else {                                          \
                if ((ui64)(x) < PACK8LIM) {                   \
                    how::put_8((ui8)(0xFE | (x) >> 56), buf); \
                    how::put_8((ui8)((x) >> 48), buf);        \
                    (ret) = 8;                                \
                } else {                                      \
                    how::put_8((ui8)(0xFF), buf);             \
                    how::put_16((ui16)((x) >> 48), buf);      \
                    (ret) = 9;                                \
                }                                             \
            }                                                 \
            how::put_16((ui16)((x) >> 32), buf);              \
        }                                                     \
        how::put_32((ui32)(x), buf);                          \
    }                                                         \
    MACRO_END

#define DO_UNPACK_14(firstByte, x, buf, how, ret) \
    MACRO_BEGIN                                   \
    if (firstByte < 0x80) {                       \
        (x) = (firstByte);                        \
        (ret) = 1;                                \
    } else {                                      \
        (x) = (firstByte & 0x7F) << 8;            \
        (x) |= how::get_8(buf);                   \
        (ret) = 2;                                \
    }                                             \
    MACRO_END

#define UNPACK_14(x, buf, how, ret)            \
    MACRO_BEGIN                                \
    ui8 firstByte = how::get_8(buf);           \
    DO_UNPACK_14(firstByte, x, buf, how, ret); \
    MACRO_END

#define DO_UNPACK_28(firstByte, x, buf, how, ret)  \
    MACRO_BEGIN                                    \
    if (firstByte < 0xC0) {                        \
        DO_UNPACK_14(firstByte, x, buf, how, ret); \
    } else {                                       \
        if (firstByte < 0xE0) {                    \
            (x) = (firstByte & 0x3F) << 16;        \
            (ret) = 3;                             \
        } else {                                   \
            (x) = (firstByte & 0x1F) << 24;        \
            (x) |= how::get_8(buf) << 16;          \
            (ret) = 4;                             \
        }                                          \
        (x) |= how::get_16(buf);                   \
    }                                              \
    MACRO_END

#define UNPACK_28(x, buf, how, ret)            \
    MACRO_BEGIN                                \
    ui8 firstByte = how::get_8(buf);           \
    DO_UNPACK_28(firstByte, x, buf, how, ret); \
    MACRO_END

#define DO_UNPACK_32(firstByte, x, buf, how, ret)  \
    MACRO_BEGIN                                    \
    if (firstByte < 0xF0) {                        \
        DO_UNPACK_28(firstByte, x, buf, how, ret); \
    } else {                                       \
        (x) = how::get_32(buf);                    \
        (ret) = 5;                                 \
    }                                              \
    MACRO_END

#define UNPACK_32(x, buf, how, ret)            \
    MACRO_BEGIN                                \
    ui8 firstByte = how::get_8(buf);           \
    DO_UNPACK_32(firstByte, x, buf, how, ret); \
    MACRO_END

#define DO_UNPACK_64(firstByte, x, buf, how, ret)         \
    MACRO_BEGIN                                           \
    if (firstByte < 0xF0) {                               \
        DO_UNPACK_28(firstByte, x, buf, how, ret);        \
    } else {                                              \
        if (firstByte < 0xFC) {                           \
            if (firstByte < 0xF8) {                       \
                (x) = (ui64)(firstByte & 0x0F) << 32;     \
                (ret) = 5;                                \
            } else {                                      \
                (x) = (ui64)(firstByte & 0x07) << 40;     \
                (x) |= (ui64)how::get_8(buf) << 32;       \
                (ret) = 6;                                \
            }                                             \
        } else {                                          \
            if (firstByte < 0xFE) {                       \
                (x) = (ui64)(firstByte & 0x03) << 48;     \
                (ret) = 7;                                \
            } else {                                      \
                if (firstByte < 0xFF) {                   \
                    (x) = (ui64)(firstByte & 0x01) << 56; \
                    (x) |= (ui64)how::get_8(buf) << 48;   \
                    (ret) = 8;                            \
                } else {                                  \
                    (x) = (ui64)how::get_16(buf) << 48;   \
                    (ret) = 9;                            \
                }                                         \
            }                                             \
            (x) |= (ui64)how::get_16(buf) << 32;          \
        }                                                 \
        (x) |= how::get_32(buf);                          \
    }                                                     \
    MACRO_END

#define UNPACK_64(x, buf, how, ret)            \
    MACRO_BEGIN                                \
    ui8 firstByte = how::get_8(buf);           \
    DO_UNPACK_64(firstByte, x, buf, how, ret); \
    MACRO_END

inline int in_long(i64& longVal, const char* ptrBuf) {
    int ret = 0;
    UNPACK_64(longVal, ptrBuf, mem_traits, ret);
    return ret;
}

inline int out_long(const i64& longVal, char* ptrBuf) {
    int ret = 0;
    PACK_64(longVal, ptrBuf, mem_traits, ret); /*7*/
    return ret;
}

inline int len_long(const i64& longVal) {
    return MY_64(longVal);
}

inline int in_long(i32& longVal, const char* ptrBuf) {
    int ret = 0;
    UNPACK_32(longVal, ptrBuf, mem_traits, ret);
    return ret;
}

inline int out_long(const i32& longVal, char* ptrBuf) {
    int ret = 0;
    PACK_32(longVal, ptrBuf, mem_traits, ret);
    return ret;
}

inline int len_long(const i32& longVal) {
    return MY_32(longVal);
}

template <typename T, typename C>
inline const C* Unpack32(T& x, const C* src) {
    int pkLen = 0;
    const char* c = reinterpret_cast<const char*>(src);
    Y_UNUSED(pkLen);
    UNPACK_32(x, c, mem_traits, pkLen);
    Y_ASSERT(pkLen);
    return reinterpret_cast<const C*>(c);
}

template <typename T, typename C>
inline const C* Unpack64(T& x, const C* src) {
    int pkLen = 0;
    const char* c = reinterpret_cast<const char*>(src);
    Y_UNUSED(pkLen);
    UNPACK_64(x, c, mem_traits, pkLen);
    Y_ASSERT(pkLen);
    return reinterpret_cast<const C*>(c);
}

template <typename T, typename C>
inline C* Pack32(const T& x, C* dest) {
    int pkLen = 0;
    Y_UNUSED(pkLen);
    char* c = reinterpret_cast<char*>(dest);
    PACK_32(x, c, mem_traits, pkLen);
    Y_ASSERT(pkLen);
    return reinterpret_cast<C*>(c);
}

template <typename T, typename C>
inline C* Pack64(const T& x, C* dest) {
    int pkLen = 0;
    Y_UNUSED(pkLen);
    char* c = reinterpret_cast<char*>(dest);
    PACK_64(x, c, mem_traits, pkLen);
    Y_ASSERT(pkLen);
    return reinterpret_cast<C*>(c);
}