FP exceptions on redhat 5.1

FP exceptions on redhat 5.1

Post by Conrad Steenbe » Sat, 12 Sep 1998 04:00:00



Hi all

I have this really annoying problem with egcs-1.02 that came with Redhat 5.1:
A floating point exception would be raised whenever an underflow occurs.

My question is this: is there an easy way to disable FP exceptions? I can live
with having a few NaN's generated now and again, but I don't want to litter
the code with if's to test for underflows...

Hope somebody out there can help!

Conrad

--
*-----------------------------------------*                                  
| Conrad S*berg                        |                                  
| Caltech, Mail Code 220-47               |                                  
| Pasadena, CA, 91125                     |                                  

| Tel: (626) 395-2965 Fax: (626) 449-8676 |                                  
*-----------------------------------------*                                  

 
 
 

FP exceptions on redhat 5.1

Post by Matt Grossma » Sat, 12 Sep 1998 04:00:00




> > Hi all

> > I have this really annoying problem with egcs-1.02 that came with Redhat 5.1:
> > A floating point exception would be raised whenever an underflow occurs.

> > My question is this: is there an easy way to disable FP exceptions? I can live
> > with having a few NaN's generated now and again, but I don't want to litter
> > the code with if's to test for underflows...

> Isn't there a compiler switch to disable IEEE handling
> of denormals?  Most alpha compilers make such a condition
> the default, since there alpha doesn't handle denormals
> in hardware.

Yes, -mieee and friends.  Do an "info gcc" and look under
Invoking GCC/Submodel Options options for further information.

It will probably make your code run slower.

Matt Grossman

http://www.oz.net/~mattg

 
 
 

FP exceptions on redhat 5.1

Post by H.W. Stockma » Sun, 13 Sep 1998 04:00:00



> Hi all

> I have this really annoying problem with egcs-1.02 that came with Redhat 5.1:
> A floating point exception would be raised whenever an underflow occurs.

> My question is this: is there an easy way to disable FP exceptions? I can live
> with having a few NaN's generated now and again, but I don't want to litter
> the code with if's to test for underflows...

Isn't there a compiler switch to disable IEEE handling
of denormals?  Most alpha compilers make such a condition
the default, since there alpha doesn't handle denormals
in hardware.

I'm quite curious about the answer, since I'm in the
market for a Linux alpha system, would use ecgs, and
have code that generates lots of unimportant denormals.

 
 
 

FP exceptions on redhat 5.1

Post by Christian Hass » Sun, 13 Sep 1998 04:00:00



> Yes, -mieee and friends.  Do an "info gcc" and look under
> Invoking GCC/Submodel Options options for further information.

> It will probably make your code run slower.

I had to use -mieee with our code (fortran), otherwise we got a lot of floating
exceptions.
Unfortunately the code runs 50% slower now.  This applies to egcs-1.0.2, egcs-1.0.3
and
egcs-1.1.  I had no such problem with the DEC compilers which obviously handle these
problem by default correctly.

Christian

 
 
 

FP exceptions on redhat 5.1

Post by Conrad Steenbe » Sun, 13 Sep 1998 04:00:00



>Isn't there a compiler switch to disable IEEE handling
>of denormals?  Most alpha compilers make such a condition
>the default, since there alpha doesn't handle denormals
>in hardware.

>I'm quite curious about the answer, since I'm in the
>market for a Linux alpha system, would use ecgs, and
>have code that generates lots of unimportant denormals.

Thanks to everyone that replied. I took the quick and dirty way out by
installing a signal handler for SIGFPE that just returns and does nothing.

I will look at the suggested compiled switches on Monday when I get back to
the alpha box and see if that makes any difference in speed.

Just for trivia, here are the times for the code I run (yes, it is not a bench-
mark, I know...)
1. Pentium Pro 200 MHz, Linux 2.0.30 with gcc 2.7.2.1: ~12h20
2. Sun ULTRASparc II 266 MHz, Solaris 2.6, Sun cc ?.?: ~19h45
3. IBM SP PowerII 300 MHz, AIX 4.1.4, xlc ?.?        : ~17h05
4. Microway Alpha 533 MHz, Linux 2.0.35, egcs 1.02   : ~ 6h24
5. Pentium II 266 MHz, Linux 2.0.30, gcc 2.7.2.1     : ~14h30      

Some observations:
1. I'm very uncertain about the IBM SP chip frequency, the thing didn't exactly
sit on my desk !
2. The Pentium Pro really performed extraordinarily well, my guess is that its
memory access patterns suited the 256k on-board cache really well.
3. Probably for the same reason, the Pentium II's slower on-board cache made
it much slower than the Pro. I guess the Xeons will remedy that, at a huge
price.
4. From the above it would take about a 600 MHz Pentium II to bring the run
time down to the level of the Alpha. Which means I'm very happy with the Alpha!

If any one is interested, The Alpha is the "performance value special" with IDE
disk and CDROM for $2800 without monitor. See www.microway.com for all the
specs.

Regards

Conrad
--
*-----------------------------------------*                                  
| Conrad S*berg                        |                                  
| Caltech, Mail Code 220-47               |                                  
| Pasadena, CA, 91125                     |                                  

| Tel: (626) 395-2965 Fax: (626) 449-8676 |                                  
*-----------------------------------------*                                  

 
 
 

FP exceptions on redhat 5.1

Post by Michal Jaegerma » Sun, 13 Sep 1998 04:00:00


: I had to use -mieee with our code (fortran), otherwise we got a lot of floating
: exceptions.
: Unfortunately the code runs 50% slower now.

It is **supposed** to be slower.  Alpha has to serialize exceptions,
insert memory barriers killing pipelines and do some other * stuff
like that.  See compiler documentation for details.

You may probably install instead an exception handler which will ignore
all this stuff - if you consider that a "correct" solution.

  Michal

 
 
 

FP exceptions on redhat 5.1

Post by Greg Linda » Mon, 14 Sep 1998 04:00:00



> I had to use -mieee with our code (fortran), otherwise we got a lot
> of floating exceptions.  Unfortunately the code runs 50% slower now.
> This applies to egcs-1.0.2, egcs-1.0.3 and egcs-1.1.  I had no such
> problem with the DEC compilers which obviously handle these problem
> by default correctly.

Digital Unix and its compiler are supposed to deal with this problem
exactly like Linux and gcc do. If you can provide a small example, I'm
sure the egcs people would love to look into it.

-- g

 
 
 

FP exceptions on redhat 5.1

Post by Andreas Kra » Thu, 17 Sep 1998 04:00:00





>> Isn't there a compiler switch to disable IEEE handling
>> of denormals?  Most alpha compilers make such a condition
>> the default, since there alpha doesn't handle denormals
>> in hardware.

> Yes, -mieee and friends.  Do an "info gcc" and look under
> Invoking GCC/Submodel Options options for further information.

I have the problem that these compiler options do not work under
Linux(ALpha). I wrote the following small test program:

void dotest(double a, double b) {
        printf("%f / %f = %f\n", a, b, a / b);

Quote:}

int main() {

        printf("trying to do division by zero in floating point\n");

        dotest(0.0, 0.0);
        dotest(1.0, 0.0);
        dotest(-1.0, 0.0);

        return 0;

Quote:}

I compiled this program using gcc with -mieee, egcs with -mieee and the
Digital cc with -ieee. The programs works correct (printing NaN's and
Inf's) under Digital Unix, but does not work under Linux. Under Linux
it just throws an exception. I also tried a static linked version from
Digital Unix under Linux and it didn't work, it throws an exception. This
shows that the floating point trap handler is integrated into the OS
and not linked to the program. The question now is, why does the IEEE
arithmetic not work under Linux (Alpha, redhat 4.2). I need this info
urgently to release the new version of our Java JIT compiler not only
for Digital Unix but also for Linux.

Andi
--

http://www.complang.tuwien.ac.at/andi/   Inst. f. Computersprachen, TU Wien
tel: (+431) 58801/18511                  Argentinierstr. 8/4/1851
fax: (+431) 5057838                      A-1040 Wien     AUSTRIA     EUROPE

 
 
 

FP exceptions on redhat 5.1

Post by James D. Free » Thu, 17 Sep 1998 04:00:00



> I compiled this program using gcc with -mieee, egcs with -mieee and the
> Digital cc with -ieee. The programs works correct (printing NaN's and
> Inf's) under Digital Unix, but does not work under Linux. Under Linux
> it just throws an exception. I also tried a static linked version from
> Digital Unix under Linux and it didn't work, it throws an exception. This
> shows that the floating point trap handler is integrated into the OS
> and not linked to the program. The question now is, why does the IEEE
> arithmetic not work under Linux (Alpha, redhat 4.2). I need this info
> urgently to release the new version of our Java JIT compiler not only
> for Digital Unix but also for Linux.

I too have had this same problem with g77.  My legacy FORTRAN code
will not run correctly under Linux; even with -mieee and/or statically
linked DU f77 code.  If this problem could be fixed, I would run Linux
all the time on the alpha.  As it stands now, I must run DU to do my
work.

One could argue: don't write code with floating point exceptions.
Agreed it is bad code, but several hundered thousand lines of legacy
FORTRAN is too much to fix.  It is cheaper to buy the DU.

 
 
 

FP exceptions on redhat 5.1

Post by Robert Harle » Fri, 18 Sep 1998 04:00:00



> I have the problem that these compiler options do not work under
> Linux(ALpha). I wrote the following small test program:

>[...]
> I compiled this program using gcc with -mieee, egcs with -mieee and the
> Digital cc with -ieee. The programs works correct (printing NaN's and
> Inf's) under Digital Unix, but does not work under Linux. Under Linux
> it just throws an exception. [...]

The difference is that in Digital Unix, no traps are enabled by
default.  Whereas on Linux, traps for invalid ops, division by zero
and overflow are enabled by default.

You should do something like this:

#include <asm/fpu.h>
extern unsigned long ieee_get_fp_control();
extern void ieee_set_fp_control(unsigned long fp_control);

  ieee_set_fp_control( ieee_get_fp_control()
                       & ~IEEE_TRAP_ENABLE_INV
                       & ~IEEE_TRAP_ENABLE_DZE
                       & ~IEEE_TRAP_ENABLE_OVF
                     );

at the beginning of your thread...

Quote:>I need this info
> urgently to release the new version of our Java JIT compiler not only
> for Digital Unix but also for Linux.

A new version of Cacao?  Send it my way!

Rob.

 
 
 

FP exceptions on redhat 5.1

Post by Andreas Kra » Fri, 18 Sep 1998 04:00:00





>> I have the problem that these compiler options do not work under
>> Linux(ALpha). I wrote the following small test program:

>>[...]
>> I compiled this program using gcc with -mieee, egcs with -mieee and the
>> Digital cc with -ieee. The programs works correct (printing NaN's and
>> Inf's) under Digital Unix, but does not work under Linux. Under Linux
>> it just throws an exception. [...]

> The difference is that in Digital Unix, no traps are enabled by
> default.  Whereas on Linux, traps for invalid ops, division by zero
> and overflow are enabled by default.

> You should do something like this:

> #include <asm/fpu.h>
> extern unsigned long ieee_get_fp_control();
> extern void ieee_set_fp_control(unsigned long fp_control);

>   ieee_set_fp_control( ieee_get_fp_control()
>                        & ~IEEE_TRAP_ENABLE_INV
>                        & ~IEEE_TRAP_ENABLE_DZE
>                        & ~IEEE_TRAP_ENABLE_OVF
>                      );

> at the beginning of your thread...

Thanks, no it works.
--

http://www.complang.tuwien.ac.at/andi/   Inst. f. Computersprachen, TU Wien
tel: (+431) 58801/18511                  Argentinierstr. 8/4/1851
fax: (+431) 5057838                      A-1040 Wien     AUSTRIA     EUROPE
 
 
 

FP exceptions on redhat 5.1

Post by Michal Jaegerma » Fri, 18 Sep 1998 04:00:00


: The difference is that in Digital Unix, no traps are enabled by
: default.  Whereas on Linux, traps for invalid ops, division by zero
: and overflow are enabled by default.

: You should do something like this:

: #include <asm/fpu.h>
: extern unsigned long ieee_get_fp_control();
: extern void ieee_set_fp_control(unsigned long fp_control);

:   ieee_set_fp_control( ieee_get_fp_control()
:                        & ~IEEE_TRAP_ENABLE_INV
:                        & ~IEEE_TRAP_ENABLE_DZE
:                        & ~IEEE_TRAP_ENABLE_OVF
:                      );

: at the beginning of your thread...

Hm, I wonder if this depends on Alpha CPU type and/or libraries.
The reason for that is that I checked that on two different EB64+
machines (one is running at the moment RH 5.1, mostly, and the other
one RH 5.0) and on both ieee_get_fp_control() returns, in a default
settings, 0L.

In any case, if you want to see what your machine is doing when you
ask it to do something stupid in a floating point :-) here is a small
demo program and a simple makefile for various hacks:

   Enjoy,
   Michal

*****
xpm.c
*****
/*
 * This program demonstrates various ways of disabling floating
 * point traps on Linux/Alpha.
 * Installing your own "do nothing" signal handles disables
 * traps in floating poing completely but, as you can see, may
 * have consequences which you may not like.
 * Using various -mieee.... options (see 'info gcc' for discussion)
 * forces serialization of exception handling which can be processed
 * then according with IEEE rules - this carries an extra cost.
 *
 * To install "ignore" handler in a Fortran program write that in C
 * and link with your Fortran using usual conventions.
 *
 * To "really ignore" floating point exception signal just return
 * from 'handler'
 */

#include <stdio.h>
#include <signal.h>
#include <asm/fpu.h>

extern unsigned long ieee_get_fp_control();
extern void ieee_set_fp_control(unsigned long fp_control);

#ifdef PRIVATE_HANDLER
static void handler(int x)
{
    fprintf(stdout, "ignoring signal %d\t", x);
    return;

Quote:}

void install_fpe_handler(void)
{
    signal(SIGFPE, &handler);
    return;
Quote:}

#else
void install_fpe_handler(void)
{
    return;   /* do nothing */
Quote:}

#endif

void traptest(double a, double b) {
    double result = a / b;
    printf("%g / %g = %g\n", a, b, result);
    /* printf("%f / %f = %f\n", a, b, result); */

Quote:}

/* On Linux/Alpha traps for invalid ops, division by zero
   and overflow may be enabled - depending on library versions (?) */

int main(void) {

    printf("fpu mask is 0x%016lx\n", ieee_get_fp_control());

    install_fpe_handler();

#ifdef DISABLE_TRAPS
# warning "disabling invalid ops, division by zero and overflow traps"
    ieee_set_fp_control(
        ieee_get_fp_control()
        & ~IEEE_TRAP_ENABLE_INV
        & ~IEEE_TRAP_ENABLE_DZE
        & ~IEEE_TRAP_ENABLE_OVF
        );
    printf("fpu mask is 0x%016lx\n", ieee_get_fp_control());
/*    ieee_set_fp_control(-1L); *//* uncommenting this enables all in sight */
/*    ieee_set_fp_control(IEEE_TRAP_ENABLE_MASK); */
#endif /* DISABLE_TRAPS */
    printf("fpu mask is 0x%016lx\n", ieee_get_fp_control());

    printf("trying to cause various traps in floating point\n");

    traptest(0.0, 0.0);
    traptest(1.0, 0.0);
    traptest(-1.0, 0.0);
    traptest(1.0, 1.0e+309);
    traptest(1.0, 1.0e-309);

    return 0;

Quote:}

*****
Makefile
*****
CFLAGS = -g -Wall -O2
MFLAGS = -mieee-with-inexact
#MFLAGS = -mieee
HFLAGS = -DPRIVATE_HANDLER
DFLAGS = -DDISABLE_TRAPS
PROGS = xpm xpmm xpmd xpmmd xpmh xpmmh xpmhd xpmmhd

all: $(PROGS)

xpm:

xpmm:

xpmd:

xpmmd:

xpmh:

xpmmh:

xpmhd:

xpmmhd:

run: $(PROGS)
        for f in $(PROGS) ; do echo $$f ; ./$$f || echo "TRAP" ; done

clean:
        rm -f $(PROGS)