Welcome to mirror list, hosted at ThFree Co, Russian Federation.

varint.h « inc « Runtime « Native « src - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 69e6efee9d1fb0cc9e3228da1f8ba704cbebaa0e (plain)
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
class VarInt
{
public:
    static UInt32 ReadUnsigned(PTR_UInt8 & pbEncoding)
    {
        UIntNative lengthBits = *pbEncoding & 0x0F;
        size_t  negLength = s_negLengthTab[lengthBits];
        UIntNative shift = s_shiftTab[lengthBits];
        UInt32 result = *(PTR_UInt32)(pbEncoding - negLength - 4);

        result >>= shift;
        pbEncoding -= negLength;

        return result;
    }

    //
    // WARNING: This method returns the negative of the length of the value that it just skipped!
    //
    // This was helpful in the GC info scan loop because it allowed us to always skip past unsigned values in 
    // the  body of the loop.  At the end of loop, we use this negative sign to distinguish between two cases
    // and that allows us to decode the unsigned value that we need outside of the loop.  Note that we encode
    // the negatives in the s_negLengthTable to avoid any additional operations in the body of the GC scan 
    // loop.
    // 
    static IntNative SkipUnsigned(PTR_UInt8 & pbEncoding)
    {
        UIntNative lengthBits = *pbEncoding & 0x0F;
        size_t negLength = s_negLengthTab[lengthBits];
        pbEncoding -= negLength;
        return negLength;
    }

    static UIntNative WriteUnsigned(PTR_UInt8 pbDest, UInt32 value)
    {
        if (pbDest == NULL)
        {
            if (value < 128)
                return 1;

            if (value < 128*128)
                return 2;

            if (value < 128*128*128)
                return 3;

            if (value < 128*128*128*128)
                return 4;

            return 5;
        }

        if (value < 128)
        {
            *pbDest++ = (UInt8)(value*2 + 0);
            return 1;
        }

        if (value < 128*128)
        {
            *pbDest++ = (UInt8)(value*4 + 1);
            *pbDest++ = (UInt8)(value >> 6);
            return 2;
        }

        if (value < 128*128*128)
        {
            *pbDest++ = (UInt8)(value*8 + 3);
            *pbDest++ = (UInt8)(value >> 5);
            *pbDest++ = (UInt8)(value >> 13);
            return 3;
        }

        if (value < 128*128*128*128)
        {
            *pbDest++ = (UInt8)(value*16 + 7);
            *pbDest++ = (UInt8)(value >> 4);
            *pbDest++ = (UInt8)(value >> 12);
            *pbDest++ = (UInt8)(value >> 20);
            return 4;
        }

        *pbDest++ = 15;
        *pbDest++ = (UInt8)value;
        *pbDest++ = (UInt8)(value >> 8);
        *pbDest++ = (UInt8)(value >> 16);
        *pbDest++ = (UInt8)(value >> 24);
        return 5;
    }

private:
    static Int8 s_negLengthTab[16];
    static UInt8 s_shiftTab[16];
};

__declspec(selectany)
Int8 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
};

__declspec(selectany)
UInt8 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
};