2
0

LEB128.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. //===- LEB128.cpp - LEB128 utility functions implementation -----*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements some utility functions for encoding SLEB128 and
  11. // ULEB128 values.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/Support/LEB128.h"
  15. namespace llvm {
  16. /// Utility function to get the size of the ULEB128-encoded value.
  17. unsigned getULEB128Size(uint64_t Value) {
  18. unsigned Size = 0;
  19. do {
  20. Value >>= 7;
  21. Size += sizeof(int8_t);
  22. } while (Value);
  23. return Size;
  24. }
  25. /// Utility function to get the size of the SLEB128-encoded value.
  26. unsigned getSLEB128Size(int64_t Value) {
  27. unsigned Size = 0;
  28. int Sign = Value >> (8 * sizeof(Value) - 1);
  29. bool IsMore;
  30. do {
  31. unsigned Byte = Value & 0x7f;
  32. Value >>= 7;
  33. IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
  34. Size += sizeof(int8_t);
  35. } while (IsMore);
  36. return Size;
  37. }
  38. } // namespace llvm