Skip to content

Commit 4e13e9f

Browse files
committed
+ runtime check of rounding mode support
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).
1 parent 802aef1 commit 4e13e9f

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

include/boost/numeric/interval/hw_rounding.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#ifndef BOOST_NUMERIC_INTERVAL_HW_ROUNDING_HPP
1212
#define BOOST_NUMERIC_INTERVAL_HW_ROUNDING_HPP
1313

14+
#include <cassert>
15+
1416
#include <boost/numeric/interval/rounding.hpp>
1517
#include <boost/numeric/interval/rounded_arith.hpp>
1618

@@ -46,23 +48,50 @@ namespace boost {
4648
namespace numeric {
4749
namespace interval_lib {
4850

51+
namespace detail
52+
{
53+
template<class T>
54+
struct runtime_checker
55+
{
56+
protected:
57+
runtime_checker()
58+
{
59+
static helper _;
60+
}
61+
62+
private:
63+
struct helper
64+
{
65+
helper()
66+
{
67+
T a = -(T(-1.1) * T(10.1));
68+
T b = +(T(+1.1) * T(10.1));
69+
assert(a != b);
70+
}
71+
};
72+
};
73+
}
74+
4975
/*
5076
* Three specializations of rounded_math<T>
5177
*/
5278

5379
template<>
5480
struct rounded_math<float>
5581
: save_state<rounded_arith_opp<float> >
82+
, private detail::runtime_checker<float>
5683
{};
5784

5885
template<>
5986
struct rounded_math<double>
6087
: save_state<rounded_arith_opp<double> >
88+
, private detail::runtime_checker<double>
6189
{};
6290

6391
template<>
6492
struct rounded_math<long double>
6593
: save_state<rounded_arith_opp<long double> >
94+
, private detail::runtime_checker<long double>
6695
{};
6796

6897
} // namespace interval_lib

0 commit comments

Comments
 (0)