Enabling floating point interrupts on Mac OS X Intel

前端 未结 2 842
隐瞒了意图╮
隐瞒了意图╮ 2020-12-05 14:44

On Linux, feenableexcept and fedisableexcept can be used to control the generation of SIGFPE interrupts on floating point exceptions. How can I do this on Mac OS X Intel?

相关标签:
2条回答
  • 2020-12-05 15:25

    Exceptions for sse can be enabled using _MM_SET_EXCEPTION_MASK from xmmintrin.h. For example, to enable invalid (nan) exceptions, do

    #include <xmmintrin.h>
    ...
    _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() & ~_MM_MASK_INVALID);
    
    0 讨论(0)
  • 2020-12-05 15:31

    On Mac OS X this is moderately complicated. OS X uses the SSE unit for all FP math by default, not the x87 FP unit. The SSE unit does not honor the interrupt options, so that means that in addition to enabling interrupts, you need to make sure to compile all your code not to use SSE math.

    You can disable the math by adding "-mno-sse -mno-sse2 -mno-sse3" to your CFLAGS. Once you do that you can use some inline assembly to configure your FP exceptions, with basically the same flags as Linux.

    short fpflags = 0x1332 // Default FP flags, change this however you want. 
    asm("fnclex");
    asm("fldcw _fpflags");
    

    The one catch you may find is that since OS X is built entirely using sse there may be uncaught bugs. I know there used to be a big with the signal handler not passing back the proper codes, but that was a few years ago, hopefully it is fixed now.

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