1 //===- llvm/Support/LEB128.h - [SU]LEB128 utility functions -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares some utility functions for encoding SLEB128 and
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SYSTEM_LEB128_H
16 #define LLVM_SYSTEM_LEB128_H
18 #include <llvm/Support/raw_ostream.h>
22 /// Utility function to encode a SLEB128 value.
23 static inline void encodeSLEB128(int64_t Value, raw_ostream &OS) {
26 uint8_t Byte = Value & 0x7f;
27 // NOTE: this assumes that this signed shift is an arithmetic right shift.
29 More = !((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
30 ((Value == -1) && ((Byte & 0x40) != 0))));
32 Byte |= 0x80; // Mark this byte that that more bytes will follow.
37 /// Utility function to encode a ULEB128 value.
38 static inline void encodeULEB128(uint64_t Value, raw_ostream &OS,
39 unsigned Padding = 0) {
41 uint8_t Byte = Value & 0x7f;
43 if (Value != 0 || Padding != 0)
44 Byte |= 0x80; // Mark this byte that that more bytes will follow.
48 // Pad with 0x80 and emit a null byte at the end.
50 for (; Padding != 1; --Padding)
58 #endif // LLVM_SYSTEM_LEB128_H