Skip to content

Commit

Permalink
adding doubledouble constant generation example
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravenwater committed Aug 2, 2024
1 parent 3028a74 commit 4ce0ca5
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 46 deletions.
10 changes: 8 additions & 2 deletions include/universal/number/dd/dd_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

namespace sw { namespace universal {

// fwd references to free functions used in to_digits()
// fwd references to free functions used in to_digits()
dd operator*(const dd& lhs, const dd&);
dd pown(dd const&, int);

Expand Down Expand Up @@ -760,6 +760,12 @@ class dd {

//////////////////////// helper functions /////////////////////////////////

inline std::string to_pair(const dd& v, int precision = 17) {
std::stringstream s;
// 53 bits = 16 decimal digits, 17 to include last, 15 typical valid digits
s << std::setprecision(precision) << "( " << v.high() << ", " << v.low() << ')';
return s.str();
}

inline std::string to_binary(const dd& number, bool bNibbleMarker = false) {
std::stringstream s;
Expand Down Expand Up @@ -989,7 +995,7 @@ inline dd pown(dd const& a, int n) {

// generate an dd format ASCII format
inline std::ostream& operator<<(std::ostream& ostr, const dd& v) {
return ostr << "( " << v.high() << ", " << v.low() << ')';
return ostr << v.to_string(ostr.precision(), ostr.width(), ostr.flags(), ostr.showpos, ostr.uppercase, ostr.fill());
}

// read an ASCII dd format
Expand Down
33 changes: 0 additions & 33 deletions static/dd/api/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,6 @@ void Progression(double v) {
std::cout << to_binary(a, true) << " : " << a << '\n';
}

#if defined(DOUBLEDOUBLE_CONSTANTS)
dd _zero(0.0);
dd _one(1.0);
dd _ten(10.0);

dd _tenth("0.1");
dd _third("0.333333333333333333333333333333333333");

dd _2pi("6.283185307179586476925286766559005768");
dd _pi("3.141592653589793238462643383279502884");
dd _pi2("1.570796326794896619231321691639751442");
dd _pi4("0.785398163397448309615660845819875721");
dd _3pi4 = _pi2 + _pi4;

dd _e("2.718281828459045235360287471352662498");

dd _ln2("0.693147180559945309417232121458176568");
dd _ln10("2.302585092994045684017991454684364208");

dd _lge("1.442695040888963407359924681001892137");
dd _lg10("3.321928094887362347870319429489390176");

dd _log2("0.301029995663981195213738894724493027");
dd _loge("0.434294481903251827651128918916605082");

dd _sqrt2("1.414213562373095048801688724209698079");

dd _inv_pi("0.318309886183790671537767526745028724");
dd _inv_pi2("0.636619772367581343075535053490057448");
dd _inv_e("0.367879441171442321595523770161460867");
dd _inv_sqrt2("0.707106781186547524400844362104849039");
#endif

namespace sw {
namespace universal {

Expand Down
133 changes: 133 additions & 0 deletions static/dd/api/constants.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// constants.cpp: test suite runner for creating and verifying doubledouble constants
//
// Copyright (C) 2017 Stillwater Supercomputing, Inc.
// SPDX-License-Identifier: MIT
//
// This file is part of the universal numbers project, which is released under an MIT Open Source license.
#include <universal/utility/directives.hpp>
#include <limits>
#include <universal/number/dd/dd.hpp>
#include <universal/verification/test_suite.hpp>
#include <universal/verification/test_suite_arithmetic.hpp>
#include <universal/verification/test_suite_randoms.hpp>

sw::universal::dd GenerateDoubleDouble(const std::string& str) {
using namespace sw::universal;
dd v(str);
auto oldPrec = std::cout.precision();
// 53 bits = 16 decimal digits, 17 to include last, 15 typical valid digits
std::cout << std::setprecision(std::numeric_limits<double>::max_digits10);
std::cout << to_pair(v) << '\n';
std::cout << std::setprecision(oldPrec);
return v;
}

void report(const sw::universal::dd& v, int precision = 17) {
auto oldPrec = std::cout.precision();
std::cout << std::setprecision(precision) << to_pair(v) << " : " << v << '\n';
std::cout << std::setprecision(oldPrec);
}

// Regression testing guards: typically set by the cmake configuration, but MANUAL_TESTING is an override
#define MANUAL_TESTING 1
// REGRESSION_LEVEL_OVERRIDE is set by the cmake file to drive a specific regression intensity
// It is the responsibility of the regression test to organize the tests in a quartile progression.
//#undef REGRESSION_LEVEL_OVERRIDE
#ifndef REGRESSION_LEVEL_OVERRIDE
#undef REGRESSION_LEVEL_1
#undef REGRESSION_LEVEL_2
#undef REGRESSION_LEVEL_3
#undef REGRESSION_LEVEL_4
#define REGRESSION_LEVEL_1 1
#define REGRESSION_LEVEL_2 1
#define REGRESSION_LEVEL_3 1
#define REGRESSION_LEVEL_4 1
#endif

int main()
try {
using namespace sw::universal;

std::string test_suite = "doubledouble conversion validation";
std::string test_tag = "doubledouble conversion";
bool reportTestCases = false;
int nrOfFailedTestCases = 0;

ReportTestSuiteHeader(test_suite, reportTestCases);

#if MANUAL_TESTING

dd _zero("0.0"); report(_zero);
dd _one("1.0"); report(_one);
dd _ten("10.0"); report(_ten);

dd _tenth("0.1"); report(_tenth);
dd _third("0.333333333333333333333333333333333333"); report(_third);

dd _2pi("6.283185307179586476925286766559005768"); report(_2pi);
dd _pi("3.141592653589793238462643383279502884"); report(_pi);
dd _pi2("1.570796326794896619231321691639751442"); report(_pi2);
dd _pi4("0.785398163397448309615660845819875721"); report(_pi4);
dd _3pi4 = _pi2 + _pi4; report(_3pi4);

dd _e("2.718281828459045235360287471352662498"); report(_e);

dd _ln2("0.693147180559945309417232121458176568"); report(_ln2);
dd _ln10("2.302585092994045684017991454684364208"); report(_ln10);

dd _lge("1.442695040888963407359924681001892137"); report(_lge);
dd _lg10("3.321928094887362347870319429489390176"); report(_lg10);

dd _log2("0.301029995663981195213738894724493027"); report(_log2);
dd _loge("0.434294481903251827651128918916605082"); report(_loge);

dd _sqrt2("1.414213562373095048801688724209698079"); report(_sqrt2);

dd _inv_pi("0.318309886183790671537767526745028724"); report(_inv_pi);
dd _inv_pi2("0.636619772367581343075535053490057448"); report(_inv_pi2);
dd _inv_e("0.367879441171442321595523770161460867"); report(_inv_e);
dd _inv_sqrt2("0.707106781186547524400844362104849039"); report(_inv_sqrt2);

ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
return EXIT_SUCCESS; // ignore failures
#else // !MANUAL_TESTING

#if REGRESSION_LEVEL_1



#endif

#if REGRESSION_LEVEL_2
#endif

#if REGRESSION_LEVEL_3
#endif

#if REGRESSION_LEVEL_4
#endif

ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS);
#endif // MANUAL_TESTING
}
catch (char const* msg) {
std::cerr << "Caught ad-hoc exception: " << msg << std::endl;
return EXIT_FAILURE;
}
catch (const sw::universal::universal_arithmetic_exception& err) {
std::cerr << "Caught unexpected universal arithmetic exception : " << err.what() << std::endl;
return EXIT_FAILURE;
}
catch (const sw::universal::universal_internal_exception& err) {
std::cerr << "Caught unexpected universal internal exception: " << err.what() << std::endl;
return EXIT_FAILURE;
}
catch (const std::runtime_error& err) {
std::cerr << "Caught runtime exception: " << err.what() << std::endl;
return EXIT_FAILURE;
}
catch (...) {
std::cerr << "Caught unknown exception" << std::endl;
return EXIT_FAILURE;
}
12 changes: 1 addition & 11 deletions static/dd/conversion/conversion.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// addition.cpp: test suite runner for addition on bfloat16s
// conversion.cpp: test suite runner for conversion operators for doubledouble
//
// Copyright (C) 2017 Stillwater Supercomputing, Inc.
// SPDX-License-Identifier: MIT
Expand Down Expand Up @@ -48,16 +48,6 @@ try {

#if REGRESSION_LEVEL_1

constexpr unsigned nrOfRandoms = 1000;
std::stringstream s;
s << test_tag << " " << nrOfRandoms << " random pairs";
std::string description = s.str();
nrOfFailedTestCases += ReportTestResult(
VerifyBinaryOperatorThroughRandoms<dd>(reportTestCases, RandomsOp::OPCODE_ADD, nrOfRandoms),
description,
test_tag
);

#endif

#if REGRESSION_LEVEL_2
Expand Down

0 comments on commit 4ce0ca5

Please sign in to comment.