Skip to content

Commit

Permalink
code hygiene in the posit universe
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravenwater committed Oct 22, 2024
1 parent e6f1e62 commit 76b3562
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
1 change: 0 additions & 1 deletion include/universal/number/posit/specialized/posit_8_0.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ class posit<NBITS_IS_8, ES_IS_0> {
void clear() noexcept { _bits = 0; }
void setzero() noexcept { clear(); }
void setnar() noexcept { _bits = 0x80; }
void setnan(bool sign = true) noexcept { setnar(); }
//void setnan(bool sign = false) noexcept { setnar(); }
posit& setBitblock(const sw::universal::bitblock<NBITS_IS_8>& raw) noexcept {
_bits = uint8_t(raw.to_ulong());
Expand Down
24 changes: 12 additions & 12 deletions include/universal/number/posit/specialized/posit_8_2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,25 +461,25 @@ class posit<NBITS_IS_8, ES_IS_2> {
}

// Selectors
inline bool sign() const noexcept { return (_bits & sign_mask); }
inline bool isnar() const noexcept { return (_bits == sign_mask); }
inline bool iszero() const noexcept { return (_bits == 0x00u); }
inline bool isone() const noexcept { return (_bits == 0x40u); } // pattern 010000...
inline bool isminusone() const noexcept { return (_bits == 0xC0u); } // pattern 110000...
inline bool isneg() const noexcept { return (_bits & sign_mask); }
inline bool ispos() const noexcept { return !isneg(); }
inline bool ispowerof2() const noexcept { return !(_bits & 0x1u); }
bool sign() const noexcept { return (_bits & sign_mask); }
bool isnar() const noexcept { return (_bits == sign_mask); }
bool iszero() const noexcept { return (_bits == 0x00u); }
bool isone() const noexcept { return (_bits == 0x40u); } // pattern 010000...
bool isminusone() const noexcept { return (_bits == 0xC0u); } // pattern 110000...
bool isneg() const noexcept { return (_bits & sign_mask); }
bool ispos() const noexcept { return !isneg(); }
bool ispowerof2() const noexcept { return !(_bits & 0x1u); }

inline int sign_value() const noexcept { return (_bits & sign_mask ? -1 : 1); }
int sign_value() const noexcept { return (_bits & sign_mask ? -1 : 1); }

bitblock<NBITS_IS_8> get() const noexcept { bitblock<NBITS_IS_8> bb; bb = int(_bits); return bb; }
uint8_t bits() const noexcept { return _bits; }
unsigned long long encoding() const noexcept { return (unsigned long long)(_bits); }

// Modifiers
inline void clear() noexcept { _bits = 0; }
inline void setzero() noexcept { clear(); }
inline void setnar() noexcept { _bits = sign_mask; }
constexpr void clear() noexcept { _bits = 0; }
constexpr void setzero() noexcept { clear(); }
constexpr void setnar() noexcept { _bits = sign_mask; }
posit& setBitblock(const bitblock<NBITS_IS_8>& raw) noexcept {
_bits = uint8_t(raw.to_ulong());
return *this;
Expand Down
24 changes: 20 additions & 4 deletions static/posit/api/manipulators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,17 @@ template<unsigned nbits, unsigned es>
void GenerateTable() {
using namespace sw::universal;
posit<nbits, es> p;
unsigned COLUMNWIDTH = 15u;
std::cout << std::setw(COLUMNWIDTH) << "raw" << " : " <<
std::setw(COLUMNWIDTH) << "to_binary" << " : " <<
std::setw(COLUMNWIDTH) << "color_print" << " : " <<
std::setw(COLUMNWIDTH) << "value" << '\n';
for (unsigned i = 0; i < (1ul << nbits); ++i) {
p.setbits(i);
std::cout << to_binary(p, false) << " : " << color_print(p) << " : " << p << '\n';
std::cout << std::setw(COLUMNWIDTH) << p.get() << " : "
<< std::setw(COLUMNWIDTH) << to_binary(p, false) << " : "
<< " " << color_print(p) << " : "
<< std::setw(COLUMNWIDTH) << p << '\n';
}
}

Expand All @@ -46,12 +54,20 @@ try {

{
// report the type
posit<8, 2> p8;
std::cout << type_tag(p8) << '\n';
posit<8, 2> a(SpecificValue::maxpos), b(SpecificValue::minneg), c;
std::cout << type_tag(a) << '\n';
c = a * b;
std::cout << a << " * " << b << " = " << c << '\n';
std::cout << color_print(a) << " * " << color_print(b) << " = " << color_print(c) << '\n';
}

{
GenerateTable<5, 2>();
std::cout << "\nTable of encodings\n";
constexpr unsigned nbits = 5;
constexpr unsigned es = 2;
posit<nbits, es> p5;
std::cout << type_tag(p5) << '\n';
GenerateTable<nbits, es>();
}


Expand Down

0 comments on commit 76b3562

Please sign in to comment.