SILICON
Toggle main menu visibility
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
29
namespace
SILICON::core
{
30
31
using
BigInt
= boost::multiprecision::cpp_int;
32
33
enum class
BusValueFormat
{
Raw
,
Signed
,
Unsigned
,
Hex
,
Oct
,
Bin
,
Unknown
};
34
36
struct
ParsedBusValue
{
37
BusValue
value
;
38
BusValueFormat
format
=
BusValueFormat::Unknown
;
39
};
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>
58
parseInteger
(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
SILICON::core
Definition
activeKeyGuard.hpp:28
SILICON::core::valueFromStr
ParsedBusValue valueFromStr(const std::string_view value)
Parse user-facing decimal, hexadecimal, octal, binary, or four-state text.
Definition
num_formatting.cpp:355
SILICON::core::BigInt
boost::multiprecision::cpp_int BigInt
Definition
num_formatting.hpp:31
SILICON::core::parseInteger
std::optional< BigInt > parseInteger(std::string_view text, const BusValueFormat format, const std::size_t bitWidth)
Definition
num_formatting.cpp:198
SILICON::core::resizeParsedValue
std::optional< BusValue > resizeParsedValue(const ParsedBusValue &parsed, const std::size_t width)
Adapts a parsed textual value to an exact destination width.
Definition
num_formatting.cpp:394
SILICON::core::integerFromBusValue
BigInt integerFromBusValue(const BusValue &value, const Signedness signedness)
Decodes an LSB-first BusValue as an unsigned arbitrary-precision integer. Empty values yield zero....
Definition
num_formatting.cpp:272
SILICON::core::maxValueForBusWidth
BusValue maxValueForBusWidth(const std::size_t width)
Definition
num_formatting.cpp:252
SILICON::core::busValueFromBits
BusValue busValueFromBits(const std::string_view bits)
Strictly decodes an MSB-first raw string containing 0, 1, X, or E.
Definition
num_formatting.cpp:295
SILICON::core::BusValueFormat
BusValueFormat
Definition
num_formatting.hpp:33
SILICON::core::BusValueFormat::Oct
@ Oct
Definition
num_formatting.hpp:33
SILICON::core::BusValueFormat::Raw
@ Raw
Definition
num_formatting.hpp:33
SILICON::core::BusValueFormat::Signed
@ Signed
Definition
num_formatting.hpp:33
SILICON::core::BusValueFormat::Bin
@ Bin
Definition
num_formatting.hpp:33
SILICON::core::BusValueFormat::Unknown
@ Unknown
Definition
num_formatting.hpp:33
SILICON::core::BusValueFormat::Hex
@ Hex
Definition
num_formatting.hpp:33
SILICON::core::BusValueFormat::Unsigned
@ Unsigned
Definition
num_formatting.hpp:33
SILICON::core::formatInteger
std::string formatInteger(const BigInt &value, const BusValueFormat format, const std::size_t bitWidth)
Definition
num_formatting.cpp:166
SILICON::core::busValueFromInteger
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...
Definition
num_formatting.cpp:257
SILICON::core::Signedness
Signedness
Definition
wire.hpp:118
SILICON::core::Signedness::UNSIGNED
@ UNSIGNED
Definition
wire.hpp:118
SILICON::core::formatValue
std::string formatValue(const BusValue &value, const BusValueFormat format, const std::size_t fixedWidth)
Format an LSB-first BusValue for display.
Definition
num_formatting.cpp:311
SILICON::core::BusValue
std::vector< State > BusValue
Definition
wire.hpp:120
SILICON::core::ParsedBusValue
A bit-vector together with the textual interpretation that produced it.
Definition
num_formatting.hpp:36
SILICON::core::ParsedBusValue::format
BusValueFormat format
Definition
num_formatting.hpp:38
SILICON::core::ParsedBusValue::value
BusValue value
Definition
num_formatting.hpp:37
wire.hpp
src
utils
num_formatting.hpp
Generated by
1.17.0