Fortran bug

Ray Bovet bovet at hao.ucar.edu
Sat Mar 3 04:48:24 AEST 1990


In article <5401 at brazos.Rice.edu> verena at vuse.vanderbilt.edu (Verena Meiser Umar) writes:
>X-Sun-Spots-Digest: Volume 9, Issue 63, message 3
>
>We are having a problem with division by zero when running  Fortran
>programs.  The Sun3 architecture does not report the occurrence of such a
>division at all, and the Sun4 architecture gives the following warning  -
>BUT not until the end of the run:
>Verena Umar
>verena at vuse.vanderbilt.edu

I wrote the following routine to solve this problem (at least on Sun4s).
The routine causes a crash as soon as the "nasty" IEEE errors like divide
by zero occur.  You just need to call the routine once at the beginning of
your program.  It should be fairly straightforward to modify the routine
if you want to ignore some errors I thought were fatal or vice versa.

				Ray


#include <floatingpoint.h>
#include <signal.h>
#include <stdio.h>
extern catch_div_by_zero(), catch_overflow(), catch_invalid();
/* extern catch_inexact(), catch_underflow(); */

fp_abort_()
/*
  This routine exists to force a program to abort when it encounters
  a floating point error such as a divide by zero, an overflow, or
  an invalid operation.  This used to be standard behavior until IEEE
  came along.  Unfortunately, this code is entirely Sun specific!
  It used to flush away inexact and underflow errors.  We have disabled
  the feature of flushing away inexact and underflow both because it
  slows the program down tremendously and because of philosophical
  questions about whether that is a good idea!  As far as I can tell,
  SIGFPE_IGNORE is the default action on underflow and inexact and is
  included here only for completeness' sake.
*/

{
/*  if (ieee_handler ("set", "inexact", catch_inexact))
    printf ("Can't set inexact.\n"); */
  if (ieee_handler ("set", "inexact", SIGFPE_IGNORE))
    printf ("Can't set inexact.\n");
  if (ieee_handler ("set", "division", catch_div_by_zero))
    printf ("Can't set division.\n");
/*  if (ieee_handler ("set", "underflow", catch_underflow))
    printf ("Can't set underflow.\n"); */
  if (ieee_handler ("set", "underflow", SIGFPE_IGNORE))
    printf ("Can't set underflow.\n");
  if (ieee_handler ("set", "overflow", catch_overflow))
    printf ("Can't set overflow.\n");
  if (ieee_handler ("set", "invalid", catch_invalid))
    printf ("Can't set invalid.\n");
}

catch_div_by_zero (sig, code, scp)
int sig, code;
struct sigcontext *scp;
{
  printf ("Divide by zero occurred at pc %X\n", scp->sc_pc);
  abort();
}

catch_overflow (sig, code, scp)
int sig, code;
struct sigcontext *scp;
{
  printf ("Floating-point overflow occurred at pc %X\n", scp->sc_pc);
  abort();
}

catch_invalid (sig, code, scp)
int sig, code;
struct sigcontext *scp;
{
  printf ("Invalid floating-point operation at pc %X\n", scp->sc_pc);
  abort();
}

catch_inexact (sig, code, scp)
int sig, code;
struct sigcontext *scp;
{
  char *to;
  ieee_flags ("clear", "exception", "inexact", &to);
}

catch_underflow (sig, code, scp)
int sig, code;
struct sigcontext *scp;
{
  char *to;
  ieee_flags ("clear", "exception", "underflow", &to);
}



More information about the Comp.sys.sun mailing list