1 //===- LEB128.cpp - LEB128 utility functions implementation -----*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements some utility functions for encoding SLEB128 and
10 // ULEB128 values.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Support/LEB128.h"
15 
16 namespace llvm {
17 
18 /// Utility function to get the size of the ULEB128-encoded value.
getULEB128Size(uint64_t Value)19 unsigned getULEB128Size(uint64_t Value) {
20   unsigned Size = 0;
21   do {
22     Value >>= 7;
23     Size += sizeof(int8_t);
24   } while (Value);
25   return Size;
26 }
27 
28 /// Utility function to get the size of the SLEB128-encoded value.
getSLEB128Size(int64_t Value)29 unsigned getSLEB128Size(int64_t Value) {
30   unsigned Size = 0;
31   int Sign = Value >> (8 * sizeof(Value) - 1);
32   bool IsMore;
33 
34   do {
35     unsigned Byte = Value & 0x7f;
36     Value >>= 7;
37     IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
38     Size += sizeof(int8_t);
39   } while (IsMore);
40   return Size;
41 }
42 
43 }  // namespace llvm
44