Handling Floating-Point exceptions in C++

后端 未结 3 1154
独厮守ぢ
独厮守ぢ 2021-01-11 14:43

I\'m finding the floating-point model/error issues quite confusing. It\'s an area I\'m not familiar with and I\'m not a low level C/asm programmer, so I would appreciate a b

相关标签:
3条回答
  • 2021-01-11 14:44

    I can't help much with the first two questions, but I have experience and a suggestion for the question about masking FPU exceptions.

    I've found the functions

    _statusfp()  (x64 and Win32)
    _statusfp2() (Win32 only)
    _fpreset()
    _controlfp_s()
    _clearfp()
    _matherr()
    

    useful when debugging FPU exceptions and in delivering a stable and fast product.

    When debugging, I selectively unmask exceptions to help isolate the line of code where an fpu exception is generated in a calculation where I cannot avoid calling other code that unpredictably generates fpu exceptions (like the .NET JIT's divide by zeros).

    In released product I use them to deliver a stable program that can tolerate serious floating point exceptions, detect when they occur, and recover gracefully.

    I mask all FPU exceptions when I have to call code that cannot be changed,does not have reliable exception handing, and occasionally generates FPU exceptions.

    Example:

    #define BAD_FPU_EX (_EM_OVERFLOW | _EM_ZERODIVIDE | _EM_INVALID)
    #define COMMON_FPU_EX (_EM_INEXACT | _EM_UNDERFLOW | _EM_DENORMAL)
    #define ALL_FPU_EX (BAD_FPU_EX | COMMON_FPU_EX)
    

    Release code:

    _fpreset();
    Use _controlfp_s() to mask ALL_FPU_EX 
    _clearfp();
    ... calculation
    unsigned int bad_fpu_ex = (BAD_FPU_EX  & _statusfp());
    _clearfp(); // to prevent reacting to existing status flags again
    if ( 0 != bad_fpu_ex )
    {
      ... use fallback calculation
      ... discard result and return error code
      ... throw exception with useful information
    }
    

    Debug code:

    _fpreset();
    _clearfp();
    Use _controlfp_s() to mask COMMON_FPU_EX and unmask BAD_FPU_EX 
    ... calculation
      "crash" in debugger on the line of code that is generating the "bad" exception.
    

    Depending on your compiler options, release builds may be using intrinsic calls to FPU ops and debug builds may call math library functions. These two methods can have significantly different error handling behavior for invalid operations like sqrt(-1.0).

    Using executables built with VS2010 on 64-bit Windows 7, I have generated slightly different double precision arithmetic values when using identical code on Win32 and x64 platforms. Even using non-optimized debug builds with /fp::precise, the fpu precision control explicitly set to _PC_53, and the fpu rounding control explicitly set to _RC_NEAR. I had to adjust some regression tests that compare double precision values to take the platform into account. I don't know if this is still an issue with VS2012, but heads up.

    0 讨论(0)
  • 2021-01-11 14:48

    I've been struggling for achieving some information about handling floating point exceptions on linux and I can tell you what I learned: There are a few ways of enabling the exception mechanism:

    1. fesetenv (FE_NOMASK_ENV); enables all exceptions
    2. feenableexcept(FE_ALL_EXCEPT );
     fpu_control_t fw;
     _FPU_GETCW(fw);
     fw |=FE_ALL_EXCEPT;
     _FPU_SETCW(fw);
    

    4.

    > fenv_t envp; include bits/fenv.h   
    > fegetenv(&envp);    
     envp.__control_word |= ~_FPU_MASK_OM;   
    > fesetenv(&envp);
    

    5.

    > fpu_control_t cw;
    > __asm__ ("fnstcw %0" : "=m" (*&cw));get config word
    >cw |= ~FE_UNDERFLOW;
    > __asm__ ("fldcw %0" : : "m" (*&cw));write config word
    

    6.C++ mode: std::feclearexcept(FE_ALL_EXCEPT);

    There are some useful links : http://frs.web.cern.ch/frs/Source/MAC_headers/fpu_control.h http://en.cppreference.com/w/cpp/numeric/fenv/fetestexcept http://technopark02.blogspot.ro/2005/10/handling-sigfpe.html

    0 讨论(0)
  • 2021-01-11 15:00

    Most of the the following information comes from Bruce Dawson's blog post on the subject (link).

    Since you're working with C++, you can create a RAII class that enables or disables floating point exceptions in a scoped manner. This lets you have greater control so that you're only exposing the exception state to your code, rather than manually managing calling _controlfp_s() yourself. In addition, floating point exception state that is set this way is system wide, so it's really advisable to remember the previous state of the control word and restore it when needed. RAII can take care of this for you and is a good solution for the issues with GDI+ that you're describing.

    The exception flags _EM_OVERFLOW, _EM_ZERODIVIDE, and _EM_INVALID are the most important to account for. _EM_OVERFLOW is raised when positive or negative infinity is the result of a calculation, whereas _EM_INVALID is raised when a result is a signaling NaN. _EM_UNDERFLOW is safe to ignore; it signals when your computation result is non-zero and between -FLT_MIN and FLT_MIN (in other words, when you generate a denormal). _EM_INEXACT is raised too frequently to be of any practical use due to the nature of floating point arithmetic, although it can be informative if trying to track down imprecise results in some situations.

    SIMD code adds more wrinkles to the mix; since you don't indicate using SIMD explicitly I'll leave out a discussion of that except to note that specifying anything other than /fp:fast can disable automatic vectorization of your code in VS 2012; see this answer for details on this.

    0 讨论(0)
提交回复
热议问题