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

+ runtime check of rounding mode support #8

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
29 changes: 29 additions & 0 deletions include/boost/numeric/interval/hw_rounding.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#ifndef BOOST_NUMERIC_INTERVAL_HW_ROUNDING_HPP
#define BOOST_NUMERIC_INTERVAL_HW_ROUNDING_HPP

#include <cassert>

#include <boost/numeric/interval/rounding.hpp>
#include <boost/numeric/interval/rounded_arith.hpp>

Expand Down Expand Up @@ -46,23 +48,50 @@ namespace boost {
namespace numeric {
namespace interval_lib {

namespace detail
{
template<class T>
struct runtime_checker
{
protected:
runtime_checker()
{
static helper _;
}

private:
struct helper
{
helper()
{
T a = -(T(-1.1) * T(10.1));
T b = +(T(+1.1) * T(10.1));
assert(a != b);
}
};
};
}

/*
* Three specializations of rounded_math<T>
*/

template<>
struct rounded_math<float>
: save_state<rounded_arith_opp<float> >
, private detail::runtime_checker<float>
{};

template<>
struct rounded_math<double>
: save_state<rounded_arith_opp<double> >
, private detail::runtime_checker<double>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this increase the runtime performance significantly if these are used in hot code paths?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't because it is invoking only once (static helper _, line 59). There is still some performance penalty due to the check that singleton of the type helper is initialized, but I believe that it's insignificant in comparison to the cost of switch of rounding mode.

{};

template<>
struct rounded_math<long double>
: save_state<rounded_arith_opp<long double> >
, private detail::runtime_checker<long double>
{};

} // namespace interval_lib
Expand Down