SILICON
Loading...
Searching...
No Matches
num_formatting.hpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include <core/wire.hpp>
21
22#include <cstddef>
23#include <optional>
24#include <string>
25#include <string_view>
26
27#include <boost/multiprecision/cpp_int.hpp>
28
29namespace SILICON::core {
30
31using BigInt = boost::multiprecision::cpp_int;
32
34
40
42[[nodiscard]] std::string formatValue(const BusValue& value, BusValueFormat format,
43 std::size_t fixedWidth = 0);
44
50[[nodiscard]] std::string formatInteger(const BigInt& value, BusValueFormat format,
51 std::size_t bitWidth);
52
57[[nodiscard]] std::optional<BigInt>
58parseInteger(std::string_view text, BusValueFormat format, std::size_t bitWidth);
59
67[[nodiscard]] ParsedBusValue valueFromStr(std::string_view text);
68
76[[nodiscard]] std::optional<BusValue> resizeParsedValue(const ParsedBusValue& parsed,
77 std::size_t width);
78
79[[nodiscard]] BusValue maxValueForBusWidth(std::size_t width);
80
86[[nodiscard]] BusValue busValueFromInteger(const BigInt& value, std::size_t width);
87
92[[nodiscard]] BigInt integerFromBusValue(const BusValue& value,
93 Signedness signedness = Signedness::UNSIGNED);
94
96[[nodiscard]] BusValue busValueFromBits(std::string_view bits);
97
98} // namespace SILICON::core
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 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
Definition wire.hpp:120
A bit-vector together with the textual interpretation that produced it.