SIGFPE(3) — C LIBRARY FUNCTIONS
NAME
sigfpe - signal handling for specific SIGFPE codes
SYNOPSIS
#include <signal.h>
#include <floatingpoint.h>
sigfpe_handler_type sigfpe(code, hdl)
sigfpe_code_type code;
sigfpe_handler_type hdl;
DESCRIPTION
This function allows signal handling to be specified for particular SIGFPE codes. A call to sigfpe defines a new handler hdl for a particular SIGFPE code and returns the old handler as the value of the function sigfpe. Normally handlers are specified as pointers to functions; the special cases SIGFPE_IGNORE, SIGFPE_ABORT, and SIGFPE_DEFAULT allow ignoring, specifying core dump via abort(3), or default handling respectively.
For these IEEE-related codes:
FPE_FLTINEX_TRAPfp_inexact - floating inexact result
FPE_FLTDIV_TRAPfp_division - floating division by zero
FPE_FLTUND_TRAPfp_underflow - floating underflow
FPE_FLTOVF_TRAPfp_overflow - floating overflow
FPE_FLTBSUN_TRAPfp_invalid - branch or set on unordered
FPE_FLTOPERR_TRAPfp_invalid - floating operand error
FPE_FLTNAN_TRAPfp_invalid - floating Not-A-Number
default handling is defined to be to call the handler specified to ieee_handler(3M).
For all other SIGFPE codes, default handling is to core dump via abort(3).
The compilation option -ffpa causes fpa recomputation to replace the default abort action for code FPE_FPA_ERROR. Note that SIGFPE_DEFAULT will restore abort rather than FPA recomputation for this code.
Three steps are required to intercept IEEE-related SIGFPE codes with sigfpe.
1)Set up a handler with sigfpe.
2)Enable the relevant IEEE trapping capability in the hardware, perhaps by using assembly-language instructions.
3)Cause the appropriate IEEE exception to occur in a floating-point operation.
Unlike ieee_handler(3M), sigfpe never changes floating-point hardware mode bits affecting IEEE trapping. No IEEE-related SIGFPE signals will be generated unless those hardware mode bits are enabled.
SIGFPE signals can be handled using sigvec(2), signal(3), signal(3F), sigfpe(3), or ieee_handler(3M). In a particular program, to avoid confusion, use only one of these interfaces to handle SIGFPE signals.
DIAGNOSTICS
Sigfpe returns BADSIG if code is not zero or a defined SIGFPE code.
EXAMPLE
A user-specified signal handler might look like this:
int sample_handler ( sig, code, scp )
int sig ;/∗ sig == SIGFPE always ∗/
int code ;
struct sigcontext ∗scp ;
{
/∗
Sample user-written sigfpe code handler.
Prints a message and continues.
struct sigcontext is defined in <signal.h>.
∗/
printf(" ieee exception code %x occurred at pc %X \n",code,scp->sc_pc);
}
and it might be set up like this:
extern int sample_handler();
main()
{
sigfpe_handler_type hdl, old_handler1, old_handler2;
/∗
∗ save current overflow and invalid handlers; set the new
∗ overflow handler to sample_handler() and set the new
∗ invalid handler to SIGFPE_ABORT (abort on invalid)
∗/
hdl = (sigfpe_handler_type) sample_handler;
old_handler1 = sigfpe(FPE_FLTOVF_TRAP, hdl);
old_handler2 = sigfpe(FPE_FLTOPERR_TRAP, SIGFPE_ABORT);
...
/∗
∗ restore old overflow and invalid handlers
∗/
sigfpe(FPE_FLTOVF_TRAP, old_handler1);
sigfpe(FPE_FLTOPERR_TRAP, old_handler2);
}
FILES
/usr/include/floatingpoint.h
/usr/include/signal.h
SEE ALSO
floatingpoint(3), signal(3), ieee_handler(3M), abort(3)
Sun Release 3.2 — Last change: 20 March 1987