Skip to content

Commit

Permalink
more constexpr guards
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravenwater committed Dec 29, 2021
1 parent 31de14b commit e237bf5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
22 changes: 19 additions & 3 deletions applications/numeric/constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ void SqrtWorkload(size_t NR_OPS) {
Scalar maxpos(sw::universal::SpecificValue::maxpos);
size_t maxval = 1024*1024*1024;
for (size_t i = 0; i < NR_OPS; ++i) {
a = (i % maxval);
a = Scalar(i % maxval);
a = (a < 0 ? -a : a);
// std::cout << "a = " << a << '\n';
a += Scalar(1.0f);
if (a < 0) continue;
c = sw::universal::sqrt(a);
}
if (a == c) std::cout << "amazing\n";
Expand Down Expand Up @@ -74,7 +77,11 @@ try {
#endif
using Fixed = fixpnt<80,75>;
using Posit = posit<64,2>;
using Float = cfloat<128, 15, uint32_t>;
using HP = cfloat< 16, 5, uint32_t, true>;
using SP = cfloat< 32, 8, uint32_t, true>;
using DP = cfloat< 64, 11, uint32_t, true>;
using EP = cfloat< 80, 11, uint32_t, true>;
using QP = cfloat<128, 15, uint32_t, true>;
//using Areal = areal<128, 15,uint32_t>;
//using Lns = lns<128, uint32_t>;

Expand All @@ -84,7 +91,11 @@ try {

constexpr size_t NR_OPS = 1024;
PerformanceRunner(type_tag(Fixed()) + "::sqrt ", SqrtWorkload< Fixed >, NR_OPS);
PerformanceRunner(type_tag(Float()) + "::sqrt ", SqrtWorkload< Float >, NR_OPS);
PerformanceRunner(type_tag(HP()) + "::sqrt ", SqrtWorkload< HP >, NR_OPS);
PerformanceRunner(type_tag(SP()) + "::sqrt ", SqrtWorkload< SP >, NR_OPS);
PerformanceRunner(type_tag(DP()) + "::sqrt ", SqrtWorkload< DP >, NR_OPS);
// PerformanceRunner(type_tag(EP()) + "::sqrt ", SqrtWorkload< EP >, NR_OPS);
// PerformanceRunner(type_tag(QP()) + "::sqrt ", SqrtWorkload< QP >, NR_OPS);

Compare<Fixed>(2.0);

Expand All @@ -95,6 +106,11 @@ try {
std::cout << sqrt(Native(f)) << " : " << type_tag(Native()) << '\n';
std::cout << sqrt(Fixed(f)) << " : " << type_tag(Fixed()) << '\n';
std::cout << sqrt(Posit(f)) << " : " << type_tag(Posit()) << '\n';
std::cout << sqrt(HP(f)) << " : " << type_tag(HP()) << '\n';
std::cout << sqrt(SP(f)) << " : " << type_tag(SP()) << '\n';
std::cout << sqrt(DP(f)) << " : " << type_tag(DP()) << '\n';
std::cout << sqrt(EP(f)) << " : " << type_tag(EP()) << '\n';
std::cout << sqrt(QP(f)) << " : " << type_tag(QP()) << '\n';
}

{
Expand Down
10 changes: 6 additions & 4 deletions include/universal/number/cfloat/cfloat_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ inline /*constexpr*/ void convert(const blocktriple<srcbits, op, bt>& src,
//
// tgt.clear(); // no need as all bits are going to be set by the code below

if constexpr (btType::bfbits < 65) {
if constexpr (btType::bfbits < 65) {
// we can use a uint64_t to construct the cfloat
int adjustment{ 0 };
// construct exponent
Expand Down Expand Up @@ -268,7 +268,8 @@ inline /*constexpr*/ void convert(const blocktriple<srcbits, op, bt>& src,
tgt.setsign(src.sign());
tgt.setexponent(src.scale());
// this api doesn't work: tgt.setfraction(src.significant());
std::cerr << "convert nbits > 64 TBD\n";
std::cerr << "bfbits = " << btType::bfbits << " nbits = " << nbits << '\n';
std::cerr << "convert to a cfloat with nbits > 64 is TBD\n";
}
}
}
Expand Down Expand Up @@ -1855,7 +1856,7 @@ class cfloat {
// where 'f' is a fraction bit, and 'e' is an extension bit
// so that normalize can be used to generate blocktriples for add/sub/mul/div/sqrt
if (isnormal()) {
if constexpr (BlockTripleConfiguration::rbits < (64 - fbits)) {
if constexpr (fbits < 64 && BlockTripleConfiguration::rbits < (64 - fbits)) {
uint64_t raw = fraction_ull();
raw |= (1ull << fbits); // add the hidden bit
raw <<= BlockTripleConfiguration::rbits; // rounding bits required for correct rounding
Expand Down Expand Up @@ -3011,7 +3012,7 @@ class cfloat {
if (guard) {
if (lsb && (!round && !sticky)) ++raw; // round to even
if (round || sticky) ++raw;
if (raw == (1ull << nbits)) { // overflow
if (raw == (1ull << fbits)) { // overflow
++exponent;
raw >>= 1u;
}
Expand All @@ -3030,6 +3031,7 @@ class cfloat {
uint64_t significant = raw;
return significant;
}

template<typename ArgumentBlockType>
constexpr void copyBits(ArgumentBlockType v) {
size_t blocksRequired = (8 * sizeof(v) + 1 ) / bitsInBlock;
Expand Down

0 comments on commit e237bf5

Please sign in to comment.