Handling Floating-Point exceptions in C++

后端 未结 3 1181
独厮守ぢ
独厮守ぢ 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: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

提交回复
热议问题