Skip to content

Commit

Permalink
Merge pull request #264 from stillwater-sc/v3.48
Browse files Browse the repository at this point in the history
Unified exception hierarchy
  • Loading branch information
Ravenwater authored Nov 19, 2021
2 parents 386301b + fa473e3 commit be62162
Show file tree
Hide file tree
Showing 158 changed files with 4,790 additions and 3,359 deletions.
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ if(NOT DEFINED UNIVERSAL_VERSION_MAJOR)
set(UNIVERSAL_VERSION_MAJOR 3)
endif()
if(NOT DEFINED UNIVERSAL_VERSION_MINOR)
set(UNIVERSAL_VERSION_MINOR 47)
set(UNIVERSAL_VERSION_MINOR 48)
endif()
if(NOT DEFINED UNIVERSAL_VERSION_PATCH)
set(UNIVERSAL_VERSION_PATCH 1)
Expand Down Expand Up @@ -737,16 +737,19 @@ endif(BUILD_EDUCATION)

# application examples
if(BUILD_APPLICATIONS)
add_subdirectory("applications/approximation")
add_subdirectory("applications/blas")
add_subdirectory("applications/chaos")
add_subdirectory("applications/chebyshev")
add_subdirectory("applications/complex")
add_subdirectory("applications/cryptography")
add_subdirectory("applications/dsp")
add_subdirectory("applications/engineering")
add_subdirectory("applications/floating-point")
add_subdirectory("applications/math")
add_subdirectory("applications/numeric")
add_subdirectory("applications/ode")
add_subdirectory("applications/optimization")
add_subdirectory("applications/pde")
add_subdirectory("applications/roots")
add_subdirectory("applications/science")
Expand Down
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ RUN cmake -DBUILD_ALL=ON .. && make
# the command 'make test' is run as part of the CI test pipeline of the release container

# add a command that when you run the container without a command, it produces something meaningful
CMD ["echo", "Universal Numbers Library Builder Version 3.47.1"]
CMD ["echo", "Universal Numbers Library Builder Version 3.48.1"]


# RELEASE stage
#FROM alpine:latest as release # hitting a segfault during startup of some playground programs
#FROM debian:buster-slim as release
FROM ubuntu:20.10 as release
FROM ubuntu:20.04 as release
LABEL Theodore Omtzigt

#RUN apk add --no-cache libc6-compat libstdc++ cmake make bash gawk sed grep bc coreutils
RUN apt-get update && apt-get install -y --no-install-recommends \
RUN apt-get update && apt-get update -y && apt-get install -y --no-install-recommends \
make \
&& apt-get clean
# create and use user stillwater
Expand Down Expand Up @@ -110,4 +110,4 @@ WORKDIR /home/stillwater/universal/build

# the command 'make test' is run as part of the CI test pipeline of this release container

CMD ["echo", "Universal Numbers Library Version 3.47.1"]
CMD ["echo", "Universal Numbers Library Version 3.48.1"]
3 changes: 3 additions & 0 deletions applications/approximation/CMakeLists.txt
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}")
67 changes: 67 additions & 0 deletions applications/approximation/taylor_series.cpp
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;
}
3 changes: 3 additions & 0 deletions applications/floating-point/CMakeLists.txt
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}")
201 changes: 201 additions & 0 deletions applications/floating-point/contract_expand.cpp
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// 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 "common.hpp"
#include <universal/utility/directives.hpp>
// pull in the posit number system
#include <universal/number/posit/posit.hpp>

Expand All @@ -21,7 +21,7 @@ void Exponentiation(int depth) {

}

int main(int argc, char** argv)
int main()
try {
using namespace sw::universal;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// 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 "common.hpp"
#include <universal/utility/directives.hpp>
// pull in the posit number system
#include <universal/number/posit/posit.hpp>

Expand Down Expand Up @@ -84,7 +84,7 @@ void GenerateTest(std::ostream& ostr, size_t N) {

#define MANUAL_TEST 1

int main(int argc, char** argv)
int main()
try {
using namespace sw::universal;

Expand Down
File renamed without changes.
Loading

0 comments on commit be62162

Please sign in to comment.