From e58bc13cd4fe083af896e5abd763e04fb9d61d77 Mon Sep 17 00:00:00 2001 From: Ravenwater Date: Tue, 26 Dec 2023 17:00:23 -0500 Subject: [PATCH] code hygiene for old posit code --- include/universal/internal/value/value.hpp | 8 +++--- .../number/posit/specialized/posit_16_2.hpp | 21 --------------- include/universal/number/posit2/fraction.hpp | 4 +-- .../universal/number/posit2/posit_impl.hpp | 6 ++--- .../verification/posit_test_suite.hpp | 2 +- .../verification/quire_test_suite.hpp | 4 +-- static/posit/api/number_traits.cpp | 11 ++++---- static/posit/api/quire_accumulation.cpp | 1 + static/posit/arithmetic/complex_add.cpp | 8 +++--- static/posit/arithmetic/division.cpp | 2 +- static/posit/specialized/posit_128_2.cpp | 4 +-- static/posit/specialized/posit_128_4.cpp | 6 ++--- static/posit/specialized/posit_256_2.cpp | 6 ++--- static/posit/specialized/posit_256_5.cpp | 6 ++--- static/posit/specialized/posit_32_2.cpp | 2 +- static/posit/specialized/posit_48_2.cpp | 4 +-- static/posit/specialized/posit_64_2.cpp | 2 +- static/posit/specialized/posit_64_3.cpp | 4 +-- static/posit/specialized/posit_8_2.cpp | 1 - static/posito/arithmetic/division.cpp | 27 ++++++++++++++++--- static/posito/arithmetic/subtraction.cpp | 2 -- 21 files changed, 64 insertions(+), 67 deletions(-) diff --git a/include/universal/internal/value/value.hpp b/include/universal/internal/value/value.hpp index b787505db..8d4ae125d 100644 --- a/include/universal/internal/value/value.hpp +++ b/include/universal/internal/value/value.hpp @@ -503,21 +503,21 @@ class value { template value round_to() { bitblock 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(fbits) - 2); if (blast && sb) round_up = true; } - else if (fbits == 1) { + else if constexpr (fbits == 1) { round_up = _fraction[0]; } return value(_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--) { diff --git a/include/universal/number/posit/specialized/posit_16_2.hpp b/include/universal/number/posit/specialized/posit_16_2.hpp index c69ff6e53..0904a88b4 100644 --- a/include/universal/number/posit/specialized/posit_16_2.hpp +++ b/include/universal/number/posit/specialized/posit_16_2.hpp @@ -304,22 +304,6 @@ class posit { 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: 00..0 @@ -337,11 +321,6 @@ class posit { 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; diff --git a/include/universal/number/posit2/fraction.hpp b/include/universal/number/posit2/fraction.hpp index 68f7f74fa..e75791b5d 100644 --- a/include/universal/number/posit2/fraction.hpp +++ b/include/universal/number/posit2/fraction.hpp @@ -224,7 +224,7 @@ class fraction { template inline std::ostream& operator<<(std::ostream& ostr, const fraction& 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) { @@ -249,7 +249,7 @@ template inline std::string to_string(const fraction& f, bool dashExtent = true, bool nibbleMarker = false) { unsigned int nrOfFractionBitsProcessed = 0; std::stringstream s; - if (nfbits > 0) { + if constexpr (nfbits > 0) { blockbinary bb = f.bits(); for (unsigned i = 0; i < nfbits; ++i) { unsigned bitIndex = nfbits - 1ull - i; diff --git a/include/universal/number/posit2/posit_impl.hpp b/include/universal/number/posit2/posit_impl.hpp index 9c9fd4403..7d4c1a483 100644 --- a/include/universal/number/posit2/posit_impl.hpp +++ b/include/universal/number/posit2/posit_impl.hpp @@ -346,9 +346,9 @@ inline posit& 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; diff --git a/include/universal/verification/posit_test_suite.hpp b/include/universal/verification/posit_test_suite.hpp index 801c2c309..b99cd7066 100644 --- a/include/universal/verification/posit_test_suite.hpp +++ b/include/universal/verification/posit_test_suite.hpp @@ -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; } diff --git a/include/universal/verification/quire_test_suite.hpp b/include/universal/verification/quire_test_suite.hpp index 2e7740a93..b9688d77a 100644 --- a/include/universal/verification/quire_test_suite.hpp +++ b/include/universal/verification/quire_test_suite.hpp @@ -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; } diff --git a/static/posit/api/number_traits.cpp b/static/posit/api/number_traits.cpp index b06b224e6..9c9374bd6 100644 --- a/static/posit/api/number_traits.cpp +++ b/static/posit/api/number_traits.cpp @@ -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 #include +#define POSIT_THROW_ARITHMETIC_EXCEPTION 1 #include #include @@ -20,16 +21,16 @@ try { numberTraits(std::cout); numberTraits(std::cout); numberTraits(std::cout); - numberTraits>(std::cout); + numberTraits >(std::cout); std::cout << minmax_range() << '\n'; - std::cout << minmax_range>() << '\n'; + std::cout << minmax_range< posit<32, 2> >() << '\n'; std::cout << dynamic_range() << '\n'; - std::cout << dynamic_range>() << '\n'; + std::cout << dynamic_range< posit<32, 2> >() << '\n'; std::cout << symmetry_range() << '\n'; - std::cout << symmetry_range>() << '\n'; + std::cout << symmetry_range< posit<32, 2> >() << '\n'; using Float = float; using Posit = sw::universal::posit<32, 2>; diff --git a/static/posit/api/quire_accumulation.cpp b/static/posit/api/quire_accumulation.cpp index 2593f2822..34f2b86fa 100644 --- a/static/posit/api/quire_accumulation.cpp +++ b/static/posit/api/quire_accumulation.cpp @@ -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 //#include //#include diff --git a/static/posit/arithmetic/complex_add.cpp b/static/posit/arithmetic/complex_add.cpp index a1db7e8d8..b92530d5b 100644 --- a/static/posit/arithmetic/complex_add.cpp +++ b/static/posit/arithmetic/complex_add.cpp @@ -46,14 +46,14 @@ int VerifyComplexAddition(bool reportTestCases) { const unsigned NR_POSITS = (unsigned(1) << nbits); int nrOfFailedTests = 0; posit ar, ai, br, bi; - std::complex> a, b, result, ref; + std::complex< posit > a, b, result, ref; std::complex 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>(ar, ai); + a = std::complex< posit >(ar, ai); da = std::complex(double(ar), double(ai)); // generate all the right sides @@ -61,12 +61,12 @@ int VerifyComplexAddition(bool reportTestCases) { br.setbits(k); for (unsigned l = 0; l < NR_POSITS; ++l) { bi.setbits(l); - b = std::complex>(br, bi); + b = std::complex< posit >(br, bi); db = std::complex(double(br), double(bi)); result = a + b; dc = da + db; - ref = std::complex>(dc.real(), dc.imag()); + ref = std::complex< posit >(dc.real(), dc.imag()); if (result.real() != ref.real() || result.imag() != ref.imag()) { nrOfFailedTests++; diff --git a/static/posit/arithmetic/division.cpp b/static/posit/arithmetic/division.cpp index bc17e9f66..c1d04abc0 100644 --- a/static/posit/arithmetic/division.cpp +++ b/static/posit/arithmetic/division.cpp @@ -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(sw::universal::SpecificValue::minpos) << std::dec << std::endl; } else { diff --git a/static/posit/specialized/posit_128_2.cpp b/static/posit/specialized/posit_128_2.cpp index b693ae81f..51fb9a2e2 100644 --- a/static/posit/specialized/posit_128_2.cpp +++ b/static/posit/specialized/posit_128_2.cpp @@ -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 #include #include @@ -53,7 +53,7 @@ try { ReportTestSuiteHeader(test_suite, reportTestCases); - size_t RND_TEST_CASES = 1024; + unsigned RND_TEST_CASES = 1024; using Scalar = posit; Scalar p; diff --git a/static/posit/specialized/posit_128_4.cpp b/static/posit/specialized/posit_128_4.cpp index 678ca6bbd..8a9e51d7d 100644 --- a/static/posit/specialized/posit_128_4.cpp +++ b/static/posit/specialized/posit_128_4.cpp @@ -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 @@ -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 #include #include @@ -53,7 +53,7 @@ try { ReportTestSuiteHeader(test_suite, reportTestCases); - size_t RND_TEST_CASES = 1024; + unsigned RND_TEST_CASES = 1024; using Scalar = posit; Scalar p; diff --git a/static/posit/specialized/posit_256_2.cpp b/static/posit/specialized/posit_256_2.cpp index 446050fc4..84a8dc18f 100644 --- a/static/posit/specialized/posit_256_2.cpp +++ b/static/posit/specialized/posit_256_2.cpp @@ -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 @@ -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 #include #include @@ -53,7 +53,7 @@ try { ReportTestSuiteHeader(test_suite, reportTestCases); - size_t RND_TEST_CASES = 1024; + unsigned RND_TEST_CASES = 1024; posit p; std::cout << dynamic_range(p) << "\n\n"; diff --git a/static/posit/specialized/posit_256_5.cpp b/static/posit/specialized/posit_256_5.cpp index 078308ce6..61f31823c 100644 --- a/static/posit/specialized/posit_256_5.cpp +++ b/static/posit/specialized/posit_256_5.cpp @@ -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 @@ -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 #include #include @@ -53,7 +53,7 @@ try { ReportTestSuiteHeader(test_suite, reportTestCases); - size_t RND_TEST_CASES = 1024; + unsigned RND_TEST_CASES = 1024; posit p; std::cout << dynamic_range(p) << "\n\n"; diff --git a/static/posit/specialized/posit_32_2.cpp b/static/posit/specialized/posit_32_2.cpp index 37284cca6..b71193979 100644 --- a/static/posit/specialized/posit_32_2.cpp +++ b/static/posit/specialized/posit_32_2.cpp @@ -87,7 +87,7 @@ try { ReportTestSuiteHeader(test_suite, reportTestCases); - size_t RND_TEST_CASES = 5000; + unsigned RND_TEST_CASES = 5000; using Scalar = posit; Scalar p; diff --git a/static/posit/specialized/posit_48_2.cpp b/static/posit/specialized/posit_48_2.cpp index 52b1fc9c6..85454353d 100644 --- a/static/posit/specialized/posit_48_2.cpp +++ b/static/posit/specialized/posit_48_2.cpp @@ -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 @@ -52,7 +52,7 @@ try { ReportTestSuiteHeader(test_suite, reportTestCases); - size_t RND_TEST_CASES = 1024; + unsigned RND_TEST_CASES = 1024; using Scalar = posit; Scalar p; diff --git a/static/posit/specialized/posit_64_2.cpp b/static/posit/specialized/posit_64_2.cpp index d40b9015e..9ad5f0bc2 100644 --- a/static/posit/specialized/posit_64_2.cpp +++ b/static/posit/specialized/posit_64_2.cpp @@ -52,7 +52,7 @@ try { ReportTestSuiteHeader(test_suite, reportTestCases); - size_t RND_TEST_CASES = 1024; + unsigned RND_TEST_CASES = 1024; using Scalar = posit; Scalar p; diff --git a/static/posit/specialized/posit_64_3.cpp b/static/posit/specialized/posit_64_3.cpp index 20e33c812..45ecce2aa 100644 --- a/static/posit/specialized/posit_64_3.cpp +++ b/static/posit/specialized/posit_64_3.cpp @@ -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 @@ -52,7 +52,7 @@ try { ReportTestSuiteHeader(test_suite, reportTestCases); - size_t RND_TEST_CASES = 5000; + unsigned RND_TEST_CASES = 5000; using Scalar = posit; Scalar p; diff --git a/static/posit/specialized/posit_8_2.cpp b/static/posit/specialized/posit_8_2.cpp index a33f014e9..3705a26ed 100644 --- a/static/posit/specialized/posit_8_2.cpp +++ b/static/posit/specialized/posit_8_2.cpp @@ -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; diff --git a/static/posito/arithmetic/division.cpp b/static/posito/arithmetic/division.cpp index 9b0ae37f6..bdf924cdc 100644 --- a/static/posito/arithmetic/division.cpp +++ b/static/posito/arithmetic/division.cpp @@ -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; @@ -210,8 +210,27 @@ try { // ToughDivisions2>(); - nrOfFailedTestCases += ReportTestResult(sw::testing::VerifyDivision>(true), "posit<16,1>", "division"); - nrOfFailedTestCases += ReportTestResult(sw::testing::VerifyDivision>(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>(true), "posit<16,1>", "division"); + // nrOfFailedTestCases += ReportTestResult(sw::testing::VerifyDivision>(true), "posit<16,2>", "division"); return 0; /* diff --git a/static/posito/arithmetic/subtraction.cpp b/static/posito/arithmetic/subtraction.cpp index 534a180ea..2737439bc 100644 --- a/static/posito/arithmetic/subtraction.cpp +++ b/static/posito/arithmetic/subtraction.cpp @@ -143,8 +143,6 @@ try { nrOfFailedTestCases += ReportTestResult(VerifyBinaryOperatorThroughRandoms<16, 1>(reportTestCases, OPCODE_SUB, nrOfRandoms), "posit<16,1>", "subtraction"); nrOfFailedTestCases += ReportTestResult(VerifyBinaryOperatorThroughRandoms<16, 2>(reportTestCases, OPCODE_SUB, nrOfRandoms), "posit<16,2>", "subtraction"); - - #endif #if REGRESSION_LEVEL_2