Fortran Library Reference

ieee_flags,ieee_handler, sigfpe: IEEE Arithmetic

These subprograms provide modes and status required to fully exploit ANSI/IEEE Std 754-1985 arithmetic in a FORTRAN program. They correspond closely to the functions ieee_flags(3M), ieee_handler(3M), and sigfpe(3).

Here is a summary:

Table 1-2 IEEE Arithmetic Support Routines
ieeer = ieee_flags( action,mode,in,out )
ieeer = ieee_handler(action,exception,hdl )
ieeer = sigfpe( code, hdl )

action

character

Input  

code

sigfpe_code_type

Input 

mode

character

Input  

in

character

Input  

exception

character

Input  

hdl

sigfpe_handler_type

Input  

out

character

Output 

Return value

INTEGER*4

Output 

See the Sun Numerical Computation Guide for details on how these functions can be used strategically.

If you use sigfpe, you must do your own setting of the corresponding trap-enable-mask bits in the floating-point status register. The details are in the SPARC architecture manual. The libm function ieee_handler sets these trap-enable-mask bits for you.

The character keywords accepted for mode and exception depend on the value of action.

Table 1-3 ieee_flags(action,mode,in,out) Parameters and Actions
action = 'clearall'mode, in, out, unused; returns 0

action = 'clear'

clear mode, in

out is unused; returns 0

mode = 'direction'
mode = 'precision' (on x86 platforms only)
mode = 'exception' in = 'inexact'       or 'division'     or 'underflow'    or 'overflow'    or 'invalid'    or 'all'    or 'common'

action = 'set'

set floating-point mode,in

out is unused; returns 0

mode = 'direction'in = 'nearest'       or 'tozero'       or 'positive'     or 'negative'
mode = 'precision' (on x86 only)in = 'extended'     or 'double'       or 'single'
mode = 'exception'in = 'inexact'       or 'division'     or 'underflow'    or 'overflow'    or 'invalid'    or 'all'    or 'common'

action = 'get'

test mode settings

in, out may be blank or one of the settings to test returns the current setting depending on mode, or 'not available' The function returns 0 or the current exception flags if mode = 'exception'

mode = 'direction'out = 'nearest'     or 'tozero'       or 'positive'     or 'negative'
mode = 'precision' (on x86 only)out = 'extended'     or 'double'       or 'single'
mode = 'exception'out = 'inexact'    or 'division'     or 'underflow'    or 'overflow'    or 'invalid'    or 'all'    or 'common'

Table 1-4 ieee_handler(action,in,out) Parameters

action = 'clear'clear user exception handing of in; out is unused

in = 'inexact'       or 'division'     or 'underflow'    or 'overflow'    or 'invalid'    or 'all'    or 'common'

action = 'set' set user exception handing of in; out is address of handler routine, or SIGFPE_DEFAULT, or SIGFPE_ABORT, or SIGFPE_IGNORE defined in f77/f77_floating point.h

in = 'inexact'       or 'division'     or 'underflow'    or 'overflow'    or 'invalid'    or 'all'    or 'common'

Example 1: Set rounding direction to round toward zero, unless the hardware does not support directed rounding modes:


    INTEGER*4 ieeer 
    character*1 mode, out, in 
    ieeer = ieee_flags( 'set', 'direction', 'tozero', out ) 

Example 2: Clear rounding direction to default (round toward nearest):


    character*1 out, in 
    ieeer = ieee_flags('clear','direction', in, out )

Example 3: Clear all accrued exception-occurred bits:


    character*18 out 
    ieeer = ieee_flags( 'clear', 'exception', 'all', out )

Example 4: Detect overflow exception as follows:


    character*18 out 
    ieeer = ieee_flags( 'get', 'exception', 'overflow', out )
    if (out .eq. 'overflow' ) stop 'overflow'

The above code sets out to overflow and ieeer to 25 (this value is platform dependent). Similar coding detects exceptions, such as invalid or inexact.

Example 5: hand1.f, write and use a signal handler (Solaris 2):


    external hand
    real r / 14.2 /,  s / 0.0 /
    i = ieee_handler( 'set', 'division', hand ) 
    t = r/s 
    end
 
    INTEGER*4 function hand ( sig, sip, uap )
    INTEGER*4 sig, address
    structure /fault/
        INTEGER*4 address
    end structure
    structure /siginfo/
        INTEGER*4 si_signo
        INTEGER*4 si_code
        INTEGER*4 si_errno
        record /fault/ fault
    end structure
    record /siginfo/ sip
    address = sip.fault.address
    write (*,10) address
 10    format('Exception at hex address ', z8 )
    end

See the Numerical Computation Guide. See also: floatingpoint(3), signal(3), sigfpe(3), f77_floatingpoint(3F), ieee_flags(3M), and ieee_handler(3M).

f77_floatingpoint.h: FORTRAN IEEE Definitions

The header file f77_floatingpoint.h defines constants and types used to implement standard floating-point according to ANSI/IEEE Std 754-1985.

Include the file in a FORTRAN 77 source program as follows:


#include "f77_floatingpoint.h" 

Use of this include file requires preprocessing prior to FORTRAN compilation.The source file referencing this include file will automatically be preprocessed if the name has a .F or .F90 extension.

Fortran 90 programs should include the file f90/floatingpoint.h instead.

IEEE Rounding Mode:

fp_direction_type

The type of the IEEE rounding direction mode. The order of enumeration varies according to hardware.  

SIGFPE Handling:

sigfpe_code_type

The type of a SIGFPE code.

sigfpe_handler_type

The type of a user-definable SIGFPE exception handler called to handle a particular SIGFPE code.

SIGFPE_DEFAULT

A macro indicating default SIGFPE exception handling: IEEE exceptions to continue with a default result and to abort for other SIGFPE codes.

SIGFPE_IGNORE

A macro indicating an alternate SIGFPE exception handling, namely to ignore and continue execution.

SIGFPE_ABORT

A macro indicating an alternate SIGFPE exception handling, namely to abort with a core dump.

IEEE Exception Handling:

N_IEEE_EXCEPTION

The number of distinct IEEE floating-point exceptions. 

fp_exception_type

The type of the N_IEEE_EXCEPTION exceptions. Each exception is given a bit number.

fp_exception_field_type

The type intended to hold at least N_IEEE_EXCEPTION bits corresponding to the IEEE exceptions numbered by fp_exception_type. Thus, fp_inexact corresponds to the least significant bit and fp_invalid to the fifth least significant bit. Some operations can set more than one exception.

IEEE Classification:

fp_class_type

A list of the classes of IEEE floating-point values and symbols.  

Refer to the Numerical Computation Guide. See also ieee_environment(3M) and f77_ieee_environment(3F).