-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
varint.h
82 lines (71 loc) · 1.45 KB
/
varint.h
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
class VarInt
{
public:
static uint32_t ReadUnsigned(PTR_uint8_t & pbEncoding)
{
uintptr_t lengthBits = *pbEncoding & 0x0F;
size_t negLength = s_negLengthTab[lengthBits];
uintptr_t shift = s_shiftTab[lengthBits];
uint32_t result = *(PTR_uint32_t)(pbEncoding - negLength - 4);
result >>= shift;
pbEncoding -= negLength;
return result;
}
private:
static int8_t s_negLengthTab[16];
static uint8_t s_shiftTab[16];
};
#ifndef __GNUC__
__declspec(selectany)
#endif
int8_t
#ifdef __GNUC__
__attribute__((weak))
#endif
VarInt::s_negLengthTab[16] =
{
-1, // 0
-2, // 1
-1, // 2
-3, // 3
-1, // 4
-2, // 5
-1, // 6
-4, // 7
-1, // 8
-2, // 9
-1, // 10
-3, // 11
-1, // 12
-2, // 13
-1, // 14
-5, // 15
};
#ifndef __GNUC__
__declspec(selectany)
#endif
uint8_t
#ifdef __GNUC__
__attribute__((weak))
#endif
VarInt::s_shiftTab[16] =
{
32-7*1, // 0
32-7*2, // 1
32-7*1, // 2
32-7*3, // 3
32-7*1, // 4
32-7*2, // 5
32-7*1, // 6
32-7*4, // 7
32-7*1, // 8
32-7*2, // 9
32-7*1, // 10
32-7*3, // 11
32-7*1, // 12
32-7*2, // 13
32-7*1, // 14
0, // 15
};