2

如何在MinGW GCC上启用浮点异常,其中fenableexcept缺失?即使是相当完整的解决方案实际上也没有抓住这一点,尽管他们似乎打算这样做。我更喜欢与未来出现的任何标准接近的最少代码。最好,代码应该在有和没有 SSE 的情况下工作。一个完整的解决方案,展示如何启用硬件信号,捕捉它,并重置它是可取的。必须使用高优化级别和完整的迂腐错误和警告进行干净编译。在单元测试场景中多次捕获的能力很重要。有几个问题提供了 部分 答案

4

1 回答 1

1

这似乎适用于我的机器。在 MinGW GCC 中使用-fnon-call-exceptions. 它还没有完全最小化。

#include <xmmintrin.h>
#include <cerrno>
#include <cfenv>
#include <cfloat> //or #include <float.h> // defines _controlfp_s
#include <cmath>
#include <csignal>

#ifdef _WIN32
void feenableexcept(uint16_t fpflags){
 /*edit 2015-12-17, my attempt at ASM code was giving me
  *problems in more complicated scenarios, so I
  *switched to using _controlfp_s. I finally posted it here
  *because of the upvote to the ASM version.*/
 /*{// http://stackoverflow.com/questions/247053/
  uint16_t mask(FE_ALL_EXCEPT & ~fpflags);
  asm("fldcw %0" : : "m" (mask) : "cc");
 } //https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html   */
 unsigned int new_word(0);
 if (fpflags & FE_INVALID) new_word |= _EM_INVALID;
 if (fpflags & FE_DIVBYZERO) new_word |= _EM_ZERODIVIDE;
 if (fpflags & FE_OVERFLOW) new_word |= _EM_OVERFLOW;
 unsigned int cw(0);
 _controlfp_s(&cw,~new_word,_MCW_EM);
}
#endif

void fe_reset_traps(){
 std::feclearexcept(FE_ALL_EXCEPT); //clear x87 FE state
#ifdef __SSE__
 _MM_SET_EXCEPTION_STATE(0); // clear SSE FE state
#endif
 feenableexcept(FE_DIVBYZERO|FE_OVERFLOW|FE_INVALID); // set x87 FE mask
#ifdef __SSE__
 //set SSE FE mask (orientation of this command is different than the above)
 _MM_SET_EXCEPTION_MASK(_MM_MASK_DENORM |_MM_MASK_UNDERFLOW|_MM_MASK_INEXACT);
#endif
}

void sigfpe_handler(int sig){
 std::signal(sig,SIG_DFL); // block signal, if needed
 std::cerr<<"A floating point exception was encountered. Exiting.\n";
 fe_reset_traps(); // in testing mode the throw may not exit, so reset traps
 std::signal(sig,&sigfpe_handler); // reinstall handler
 throw std::exception();
}

fe_reset_traps();
std::signal(SIGFPE,&sigfpe_handler); // install handler
std::cerr<<"before\n";
std::cerr<<1.0/0.0<<"\n";
std::cerr<<"should be unreachable\n";

我敢肯定它并不完美。让我们听听其他人必须做出什么贡献。

于 2015-05-11T18:57:54.607 回答