20#include <boost/multiprecision/cpp_int/import_export.hpp>
38 const std::map<BusValueFormat, std::string> formatPrefix{
45 const std::map<BusValueFormat, std::string> formatAlphabet{
47 std::format(
"{}{}{}{}",
static_cast<char>(std::to_underlying(
State::LOW)),
58 bool satisfiesAlphabet(
const std::string_view DIGITS,
const BusValueFormat format)
60 const auto alphabet = formatAlphabet.at(format);
62 return !DIGITS.empty() && std::ranges::all_of(DIGITS, [alphabet](
const char digit) {
63 return alphabet.contains(toupper(digit));
67 char upper(
const char value)
69 return static_cast<char>(std::toupper(
static_cast<unsigned char>(value)));
72 std::string_view trim(std::string_view text)
74 while (!text.empty() && std::isspace(
static_cast<unsigned char>(text.front())) != 0)
75 text.remove_prefix(1);
76 while (!text.empty() && std::isspace(
static_cast<unsigned char>(text.back())) != 0)
77 text.remove_suffix(1);
81 bool startsWithIgnoreCase(
const std::string_view value,
const std::string_view prefix)
84 && std::ranges::equal(
86 [](
const char lhs,
const char rhs) { return upper(lhs) == upper(rhs); });
89 [[nodiscard]] std::pair<BusValueFormat, std::string_view>
90 getFormat(std::string_view value)
97 for (
const auto& [format, prefix] : formatPrefix) {
98 if (!startsWithIgnoreCase(value, prefix))
102 if (satisfiesAlphabet(DIGITS, format))
103 return {format, DIGITS};
106 for (
const auto& [format, _] : formatAlphabet) {
107 if (formatPrefix.contains(format))
111 if (satisfiesAlphabet(DIGITS, format))
112 return {format, DIGITS};
120 std::vector<unsigned char> bits;
121 bits.reserve(DIGITS.size());
122 std::ranges::transform(DIGITS, std::back_inserter(bits),
123 [](
const char bit) {
return bit ==
'1'; });
126 boost::multiprecision::import_bits(result, bits.begin(), bits.end(), 1,
true);
130 auto firstNonZero = DIGITS.find_first_not_of(
'0');
131 if (firstNonZero == std::string_view::npos)
134 std::string encoded(DIGITS.substr(firstNonZero));
140 default:
throw std::invalid_argument(
"Invalid integer input format");
146 std::size_t magnitudeWidth(
const BigInt& value)
148 return value == 0 ? 1
149 :
static_cast<std::size_t
>(boost::multiprecision::msb(value)) + 1;
158 return value.str(0, std::ios_base::hex | std::ios_base::uppercase);
160 default:
throw std::invalid_argument(
"Invalid integer output format");
167 const std::size_t bitWidth)
170 throw std::invalid_argument(
"Integer bit width must be positive");
173 BigInt number = value & (modulus - 1);
177 if ((number & (
BigInt{1} << (bitWidth - 1))) != 0)
179 return formatBigInt(number, format);
184 return bits | std::views::reverse | std::views::transform([](
const State state) {
185 return static_cast<char>(std::to_underlying(state));
187 | std::ranges::to<std::string>();
193 throw std::invalid_argument(
"Invalid integer output format");
195 throw std::invalid_argument(
"Invalid integer output format");
199 const std::size_t bitWidth)
209 bool negative =
false;
210 if (text.front() ==
'+' || text.front() ==
'-') {
211 negative = text.front() ==
'-';
212 text.remove_prefix(1);
214 if (!satisfiesAlphabet(text, format))
217 BigInt value = parseMagnitude(text, format);
222 const BigInt maximum = (
BigInt{1} << (bitWidth - 1)) - 1;
223 if (value < minimum || value > maximum)
231 text.remove_prefix(1);
238 text.remove_prefix(2);
240 text.remove_prefix(2);
242 text.remove_prefix(2);
244 if (!satisfiesAlphabet(text, format))
246 const BigInt value = parseMagnitude(text, format);
247 if (value >= modulus)
263 const BigInt bitPattern = value & ((
BigInt{1} << width) - 1);
264 std::vector<unsigned char> bits;
265 boost::multiprecision::export_bits(bitPattern, std::back_inserter(bits), 1,
false);
266 std::ranges::transform(
267 bits | std::views::take(width), result.begin(),
268 [](
const unsigned char bit) { return bit == 0 ? State::LOW : State::HIGH; });
278 throw std::invalid_argument(
"Bus values must contain only 0 or 1 to form an integer");
280 std::vector<unsigned char> bits;
281 bits.reserve(value.size());
282 std::ranges::transform(value, std::back_inserter(bits),
287 boost::multiprecision::import_bits(result, bits.begin(), bits.end(), 1,
false);
290 result -=
BigInt{1} << value.size();
300 const auto [format, DIGITS] = getFormat(bits);
303 throw std::invalid_argument(
"Raw bus values may contain only 0, 1, X, or E");
305 return DIGITS | std::views::reverse | std::views::transform([](
const char digit) {
306 return static_cast<State>(upper(digit));
308 | std::ranges::to<BusValue>();
312 const std::size_t fixedWidth)
317 const bool mustBeRaw = std::ranges::any_of(value, [](
const State state) {
321 const auto rawStr = value | std::views::reverse
322 | std::views::transform([](
const State state) {
323 return static_cast<char>(std::to_underlying(state));
325 | std::ranges::to<std::string>();
339 ? unsignedNumber - (
BigInt{1} << value.size())
341 res = formatBigInt(signedNumber, format);
348 const bool needsPrefix =
350 const std::size_t padding = fixedWidth > res.size() ? fixedWidth - res.size() : 0;
351 return std::format(
"{}{}{}", needsPrefix ? formatPrefix.at(format) :
"",
352 std::string(padding,
'0'), res);
357 const auto [format, DIGITS] = getFormat(value);
362 result = DIGITS | std::views::reverse | std::views::transform([](
const char digit) {
363 return static_cast<State>(upper(digit));
365 | std::ranges::to<BusValue>();
378 const BigInt magnitude = parseMagnitude(DIGITS, format);
383 if (trim(value).front() ==
'-')
391 return {std::move(result), format};
395 const std::size_t width)
404 parsed.
value.back());
ParsedBusValue valueFromStr(const std::string_view value)
Parse user-facing decimal, hexadecimal, octal, binary, or four-state text.
boost::multiprecision::cpp_int BigInt
std::optional< BigInt > parseInteger(std::string_view text, const BusValueFormat format, const std::size_t bitWidth)
std::optional< BusValue > resizeParsedValue(const ParsedBusValue &parsed, const std::size_t width)
Adapts a parsed textual value to an exact destination width.
BigInt integerFromBusValue(const BusValue &value, const Signedness signedness)
Decodes an LSB-first BusValue as an unsigned arbitrary-precision integer. Empty values yield zero....
BusValue maxValueForBusWidth(const std::size_t width)
BusValue twosComplement(const BusValue &n)
BusValue busValueFromBits(const std::string_view bits)
Strictly decodes an MSB-first raw string containing 0, 1, X, or E.
std::string formatInteger(const BigInt &value, const BusValueFormat format, const std::size_t bitWidth)
BusValue busValueFromInteger(const BigInt &value, const std::size_t width)
Builds an LSB-first BusValue from the low width bits of an integer. Negative values use two's complem...
std::string formatValue(const BusValue &value, const BusValueFormat format, const std::size_t fixedWidth)
Format an LSB-first BusValue for display.
std::vector< State > BusValue
QVariant value(const QSettings &settings, const Definition &setting)
Reads a setting value, falling back to its declared default when unset.
BusValue normalizeBusValue(const BusValue &value, const std::size_t width, const State extension)
Resize an LSB-first value to an exact bus width.
bool fitsUnsigned(const BusValue &value, const std::size_t width)
Tests whether an LSB-first value is representable as an unsigned value.
bool fitsSigned(const BusValue &value, const std::size_t width)
Tests whether an LSB-first two's-complement value fits a signed width.
bool isKnownBinary(const State state)
Tests whether a state is a concrete binary logic value.
A bit-vector together with the textual interpretation that produced it.