/root/doris/be/src/util/coding.cpp
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 | 73.3M | uint8_t* encode_varint32(uint8_t* dst, uint32_t v) { |
14 | | // Operate on characters as unsigneds |
15 | 73.3M | static const int B = 128; |
16 | 73.3M | if (v < (1 << 7)) { |
17 | 72.7M | *(dst++) = v; |
18 | 72.7M | } else if (v < (1 << 14)) { |
19 | 459k | *(dst++) = v | B; |
20 | 459k | *(dst++) = v >> 7; |
21 | 459k | } else if (v < (1 << 21)) { |
22 | 127k | *(dst++) = v | B; |
23 | 127k | *(dst++) = (v >> 7) | B; |
24 | 127k | *(dst++) = v >> 14; |
25 | 18.4E | } else if (v < (1 << 28)) { |
26 | 10 | *(dst++) = v | B; |
27 | 10 | *(dst++) = (v >> 7) | B; |
28 | 10 | *(dst++) = (v >> 14) | B; |
29 | 10 | *(dst++) = v >> 21; |
30 | 18.4E | } else { |
31 | 18.4E | *(dst++) = v | B; |
32 | 18.4E | *(dst++) = (v >> 7) | B; |
33 | 18.4E | *(dst++) = (v >> 14) | B; |
34 | 18.4E | *(dst++) = (v >> 21) | B; |
35 | 18.4E | *(dst++) = v >> 28; |
36 | 18.4E | } |
37 | 73.3M | return dst; |
38 | 73.3M | } |
39 | | |
40 | | const uint8_t* decode_varint32_ptr_fallback(const uint8_t* p, const uint8_t* limit, |
41 | 364k | uint32_t* value) { |
42 | 364k | uint32_t result = 0; |
43 | 828k | for (uint32_t shift = 0; shift <= 28 && p < limit; shift += 7) { |
44 | 828k | uint32_t byte = *p; |
45 | 828k | p++; |
46 | 828k | if (byte & 128) { |
47 | | // More bytes are present |
48 | 463k | result |= ((byte & 127) << shift); |
49 | 463k | } else { |
50 | 365k | result |= (byte << shift); |
51 | 365k | *value = result; |
52 | 365k | return p; |
53 | 365k | } |
54 | 828k | } |
55 | 18.4E | return nullptr; |
56 | 364k | } |
57 | | |
58 | 245k | const uint8_t* decode_varint64_ptr(const uint8_t* p, const uint8_t* limit, uint64_t* value) { |
59 | 245k | uint64_t result = 0; |
60 | 885k | for (uint32_t shift = 0; shift <= 63 && p < limit; shift += 7) { |
61 | 881k | uint64_t byte = *p; |
62 | 881k | p++; |
63 | 881k | if (byte & 128) { |
64 | | // More bytes are present |
65 | 640k | result |= ((byte & 127) << shift); |
66 | 640k | } else { |
67 | 241k | result |= (byte << shift); |
68 | 241k | *value = result; |
69 | 241k | return p; |
70 | 241k | } |
71 | 881k | } |
72 | 4.01k | return nullptr; |
73 | 245k | } |
74 | | |
75 | | } // namespace doris |