Subversion Repositories QNX 8.QNX8 LLVM/Clang compiler suite

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. //===- llvm/Support/BCD.h - Binary-Coded Decimal utility functions -*- 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 declares some utility functions for encoding/decoding BCD values.
  10. //
  11. //===----------------------------------------------------------------------===//
  12.  
  13. #ifndef LLVM_SUPPORT_BCD_H
  14. #define LLVM_SUPPORT_BCD_H
  15.  
  16. #include <assert.h>
  17. #include <cstddef>
  18. #include <cstdint>
  19.  
  20. namespace llvm {
  21.  
  22. // Decode a packed BCD value.
  23. // Maximum value of int64_t is 9,223,372,036,854,775,807. These are 18 usable
  24. // decimal digits. Thus BCD numbers of up to 9 bytes can be converted.
  25. // Please note that s390 supports BCD numbers up to a length of 16 bytes.
  26. inline int64_t decodePackedBCD(const uint8_t *Ptr, size_t ByteLen,
  27.                                bool IsSigned = true) {
  28.   assert(ByteLen >= 1 && ByteLen <= 9 && "Invalid BCD number");
  29.   int64_t Value = 0;
  30.   size_t RunLen = ByteLen - static_cast<unsigned>(IsSigned);
  31.   for (size_t I = 0; I < RunLen; ++I) {
  32.     uint8_t DecodedByteValue = ((Ptr[I] >> 4) & 0x0f) * 10 + (Ptr[I] & 0x0f);
  33.     Value = (Value * 100) + DecodedByteValue;
  34.   }
  35.   if (IsSigned) {
  36.     uint8_t DecodedByteValue = (Ptr[ByteLen - 1] >> 4) & 0x0f;
  37.     uint8_t Sign = Ptr[ByteLen - 1] & 0x0f;
  38.     Value = (Value * 10) + DecodedByteValue;
  39.     if (Sign == 0x0d || Sign == 0x0b)
  40.       Value *= -1;
  41.   }
  42.   return Value;
  43. }
  44.  
  45. template <typename ResultT, typename ValT>
  46. inline ResultT decodePackedBCD(const ValT Val, bool IsSigned = true) {
  47.   return static_cast<ResultT>(decodePackedBCD(
  48.       reinterpret_cast<const uint8_t *>(&Val), sizeof(ValT), IsSigned));
  49. }
  50.  
  51. } // namespace llvm
  52.  
  53. #endif // LLVM_SUPPORT_BCD_H
  54.