Boost logo

Boost :

Subject: Re: [boost] C++11 compilers and fenv
From: John Maddock (jz.maddock_at_[hidden])
Date: 2015-05-06 13:28:40


> How are c++11 boost libraries dealing with these compiler issues?
> I got some options in mind:
> — Implement everything using custom detections, this will work in every compiler, but it will run slower for everyone too.
> — Finding a compiler with the support, implementing the library using the FENV, and assuming compilers will support it when the library is finished and reviewed.
> — Detect somehow the FENV pragma was not processed and have 2 implementations. No idea how to detect something like this.
>
> Had someone dealt with similar problem already?
> Is there a common approach?

No, but you've had some good suggestions, and I would just add my +1
that FENV is unlikely to be very widely available due to the issues
outlined.

Some other resources that may help you:

* If you're targeting C++11 then you can reasonably rely on
std::isfinite and friends.
* If as suggested you want to try bit-fiddling to get the status of an
FP value, then that code is already in Boost.Math (see fpclassify.hpp
and includes thereof) and you can steal as required ;-) Obviously any
software solution will be *much* slower than getting the hardware to
raise an exception for you.
* When it comes to testing, libs/multiprecision/test/test_arithmetic.hpp
has a template function "test_arithmetic" that's designed to test every
conceivable arithmetic operator (and permutations thereof).
* In fact it occurs to me that you could implement what you had in mind
in about 10 minutes using Boost.Multiprecision - albeit in a more
heavyweight and less flexible manner than a dedicated solution.... in
fact I've attached sample code below. Given that we known that the
"abstraction overhead" of boost::multiprecision::number with
arithmetic_backend is very small, this might make a quick/easy/dirty way
to test various FP-testing methods?

HTH, John.

Here's the test code:

#include <libs/multiprecision/performance/arithmetic_backend.hpp>
#include <boost/multiprecision/logged_adaptor.hpp>
#include <boost/multiprecision/number.hpp>

namespace boost{
    namespace multiprecision{

       typedef arithmetic_backend<double> fp_wrapper;
       typedef logged_adaptor<fp_wrapper> backend_type;

       void log_postfix_event(const fp_wrapper& val, const char*
/*event_description*/)
       {
          if(!(std::isfinite)(val.data()))
          {
             throw std::runtime_error("Oops!");
          }
       }

       typedef number<backend_type, et_off> checked_double;
    }
}

int main(int argc, char *argv[])
{
    using namespace boost::multiprecision;
    checked_double a(2), b(0);
    a /= b;
    return 0;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk