Skip to content

Commit

Permalink
bug fix of posit binary printing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravenwater committed May 3, 2023
1 parent ec7067c commit 40ef3ad
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
21 changes: 13 additions & 8 deletions include/universal/number/posit/manipulators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ std::string color_print(const posit<nbits, es>& p) {
regime<nbits, es> _regime;
exponent<nbits, es> _exponent;
fraction<fbits> _fraction;
decode(p.get(), _sign, _regime, _exponent, _fraction);
extract_fields(p.get(), _sign, _regime, _exponent, _fraction);

Color red(ColorCode::FG_RED);
Color yellow(ColorCode::FG_YELLOW);
Expand All @@ -197,13 +197,18 @@ std::string color_print(const posit<nbits, es>& p) {
Color def(ColorCode::FG_DEFAULT);
str << red << (p.isneg() ? "1" : "0");

bitblock<nbits - 1> r = _regime.get();
int regimeBits = (int)_regime.nrBits();
int nrOfRegimeBitsProcessed = 0;
for (unsigned i = 0; i < nbits - 1; ++i) {
unsigned bitIndex = nbits - 2u - i;
if (regimeBits > nrOfRegimeBitsProcessed++) {
str << yellow << (_sign ? (r[bitIndex] ? '0' : '1') : (r[bitIndex] ? '1' : '0'));
if (p.isnar()) {
for (unsigned i = 0; i < nbits - 1; ++i) str << yellow << '0';
}
else {
bitblock<nbits - 1> r = _regime.get();
int regimeBits = (int)_regime.nrBits();
int nrOfRegimeBitsProcessed = 0;
for (unsigned i = 0; i < nbits - 1; ++i) {
unsigned bitIndex = nbits - 2u - i;
if (regimeBits > nrOfRegimeBitsProcessed++) {
str << yellow << (_sign ? (r[bitIndex] ? '0' : '1') : (r[bitIndex] ? '1' : '0'));
}
}
}

Expand Down
14 changes: 12 additions & 2 deletions include/universal/number/posit/posit_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,16 @@ int decode_regime(const internal::bitblock<nbits>& raw_bits) {
// extract_fields takes a raw posit encoding and extracts the sign, regime, exponent, and fraction components
template<unsigned nbits, unsigned es, unsigned fbits>
void extract_fields(const bitblock<nbits>& raw_bits, bool& _sign, regime<nbits, es>& _regime, exponent<nbits, es>& _exponent, fraction<fbits>& _fraction) {
// check special case
// check special cases
bitblock<nbits> nar;
nar[nbits - 1] = true;
if (raw_bits == nar) {
_sign = true;
_regime.setzero();
_exponent.setzero();
_fraction.setzero();
return;
}
bitblock<nbits> zero;
if (raw_bits == zero) {
_sign = false;
Expand Down Expand Up @@ -204,8 +213,9 @@ void decode(const bitblock<nbits>& raw_bits, bool& _sign, regime<nbits, es>& _re
if (tmp.none()) {
// setnar(); special case = NaR (Not a Real)
_sign = true;
_regime.setinf();
_regime.setzero();
_exponent.reset();
_fraction.reset();
}
else {
extract_fields(raw_bits, _sign, _regime, _exponent, _fraction);
Expand Down
25 changes: 25 additions & 0 deletions static/posit/api/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define POSIT_ERROR_FREE_IO_FORMAT 0
// minimum set of include files to reflect source code dependencies
#include <universal/number/posit/posit.hpp>
#include <universal/verification/test_suite.hpp>

int main()
try {
Expand Down Expand Up @@ -57,6 +58,30 @@ try {
std::cout << "standard posit : " << type_tag(posit<256, 2>()) << '\n';
}

// special cases
{
using Posit = posit<8, 0>;
Posit a;
a.setnar(); ReportValue(a, "NaR : ");
a.maxpos(); ReportValue(a, "maxpos : ");
a = 1; ReportValue(a, " 1 : ");
a.minpos(); ReportValue(a, "minpos : ");
a.setzero(); ReportValue(a, "zero : ");
a.minneg(); ReportValue(a, "minneg : ");
a = -1; ReportValue(a, " -1 : ");
a.maxneg(); ReportValue(a, "maxneg : ");
}

// color printing
{
using Posit = posit<5, 1>;
Posit a;
for (unsigned i = 0; i < 32; ++i) {
a.setbits(i);
std::cout << to_binary(a) << " : " << color_print(a) << " : " << a << '\n';
}
}

/////////////////////////////////////////////////////////////////////////////////////
//// improving efficiency for posits through explicit BlockType specification

Expand Down

0 comments on commit 40ef3ad

Please sign in to comment.