Mathematical Library Functions fexsethandling(3M)
NAME
fexsethandling, fexgethandling, fexgetexcepthandler,
fexsetexcepthandler - control floating point exception han-
dling modes
SYNOPSIS
c99 [ flag... ] file... -lm [ library... ]
#include
int fexsethandling(int ex, int mode, void(*handler);
int fexgethandling(int ex);
void fexgetexcepthandler(fexhandlert *buf, int ex);
void fexsetexcepthandler(const fexhandlert *buf, int ex);
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 :
FEXINEXACT
FEXUNDERFLOW
FEXOVERFLOW
FEXDIVBYZERO division by zero
FEXINVZDZ 0/0 invalid operation
FEXINVIDI infinity/infinity invalid operation
FEXINVISI infinity-infinity invalid operation
FEXINVZMI 0*infinity invalid operation
SunOS 5.11 Last change: 12 Jul 2006 1
Mathematical Library Functions fexsethandling(3M)
FEXINVSQRT square root of negative operand
FEXINVSNAN signaling NaN
FEXINVINT invalid integer conversion
FEXINVCMP invalid comparison
For convenience, the following combinations of values are
also defined:
FEXNONE no exceptions
FEXINVALID all invalid operation exceptions
FEXCOMON overflow, division by zero, and invalid
operation
FEXAL all exceptions
The fexsethandling() 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:
FEXNOHANDLER Trap but do not otherwise handle the excep-
tion, 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.
FEXNONSTOP Provide the IE 754 default result for the
operation that caused the exception, set
the exception's flag, and continue execu-
tion. This is the default behavior when the
exception's trap is disabled. The handler
parameter is ignored.
SunOS 5.11 Last change: 12 Jul 2006 2
Mathematical Library Functions fexsethandling(3M)
FEXABORT Call abort(3C). The handler parameter is
ignored.
FEXSIGNAL Invoke the function *handler with the
parameters normally supplied to a signal
handler installed with sigfpe(3C).
FEXCUSTOM Invoke the function *handler as described
in the next paragraph.
In FEXCUSTOM mode, when a floating point exception occurs,
the handler function is invoked as though its prototype
were:
#include
void handler(int ex, fexinfot *info);
On entry, ex is the value (of the first twelve listed above)
corresponding to the exception that occurred, info->op indi-
cates the operation that caused the exception, info->op1 and
info->op2 contain the values of the operands, info->res con-
tains 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 excep-
tion 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 = fexnodata;
which causes the exponent-adjusted result specified by IE
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 FEXCUSTOM handler, in
some cases, one or both operands may not be. In these
cases, the handler may be invoked with info->op1.type ==
SunOS 5.11 Last change: 12 Jul 2006 3
Mathematical Library Functions fexsethandling(3M)
fexnodata or info->op2.type == fexnodata to indicate that
the respective data structures do not contain valid data.
(For example, info->op2.type == fexnodata if the excep-
tional 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 fexgethandling() function returns the current handling
mode for the exception specified by ex, which must be one of
the first twelve exceptions listed above.
The fexgetexcepthandler() function saves the current han-
dling modes and associated data for the exceptions specified
by ex in the data structure pointed to by buf. The type
fexhandlert is defined in .
The fexsetexcepthandler() function restores the handling
modes and associated data for the exceptions specified by ex
from the data structure pointed to by buf. This data struc-
ture must have been set by a previous call to
fexgetexcepthandler(). Otherwise the effect on the indi-
cated modes is undefined.
RETURN VALUES
The fexsethandling() function returns a non-zero value if
the requested exception handling mode is established. Other-
wise, it returns 0.
EXAMPLES
The following example demonstrates how to substitute a
predetermined value for the result of a 0/0 invalid opera-
tion.
#include
#include
double k;
void presub(int ex, fexinfot *info) {
info->res.type = fexdouble;
info->res.val.d = k;
}
int main() {
double x, w;
int i;
fexhandlert buf;
/*
SunOS 5.11 Last change: 12 Jul 2006 4
Mathematical Library Functions fexsethandling(3M)
* save current 0/0 handler
*/
(void) fexgetexcepthandler(&buf, FEXINVZDZ);
/*
* set up presubstitution handler for 0/0
*/
(void) fexsethandling(FEXINVZDZ, FEXCUSTOM, presub);
/*
* compute (k*x)/sin(x) for k=2.0, x=0.5, 0.4, ..., 0.1, 0.0
*/
k = 2.0;
(void) printf("Evaluating f(x) = (k*x)/sin(x)\n\n");
for (i = 5; i >= 0; i--) {
x = (double) i * 0.1;
w = (k * x) / sin(x);
(void) printf("\tx=%3.3f\t f(x) = % 1.20e\n", x, w);
}
/*
* restore old 0/0 handler
*/
(void) fexsetexcepthandler(&buf, FEXINVZDZ);
return 0;
}
The output from the preceding program reads:
Evaluating f(x) = (k*x)/sin(x)
x=0.500 f(x) = 2.08582964293348816000e]00
x=0.400 f(x) = 2.05434596443822626000e]00
x=0.300 f(x) = 2.03031801709447368000e]00
x=0.200 f(x) = 2.01339581906893761000e]00
x=0.100 f(x) = 2.00333722632695554000e]00
x=0.000 f(x) = 2.00000000000000000000e]00
When x = 0, f(x) is computed as 0/0 and an invalid operation
exception occurs. In this example, the value 2.0 is substi-
tuted for the result.
ATRIBUTES
See attributes(5) for descriptions of the following attri-
butes:
SunOS 5.11 Last change: 12 Jul 2006 5
Mathematical Library Functions fexsethandling(3M)
ATRIBUTE TYPE ATRIBUTE VALUE
Availability SUNWlibms, SUNWlmxs
Interface Stability Stable
MT-Level MT-Safe (see Notes)
SEE ALSO
sigfpe(3C), feclearexcept(3M), fegetenv(3M),
fexsetlog(3M), attributes(5)
Numerical Computation Guide
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
FEXCUSTOM mode is invoked. When the SIGFPE signal is
blocked, as it is when such a handler is invoked, the float-
ing point environment, exception flags, and retrospective
diagnostic functions described in feclearexcept(3M),
fegetenv(3M), and fexsetlog(3M) do not re-enable traps.
Thus, the handler itself always runs in FEXNONSTOP mode
with logging of retrospective diagnostics disabled.
Attempting to change these modes within the handler may not
produce the expected results.
SunOS 5.11 Last change: 12 Jul 2006 6
|