Skip to content

Commit

Permalink
organizing constants and complex in math library
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravenwater committed Aug 30, 2024
1 parent 5e2d8ae commit 2c45f4b
Show file tree
Hide file tree
Showing 21 changed files with 109 additions and 110 deletions.
5 changes: 3 additions & 2 deletions applications/mixed-precision/dnn/fit_sin.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// fit_sin.cpp: fit y = sin(x) with a third order polynomial using gradient descent
//
// 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>
Expand All @@ -11,7 +12,7 @@
#include <universal/number/lns/lns.hpp>
#include <universal/blas/blas.hpp>
#include <universal/dnn/dnn.hpp>
#include <universal/math/math_constants.hpp>
#include <universal/math/constants/double_constants.hpp>

/*
We will use a problem of fitting the function y=sin(x) with a third order polynomial as our example.
Expand All @@ -31,7 +32,7 @@ void SinFunctionFit() {

Vector x(nrSamples), y(nrSamples), y_pred(nrSamples);
// create linear samples between -pi and pi
x = blas::linspace(-m_pi, m_pi, nrSamples); // we create a vector<double> due to m_pi and then assign to vector<Scalar>
x = blas::linspace(-d_pi, d_pi, nrSamples); // we create a vector<double> due to d_pi and then assign to vector<Scalar>
y = blas::sin(x);

// model parameters
Expand Down
2 changes: 0 additions & 2 deletions applications/reproducibility/blas/inverse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
//
// This file is part of the HPRBLAS project, which is released under an MIT Open Source license.
#include <universal/utility/directives.hpp>

#include <chrono>

// enable posit arithmetic exceptions
#define POSIT_THROW_ARITHMETIC_EXCEPTION 1
// enable fast posits
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
// complex_manipulators: helper functions for complex<> types
// manipulators: helper functions for complex<> types
//
// Copyright (C) 2017 Stillwater Supercomputing, Inc.
// SPDX-License-Identifier: MIT
Expand Down
33 changes: 33 additions & 0 deletions include/universal/math/constants/double_constants.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once
// double_constants.hpp: definition of math constants in double precision
//
// 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.

namespace sw { namespace universal {

// best practice for C++11
// inject mathematical constants in our namespace

// double values
constexpr double d_pi_4 = 0.785398163397448309616; // pi/4
constexpr double d_pi_3 = 1.04719755119659774615; // pi/3
constexpr double d_pi_2 = 1.57079632679489661923; // pi/2
constexpr double d_pi = 3.14159265358979323846; // pi
constexpr double d_3pi_4 = 4.71238898038468985769; // 3*pi/4
constexpr double d_2pi = 6.28318530717958647693; // 2*pi

constexpr double d_1_pi = 0.318309886183790671538; // 1/pi
constexpr double d_2_pi = 0.636619772367581343076; // 2/pi
constexpr double d_2_sqrtpi = 1.12837916709551257390; // 2/sqrt(pi)
constexpr double d_sqrt2 = 1.41421356237309504880; // sqrt(2)
constexpr double d_sqrt1_2 = 0.707106781186547524401; // 1/sqrt(2)
constexpr double d_e = 2.71828182845904523536; // e
constexpr double d_log2e = 1.44269504088896340736; // log2(e)
constexpr double d_log10e = 0.434294481903251827651; // log10(e)
constexpr double d_ln2 = 0.693147180559945309417; // ln(2)
constexpr double d_ln10 = 2.30258509299404568402; // ln(10)

}} // namespace sw::universal
22 changes: 22 additions & 0 deletions include/universal/math/constants/longdouble_constants.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once
// longdouble_constants.hpp: definition of math constants in long double precision
//
// 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.

namespace sw { namespace universal {

// best practice for C++11
// inject mathematical constants in our namespace

// long double values
constexpr long double ld_pi_4 = 0.78539816339744830961566084581988l; // pi/4
constexpr long double ld_pi_3 = 1.0471975511965977461542144610932l; // pi/3
constexpr long double ld_pi_2 = 1.5707963267948966192313216916398l; // pi/2
constexpr long double ld_pi = 3.1415926535897932384626433832795l; // pi
constexpr long double ld_3pi_4 = 4.7123889803846898576939650749193l; // 3*pi/4
constexpr long double ld_2pi = 6.283185307179586476925286766559l; // 2*pi

}} // namespace sw::universal
46 changes: 0 additions & 46 deletions include/universal/math/math_constants.hpp

This file was deleted.

24 changes: 0 additions & 24 deletions include/universal/math/math_functions.hpp

This file was deleted.

18 changes: 16 additions & 2 deletions include/universal/math/mathlib_shim.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,21 @@
/// INCLUDE FILES that make up the library

///////////////////////////////////////////////////////////////////////////////////////
/// math functions
#include <universal/math/math_functions.hpp>
/// generic versions of the standard math library, shimming into the math stdlib
#include <universal/math/stub/abs.hpp>
#include <universal/math/stub/classify.hpp>
#include <universal/math/stub/complex.hpp>
#include <universal/math/stub/error_and_gamma.hpp>
#include <universal/math/stub/exponent.hpp>
#include <universal/math/stub/fractional.hpp>
#include <universal/math/stub/hyperbolic.hpp>
#include <universal/math/stub/hypot.hpp>
#include <universal/math/stub/logarithm.hpp>
#include <universal/math/stub/minmax.hpp>
#include <universal/math/stub/next.hpp>
#include <universal/math/stub/pow.hpp>
#include <universal/math/stub/sqrt.hpp>
#include <universal/math/stub/trigonometry.hpp>
#include <universal/math/stub/truncate.hpp>

#endif
4 changes: 2 additions & 2 deletions include/universal/number/cfloat/math/trigonometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// SPDX-License-Identifier: MIT
//
// This file is part of the universal numbers project, which is released under an MIT Open Source license.
#include <universal/math/math_constants.hpp>
#include <universal/math/constants/double_constants.hpp>

namespace sw { namespace universal {

Expand Down Expand Up @@ -57,7 +57,7 @@ cfloat<nbits, es, bt, hasSubnormal, hasSupernormal, isSaturating> asin(cfloat<nb
// cotangent an angle of x radians
template<unsigned nbits, unsigned es, typename bt, bool hasSubnormal, bool hasSupernormal, bool isSaturating>
cfloat<nbits, es, bt, hasSubnormal, hasSupernormal, isSaturating> cot(cfloat<nbits, es, bt, hasSubnormal, hasSupernormal, isSaturating> x) {
return cfloat<nbits, es, bt, hasSubnormal, hasSupernormal, isSaturating>(std::tan(sw::universal::m_pi_2-double(x)));
return cfloat<nbits, es, bt, hasSubnormal, hasSupernormal, isSaturating>(std::tan(sw::universal::d_pi_2-double(x)));
}

// secant of an angle of x radians
Expand Down
7 changes: 4 additions & 3 deletions include/universal/number/erational/math/trigonometry.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#pragma once
// trigonometric.hpp: trigonometric functions for adaptive precision decimal rationals
//
// Copyright (C) 2017-2023 Stillwater Supercomputing, Inc.
// 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/math/math_constants.hpp>
#include <universal/math/constants/double_constants.hpp>

namespace sw { namespace universal {

Expand Down Expand Up @@ -48,7 +49,7 @@ erational asin(erational x) {

// cotangent an angle of x radians
erational cot(erational x) {
return erational(std::tan(sw::universal::m_pi_2-double(x)));
return erational(std::tan(sw::universal::d_pi_2-double(x)));
}

// secant of an angle of x radians
Expand Down
4 changes: 2 additions & 2 deletions include/universal/number/fixpnt/math/trigonometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// SPDX-License-Identifier: MIT
//
// This file is part of the universal numbers project, which is released under an MIT Open Source license.
#include <universal/math/math_constants.hpp>
#include <universal/math/constants/double_constants.hpp>

namespace sw { namespace universal {

Expand Down Expand Up @@ -57,7 +57,7 @@ fixpnt<nbits, rbits, arithmetic, bt> asin(fixpnt<nbits, rbits, arithmetic, bt> x
// cotangent an angle of x radians
template<unsigned nbits, unsigned rbits, bool arithmetic, typename bt>
fixpnt<nbits, rbits, arithmetic, bt> cot(fixpnt<nbits, rbits, arithmetic, bt> x) {
return fixpnt<nbits, rbits, arithmetic, bt>(std::tan(sw::universal::m_pi_2-double(x)));
return fixpnt<nbits, rbits, arithmetic, bt>(std::tan(sw::universal::d_pi_2-double(x)));
}

// secant of an angle of x radians
Expand Down
4 changes: 2 additions & 2 deletions include/universal/number/lns/math/trigonometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// SPDX-License-Identifier: MIT
//
// This file is part of the universal numbers project, which is released under an MIT Open Source license.
#include <universal/math/math_constants.hpp>
#include <universal/math/constants/double_constants.hpp>

namespace sw { namespace universal {

Expand Down Expand Up @@ -57,7 +57,7 @@ lns<nbits, rbits, bt, xtra...> asin(lns<nbits, rbits, bt, xtra...> x) {
// cotangent an angle of x radians
template<unsigned nbits, unsigned rbits, typename bt, auto... xtra>
lns<nbits, rbits, bt, xtra...> cot(lns<nbits, rbits, bt, xtra...> x) {
return lns<nbits, rbits, bt, xtra...>(std::tan(sw::universal::m_pi_2-double(x)));
return lns<nbits, rbits, bt, xtra...>(std::tan(sw::universal::d_pi_2-double(x)));
}

// secant of an angle of x radians
Expand Down
4 changes: 2 additions & 2 deletions include/universal/number/posit/math/trigonometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// SPDX-License-Identifier: MIT
//
// This file is part of the universal numbers project, which is released under an MIT Open Source license.
#include <universal/math/math_constants.hpp> // for m_pi_2
#include <universal/math/constants/double_constants.hpp> // for m_pi_2

namespace sw { namespace universal {

Expand Down Expand Up @@ -64,7 +64,7 @@ posit<nbits,es> asin(posit<nbits,es> x) {
// cotangent an angle of x radians
template<unsigned nbits, unsigned es>
posit<nbits,es> cot(posit<nbits,es> x) {
return posit<nbits,es>(std::tan(sw::universal::m_pi_2-double(x)));
return posit<nbits,es>(std::tan(sw::universal::d_pi_2-double(x)));
}

// secant of an angle of x radians
Expand Down
4 changes: 2 additions & 2 deletions include/universal/number/posito/math/trigonometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// SPDX-License-Identifier: MIT
//
// This file is part of the universal numbers project, which is released under an MIT Open Source license.
#include <universal/math/math_constants.hpp> // for m_pi_2
#include <universal/math/constants/double_constants.hpp> // for m_pi_2

namespace sw { namespace universal {

Expand Down Expand Up @@ -64,7 +64,7 @@ posito<nbits,es> asin(posito<nbits,es> x) {
// cotangent an angle of x radians
template<unsigned nbits, unsigned es>
posito<nbits,es> cot(posito<nbits,es> x) {
return posito<nbits,es>(std::tan(sw::universal::m_pi_2-double(x)));
return posito<nbits,es>(std::tan(sw::universal::d_pi_2-double(x)));
}

// secant of an angle of x radians
Expand Down
2 changes: 1 addition & 1 deletion include/universal/verification/test_reporters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <complex>
#include <universal/native/integers.hpp>
#include <universal/native/ieee754.hpp>
#include <universal/math/complex_manipulators.hpp>
#include <universal/math/complex/manipulators.hpp>
#include <universal/verification/test_status.hpp>

// NOTE: reporters write to cerr
Expand Down
18 changes: 9 additions & 9 deletions linalg/data/serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <universal/blas/generators.hpp>
#include <universal/blas/serialization/datafile.hpp>
#include <universal/verification/test_suite.hpp>
#include <universal/math/math_constants.hpp>
#include <universal/math/constants/double_constants.hpp>

void ReportNativeHexFormats() {
using namespace sw::universal;
Expand Down Expand Up @@ -45,31 +45,31 @@ void ReportNumberSystemFormats() {
ReportFormats(c);
}
{
fixpnt<40, 32, Modulo, uint32_t> a(m_pi);
fixpnt<40, 32, Modulo, uint32_t> a(d_pi);
ReportFormats(a);
ReportFormats(-a);
fixpnt<40, 32, Saturate, uint32_t> b(m_pi);
fixpnt<40, 32, Saturate, uint32_t> b(d_pi);
ReportFormats(b);

}
{
cfloat<64, 11, uint32_t, true, false, false> a(m_pi);
cfloat<64, 11, uint32_t, true, false, false> a(d_pi);
ReportFormats(a);
}
{
posit<64, 5> a(m_pi);
posit<64, 5> a(d_pi);
ReportFormats(a);
}
{
lns<64, 32> a(m_pi);
lns<64, 32> a(d_pi);
ReportFormats(a);
}
{
dbns<8,3,uint8_t> a(m_pi);
dbns<8,3,uint8_t> a(d_pi);
ReportFormats(a);
dbns<10, 4, uint8_t> b(m_pi);
dbns<10, 4, uint8_t> b(d_pi);
ReportFormats(b);
dbns<12, 5, uint8_t> c(m_pi);
dbns<12, 5, uint8_t> c(d_pi);
ReportFormats(c);
}
}
Expand Down
2 changes: 1 addition & 1 deletion playground/efunc_posits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ try {
d = conj(c); std::cout << " conj(" << c << ") = " << std::setw(POSIT_COLUMN_WIDTH) << d << " " << color_print(d.real()) << "," << color_print(d.imag()) << '\n';

// sqrt and inverse sqrt
x = m_pi_4;
x = d_pi_4;
p = sqrt(x); std::cout << " sqrt(" << x << ") = " << std::setw(POSIT_COLUMN_WIDTH) << p << " " << color_print(p) << '\n';
p = rsqrt(x); std::cout << "rsqrt(" << x << ") = " << std::setw(POSIT_COLUMN_WIDTH) << p << " " << color_print(p) << '\n';

Expand Down
2 changes: 1 addition & 1 deletion playground/skeleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void PiExamples() {
std::cout << "Value of PI as a function of the posit configuration\n";
posit<nbits, es> p;

p = m_pi;
p = d_pi;
std::cout << type_tag(p) << " value of PI = " << p << " " << color_print(p) << " " << pretty_print(p) << '\n';

// convert posit back to float
Expand Down
2 changes: 1 addition & 1 deletion static/dbns/api/lns_comparison.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ try {

l.setbits(0xf5);
std::cout << to_binary(l) << " : " << l << " : " << color_print(l) << '\n';
std::cout << to_binary(l.extractExponent(0), 4) << " : " << to_binary(l.extractExponent(1), 4) << '\n';
std::cout << to_binary(l.extractExponent(0), true, 4) << " : " << to_binary(l.extractExponent(1), true, 4) << '\n';

std::cout << dynamic_range(l) << '\n';
}
Expand Down
Loading

0 comments on commit 2c45f4b

Please sign in to comment.