Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling floating point interrupts on Mac OS X Intel

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?

Inline assembly for enabling floating point interrupts is provided in http://developer.apple.com/documentation/Performance/Conceptual/Mac_OSX_Numerics/Mac_OSX_Numerics.pdf, pp. 7-15, but only for PowerPC assembly.

like image 928
Geoffrey Irving Avatar asked Oct 29 '08 14:10

Geoffrey Irving


2 Answers

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);
like image 193
Geoffrey Irving Avatar answered Nov 04 '22 13:11

Geoffrey Irving


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.

like image 40
Louis Gerbarg Avatar answered Nov 04 '22 11:11

Louis Gerbarg