[InstCombine] Signed saturation patterns
[llvm-complete.git] / lib / Support / LEB128.cpp
blobd41b673e9c8a5adf11a9f9856433ab072b693dd8
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.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/LEB128.h"
16 namespace llvm {
18 /// Utility function to get the size of the ULEB128-encoded 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;
28 /// Utility function to get the size of the SLEB128-encoded value.
29 unsigned getSLEB128Size(int64_t Value) {
30 unsigned Size = 0;
31 int Sign = Value >> (8 * sizeof(Value) - 1);
32 bool IsMore;
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;
43 } // namespace llvm