/* This code is provided only as a guide to writing your own SIGFPE signal handler. Writing a complete SIGFPE signal handler is more complex than the sample code below. */ #include #include #include #include #include #include void handler(int sig, int code, struct sigcontext *sc) { /* Examine the cause or flag bits to figure out what floating point exception occurred - use sc->sc_fpc_csr. Locate the pc and fetch the instruction that caused the fpe - use sc->sc_pc. Make sure we were really doing a FPU operation by checking the operation code located at address sc->sc_pc. Determine what action to take for the fpe. Make an adjustment to the operands or the result - use sc->sc_fregs. Figure out where to restart the process that caused the fpe. This is accomplished by updating sc->sc_pc. Account for the case when the exception occurs in the shadow of a branch or jump instruction. */ } main() { double operandx,operandy,result; union fpc_csr exp; int i; /* set the FCSR bits to trap all floating point exceptions */ exp.fc_word = get_fpc_csr(); exp.fc_struct.en_invalid = 1; exp.fc_struct.en_divide0 = 1; exp.fc_struct.en_overflow = 1; exp.fc_struct.en_underflow= 1; exp.fc_struct.en_inexact = 1; i = set_fpc_csr(exp); /* establish handler for SIGFPE signal */ sigset(SIGFPE,handler); result = 0.; /* Generate a divide by zero. This invokes handler(). */ operandx = 10000.; operandy = 0.; result = operandx / operandy; /* Generate an overflow. This invokes handler(). */ operandx = -10000.; operandy = DBL_MAX; result = operandx * operandy; /* Generate an underflow. This invokes handler(). */ operandx = 1.; operandy = DBL_MAX; result = operandx/operandy; /* Generate an invalid operation. This invokes handler(). */ operandx = 0.; operandy = 0.; result = operandx/operandy; /* Generate an inexact. This invokes handler(). */ operandx = 0.1; operandy = 0.1; result = operandx * operandy; exit(1); }