LLVM API Documentation

LEB128.cpp
Go to the documentation of this file.
00001 //===- LEB128.cpp - LEB128 utility functions implementation -----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements some utility functions for encoding SLEB128 and
00011 // ULEB128 values.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/Support/LEB128.h"
00016 
00017 namespace llvm {
00018 
00019 /// Utility function to get the size of the ULEB128-encoded value.
00020 unsigned getULEB128Size(uint64_t Value) {
00021   unsigned Size = 0;
00022   do {
00023     Value >>= 7;
00024     Size += sizeof(int8_t);
00025   } while (Value);
00026   return Size;
00027 }
00028 
00029 /// Utility function to get the size of the SLEB128-encoded value.
00030 unsigned getSLEB128Size(int64_t Value) {
00031   unsigned Size = 0;
00032   int Sign = Value >> (8 * sizeof(Value) - 1);
00033   bool IsMore;
00034 
00035   do {
00036     unsigned Byte = Value & 0x7f;
00037     Value >>= 7;
00038     IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
00039     Size += sizeof(int8_t);
00040   } while (IsMore);
00041   return Size;
00042 }
00043 
00044 }  // namespace llvm