Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP-UX Floating-Point Guide: HP 9000 Computers > Chapter 6 Floating-Point Trap Handling

Enabling Traps

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

 » Index

When you enable a trap without providing a trap handler (a mechanism for handling it), the trap causes a SIGFPE signal. The signal, in turn, causes your program to abort with an error message that is more or less informative, depending on the method you use to enable the trap. If you want your program to abort when it encounters a trap, then enabling the trap may be all you want to do. However, you may want to handle the trap gracefully; see “Handling Traps” for information on trap handling.

HP 9000 systems provide several methods of enabling traps:

  • The +FP compiler option (all compilers)

  • The fesettrapenable function

  • The +fp_exception (Fortran 90) and +T (HP FORTRAN/9000) compile-line options

We discuss these briefly in the following subsections.

Using the +FP Compiler Option

The +FP option, described in “Command-Line Mode Control: The +FP Compiler Option”, allows you to enable traps from the compiler command line. No change in your program is required.

The disadvantage of using this option is that you cannot know exactly where in your program the exception occurred. Moreover, unless you enable a trap for only one exception, you cannot know the type of exception that occurred. You merely get a core dump. The following Fortran program, for example, generates an overflow.

Example 6-1 Sample Program: overflow.f

      PROGRAM OVERFLOW
DOUBLE PRECISION X, Y, Z

X = 1.79D308
Y = 2.2D-308
Z = X / Y
PRINT 30, X, Y, Z
30 FORMAT (1PE11.4, ' divided by', 1PE11.4, ' = ', 1PE11.4)
END

If you compile this program with traps disabled (the default), it produces the following output:

 1.7900+308 divided by 2.2000-308 = +INF

If you compile it with the +FP option, however, you get a stack trace and core dump. (The O flag of the +FP option enables traps for overflow exceptions.)

$ f90 +FPO overflow.f
overflow.f
program OVERFLOW

11 Lines Compiled
$ ./a.out
( 0) 0xc1602c5c traceback + 0x14 [/usr/lib/pa1.1/libcl.1]
( 1) 0xc0126500 _sigreturn [/usr/lib/libc.1]
( 2) 0x000024b4 _start + 0x6c [./a.out]
Floating exception (core dumped)

Using the fesettrapenable Function

The fesettrapenable function is part of the fenv(5) suite and is described in detail in “Exception Bits”. It is provided in the C math library.

The fesettrapenable routine can enable one or more traps in any combination.

Using fesettrapenable to enable traps has the same disadvantages as the +FP option. You get a core dump, and you cannot determine the exception type. Moreover, the use of these routines lacks the simplicity of the +FP option, because you must modify your code in order to use them.

NOTE: Do not use this function at an optimization level greater than 0. See “Run-Time Mode Control: The fenv(5) Suite”. (You may instead use the FLOAT_TRAPS_ON pragma to suppress optimization.)

Here is the program from “Using the +FP Compiler Option”, modified to enable a trap for the overflow exception using fesettrapenable. It does this by using an !$HP$ ALIAS or $ALIAS directive to tell the Fortran compiler that the function's arguments are passed by value.

Example 6-2 Sample Program: overflow_trap.f

      PROGRAM OVERFLOW_TRAP
C F90:
!$HP$ ALIAS FESETTRAPENABLE = 'fesettrapenable' (%val)
C F77:
C $ALIAS FESETTRAPENABLE = 'fesettrapenable' (%val)
PARAMETER (FE_OVERFLOW = Z'20000000')
EXTERNAL FESETTRAPENABLE
DOUBLE PRECISION X, Y, Z

CALL FESETTRAPENABLE(FE_OVERFLOW)

X = 1.79D308
Y = 2.2D-308
Z = X / Y
WRITE(*,*) X, ' divided by', Y, ' = ', Z
END

You do not need to specify any options when you compile this program, but you need to link in the C math library. The result is even less useful than in the preceding section:

$ f90 overflow_trap.f -lm
overflow_trap.f
program OVERFLOW_TRAP

13 Lines Compiled
$ ./a.out
Floating exception (core dumped)

Using the +fp_exception or +T Compiler Option (Fortran only)

The +fp_exception option, available with the HP Fortran 90 compiler, enables traps for four IEEE exceptions: invalid operation, division by zero, overflow, and underflow. (Few programs need to trap for the inexact exception, which occurs often and conveys little information.) The HP FORTRAN/9000 equivalent is +T. For Fortran applications, these options have the simplicity of +FP. They also provides more information than either +FP or fesettrapenable. They tell what kind of exception occurred and the virtual address of the statement that triggered the exception. Moreover, they do not cause a core dump.

For example, if you use +fp_exception or +T to compile the program in “Using the +FP Compiler Option”, it produces a result like the following:

$ f77 +T overflow.f 
overflow.f:
MAIN overflow:
$ ./a.out
PROGRAM ABORTED : IEEE overflow

PROCEDURE TRACEBACK:

( 0) 0x000024e8 _start + 0x88 [./a.out]

The effect is similar with +fp_exception:

$ f90 +fp_exception overflow.f
overflow.f
program OVERFLOW

11 Lines Compiled
$ ./a.out
PROGRAM ABORTED : IEEE overflow

PROCEDURE TRACEBACK:

( 0) 0x000024ec _start + 0x74 [././a.out]

See “Command-Line Mode Control: The +FP Compiler Option” for information about how +fp_exception and +T interact with +FP if you use them together. See the f90(1) man page and the HP Fortran 90 Programmer's Reference for details about using +fp_exception. See the f77(1) man page and the HP FORTRAN/9000 Programmer's Guide for details about using +T.

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1997 Hewlett-Packard Development Company, L.P.