LEB128.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. #include "llvm/Support/LEB128.h"
  14. namespace llvm {
  15. /// Utility function to get the size of the ULEB128-encoded value.
  16. unsigned getULEB128Size(uint64_t Value) {
  17. unsigned Size = 0;
  18. do {
  19. Value >>= 7;
  20. Size += sizeof(int8_t);
  21. } while (Value);
  22. return Size;
  23. }
  24. /// Utility function to get the size of the SLEB128-encoded value.
  25. unsigned getSLEB128Size(int64_t Value) {
  26. unsigned Size = 0;
  27. int Sign = Value >> (8 * sizeof(Value) - 1);
  28. bool IsMore;
  29. do {
  30. unsigned Byte = Value & 0x7f;
  31. Value >>= 7;
  32. IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
  33. Size += sizeof(int8_t);
  34. } while (IsMore);
  35. return Size;
  36. }
  37. } // namespace llvm