Oracle® Solaris Studio 12.4: Numerical Computation Guide

Exit Print View

Updated: January 2015
 
 

4.3.1 ieee_flags(3m)

ieee_flags provides an interface for IEE 754 exception flags that is similar Oracle Solaris Studio C, C++, and Fortran. However, this interface is only available on the Oracle Solaris OS. Use the C99 Exception Flag Functions for C and C++ programs that are intended to be more widely portable.

The syntax for a call to ieee_flags(3m) is:

i = ieee_flags(action, mode, in, out);

A program can test, set, or clear the accrued exception status flags using the ieee_flags function by supplying the string "exception" as the second argument. For example, to clear the overflow exception flag from Fortran, write:

      character*8 out
      call ieee_flags('clear', 'exception', 'overflow', out) 

To determine whether an exception has occurred from C or C++, use:

      i = ieee_flags("get", "exception", in, out); 

When the action is "get", the string returned in out is one of the following:

  • "not available" — if information on exceptions is not available

  • "" (an empty string) — if there are no accrued exceptions or, in the case of x86, the denormal operand is the only accrued exception

  • The name of the exception named in the third argument, in, if that exception has occurred

  • Otherwise, the name of the highest priority exception that has occurred

For example, in the following Fortran call the string returned in out is "division" if the division-by-zero exception has occurred. Otherwise it is the name of the highest priority exception that has occurred:

      character*8 out
      i = ieee_flags('get', 'exception', 'division', out)

Note that in is ignored unless it names a particular exception. For example, the argument "all" is ignored in the following C call:

      i = ieee_flags("get", "exception", "all", out); 

Besides returning the name of an exception in out, ieee_flags returns an integer value that combines all of the exception flags currently raised. This value is the bitwise “or” of all the accrued exception flags, where each flag is represented by a single bit as shown in Table 4–2. The positions of the bits corresponding to each exception are given by the fp_exception_type values defined in the file sys/ieeefp.h. (Note that these bit positions are machine-dependent and need not be contiguous.)

Table 4-2  Exception Bits
Exception
Bit Position
Accrued Exception Bit
invalid
fp_invalid
i & (1 << fp_invalid)
overflow
fp_overflow
i & (1 << fp_overflow)
division
fp_division
i & (1 << fp_division)
underflow
fp_underflow
i & (1 << fp_underflow)
inexact
fp_inexact
i & (1 << fp_inexact)
denormalized
fp_denormalized
i & (1 << fp_denormalized) (x86 only)

This fragment of a C or C++ program shows one way to decode the return value.

/*
 *   Decode integer that describes all accrued exceptions. 
 *   fp_inexact etc. are defined in <sys/ieeefp.h> 
 */ 
 
char *out; 
int invalid, division, overflow, underflow, inexact; 
 
code = ieee_flags("get", "exception", "", &out); 
printf ("out is %s, code is %d, in hex: 0x%08X\n", 
  out, code, code); 
inexact  =  (code >> fp_inexact) & 0x1; 
division  =  (code >> fp_division) & 0x1; 
underflow  =  (code >> fp_underflow) & 0x1; 
overflow  =  (code >> fp_overflow) & 0x1; 
invalid  =  (code >> fp_invalid) & 0x1; 
printf("%d %d %d %d %d \n", invalid, division, overflow,
  underflow, inexact);