Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:27

0001 //===- llvm/Support/BCD.h - Binary-Coded Decimal utility functions -*- C++ -*-//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file declares some utility functions for encoding/decoding BCD values.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_SUPPORT_BCD_H
0014 #define LLVM_SUPPORT_BCD_H
0015 
0016 #include <assert.h>
0017 #include <cstddef>
0018 #include <cstdint>
0019 
0020 namespace llvm {
0021 
0022 // Decode a packed BCD value.
0023 // Maximum value of int64_t is 9,223,372,036,854,775,807. These are 18 usable
0024 // decimal digits. Thus BCD numbers of up to 9 bytes can be converted.
0025 // Please note that s390 supports BCD numbers up to a length of 16 bytes.
0026 inline int64_t decodePackedBCD(const uint8_t *Ptr, size_t ByteLen,
0027                                bool IsSigned = true) {
0028   assert(ByteLen >= 1 && ByteLen <= 9 && "Invalid BCD number");
0029   int64_t Value = 0;
0030   size_t RunLen = ByteLen - static_cast<unsigned>(IsSigned);
0031   for (size_t I = 0; I < RunLen; ++I) {
0032     uint8_t DecodedByteValue = ((Ptr[I] >> 4) & 0x0f) * 10 + (Ptr[I] & 0x0f);
0033     Value = (Value * 100) + DecodedByteValue;
0034   }
0035   if (IsSigned) {
0036     uint8_t DecodedByteValue = (Ptr[ByteLen - 1] >> 4) & 0x0f;
0037     uint8_t Sign = Ptr[ByteLen - 1] & 0x0f;
0038     Value = (Value * 10) + DecodedByteValue;
0039     if (Sign == 0x0d || Sign == 0x0b)
0040       Value *= -1;
0041   }
0042   return Value;
0043 }
0044 
0045 template <typename ResultT, typename ValT>
0046 inline ResultT decodePackedBCD(const ValT Val, bool IsSigned = true) {
0047   return static_cast<ResultT>(decodePackedBCD(
0048       reinterpret_cast<const uint8_t *>(&Val), sizeof(ValT), IsSigned));
0049 }
0050 
0051 } // namespace llvm
0052 
0053 #endif // LLVM_SUPPORT_BCD_H