Skip to content

Commit

Permalink
WIP: efloat construction and assignment of native floating-point values
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravenwater committed Oct 30, 2024
1 parent bd9e3f0 commit 40bbc20
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
13 changes: 12 additions & 1 deletion elastic/efloat/api/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,22 @@ try {
bool isTrivial = bool(std::is_trivial<TestType>());
std::string testType = sw::universal::type_tag(TestType());
std::cout << (isTrivial ? testType + std::string(" is trivial") : testType + std::string(" is not trivial")) << '\n';

}

// construction, initialization, and copy construction
{
using TestType = efloat;

TestType a{ 1.0f }, b(2.0);
#if LONG_DOUBLE_SUPPORT
TestType c{ 4.0l };
#else
TestType c{ 4.0 };
#endif
std::cout << "a : " << to_triple(a) << '\n';
std::cout << "b : " << to_triple(b) << '\n';
c -= a + b - 1.0;
std::cout << "c : " << to_triple(c) << '\n';

}

Expand Down
26 changes: 21 additions & 5 deletions include/universal/number/efloat/efloat_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ bool parse(const std::string& number, efloat& v);
class efloat {

public:
efloat() : _sign(false), exp(0) { }
efloat() : _sign(false), exp(0), limb{ 0 } { }

efloat(const efloat&) = default;
efloat(efloat&&) = default;
Expand Down Expand Up @@ -133,10 +133,10 @@ class efloat {
bool isodd() const { return false; }
bool iseven() const { return !isodd(); }
bool ispos() const { return !_sign; }
bool ineg() const { return _sign; }
bool isneg() const { return _sign; }

// value information selectors
int sign() const { return (_sign ? -1 : 1); }
int sign() const { return (_sign ? -1 : 1); }
int64_t scale() const { return exp; }
std::vector<uint32_t> bits() const { return limb; }

Expand Down Expand Up @@ -175,7 +175,23 @@ class efloat {
template<typename Real,
typename = typename std::enable_if< std::is_floating_point<Real>::value, Real >::type>
efloat& convert_ieee754(Real rhs) noexcept {

clear();
_sign = sw::universal::sign(rhs);
exp = sw::universal::scale(rhs);
if constexpr (sizeof(Real) == 4) {
uint32_t bits = sw::universal::_extractSignificant<uint32_t, Real>(rhs);
bits <<= 8; // 32 - 23 = 9 bits to get the hidden bit to land on bit 31
limb.push_back(bits);
}
else if constexpr (sizeof(Real) == 8) {
uint64_t bits = sw::universal::_extractSignificant<uint64_t, Real>(rhs);
bits <<= 11; // 64 - 52 = 12 bits to get the hidden bit to land on bit 63
limb.push_back(static_cast<uint32_t>(bits >> 32));
limb.push_back(static_cast<uint32_t>(bits & 0xFFFF'FFFF));
}
else {
static_assert(true);
}
return *this;
}

Expand Down Expand Up @@ -245,7 +261,7 @@ inline std::istream& operator>>(std::istream& istr, efloat& p) {
std::string txt;
istr >> txt;
if (!parse(txt, p)) {
std::cerr << "unable to parse -" << txt << "- into a posit value\n";
std::cerr << "unable to parse -" << txt << "- into a floating-point value\n";
}
return istr;
}
Expand Down
23 changes: 23 additions & 0 deletions include/universal/number/efloat/manipulators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,29 @@ inline std::string components(const EfloatType& v) {
return s.str();
}

template<typename EfloatType,
std::enable_if_t< is_efloat<EfloatType>, bool> = true
>
inline std::string to_triple(const EfloatType& v) {
std::stringstream s;
s << (v.isneg() ? "(-, " : "(+, ");
s << v.scale() << ", ";
bool firstLimb{ true };
for (auto l : v.bits()) {
if (!firstLimb) s << '\'';
uint64_t mask = (1ull << 31);
for (int i = 31; i >= 0; --i) {
s << ((l & mask) ? '1' : '0');
if (firstLimb && i == 31) s << '.';
if (i > 0 && i % 4 == 0) s << '\'';
mask >>= 1;
}
firstLimb = false;
}
s << ')';
return s.str();
}

// generate a binary string for efloat
template<typename EfloatType,
std::enable_if_t< is_efloat<EfloatType>, bool> = true
Expand Down

0 comments on commit 40bbc20

Please sign in to comment.