Line | Count | Source |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under both the GPLv2 (found in the |
3 | | // COPYING file in the root directory) and Apache 2.0 License |
4 | | // (found in the LICENSE.Apache file in the root directory). |
5 | | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
6 | | // Use of this source code is governed by a BSD-style license that can be |
7 | | // found in the LICENSE file. See the AUTHORS file for names of contributors. |
8 | | |
9 | | #include "util/coding.h" |
10 | | |
11 | | namespace doris { |
12 | | |
13 | 36.9M | uint8_t* encode_varint32(uint8_t* dst, uint32_t v) { |
14 | | // Operate on characters as unsigneds |
15 | 36.9M | static const int B = 128; |
16 | 36.9M | if (v < (1 << 7)) { |
17 | 36.3M | *(dst++) = uint8_t(v); |
18 | 36.3M | } else if (v < (1 << 14)) { |
19 | 563k | *(dst++) = uint8_t(v | B); |
20 | 563k | *(dst++) = uint8_t(v >> 7); |
21 | 563k | } else if (v < (1 << 21)) { |
22 | 45.0k | *(dst++) = uint8_t(v | B); |
23 | 45.0k | *(dst++) = uint8_t((v >> 7) | B); |
24 | 45.0k | *(dst++) = uint8_t(v >> 14); |
25 | 18.4E | } else if (v < (1 << 28)) { |
26 | 0 | *(dst++) = uint8_t(v | B); |
27 | 0 | *(dst++) = uint8_t((v >> 7) | B); |
28 | 0 | *(dst++) = uint8_t((v >> 14) | B); |
29 | 0 | *(dst++) = uint8_t(v >> 21); |
30 | 18.4E | } else { |
31 | 18.4E | *(dst++) = uint8_t(v | B); |
32 | 18.4E | *(dst++) = uint8_t((v >> 7) | B); |
33 | 18.4E | *(dst++) = uint8_t((v >> 14) | B); |
34 | 18.4E | *(dst++) = uint8_t((v >> 21) | B); |
35 | 18.4E | *(dst++) = uint8_t(v >> 28); |
36 | 18.4E | } |
37 | 36.9M | return dst; |
38 | 36.9M | } |
39 | | |
40 | | const uint8_t* decode_varint32_ptr_fallback(const uint8_t* p, const uint8_t* limit, |
41 | 587k | uint32_t* value) { |
42 | 587k | uint32_t result = 0; |
43 | 1.25M | for (uint32_t shift = 0; shift <= 28 && p < limit; shift += 7) { |
44 | 1.25M | uint32_t byte = *p; |
45 | 1.25M | p++; |
46 | 1.25M | if (byte & 128) { |
47 | | // More bytes are present |
48 | 666k | result |= ((byte & 127) << shift); |
49 | 666k | } else { |
50 | 587k | result |= (byte << shift); |
51 | 587k | *value = result; |
52 | 587k | return p; |
53 | 587k | } |
54 | 1.25M | } |
55 | 94 | return nullptr; |
56 | 587k | } |
57 | | |
58 | 176k | const uint8_t* decode_varint64_ptr(const uint8_t* p, const uint8_t* limit, uint64_t* value) { |
59 | 176k | uint64_t result = 0; |
60 | 582k | for (uint32_t shift = 0; shift <= 63 && p < limit; shift += 7) { |
61 | 582k | uint64_t byte = *p; |
62 | 582k | p++; |
63 | 582k | if (byte & 128) { |
64 | | // More bytes are present |
65 | 405k | result |= ((byte & 127) << shift); |
66 | 405k | } else { |
67 | 176k | result |= (byte << shift); |
68 | 176k | *value = result; |
69 | 176k | return p; |
70 | 176k | } |
71 | 582k | } |
72 | 7 | return nullptr; |
73 | 176k | } |
74 | | } // namespace doris |