Skip to content

Commit

Permalink
code hygiene for old posit code
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravenwater committed Dec 26, 2023
1 parent c05d12d commit e58bc13
Show file tree
Hide file tree
Showing 21 changed files with 64 additions and 67 deletions.
8 changes: 4 additions & 4 deletions include/universal/internal/value/value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,21 +503,21 @@ class value {
template<unsigned tgt_size>
value<tgt_size> round_to() {
bitblock<tgt_size> rounded_fraction;
if (tgt_size == 0) {
if constexpr (tgt_size == 0) {
bool round_up = false;
if (fbits >= 2) {
if constexpr (fbits >= 2) {
bool blast = _fraction[fbits - 1ull];
bool sb = anyAfter(_fraction, static_cast<int>(fbits) - 2);
if (blast && sb) round_up = true;
}
else if (fbits == 1) {
else if constexpr (fbits == 1) {
round_up = _fraction[0];
}
return value<tgt_size>(_sign, (round_up ? _scale + 1 : _scale), rounded_fraction, _zero, _inf);
}
else {
if (!_zero || !_inf) {
if (tgt_size < fbits) {
if constexpr (tgt_size < fbits) {
int rb = int(tgt_size) - 1;
int lb = int(fbits) - int(tgt_size) - 1;
for (int i = int(fbits) - 1; i > lb; i--, rb--) {
Expand Down
21 changes: 0 additions & 21 deletions include/universal/number/posit/specialized/posit_16_2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,22 +304,6 @@ class posit<NBITS_IS_16, ES_IS_2> {
lhs = lhs & sign_mask ? -lhs : lhs;
rhs = rhs & sign_mask ? -rhs : rhs;

/* {
uint16_t lhs_exp{ 0 }, lhs_fraction, rhs_exp{ 0 }, rhs_fraction;
uint16_t lhs_m = decode_posit(lhs, lhs_exp, lhs_fraction);
uint16_t rhs_m = decode_posit(rhs, rhs_exp, rhs_fraction);
uint16_t m = lhs_m + rhs_m;
uint16_t exp = lhs_exp + rhs_exp;
uint32_t result_fraction = lhs_fraction * rhs_fraction;
std::cout << "lhs : " << to_binary(lhs_fraction, 32, true) << '\n';
std::cout << "rhs : " << to_binary(rhs_fraction, 32, true) << '\n';
std::cout << "result : " << to_binary(result_fraction, 32, true) << '\n';
std::cout << "exp : " << int(exp) << '\n';
}
*/

// decode the regime of lhs
int8_t m = 0; // regime pattern length
uint16_t remaining; // Remaining bits after the regime: 0<remaining_bits>0..0
Expand All @@ -337,11 +321,6 @@ class posit<NBITS_IS_16, ES_IS_2> {
uint16_t rhs_fraction = (0x4000 | remaining << 1) & 0x7FFF; // 0x4000 is the hidden bit
uint32_t result_fraction = (uint32_t)lhs_fraction * rhs_fraction;

//std::cout << "lhs : " << to_binary(lhs_fraction, 32, true) << '\n';
//std::cout << "rhs : " << to_binary(rhs_fraction, 32, true) << '\n';
//std::cout << "result : " << to_binary(result_fraction, 32, true) << '\n';
//std::cout << "exp : " << int(exp) << '\n';

if (exp > 3) {
++m;
exp &= 0x3;
Expand Down
4 changes: 2 additions & 2 deletions include/universal/number/posit2/fraction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class fraction {
template<unsigned nfbits, typename bbt>
inline std::ostream& operator<<(std::ostream& ostr, const fraction<nfbits, bbt>& f) {
unsigned nrOfFractionBitsProcessed = 0;
if (nfbits > 0) {
if constexpr (nfbits > 0) {
int upperbound = int(nfbits) - 1;
for (int i = upperbound; i >= 0; --i) {
if (f._nrBits > ++nrOfFractionBitsProcessed) {
Expand All @@ -249,7 +249,7 @@ template<unsigned nfbits, typename bbt>
inline std::string to_string(const fraction<nfbits, bbt>& f, bool dashExtent = true, bool nibbleMarker = false) {
unsigned int nrOfFractionBitsProcessed = 0;
std::stringstream s;
if (nfbits > 0) {
if constexpr (nfbits > 0) {
blockbinary<nfbits, bbt, BinaryNumberType::Unsigned> bb = f.bits();
for (unsigned i = 0; i < nfbits; ++i) {
unsigned bitIndex = nfbits - 1ull - i;
Expand Down
6 changes: 3 additions & 3 deletions include/universal/number/posit2/posit_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,9 @@ inline posit<nbits, es, bt>& convert_(bool _sign, int _scale, const blocksignifi

// construct the untruncated posit
// pt = BitOr[BitShiftLeft[reg, es + nf + 1], BitShiftLeft[esval, nf + 1], BitShiftLeft[fv, 1], sb];
regime <<= es + nrFbits + 1;
exponent <<= nrFbits + 1;
fraction <<= 1;
regime <<= es + nrFbits + 1u;
exponent <<= nrFbits + 1u;
fraction <<= 1u;
sticky_bit.setbit(0, sb);

pt_bits |= regime;
Expand Down
2 changes: 1 addition & 1 deletion include/universal/verification/posit_test_suite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace sw { namespace universal {
unsigned NR_TEST_CASES = (unsigned(1) << (max + 1));
unsigned HALF = (unsigned(1) << max);

if (nbits > 20) {
if constexpr (nbits > 20) {
std::cout << "VerifyConversion<" << nbits << "," << es << ">: NR_TEST_CASES = " << NR_TEST_CASES << " constrained due to nbits > 20" << std::endl;
}

Expand Down
4 changes: 2 additions & 2 deletions include/universal/verification/quire_test_suite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,10 @@ int ValidateQuireAccumulation(bool bReportIndividualTestCases, const std::vector

if (!presult.iszero()) {
nrOfFailedTests++;
if (bReportIndividualTestCases) ReportQuireNonZeroError("FAIL", "fdp", t.size(), t[0], presult);
if (bReportIndividualTestCases) ReportQuireNonZeroError("FAIL", "fdp", unsigned(t.size()), t[0], presult);
}
else {
if (bReportIndividualTestCases) ReportQuireNonZeroSuccess("PASS", "fdp", t.size(), t[0], presult);
if (bReportIndividualTestCases) ReportQuireNonZeroSuccess("PASS", "fdp", unsigned(t.size()), t[0], presult);
//std::cout << to_hex(q0.get()) << " " << to_hex(pa.get()) << " " << to_hex(pb.get()) << " " << to_hex(q.get()) << " " << to_hex(presult.get()) << std::endl;
}

Expand Down
11 changes: 6 additions & 5 deletions static/posit/api/number_traits.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// numeric_traits.cpp: test suite runner of the numeric_limits specialization for posits
//
// Copyright (C) 2017-2022 Stillwater Supercomputing, Inc.
// Copyright (C) 2017 Stillwater Supercomputing, Inc.
//
// This file is part of the universal numbers project, which is released under an MIT Open Source license.
#include <universal/utility/directives.hpp>
#include <universal/native/ieee754.hpp>
#define POSIT_THROW_ARITHMETIC_EXCEPTION 1
#include <universal/number/posit/posit.hpp>
#include <universal/verification/posit_math_test_suite.hpp>

Expand All @@ -20,16 +21,16 @@ try {
numberTraits<short>(std::cout);
numberTraits<unsigned>(std::cout);
numberTraits<float>(std::cout);
numberTraits<posit<32, 2>>(std::cout);
numberTraits<posit<32, 2> >(std::cout);

std::cout << minmax_range<float>() << '\n';
std::cout << minmax_range<posit<32, 2>>() << '\n';
std::cout << minmax_range< posit<32, 2> >() << '\n';

std::cout << dynamic_range<float>() << '\n';
std::cout << dynamic_range<posit<32, 2>>() << '\n';
std::cout << dynamic_range< posit<32, 2> >() << '\n';

std::cout << symmetry_range<float>() << '\n';
std::cout << symmetry_range<posit<32, 2>>() << '\n';
std::cout << symmetry_range< posit<32, 2> >() << '\n';

using Float = float;
using Posit = sw::universal::posit<32, 2>;
Expand Down
1 change: 1 addition & 0 deletions static/posit/api/quire_accumulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define HARDWARE_QA_OUTPUT 0

// type definitions for the important types, posit<> and quire<>
#define POSIT_THROW_ARITHMETIC_EXCEPTION 1
#include <universal/number/posit/posit.hpp>
//#include <universal/traits/posit_traits.hpp>
//#include <universal/number/posit/quire.hpp>
Expand Down
8 changes: 4 additions & 4 deletions static/posit/arithmetic/complex_add.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,27 @@ int VerifyComplexAddition(bool reportTestCases) {
const unsigned NR_POSITS = (unsigned(1) << nbits);
int nrOfFailedTests = 0;
posit<nbits, es> ar, ai, br, bi;
std::complex<posit<nbits, es>> a, b, result, ref;
std::complex< posit<nbits, es> > a, b, result, ref;

std::complex<double> da, db, dc;
for (unsigned i = 0; i < NR_POSITS; ++i) {
ar.setbits(i);
for (unsigned j = 0; j < NR_POSITS; ++j) {
ai.setbits(j);
a = std::complex<posit<nbits, es>>(ar, ai);
a = std::complex< posit<nbits, es> >(ar, ai);
da = std::complex<double>(double(ar), double(ai));

// generate all the right sides
for (unsigned k = 0; k < NR_POSITS; ++k) {
br.setbits(k);
for (unsigned l = 0; l < NR_POSITS; ++l) {
bi.setbits(l);
b = std::complex<posit<nbits, es>>(br, bi);
b = std::complex< posit<nbits, es> >(br, bi);
db = std::complex<double>(double(br), double(bi));

result = a + b;
dc = da + db;
ref = std::complex<posit<nbits, es>>(dc.real(), dc.imag());
ref = std::complex< posit<nbits, es> >(dc.real(), dc.imag());

if (result.real() != ref.real() || result.imag() != ref.imag()) {
nrOfFailedTests++;
Expand Down
2 changes: 1 addition & 1 deletion static/posit/arithmetic/division.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void GenerateWorstCaseDivision() {
p_plus_eps++;
p_minus_eps--;
p_result = p_plus_eps / p_minus_eps;
if (es < 2) {
if constexpr (es < 2) {
std::cout << posit_descriptor.str() << " minpos = " << std::fixed << std::setprecision(nbits) << sw::universal::posit<nbits, es>(sw::universal::SpecificValue::minpos) << std::dec << std::endl;
}
else {
Expand Down
4 changes: 2 additions & 2 deletions static/posit/specialized/posit_128_2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//#define POSIT_FAST_SPECIALIZATION // turns on all fast specializations
#define POSIT_FAST_POSIT_128_2 1 // TODO: fast posit<128,2> not implemented yet
// second: enable posit arithmetic exceptions
#define POSIT_THROW_ARITHMETIC_EXCEPTION 0
#define POSIT_THROW_ARITHMETIC_EXCEPTION 1
#include <universal/number/posit/posit.hpp>
#include <universal/verification/posit_test_suite.hpp>
#include <universal/verification/posit_test_randoms.hpp>
Expand Down Expand Up @@ -53,7 +53,7 @@ try {

ReportTestSuiteHeader(test_suite, reportTestCases);

size_t RND_TEST_CASES = 1024;
unsigned RND_TEST_CASES = 1024;

using Scalar = posit<nbits, es>;
Scalar p;
Expand Down
6 changes: 3 additions & 3 deletions static/posit/specialized/posit_128_4.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// posit_128_4.cpp: test suite runner for specialized 128-bit posit<128,4>
//
// Copyright (C) 2017-2023 Stillwater Supercomputing, Inc.
// Copyright (C) 2017 Stillwater Supercomputing, Inc.
//
// This file is part of the universal numbers project, which is released under an MIT Open Source license.
#include <universal/utility/directives.hpp>
Expand All @@ -10,7 +10,7 @@
//#define POSIT_FAST_SPECIALIZATION // turns on all fast specializations
#define POSIT_FAST_POSIT_128_4 1// TODO: fast posit<128,4> not implemented yet
// second: enable posit arithmetic exceptions
#define POSIT_THROW_ARITHMETIC_EXCEPTION 0
#define POSIT_THROW_ARITHMETIC_EXCEPTION 1
#include <universal/number/posit/posit.hpp>
#include <universal/verification/posit_test_suite.hpp>
#include <universal/verification/posit_test_randoms.hpp>
Expand Down Expand Up @@ -53,7 +53,7 @@ try {

ReportTestSuiteHeader(test_suite, reportTestCases);

size_t RND_TEST_CASES = 1024;
unsigned RND_TEST_CASES = 1024;

using Scalar = posit<nbits, es>;
Scalar p;
Expand Down
6 changes: 3 additions & 3 deletions static/posit/specialized/posit_256_2.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// posit_256_2.cpp: test suite runner for fast specialized 256-bit posit<256,2>
//
// Copyright (C) 2017-2023 Stillwater Supercomputing, Inc.
// Copyright (C) 2017 Stillwater Supercomputing, Inc.
//
// This file is part of the universal numbers project, which is released under an MIT Open Source license.
#include <universal/utility/directives.hpp>
Expand All @@ -10,7 +10,7 @@
//#define POSIT_FAST_SPECIALIZATION // turns on all fast specializations
#define POSIT_FAST_POSIT_256_2 1 // TODO: fast posit<256,2> not implemented yet
// second: enable posit arithmetic exceptions
#define POSIT_THROW_ARITHMETIC_EXCEPTION 0
#define POSIT_THROW_ARITHMETIC_EXCEPTION 1
#include <universal/number/posit/posit.hpp>
#include <universal/verification/posit_test_suite.hpp>
#include <universal/verification/posit_test_randoms.hpp>
Expand Down Expand Up @@ -53,7 +53,7 @@ try {

ReportTestSuiteHeader(test_suite, reportTestCases);

size_t RND_TEST_CASES = 1024;
unsigned RND_TEST_CASES = 1024;

posit<nbits, es> p;
std::cout << dynamic_range(p) << "\n\n";
Expand Down
6 changes: 3 additions & 3 deletions static/posit/specialized/posit_256_5.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// posit_256_5.cpp: test suite runner for fast specialized 256-bit posit<256,5>
//
// Copyright (C) 2017-2023 Stillwater Supercomputing, Inc.
// Copyright (C) 2017 Stillwater Supercomputing, Inc.
//
// This file is part of the universal numbers project, which is released under an MIT Open Source license.
#include <universal/utility/directives.hpp>
Expand All @@ -10,7 +10,7 @@
//#define POSIT_FAST_SPECIALIZATION // turns on all fast specializations
#define POSIT_FAST_POSIT_256_5 1 // TODO: fast posit<256,5> not implemented yet
// second: enable posit arithmetic exceptions
#define POSIT_THROW_ARITHMETIC_EXCEPTION 0
#define POSIT_THROW_ARITHMETIC_EXCEPTION 1
#include <universal/number/posit/posit.hpp>
#include <universal/verification/posit_test_suite.hpp>
#include <universal/verification/posit_test_randoms.hpp>
Expand Down Expand Up @@ -53,7 +53,7 @@ try {

ReportTestSuiteHeader(test_suite, reportTestCases);

size_t RND_TEST_CASES = 1024;
unsigned RND_TEST_CASES = 1024;

posit<nbits, es> p;
std::cout << dynamic_range(p) << "\n\n";
Expand Down
2 changes: 1 addition & 1 deletion static/posit/specialized/posit_32_2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ try {

ReportTestSuiteHeader(test_suite, reportTestCases);

size_t RND_TEST_CASES = 5000;
unsigned RND_TEST_CASES = 5000;

using Scalar = posit<nbits, es>;
Scalar p;
Expand Down
4 changes: 2 additions & 2 deletions static/posit/specialized/posit_48_2.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// posit_48_2.cpp: test suite runner for specialized extended standard 48-bit posit<48,2>
//
// Copyright (C) 2017-2023 Stillwater Supercomputing, Inc.
// Copyright (C) 2017 Stillwater Supercomputing, Inc.
//
// This file is part of the universal numbers project, which is released under an MIT Open Source license.
#include <universal/utility/directives.hpp>
Expand Down Expand Up @@ -52,7 +52,7 @@ try {

ReportTestSuiteHeader(test_suite, reportTestCases);

size_t RND_TEST_CASES = 1024;
unsigned RND_TEST_CASES = 1024;

using Scalar = posit<nbits, es>;
Scalar p;
Expand Down
2 changes: 1 addition & 1 deletion static/posit/specialized/posit_64_2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ try {

ReportTestSuiteHeader(test_suite, reportTestCases);

size_t RND_TEST_CASES = 1024;
unsigned RND_TEST_CASES = 1024;

using Scalar = posit<nbits, es>;
Scalar p;
Expand Down
4 changes: 2 additions & 2 deletions static/posit/specialized/posit_64_3.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// posit_64_3.cpp: test suite runner for fast specialized posit<64,3>
//
// Copyright (C) 2017-2023 Stillwater Supercomputing, Inc.
// Copyright (C) 2017 Stillwater Supercomputing, Inc.
//
// This file is part of the universal numbers project, which is released under an MIT Open Source license.
#include <universal/utility/directives.hpp>
Expand Down Expand Up @@ -52,7 +52,7 @@ try {

ReportTestSuiteHeader(test_suite, reportTestCases);

size_t RND_TEST_CASES = 5000;
unsigned RND_TEST_CASES = 5000;

using Scalar = posit<nbits, es>;
Scalar p;
Expand Down
1 change: 0 additions & 1 deletion static/posit/specialized/posit_8_2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ try {
std::cout << std::setw(4) << 0x7C << " : " << color_print(p) << " : " << p << '\n';
p.setbits(0x7D);
std::cout << std::setw(4) << 0x7D << " : " << color_print(p) << " : " << p << '\n';
float f = float(p);

// goto epilog;

Expand Down
27 changes: 23 additions & 4 deletions static/posito/arithmetic/division.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ namespace sw {
nrOfFailedTests++;
}
else {
//if (reportTestCases) ReportBinaryArithmeticSuccess("PASS", "/", pa, pb, pdiv, pref);
if (reportTestCases) ReportBinaryArithmeticSuccess("PASS", "/", pa, pb, pdiv, pref);
}

if (nrOfFailedTests > 0) return 1;
}
}
return nrOfFailedTests;
Expand Down Expand Up @@ -210,8 +210,27 @@ try {

// ToughDivisions2<posit<16,1>>();

nrOfFailedTestCases += ReportTestResult(sw::testing::VerifyDivision<posit<16, 1>>(true), "posit<16,1>", "division");
nrOfFailedTestCases += ReportTestResult(sw::testing::VerifyDivision<posit<16, 2>>(true), "posit<16,2>", "division");
/*
PASS 1.3877787807814456755e-17 / 1.3877787807814456755e-17 == 1
PASS 1.3877787807814456755e-17 / 2.2204460492503130808e-16 == 0.0625
FAIL 1.3877787807814456755e-17 / 8.8817841970012523234e-16 != 0.0625 golden reference is 0.015625
0b0.000000000000001.. / 0b0.00000000000001.1. != 0b0.01.00.00000000000 golden reference is 0b0.001.10.0000000000
*/
{
posit<16, 2> a, b, c;
a = 1.3877787807814456755e-17;
b = 8.8817841970012523234e-16;
c = a / b;
ReportBinaryOperation(a, "/", b, c);
double da = double(a);
double db = double(b);
double dc = da / db;
ReportBinaryOperation(da, "/", db, dc);
posit<16, 2> ref(dc);
ReportValue(ref, "reference");
}
// nrOfFailedTestCases += ReportTestResult(sw::testing::VerifyDivision<posit<16, 1>>(true), "posit<16,1>", "division");
// nrOfFailedTestCases += ReportTestResult(sw::testing::VerifyDivision<posit<16, 2>>(true), "posit<16,2>", "division");

return 0;
/*
Expand Down
Loading

0 comments on commit e58bc13

Please sign in to comment.