Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 18 Aug 1998 11:01:10 -0500 (CDT)
From:      Dave Bodenstab <imdave@mcs.net>
To:        freebsd-questions@FreeBSD.ORG, griepent@wias-berlin.de
Subject:   Re: Floating point exceptions on i386 and FreeBSD-2.2.X
Message-ID:  <199808181601.LAA12892@base486.home.org>

next in thread | raw e-mail | index | archive | help
From: griepent@wias-berlin.de
>
> But there occur so much serious floating point exceptions and
> strange underflows! Can I trust any longer the libm of FreeBSD?
> Most of my programs written in C use the Mesa library.
> They run very well on other architectures like Alpha machines with DEC Unix,
> Silicons with Irix and also i386 machines with Linux.
> Using the same compiler gcc-2.7.2.1 under FreeBSD-2.2.X I had
> always to compile libm, Mesa-2.6 libraries and my own programs
> without any optimization flags to get partially executable code!

This subject has been discussed in the past...  The reason that
Linux gives no exceptions is that the floating point exceptions are
*masked* by default.  The default for FreeBSD, on the other hand, 
is that floating point exceptions are *unmasked*.  Therefore,
what you are seeing is that the software you run under Linux
does a poor job of dealing with numerical precision -- the programs
simply ignores any errors.

In some cases this is OK -- Intel's NPX applies reasonable defaults
for floating point exceptions.  

> It is a pity but sticking to Linux seems to be the only way
> for me to get reliable numerical results.

To get the same results with FreeBSD, you need to modify the programs
to set the exception mask yourself.  Try the following program:

-----
#include <stdio.h>
#include <math.h>
#include <floatingpoint.h>

main( int argc, char **argv )
{
  double a, b;

  /*
   * Mask all exceptions if argc > 1
   */
  if ( argc > 1 )
    fpsetmask( ~ (FP_X_INV | FP_X_DNML | FP_X_DZ | FP_X_OFL | FP_X_UFL | FP_X_IMP) );

  a = 1.0;
  b = cos(0.0) - 1.0;

  a /= b;

  /*
   * This used to be necessary to avoid a kernel message ``pid %d (%s)
   * exited with masked floating point exceptions 0x%02x''.  The kernel
   * now wraps this message with ``#ifdef NPX_DEBUG''
   */
  fpresetsticky( FP_X_INV | FP_X_DNML | FP_X_DZ | FP_X_OFL | FP_X_UFL | FP_X_IMP );
}
-----

bash$ cc fpx.c -lm
bash$ ./a.out
Floating point exception (core dumped)
bash$ ./a.out no
bash$ 


Dave Bodenstab
imdave@mcs.net


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199808181601.LAA12892>