Skip to content

Commit 76f759c

Browse files
committed
updating fixpnt cli command with universal type name and new types
1 parent 1047383 commit 76f759c

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed

include/universal/number/cfloat/cfloat.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ using fp8e5m2 = cfloat<8, 5, std::uint8_t, true, true, false>;
122122
// TODO: guard with cfloat trait
123123
template<typename Scalar>
124124
void ShowRepresentations(std::ostream& ostr, Scalar f) {
125-
auto oldprec = ostr.precision(); // save stream state
125+
auto defaultPrecision = ostr.precision(); // save stream state
126126

127127
constexpr int max_digits10 = std::numeric_limits<Scalar>::max_digits10; // floating-point attribute for printing scientific format
128128

@@ -132,7 +132,7 @@ void ShowRepresentations(std::ostream& ostr, Scalar f) {
132132
ostr << "binary form : " << to_binary(v, true) << '\n';
133133
ostr << "color coded : " << color_print(v) << '\n';
134134

135-
ostr << std::setprecision(oldprec);
135+
ostr << std::setprecision(defaultPrecision);
136136
}
137137

138138
}} // namespace sw::universal

tools/cmd/fixpnt.cpp

+32-26
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,36 @@
1010
#include <typeinfo>
1111
#include <universal/number/fixpnt/fixpnt.hpp>
1212

13-
const char* msg = "\n\
14-
class sw::universal::fixpnt < 8, 4, 1, unsigned char>\n\
15-
1.0625 \n\
16-
b0001.0001 \n\
17-
class sw::universal::fixpnt < 12, 4, 1, unsigned char>\n\
18-
1.0625\n\
19-
b0000'0001.0001\n\
20-
class sw::universal::fixpnt < 16, 8, 1, unsigned char>\n\
21-
1.06250000\n\
22-
b0000'0001.0001'0000\n\
23-
class sw::universal::fixpnt < 32, 16, 1, unsigned char>\n\
24-
1.0625000000000000\n\
25-
b0000'0000'0000'0001.0001'0000'0000'0000\n\
26-
class sw::universal::fixpnt < 64, 32, 1, unsigned char>\n\
27-
1.06250000000000000000000000000000\n\
28-
b0000'0000'0000'0000'0000'0000'0000'0001.0001'0000'0000'0000'0000'0000'0000'0000\n";
13+
// ShowRepresentations prints the different output formats for the Scalar type
14+
// TODO: guard with cfloat trait
15+
template<typename Scalar>
16+
void ShowRepresentations(std::ostream& ostr, Scalar value) {
17+
using namespace sw::universal;
18+
19+
auto defaultPrecision = ostr.precision(); // save stream state
20+
21+
constexpr int max_digits10 = std::numeric_limits<Scalar>::max_digits10; // floating-point attribute for printing scientific format
22+
23+
std::cout << '\n' << type_tag(Scalar()) << '\n';
24+
Scalar v(value); // convert to target fixpnt
25+
ostr << "fixed form : " << std::setprecision(max_digits10) << v << '\n';
26+
ostr << "triple form : " << to_triple(v) << '\n';
27+
ostr << "binary form : " << to_binary(v, true) << '\n';
28+
ostr << "color coded : " << color_print(v) << '\n';
29+
30+
ostr << std::setprecision(defaultPrecision);
31+
}
32+
33+
void Show(std::ostream& ostr, double d) {
34+
using namespace sw::universal;
2935

30-
template<typename FixedPoint>
31-
void attributes(const FixedPoint& v) {
32-
constexpr int max_digits10 = std::numeric_limits<FixedPoint>::max_digits10;
33-
std::cout << " " << typeid(FixedPoint).name() << '\n' << std::setprecision(max_digits10) << v << "\n" << to_binary(v, true) << std::endl;
36+
ShowRepresentations(ostr, fixpnt< 8, 4>(d));
37+
ShowRepresentations(ostr, fixpnt<12, 4>(d));
38+
ShowRepresentations(ostr, fixpnt<16, 8>(d));
39+
ShowRepresentations(ostr, fixpnt<24, 8>(d));
40+
ShowRepresentations(ostr, fixpnt<32, 16>(d));
41+
ShowRepresentations(ostr, fixpnt<48, 16>(d));
42+
ShowRepresentations(ostr, fixpnt<64, 32>(d));
3443
}
3544

3645
// receive a float and print its components
@@ -43,16 +52,13 @@ try {
4352
std::cerr << "Show the sign/scale/fraction components of a fixed-point value.\n";
4453
std::cerr << "Usage: fixpnt float_value\n";
4554
std::cerr << "Example: fixpnt 1.0625\n";
46-
std::cerr << msg << '\n';
55+
Show(std::cerr, 1.0625);
4756
return EXIT_SUCCESS; // signal successful completion for ctest
4857
}
4958
double d = atof(argv[1]);
5059

51-
attributes(fixpnt< 8, 4>(d));
52-
attributes(fixpnt<12, 4>(d));
53-
attributes(fixpnt<16, 8>(d));
54-
attributes(fixpnt<32,16>(d));
55-
attributes(fixpnt<64,32>(d));
60+
std::cout << "fixpnt " << argv[1] << '\n';
61+
Show(std::cout, d);
5662

5763
return EXIT_SUCCESS;
5864
}

0 commit comments

Comments
 (0)