Skip to content

Commit

Permalink
+ runtime check of rounding mode support
Browse files Browse the repository at this point in the history
For example, following program

<code>
    #include <boost/numeric/interval.hpp>

    int main()
    {
        boost::numeric::interval<double>::traits_type::rounding _;
    }
</code>

will work fine if is built with gcc using '-frounding-math', but will fail otherwise.
Running on Valgrind is another practical case when this program will crash (even if it was correctly built).
  • Loading branch information
AndreyG committed Jan 5, 2019
1 parent 802aef1 commit 51779c6
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 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,51 @@ namespace boost {
namespace numeric {
namespace interval_lib {

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

private:
struct helper
{
helper()
{
save_state<rounded_arith_opp<T> > rounding;
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>
{};

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

} // namespace interval_lib
Expand Down

0 comments on commit 51779c6

Please sign in to comment.