Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment with C++20 modules. #783

Draft
wants to merge 8 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ cmake-build-debug/*
.cmake/*
build.ninja
.ninja*

# Modules
*.gcm
a.out
3 changes: 3 additions & 0 deletions include/boost/math/constants/calculate_constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

#ifndef BOOST_MATH_CALCULATE_CONSTANTS_CONSTANTS_INCLUDED
#define BOOST_MATH_CALCULATE_CONSTANTS_CONSTANTS_INCLUDED

#ifndef BOOST_MATH_AS_MODULE
#include <type_traits>
#endif

namespace boost{ namespace math{ namespace constants{ namespace detail{

Expand Down
22 changes: 14 additions & 8 deletions include/boost/math/constants/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
#ifndef BOOST_MATH_CONSTANTS_CONSTANTS_INCLUDED
#define BOOST_MATH_CONSTANTS_CONSTANTS_INCLUDED

#ifndef BOOST_MATH_AS_MODULE
#include <boost/math/tools/config.hpp>
#include <boost/math/tools/cxx03_warn.hpp>
#include <boost/math/policies/policy.hpp>
#include <boost/math/tools/precision.hpp>
#include <boost/math/tools/convert_from_string.hpp>
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4127 4701)
Expand All @@ -21,6 +21,8 @@
#endif
#include <utility>
#include <type_traits>
#endif
#include <boost/math/tools/convert_from_string.hpp>

#if defined(__GNUC__) && defined(BOOST_MATH_USE_FLOAT128)
//
Expand Down Expand Up @@ -50,7 +52,7 @@ namespace boost{ namespace math
// (This is necessary because you can't use a numeric constant
// since even a long double might not have enough digits).

enum construction_method
BOOST_MATH_MODULE_EXPORT enum construction_method
{
construct_from_float = 1,
construct_from_double = 2,
Expand All @@ -71,7 +73,11 @@ namespace boost{ namespace math
//
// Max number of binary digits in the string representations of our constants:
//
#ifndef BOOST_MATH_AS_MODULE
static constexpr int max_string_digits = (101 * 1000L) / 301L;
#else
inline constexpr int max_string_digits = (101 * 1000L) / 301L;
#endif

template <typename Real, typename Policy>
struct construction_traits
Expand Down Expand Up @@ -231,16 +237,16 @@ namespace boost{ namespace math
\
\
/* The actual forwarding function: */ \
template <typename T, typename Policy> inline constexpr typename detail::constant_return<T, Policy>::type name(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(T) BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(Policy)) BOOST_MATH_NOEXCEPT(T)\
BOOST_MATH_MODULE_EXPORT template <typename T, typename Policy> inline constexpr typename detail::constant_return<T, Policy>::type name(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(T) BOOST_MATH_APPEND_EXPLICIT_TEMPLATE_TYPE_SPEC(Policy)) BOOST_MATH_NOEXCEPT(T)\
{ return detail:: BOOST_MATH_JOIN(constant_, name)<T>::get(typename construction_traits<T, Policy>::type()); }\
template <typename T> inline constexpr typename detail::constant_return<T>::type name(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(T)) BOOST_MATH_NOEXCEPT(T)\
BOOST_MATH_MODULE_EXPORT template <typename T> inline constexpr typename detail::constant_return<T>::type name(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(T)) BOOST_MATH_NOEXCEPT(T)\
{ return name<T, boost::math::policies::policy<> >(); }\
\
\
/* Now the namespace specific versions: */ \
} namespace float_constants{ static constexpr float name = BOOST_MATH_JOIN(x, F); }\
namespace double_constants{ static constexpr double name = x; } \
namespace long_double_constants{ static constexpr long double name = BOOST_MATH_JOIN(x, L); }\
} BOOST_MATH_MODULE_EXPORT namespace float_constants{ constexpr float name = BOOST_MATH_JOIN(x, F); }\
BOOST_MATH_MODULE_EXPORT namespace double_constants{ constexpr double name = x; } \
BOOST_MATH_MODULE_EXPORT namespace long_double_constants{ constexpr long double name = BOOST_MATH_JOIN(x, L); }\
namespace constants{

BOOST_DEFINE_MATH_CONSTANT(half, 5.000000000000000000000000000000000000e-01, "5.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e-01")
Expand Down Expand Up @@ -327,7 +333,7 @@ namespace boost{ namespace math
BOOST_DEFINE_MATH_CONSTANT(laplace_limit, 0.662743419349181580974742097109252907056233549115022417, "0.66274341934918158097474209710925290705623354911502241752039253499097185308651127724965480259895818168")
#endif

template <typename T>
BOOST_MATH_MODULE_EXPORT template <typename T>
inline constexpr T tau() { return two_pi<T>(); }

} // namespace constants
Expand Down
74 changes: 39 additions & 35 deletions include/boost/math/policies/error_handling.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#define BOOST_MATH_POLICY_ERROR_HANDLING_HPP

#include <boost/math/tools/config.hpp>

#ifndef BOOST_MATH_AS_MODULE
#include <boost/math/tools/throw_exception.hpp>
#include <iomanip>
#include <string>
#include <cstring>
Expand All @@ -19,13 +22,13 @@
#include <complex>
#include <cmath>
#include <cstdint>
#include <boost/math/policies/policy.hpp>
#include <boost/math/tools/precision.hpp>
#ifndef BOOST_MATH_NO_EXCEPTIONS
#include <stdexcept>
#include <boost/math/tools/throw_exception.hpp>
#include <sstream>
#endif

#include <boost/math/tools/precision.hpp>
#include <boost/math/policies/policy.hpp>

#ifdef _MSC_VER
# pragma warning(push) // Quiet warnings in boost/format.hpp
# pragma warning(disable: 4996) // _SCL_SECURE_NO_DEPRECATE
Expand All @@ -36,46 +39,47 @@
// Note that this only occurs when the compiler can deduce code is unreachable,
// for example when policy macros are used to ignore errors rather than throw.
#endif
#include <sstream>

namespace boost{ namespace math{

#ifndef BOOST_MATH_NO_EXCEPTIONS

class evaluation_error : public std::runtime_error
BOOST_MATH_MODULE_EXPORT class evaluation_error : public std::runtime_error
{
public:
explicit evaluation_error(const std::string& s) : std::runtime_error(s){}
evaluation_error(const std::string& s) : std::runtime_error(s){}
#if defined(BOOST_MATH_AS_MODULE) && defined(__GNUC__)
~evaluation_error() {}
#endif
};

class rounding_error : public std::runtime_error
BOOST_MATH_MODULE_EXPORT class rounding_error : public std::runtime_error
{
public:
explicit rounding_error(const std::string& s) : std::runtime_error(s){}
};

rounding_error(const std::string& s) : std::runtime_error(s){}
#if defined(BOOST_MATH_AS_MODULE) && defined(__GNUC__)
~rounding_error() {}
#endif
};

namespace policies{
//
// Forward declarations of user error handlers,
// it's up to the user to provide the definition of these:
//
template <class T>
BOOST_MATH_MODULE_EXPORT template <class T>
T user_domain_error(const char* function, const char* message, const T& val);
template <class T>
BOOST_MATH_MODULE_EXPORT template <class T>
T user_pole_error(const char* function, const char* message, const T& val);
template <class T>
BOOST_MATH_MODULE_EXPORT template <class T>
T user_overflow_error(const char* function, const char* message, const T& val);
template <class T>
BOOST_MATH_MODULE_EXPORT template <class T>
T user_underflow_error(const char* function, const char* message, const T& val);
template <class T>
BOOST_MATH_MODULE_EXPORT template <class T>
T user_denorm_error(const char* function, const char* message, const T& val);
template <class T>
BOOST_MATH_MODULE_EXPORT template <class T>
T user_evaluation_error(const char* function, const char* message, const T& val);
template <class T, class TargetType>
TargetType user_rounding_error(const char* function, const char* message, const T& val, const TargetType& t);
template <class T>
BOOST_MATH_MODULE_EXPORT template <class T, class TargetType>
T user_rounding_error(const char* function, const char* message, const T& val, const TargetType& t);
BOOST_MATH_MODULE_EXPORT template <class T>
T user_indeterminate_result_error(const char* function, const char* message, const T& val);

namespace detail
Expand Down Expand Up @@ -657,7 +661,7 @@ inline T raise_indeterminate_result_error(

} // namespace detail

template <class T, class Policy>
BOOST_MATH_MODULE_EXPORT template <class T, class Policy>
inline constexpr T raise_domain_error(const char* function, const char* message, const T& val, const Policy&) noexcept(is_noexcept_error_policy<Policy>::value && BOOST_MATH_IS_FLOAT(T))
{
typedef typename Policy::domain_error_type policy_type;
Expand All @@ -666,7 +670,7 @@ inline constexpr T raise_domain_error(const char* function, const char* message,
val, policy_type());
}

template <class T, class Policy>
BOOST_MATH_MODULE_EXPORT template <class T, class Policy>
inline constexpr T raise_pole_error(const char* function, const char* message, const T& val, const Policy&) noexcept(is_noexcept_error_policy<Policy>::value && BOOST_MATH_IS_FLOAT(T))
{
typedef typename Policy::pole_error_type policy_type;
Expand All @@ -675,7 +679,7 @@ inline constexpr T raise_pole_error(const char* function, const char* message, c
val, policy_type());
}

template <class T, class Policy>
BOOST_MATH_MODULE_EXPORT template <class T, class Policy>
inline constexpr T raise_overflow_error(const char* function, const char* message, const Policy&) noexcept(is_noexcept_error_policy<Policy>::value && BOOST_MATH_IS_FLOAT(T))
{
typedef typename Policy::overflow_error_type policy_type;
Expand All @@ -684,7 +688,7 @@ inline constexpr T raise_overflow_error(const char* function, const char* messag
policy_type());
}

template <class T, class Policy>
BOOST_MATH_MODULE_EXPORT template <class T, class Policy>
inline constexpr T raise_overflow_error(const char* function, const char* message, const T& val, const Policy&) noexcept(is_noexcept_error_policy<Policy>::value && BOOST_MATH_IS_FLOAT(T))
{
typedef typename Policy::overflow_error_type policy_type;
Expand All @@ -693,7 +697,7 @@ inline constexpr T raise_overflow_error(const char* function, const char* messag
val, policy_type());
}

template <class T, class Policy>
BOOST_MATH_MODULE_EXPORT template <class T, class Policy>
inline constexpr T raise_underflow_error(const char* function, const char* message, const Policy&) noexcept(is_noexcept_error_policy<Policy>::value && BOOST_MATH_IS_FLOAT(T))
{
typedef typename Policy::underflow_error_type policy_type;
Expand All @@ -702,7 +706,7 @@ inline constexpr T raise_underflow_error(const char* function, const char* messa
policy_type());
}

template <class T, class Policy>
BOOST_MATH_MODULE_EXPORT template <class T, class Policy>
inline constexpr T raise_denorm_error(const char* function, const char* message, const T& val, const Policy&) noexcept(is_noexcept_error_policy<Policy>::value && BOOST_MATH_IS_FLOAT(T))
{
typedef typename Policy::denorm_error_type policy_type;
Expand All @@ -712,7 +716,7 @@ inline constexpr T raise_denorm_error(const char* function, const char* message,
policy_type());
}

template <class T, class Policy>
BOOST_MATH_MODULE_EXPORT template <class T, class Policy>
inline constexpr T raise_evaluation_error(const char* function, const char* message, const T& val, const Policy&) noexcept(is_noexcept_error_policy<Policy>::value && BOOST_MATH_IS_FLOAT(T))
{
typedef typename Policy::evaluation_error_type policy_type;
Expand All @@ -721,7 +725,7 @@ inline constexpr T raise_evaluation_error(const char* function, const char* mess
val, policy_type());
}

template <class T, class TargetType, class Policy>
BOOST_MATH_MODULE_EXPORT template <class T, class TargetType, class Policy>
inline constexpr TargetType raise_rounding_error(const char* function, const char* message, const T& val, const TargetType& t, const Policy&) noexcept(is_noexcept_error_policy<Policy>::value && BOOST_MATH_IS_FLOAT(T))
{
typedef typename Policy::rounding_error_type policy_type;
Expand All @@ -730,7 +734,7 @@ inline constexpr TargetType raise_rounding_error(const char* function, const cha
val, t, policy_type());
}

template <class T, class R, class Policy>
BOOST_MATH_MODULE_EXPORT template <class T, class R, class Policy>
inline constexpr T raise_indeterminate_result_error(const char* function, const char* message, const T& val, const R& result, const Policy&) noexcept(is_noexcept_error_policy<Policy>::value && BOOST_MATH_IS_FLOAT(T))
{
typedef typename Policy::indeterminate_result_error_type policy_type;
Expand Down Expand Up @@ -831,8 +835,8 @@ BOOST_MATH_FORCEINLINE constexpr bool check_denorm(std::complex<T> /* val */, R*

} // namespace detail

template <class R, class Policy, class T>
BOOST_MATH_FORCEINLINE R checked_narrowing_cast(T val, const char* function) noexcept(BOOST_MATH_IS_FLOAT(R) && BOOST_MATH_IS_FLOAT(T) && is_noexcept_error_policy<Policy>::value)
BOOST_MATH_MODULE_EXPORT template <class R, class Policy, class T>
inline R checked_narrowing_cast(T val, const char* function) noexcept(BOOST_MATH_IS_FLOAT(R) && BOOST_MATH_IS_FLOAT(T) && is_noexcept_error_policy<Policy>::value)
{
typedef typename Policy::overflow_error_type overflow_type;
typedef typename Policy::underflow_error_type underflow_type;
Expand All @@ -851,7 +855,7 @@ BOOST_MATH_FORCEINLINE R checked_narrowing_cast(T val, const char* function) noe
return static_cast<R>(val);
}

template <class T, class Policy>
BOOST_MATH_MODULE_EXPORT template <class T, class Policy>
inline void check_series_iterations(const char* function, std::uintmax_t max_iter, const Policy& pol) noexcept(BOOST_MATH_IS_FLOAT(T) && is_noexcept_error_policy<Policy>::value)
{
if(max_iter >= policies::get_max_series_iterations<Policy>())
Expand All @@ -860,7 +864,7 @@ inline void check_series_iterations(const char* function, std::uintmax_t max_ite
"Series evaluation exceeded %1% iterations, giving up now.", static_cast<T>(static_cast<double>(max_iter)), pol);
}

template <class T, class Policy>
BOOST_MATH_MODULE_EXPORT template <class T, class Policy>
inline void check_root_iterations(const char* function, std::uintmax_t max_iter, const Policy& pol) noexcept(BOOST_MATH_IS_FLOAT(T) && is_noexcept_error_policy<Policy>::value)
{
if(max_iter >= policies::get_max_root_iterations<Policy>())
Expand Down
Loading
Loading