Enabling floating point interrupts on Mac OS X Intel
Asked Answered
D

2

19

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.

Discernment answered 29/10, 2008 at 14:41 Comment(0)
D
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);
Discernment answered 4/12, 2008 at 14:6 Comment(0)
F
7

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.

Fitzsimmons answered 31/10, 2008 at 4:23 Comment(2)
This maybe was not so relevant in 2008, but it's worth noting that 64 bit code uses the SSE unit rather than x87 FP unit on all platforms (not only OS X).Trucking
@Trucking on Linux long double requires x87 FPU. Similarly on Win64 with MinGW-W64.Theona

© 2022 - 2024 — McMap. All rights reserved.