12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- #pragma once
- #ifdef __GNUC__
- #pragma GCC diagnostic push
- #pragma GCC diagnostic ignored "-Wunused-parameter"
- #endif
- #ifndef LLVM_SUPPORT_BCD_H
- #define LLVM_SUPPORT_BCD_H
- #include <assert.h>
- #include <cstddef>
- #include <cstdint>
- namespace llvm {
- inline int64_t decodePackedBCD(const uint8_t *Ptr, size_t ByteLen,
- bool IsSigned = true) {
- assert(ByteLen >= 1 && ByteLen <= 9 && "Invalid BCD number");
- int64_t Value = 0;
- size_t RunLen = ByteLen - static_cast<unsigned>(IsSigned);
- for (size_t I = 0; I < RunLen; ++I) {
- uint8_t DecodedByteValue = ((Ptr[I] >> 4) & 0x0f) * 10 + (Ptr[I] & 0x0f);
- Value = (Value * 100) + DecodedByteValue;
- }
- if (IsSigned) {
- uint8_t DecodedByteValue = (Ptr[ByteLen - 1] >> 4) & 0x0f;
- uint8_t Sign = Ptr[ByteLen - 1] & 0x0f;
- Value = (Value * 10) + DecodedByteValue;
- if (Sign == 0x0d || Sign == 0x0b)
- Value *= -1;
- }
- return Value;
- }
- template <typename ResultT, typename ValT>
- inline ResultT decodePackedBCD(const ValT Val, bool IsSigned = true) {
- return static_cast<ResultT>(decodePackedBCD(
- reinterpret_cast<const uint8_t *>(&Val), sizeof(ValT), IsSigned));
- }
- }
- #endif
- #ifdef __GNUC__
- #pragma GCC diagnostic pop
- #endif
|