-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #264 from stillwater-sc/v3.48
Unified exception hierarchy
- Loading branch information
Showing
158 changed files
with
4,790 additions
and
3,359 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
file (GLOB SOURCES "./*.cpp") | ||
|
||
compile_all("true" "approx" "Applications/Approximation" "${SOURCES}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// taylor_series.cpp: experiments with number systems approximating the Reals approximating functions | ||
// | ||
// Copyright (C) 2017-2021 Stillwater Supercomputing, Inc. | ||
// | ||
// This file is part of the UNIVERSAL project, which is released under an MIT Open Source license. | ||
#include <universal/verification/test_suite.hpp> | ||
#include <universal/number/cfloat/cfloat.hpp> | ||
|
||
/* | ||
* From Wikipedia: | ||
* | ||
* In mathematics, the Taylor series of a function is an infinite sum of terms | ||
* that are expressed in terms of the function's derivatives at a single point. | ||
* For most common functions, the function and the sum of its Taylor series are | ||
* equal near this point. Taylor's series are named after Brook Taylor, who | ||
* introduced them in 1715. | ||
* | ||
* If 0 is the point where the derivatives are considered, a Taylor series is also | ||
* called a Maclaurin series, after Colin Maclaurin, who made extensive use of | ||
* this special case of Taylor series in the 18th century. The partial sum | ||
* formed by the first n + 1 terms of a Taylor series is a polynomial of degree n | ||
* that is called the nth Taylor polynomial of the function. | ||
* | ||
* Taylor polynomials are approximations of a function, which become generally | ||
* better as n increases. Taylor's theorem gives quantitative estimates on the | ||
* error introduced by the use of such approximations. If the Taylor series | ||
* of a function is convergent, its sum is the limit of the infinite sequence | ||
* of the Taylor polynomials. A function may differ from the sum of its Taylor | ||
* series, even if its Taylor series is convergent. A function is analytic at | ||
* a point x if it is equal to the sum of its Taylor series in some open interval | ||
* (or open disk in the complex plane) containing x. This implies that the | ||
* function is analytic at every point of the interval (or disk). | ||
* | ||
*/ | ||
|
||
int main(int argc, char** argv) | ||
try { | ||
using namespace sw::universal; | ||
|
||
std::string test_suite = "Experiments with Taylor Series Expansion"; | ||
std::string test_tag = "operator"; | ||
std::cout << test_suite << '\n'; | ||
int nrOfFailedTestCases = 0; | ||
|
||
ReportTestSuiteResults(test_suite, nrOfFailedTestCases); | ||
return EXIT_SUCCESS; | ||
} | ||
catch (char const* msg) { | ||
std::cerr << msg << std::endl; | ||
return EXIT_FAILURE; | ||
} | ||
catch (const sw::universal::universal_arithmetic_exception& err) { | ||
std::cerr << "Caught an unexpected universal arithmetic exception: " << err.what() << std::endl; | ||
return EXIT_FAILURE; | ||
} | ||
catch (const sw::universal::universal_internal_exception& err) { | ||
std::cerr << "Caught an unexpected universal internal exception: " << err.what() << std::endl; | ||
return EXIT_FAILURE; | ||
} | ||
catch (std::runtime_error& err) { | ||
std::cerr << "Caught an unexpected runtime exception: " << err.what() << std::endl; | ||
return EXIT_FAILURE; | ||
} | ||
catch (...) { | ||
std::cerr << "Caught unknown exception" << std::endl; | ||
return EXIT_FAILURE; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
file (GLOB SOURCES "./*.cpp") | ||
|
||
compile_all("true" "fp" "Applications/Floating Point Arithmetic" "${SOURCES}") |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
// contract_expand.cpp: evaluation of contractions and expansions of posit number systems | ||
// | ||
// Copyright (C) 2017-2021 Stillwater Supercomputing, Inc. | ||
// | ||
// This file is part of the UNIVERSAL project, which is released under an MIT Open Source license. | ||
#include <universal/utility/directives.hpp> | ||
//#include <universal/number/posit/posit.hpp> | ||
#include <universal/number/cfloat/cfloat.hpp> | ||
|
||
template<typename Scalar> | ||
void ContractionExpansion(int depth) { | ||
using namespace sw::universal; | ||
|
||
int columnWidth = 20; | ||
Scalar seed = 2.0; | ||
std::cout << "Contraction/Expansion sequence sqrt(sqrt(sqrt(...sqrt(x))))))^depth => seed with seed = " << seed << '\n'; | ||
std::cout << std::setw(3) << "#" | ||
<< std::setw(columnWidth) << "contraction" | ||
<< std::setw(columnWidth) << "expansion" | ||
<< std::setw(columnWidth) << "error" | ||
<< '\n'; | ||
for (int i = 1; i < depth; ++i) { | ||
Scalar x = seed; | ||
for (int k = 1; k < i; ++k) { | ||
x = sqrt(x); | ||
} | ||
Scalar contraction = x; | ||
for (int k = 1; k < i; ++k) { | ||
x = exp2(x); | ||
} | ||
Scalar expansion = x; | ||
std::cout << std::setw(3) << i << " " | ||
<< std::setw(columnWidth) << contraction << " " | ||
<< std::setw(columnWidth) << expansion << " " | ||
<< std::setw(columnWidth) << expansion - seed | ||
<< '\n'; | ||
} | ||
} | ||
|
||
// build a table of values for x, sqrt(x), pow(x,2) | ||
template<typename Scalar> | ||
void RangeTable(std::ostream& ostr) { | ||
using namespace sw::universal; | ||
constexpr size_t nbits = Scalar::nbits; | ||
static_assert(nbits < 16, "size of the table is constrained to nbits < 16"); | ||
|
||
size_t COLUMN_WIDTH = 10; | ||
size_t NR_SAMPLES = (1ull << (nbits - 1)); // ignore negative values | ||
Scalar x; | ||
ostr << std::setw(COLUMN_WIDTH) << "x" << ',' | ||
<< std::setw(COLUMN_WIDTH) << "y = x" << ',' | ||
<< std::setw(COLUMN_WIDTH) << "y = sqrt(x)" << ',' | ||
<< std::setw(COLUMN_WIDTH) << "y = x^2" << ',' | ||
<< std::setw(COLUMN_WIDTH) << "y = sqrt(x^2)" << ',' | ||
<< std::setw(COLUMN_WIDTH) << "y = sqrt(x)^2" | ||
<< '\n'; | ||
for (size_t i = 0; i < NR_SAMPLES; ++i) { | ||
x.setbits(i); | ||
Scalar sqrt_x = pow(x, 0.5); | ||
Scalar x_sqr = pow(x, 2.0); | ||
Scalar x_t1 = pow(pow(x, 2.0), 0.5); | ||
Scalar x_t2 = pow(pow(x, 0.5), 2.0); | ||
ostr << std::setw(COLUMN_WIDTH) << x << ',' | ||
<< std::setw(COLUMN_WIDTH) << x << ',' | ||
<< std::setw(COLUMN_WIDTH) << sqrt_x << ',' | ||
<< std::setw(COLUMN_WIDTH) << x_sqr << ',' | ||
<< std::setw(COLUMN_WIDTH) << x_t1 << ',' | ||
<< std::setw(COLUMN_WIDTH) << x_t2 | ||
<< '\n'; | ||
} | ||
} | ||
|
||
void SquareRootSquared(std::ostream& ostr) { | ||
constexpr size_t nbits = 8; // the sampling | ||
size_t COLUMN_WIDTH = 10; | ||
size_t NR_SAMPLES = (1ull << (nbits - 1)); // ignore negative values | ||
using c8_2 = sw::universal::cfloat< 8, 2, uint8_t, true, true, false>; | ||
using c10_2 = sw::universal::cfloat<10, 2, uint8_t, true, true, false> ; | ||
using c12_2 = sw::universal::cfloat<12, 2, uint8_t, true, true, false> ; | ||
using c14_2 = sw::universal::cfloat<14, 2, uint8_t, true, true, false> ; | ||
using c16_2 = sw::universal::cfloat<16, 2, uint8_t, true, true, false> ; | ||
ostr << std::setw(COLUMN_WIDTH) << "x" << ',' | ||
<< std::setw(COLUMN_WIDTH) << "y = x" << ',' | ||
<< std::setw(COLUMN_WIDTH) << "cfloat<8,2>" << ',' | ||
<< std::setw(COLUMN_WIDTH) << "cfloat<10,2>" << ',' | ||
<< std::setw(COLUMN_WIDTH) << "cfloat<12,2>" << ',' | ||
<< std::setw(COLUMN_WIDTH) << "cfloat<14,2>" << ',' | ||
<< std::setw(COLUMN_WIDTH) << "cfloat<16,2>" << ',' | ||
<< '\n'; | ||
c8_2 x; | ||
for (size_t i = 0; i < NR_SAMPLES; ++i) { | ||
x.setbits(i); | ||
float v = float(x); | ||
c8_2 v8(v); | ||
c10_2 v10(v); | ||
c12_2 v12(v); | ||
c14_2 v14(v); | ||
c16_2 v16(v); | ||
c16_2 w, z; | ||
ostr << std::setw(COLUMN_WIDTH) << x << ',' | ||
<< std::setw(COLUMN_WIDTH) << v8 << ',' | ||
<< std::setw(COLUMN_WIDTH) << pow(pow(v8, 0.5), 2.0) << ',' | ||
<< std::setw(COLUMN_WIDTH) << pow(pow(v10, 0.5), 2.0) << ',' | ||
<< std::setw(COLUMN_WIDTH) << pow(pow(v12, 0.5), 2.0) << ',' | ||
<< std::setw(COLUMN_WIDTH) << pow(pow(v14, 0.5), 2.0) << ',' | ||
<< std::setw(COLUMN_WIDTH) << pow(pow(v16, 0.5), 2.0) | ||
<< '\n'; | ||
} | ||
|
||
} | ||
|
||
void SquareRootSquared2(std::ostream& ostr) { | ||
constexpr size_t nbits = 8; // the sampling | ||
size_t COLUMN_WIDTH = 10; | ||
size_t NR_SAMPLES = (1ull << (nbits - 1)); // ignore negative values | ||
using c8_2 = sw::universal::cfloat<8, 2>; | ||
using c10_2 = sw::universal::cfloat<10, 2>; | ||
using c12_3 = sw::universal::cfloat<12, 3>; | ||
using c14_4 = sw::universal::cfloat<14, 4>; | ||
using c16_5 = sw::universal::cfloat<16, 5>; | ||
ostr << std::setw(COLUMN_WIDTH) << "x" << ',' | ||
<< std::setw(COLUMN_WIDTH) << "y = x" << ',' | ||
<< std::setw(COLUMN_WIDTH) << "cfloat<8,2>" << ',' | ||
<< std::setw(COLUMN_WIDTH) << "cfloat<10,2>" << ',' | ||
<< std::setw(COLUMN_WIDTH) << "cfloat<12,3>" << ',' | ||
<< std::setw(COLUMN_WIDTH) << "cfloat<14,4>" << ',' | ||
<< std::setw(COLUMN_WIDTH) << "cfloat<16,5>" << ',' | ||
<< '\n'; | ||
c8_2 x; | ||
for (size_t i = 0; i < NR_SAMPLES; ++i) { | ||
x.setbits(i); | ||
float v = float(x); | ||
c8_2 v8(v); | ||
c10_2 v10(v); | ||
c12_3 v12(v); | ||
c14_4 v14(v); | ||
c16_5 v16(v); | ||
ostr << std::setw(COLUMN_WIDTH) << x << ',' | ||
<< std::setw(COLUMN_WIDTH) << v8 << ',' | ||
<< std::setw(COLUMN_WIDTH) << pow(pow(v8, 0.5), 2.0) << ',' | ||
<< std::setw(COLUMN_WIDTH) << pow(pow(v10, 0.5), 2.0) << ',' | ||
<< std::setw(COLUMN_WIDTH) << pow(pow(v12, 0.5), 2.0) << ',' | ||
<< std::setw(COLUMN_WIDTH) << pow(pow(v14, 0.5), 2.0) << ',' | ||
<< std::setw(COLUMN_WIDTH) << pow(pow(v16, 0.5), 2.0) | ||
<< '\n'; | ||
} | ||
} | ||
|
||
int main() | ||
try { | ||
using namespace sw::universal; | ||
|
||
// print detailed bit-level computational intermediate results | ||
// bool verbose = false; | ||
|
||
// preserve the existing ostream precision | ||
auto precision = std::cout.precision(); | ||
std::cout << std::setprecision(12); | ||
|
||
/* | ||
{ | ||
constexpr size_t nbits = 32; | ||
constexpr size_t es = 2; | ||
using Posit = posit<nbits, es>; | ||
ContractionExpansion<Posit>(10); | ||
} | ||
*/ | ||
|
||
{ | ||
using Real = cfloat<8, 2, uint8_t, false, false, false>; | ||
RangeTable<Real>(std::cout); | ||
} | ||
|
||
|
||
// SquareRootSquared(std::cout); | ||
|
||
// restore the previous ostream precision | ||
std::cout << std::setprecision(precision); | ||
|
||
return EXIT_SUCCESS; | ||
} | ||
catch (char const* msg) { | ||
std::cerr << "Caught an ad-hoc exception: " << msg << std::endl; | ||
return EXIT_FAILURE; | ||
} | ||
catch (const sw::universal::universal_arithmetic_exception& err) { | ||
std::cerr << "Caught an unexpected universal arithmetic exception: " << err.what() << std::endl; | ||
return EXIT_FAILURE; | ||
} | ||
catch (const sw::universal::universal_internal_exception& err) { | ||
std::cerr << "Caught an unexpected universal internal exception: " << err.what() << std::endl; | ||
return EXIT_FAILURE; | ||
} | ||
catch (std::runtime_error& err) { | ||
std::cerr << "Caught a runtime exception: " << err.what() << std::endl; | ||
return EXIT_FAILURE; | ||
} | ||
catch (...) { | ||
std::cerr << "Caught an unknown exception" << std::endl; | ||
return EXIT_FAILURE; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.