| United States-English |
|
|
|
![]() |
HP-UX Floating-Point Guide: HP 9000 Computers > Chapter 6 Floating-Point Trap HandlingHandling Traps |
|
Once you have enabled traps, either by a compiler option or by a call to a routine, you need a mechanism for handling them when they occur. It may be convenient simply to have your program abort (particularly if you enable traps with a method that does not cause a core dump). You may, however, prefer to establish an error-handling routine that generates a helpful error message and exits the program gracefully. HP 9000 systems provide the following methods of handling traps:
We discuss these briefly in the following subsections. If you want to continue execution after handling a trap, do so with caution. The result of the calculation that encountered the exception is likely to be useless, and you will probably want to substitute for it a carefully chosen value that will be meaningful in subsequent operations. Be particularly cautious about continuing execution if you use the C sigaction(2) function for error handling. Floating-point exceptions are hardware exceptions, whereas an error-handling process (such as the C sigaction(2) function or the HP Fortran ON statement) occurs in software. A hardware exception usually causes an operation to be interrupted at a point when values are stored in registers but not yet stored in memory. If you use sigaction(2) to try to substitute a new value for the one that caused the error, chances are you will return to a point later in the instruction sequence than the point at which the error occurred, so some essential steps may be eliminated and the register values may be invalid. This particular problem does not occur with the HP Fortran ON statement; the software updates the registers appropriately. The ON statement may be used to handle arithmetic exceptions in both HP Fortran 90 and HP FORTRAN/9000. In HP FORTRAN/9000 the statement must be used in conjunction with the +T compiler option. In HP Fortran 90 it may be used in conjunction with the +fp_exception option, but this is not required. The ON statement allows you to specify a particular action to be taken when a particular exception arises. The action may be any of the following:
You can use the ON statement to handle any of the following exceptions:
For example, the following program calls the subroutine HANDLE_OFL to handle a DOUBLE PRECISION overflow. The subroutine prints a message describing the error, prints the biased value passed to the subroutine, then the correct value, and exits. Example 6-3 Sample Program: overflow_on.f
See the PA-RISC 1.1 Architecture and Instruction Set Reference Manual for a full explanation of the bias of 1536; when traps are enabled, the IEEE standard requires a biased result to be returned for overflow and underflow. If you compile and run this program using HP Fortran 90 and HP FORTRAN/9000 respectively, it produces the following result:
One situation where it is useful to assign a value to the trap handler argument and continue program execution is that of an underflow exception (described in “Underflow Conditions”). Substituting a value of 0 for the result of an operation that underflows may be exactly what you want to do. In fact, the system may perform this substitution for you; the IEEE standard specifies that 0 may be the result of an operation that underflows, and on HP 9000 systems it often is (when the result of the operation is less than the smallest denormalized value). If you want to guarantee a result of 0, you can call a handler as follows: Example 6-4 Sample Program: underflow_on.f
If you compile and run this program, it produces the following result:
If your system supports fast underflow mode, you can use it both to guarantee a result of 0 for all underflows and to avoid the overhead of incurring a trap. You can enable fast underflow mode with either the +FP option (see “Command-Line Mode Control: The +FP Compiler Option”) or the fesetflushtozero routine (see “Underflow Mode: fegetflushtozero and fesetflushtozero”). The ON statement is documented fully in the HP Fortran 90 Programmer's Reference and the HP FORTRAN/9000 Programmer's Guide. For C programs, the standard method of handling errors is to use the sigaction(2) function. The function establishes the address of a signal-handling function that is called whenever the specified HP-UX signal is raised. The major problem with this method is that there is only one HP-UX signal, SIGFPE, for all IEEE and integer exceptions. When SIGFPE is raised, the only way to find out which exception generated the signal is to examine the floating-point exception registers, which can be obtained from the sigcontext structure that is an argument to the sigaction(2) function. (Or you can enable a trap for only one exception.) The following C program uses fesettrapenable to enable a trap for the overflow exception, and calls sigaction(2) to specify that the function handle_sigfpe is to be called when SIGFPE is raised. Example 6-5 Sample Program: overflow_sig.c
Example 6-6 Sample Program: overflow_sig.c (Cont.)
If you compile and run this program, it produces a result like the following:
The code 14 signifies an assist exception trap (see signal(5)), which indicates a floating-point exception. The main advantage of using a signal handler is that it eliminates the core dump that you get without it if you compile with +FP or use fesettrapenable. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||