Fix #2 Preserve unsigned 64-bit decoded values
This commit is contained in:
1 parent
2c057b631d
commit
69074e3321
6 files changed
+112
-15
No files matched your search
+11
-8
@@ -55,10 +55,12 @@ DecodedFrameValue DbcDecoder::Decode (const DecodeDatabase &database,
|
||||
if (signal.valueType == ValueType::Signed)
|
||||
decoded.rawValue = SignExtend (unsignedValue, signal.length);
|
||||
else
|
||||
decoded.rawValue = static_cast<std::int64_t> (unsignedValue);
|
||||
decoded.rawValue = unsignedValue;
|
||||
|
||||
decoded.physicalValue =
|
||||
(static_cast<double> (decoded.rawValue) * signal.factor) + signal.offset;
|
||||
std::visit ([&signal] (auto rawValue) {
|
||||
return (static_cast<double> (rawValue) * signal.factor) + signal.offset;
|
||||
}, decoded.rawValue);
|
||||
decoded.valid = true;
|
||||
|
||||
result.signals.push_back (decoded);
|
||||
@@ -137,16 +139,17 @@ bool DbcDecoder::ExtractMotorola (const std::vector<std::uint8_t> &data,
|
||||
}
|
||||
|
||||
std::int64_t DbcDecoder::SignExtend (std::uint64_t value, std::uint32_t bitLength) {
|
||||
if ((bitLength == 0U) || (bitLength >= 64U))
|
||||
return static_cast<std::int64_t> (value);
|
||||
|
||||
const std::uint64_t signMask = (static_cast<std::uint64_t> (1U) << (bitLength - 1U));
|
||||
const std::uint64_t valueMask = (static_cast<std::uint64_t> (1U) << bitLength) - 1U;
|
||||
if ((bitLength == 0U) || (bitLength > 64U))
|
||||
return 0;
|
||||
|
||||
const std::uint64_t signMask = std::uint64_t (1U) << (bitLength - 1U);
|
||||
const std::uint64_t valueMask = (bitLength == 64U)
|
||||
? ~std::uint64_t (0U) : (std::uint64_t (1U) << bitLength) - 1U;
|
||||
value &= valueMask;
|
||||
|
||||
if ((value & signMask) == 0U)
|
||||
return static_cast<std::int64_t> (value);
|
||||
|
||||
return static_cast<std::int64_t> (value | (~valueMask));
|
||||
// The complemented magnitude fits int64_t, including for INT64_MIN.
|
||||
return -1 - static_cast<std::int64_t> ((~value) & valueMask);
|
||||
}
|
||||
Reference in new issue
Block a user