Description

These functions provide control of floating point exception handling modes. For each
function, the ex argument specifies one or more exceptions indicated by a
bitwise-OR of any of the following values defined in <fenv.h>:

FEX_INEXACT

FEX_UNDERFLOW

FEX_OVERFLOW

FEX_DIVBYZERO

division by zero

FEX_INV_ZDZ

0/0 invalid operation

FEX_INV_IDI

infinity/infinity invalid operation

FEX_INV_ISI

infinity–infinity invalid operation

FEX_INV_ZMI

0*infinity invalid operation

FEX_INV_SQRT

square root of negative operand

FEX_INV_SNAN

signaling NaN

FEX_INV_INT

invalid integer conversion

FEX_INV_CMP

invalid comparison

For convenience, the following combinations of values are also defined:

FEX_NONE

no exceptions

FEX_INVALID

all invalid operation exceptions

FEX_COMMON

overflow, division by zero, and invalid operation

FEX_ALL

all exceptions

The fex_set_handling() function establishes the specified mode for handling the floating point
exceptions identified by ex. The selected mode determines the action to
be taken when one of the indicated exceptions occurs. It must be
one of the following values:

FEX_NOHANDLER

Trap but do not otherwise handle the exception, evoking instead whatever ambient behavior would normally be in effect. This is the default behavior when the exception's trap is enabled. The handler parameter is ignored.

FEX_NONSTOP

Provide the IEEE 754 default result for the operation that caused the exception, set the exception's flag, and continue execution. This is the default behavior when the exception's trap is disabled. The handler parameter is ignored.

Invoke the function *handler with the parameters normally supplied to a signal handler installed with sigfpe(3C).

FEX_CUSTOM

Invoke the function *handler as described in the next paragraph.

In FEX_CUSTOM mode, when a floating point exception occurs, the handler function
is invoked as though its prototype were:

#include <fenv.h>
void handler(int ex, fex_info_t *info);

On entry, ex is the value (of the first twelve listed above)
corresponding to the exception that occurred, info->op indicates the operation that caused
the exception, info->op1 and info->op2 contain the values of the operands, info->res contains
the default untrapped result value, and info->flags reflects the exception flags that
the operation would have set had it not been trapped. If
the handler returns, the value contained in info->res on exit is substituted for
the result of the operation, the flags indicated by info->flags are set,
and execution resumes at the point where the exception occurred. The
handler might modify info->res and info->flags to supply any desired result value
and flags. Alternatively, if the exception is underflow or overflow, the hander
might set

info->res.type = fex_nodata;

which causes the exponent-adjusted result specified by IEEE 754 to be substituted.
If the handler does not modify info->res or info->flags, the effect
is the same as if the exception had not been trapped.

Although the default untrapped result of an exceptional operation is always available
to a FEX_CUSTOM handler, in some cases, one or both operands may
not be. In these cases, the handler may be invoked with
info->op1.type == fex_nodata or info->op2.type == fex_nodata to indicate that the respective data structures do not
contain valid data. (For example, info->op2.type == fex_nodata if the exceptional operation is
a unary operation.) Before accessing the operand values, a custom handler
should always examine the type field of the operand data structures to
ensure that they contain valid data in the appropriate format.

The fex_get_handling() function returns the current handling mode for the exception specified
by ex, which must be one of the first twelve exceptions listed
above.

The fex_getexcepthandler() function saves the current handling modes and associated data for
the exceptions specified by ex in the data structure pointed to by
buf. The type fex_handler_t is defined in <fenv.h>.

The fex_setexcepthandler() function restores the handling modes and associated data for the
exceptions specified by ex from the data structure pointed to by buf.
This data structure must have been set by a previous call
to fex_getexcepthandler(). Otherwise the effect on the indicated modes is undefined.

Return Values

The fex_set_handling() function returns a non-zero value if the requested exception handling
mode is established. Otherwise, it returns 0.

Examples

The following example demonstrates how to substitute a predetermined value for the
result of a 0/0 invalid operation.

See Also

Notes

In a multithreaded application, the preceding functions affect exception handling modes only
for the calling thread.

The functions described on this page automatically install and deinstall SIGFPE handlers
and set and clear the trap enable mode bits in the floating
point status register as needed. If a program uses these functions
and attempts to install a SIGFPE handler or control the trap enable mode
bits independently, the resulting behavior is not defined.

All traps are disabled before a handler installed in FEX_CUSTOM mode is
invoked. When the SIGFPE signal is blocked, as it is when
such a handler is invoked, the floating point environment, exception flags, and
retrospective diagnostic functions described in feclearexcept(3M), fegetenv(3M), and fex_set_log(3M) do not re-enable
traps. Thus, the handler itself always runs in FEX_NONSTOP mode with
logging of retrospective diagnostics disabled. Attempting to change these modes within
the handler may not produce the expected results.