2 .\" Copyright (c) 2006, Sun Microsystems, Inc. All Rights Reserved.
3 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
4 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
5 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
6 .TH FEX_SET_HANDLING 3M "Jul 12, 2006" "SunOS 5.11" "Mathematical Library Functions"
8 fex_set_handling, fex_get_handling, fex_getexcepthandler, fex_setexcepthandler
9 \- control floating point exception handling modes
13 c99 [ \fIflag\fR... ] \fIfile\fR... -lm [ \fIlibrary\fR... ]
16 \fBint\fR \fBfex_set_handling\fR(\fBint\fR \fIex\fR, \fBint\fR \fImode\fR, \fBvoid(*\fR\fIhandler\fR);
21 \fBint\fR \fBfex_get_handling\fR(\fBint\fR \fIex\fR);
26 \fBvoid\fR \fBfex_getexcepthandler\fR(\fBfex_handler_t *\fR\fIbuf\fR, \fBint\fR \fIex\fR);
31 \fBvoid\fR \fBfex_setexcepthandler\fR(\fBconst fex_handler_t *\fR\fIbuf\fR, \fBint\fR \fIex\fR);
37 These functions provide control of floating point exception handling modes. For
38 each function, the \fIex\fR argument specifies one or more exceptions indicated
39 by a bitwise-OR of any of the following values defined in <\fBfenv.h\fR>:
44 \fB\fBFEX_INEXACT\fR\fR
55 \fB\fBFEX_UNDERFLOW\fR\fR
66 \fB\fBFEX_OVERFLOW\fR\fR
77 \fB\fBFEX_DIVBYZERO\fR\fR
88 \fB\fBFEX_INV_ZDZ\fR\fR
99 \fB\fBFEX_INV_IDI\fR\fR
103 infinity/infinity invalid operation
110 \fB\fBFEX_INV_ISI\fR\fR
114 infinity-infinity invalid operation
121 \fB\fBFEX_INV_ZMI\fR\fR
125 0*infinity invalid operation
132 \fB\fBFEX_INV_SQRT\fR\fR
136 square root of negative operand
143 \fB\fBFEX_INV_SNAN\fR\fR
154 \fB\fBFEX_INV_INT\fR\fR
158 invalid integer conversion
165 \fB\fBFEX_INV_CMP\fR\fR
174 For convenience, the following combinations of values are also defined:
190 \fB\fBFEX_INVALID\fR\fR
194 all invalid operation exceptions
201 \fB\fBFEX_COMMON\fR\fR
205 overflow, division by zero, and invalid operation
221 The \fBfex_set_handling()\fR function establishes the specified \fImode\fR for
222 handling the floating point exceptions identified by \fIex\fR. The selected
223 \fImode\fR determines the action to be taken when one of the indicated
224 exceptions occurs. It must be one of the following values:
229 \fB\fBFEX_NOHANDLER\fR\fR
233 Trap but do not otherwise handle the exception, evoking instead whatever
234 ambient behavior would normally be in effect. This is the default behavior
235 when the exception's trap is enabled. The \fIhandler\fR parameter is ignored.
242 \fB\fBFEX_NONSTOP\fR\fR
246 Provide the IEEE 754 default result for the operation that caused the
247 exception, set the exception's flag, and continue execution. This is the
248 default behavior when the exception's trap is disabled. The \fIhandler\fR
249 parameter is ignored.
256 \fB\fBFEX_ABORT\fR\fR
260 Call \fBabort\fR(3C). The \fIhandler\fR parameter is ignored.
267 \fB\fBFEX_SIGNAL\fR\fR
271 Invoke the function *\fIhandler\fR with the parameters normally supplied to a
272 signal handler installed with \fBsigfpe\fR(3C).
279 \fB\fBFEX_CUSTOM\fR\fR
283 Invoke the function *\fIhandler\fR as described in the next paragraph.
288 In \fBFEX_CUSTOM\fR mode, when a floating point exception occurs, the handler
289 function is invoked as though its prototype were:
294 void handler(int ex, fex_info_t *info);
300 On entry, \fIex\fR is the value (of the first twelve listed above)
301 corresponding to the exception that occurred, \fBinfo->op\fR indicates the
302 operation that caused the exception, \fBinfo->op1\fR and \fBinfo->op2\fR
303 contain the values of the operands, \fBinfo->res\fR contains the default
304 untrapped result value, and \fBinfo->flags\fR reflects the exception flags that
305 the operation would have set had it not been trapped. If the handler returns,
306 the value contained in \fBinfo->res\fR on exit is substituted for the result of
307 the operation, the flags indicated by \fBinfo->flags\fR are set, and execution
308 resumes at the point where the exception occurred. The handler might modify
309 \fBinfo->res\fR and \fBinfo->flags\fR to supply any desired result value and
310 flags. Alternatively, if the exception is underflow or overflow, the hander
314 info->res.type = fex_nodata;
317 which causes the exponent-adjusted result specified by IEEE 754 to be
318 substituted. If the handler does not modify \fBinfo->res\fR or
319 \fBinfo->flags\fR, the effect is the same as if the exception had not been
323 Although the default untrapped result of an exceptional operation is always
324 available to a \fBFEX_CUSTOM\fR handler, in some cases, one or both operands
325 may not be. In these cases, the handler may be invoked with \fBinfo->op1.type
326 == fex_nodata\fR or \fBinfo->op2.type == fex_nodata\fR to indicate that the
327 respective data structures do not contain valid data. (For example,
328 \fBinfo->op2.type == fex_nodata\fR if the exceptional operation is a unary
329 operation.) Before accessing the operand values, a custom handler should
330 always examine the \fBtype\fR field of the operand data structures to ensure
331 that they contain valid data in the appropriate format.
334 The \fBfex_get_handling()\fR function returns the current handling mode for the
335 exception specified by \fIex\fR, which must be one of the first twelve
336 exceptions listed above.
339 The \fBfex_getexcepthandler()\fR function saves the current handling modes and
340 associated data for the exceptions specified by \fIex\fR in the data structure
341 pointed to by \fIbuf\fR. The type \fBfex_handler_t\fR is defined in
345 The \fBfex_setexcepthandler()\fR function restores the handling modes and
346 associated data for the exceptions specified by \fIex\fR from the data
347 structure pointed to by \fIbuf\fR. This data structure must have been set by a
348 previous call to \fBfex_getexcepthandler()\fR. Otherwise the effect on the
349 indicated modes is undefined.
353 The \fBfex_set_handling()\fR function returns a non-zero value if the requested
354 exception handling mode is established. Otherwise, it returns 0.
358 The following example demonstrates how to substitute a predetermined value for
359 the result of a 0/0 invalid operation.
368 void presub(int ex, fex_info_t *info) {
369 info->res.type = fex_double;
378 * save current 0/0 handler
380 (void) fex_getexcepthandler(&buf, FEX_INV_ZDZ);
382 * set up presubstitution handler for 0/0
384 (void) fex_set_handling(FEX_INV_ZDZ, FEX_CUSTOM, presub);
386 * compute (k*x)/sin(x) for k=2.0, x=0.5, 0.4, ..., 0.1, 0.0
389 (void) printf("Evaluating f(x) = (k*x)/sin(x)\en\en");
390 for (i = 5; i >= 0; i--) {
391 x = (double) i * 0.1;
392 w = (k * x) / sin(x);
393 (void) printf("\etx=%3.3f\et f(x) = % 1.20e\en", x, w);
396 * restore old 0/0 handler
398 (void) fex_setexcepthandler(&buf, FEX_INV_ZDZ);
406 The output from the preceding program reads:
410 Evaluating f(x) = (k*x)/sin(x)
412 x=0.500 f(x) = 2.08582964293348816000e+00
413 x=0.400 f(x) = 2.05434596443822626000e+00
414 x=0.300 f(x) = 2.03031801709447368000e+00
415 x=0.200 f(x) = 2.01339581906893761000e+00
416 x=0.100 f(x) = 2.00333722632695554000e+00
417 x=0.000 f(x) = 2.00000000000000000000e+00
423 When \fIx\fR = 0, \fIf(x)\fR is computed as 0/0 and an invalid operation
424 exception occurs. In this example, the value 2.0 is substituted for the
429 See \fBattributes\fR(5) for descriptions of the following attributes:
438 ATTRIBUTE TYPE ATTRIBUTE VALUE
439 Availability SUNWlibms, SUNWlmxs
440 Interface Stability Stable
441 MT-Level MT-Safe (see Notes)
447 \fBsigfpe\fR(3C), \fBfeclearexcept\fR(3M), \fBfegetenv\fR(3M),
448 \fBfex_set_log\fR(3M), \fBattributes\fR(5)
451 \fINumerical Computation Guide\fR
455 In a multithreaded application, the preceding functions affect exception
456 handling modes only for the calling thread.
459 The functions described on this page automatically install and deinstall
460 \fBSIGFPE\fR handlers and set and clear the trap enable mode bits in the
461 floating point status register as needed. If a program uses these functions
462 and attempts to install a \fBSIGFPE\fR handler or control the trap enable mode
463 bits independently, the resulting behavior is not defined.
466 All traps are disabled before a handler installed in \fBFEX_CUSTOM\fR mode is
467 invoked. When the \fBSIGFPE\fR signal is blocked, as it is when such a handler
468 is invoked, the floating point environment, exception flags, and retrospective
469 diagnostic functions described in \fBfeclearexcept\fR(3M), \fBfegetenv\fR(3M),
470 and \fBfex_set_log\fR(3M) do not re-enable traps. Thus, the handler itself
471 always runs in \fBFEX_NONSTOP\fR mode with logging of retrospective diagnostics
472 disabled. Attempting to change these modes within the handler may not produce
473 the expected results.