This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: Glibc issue, was: Trying to include floating point trapping in fortran program
- To: Duncan Galloway <duncan at space dot mit dot edu>, gcc-bugs at gcc dot gnu dot org
- Subject: Re: Glibc issue, was: Trying to include floating point trapping in fortran program
- From: Toon Moene <toon at moene dot indiv dot nluug dot nl>
- Date: Wed, 20 Sep 2000 23:23:27 +0200
- Organization: Moene Computational Physics, Maartensdijk, The Netherlands
- References: <39C7802C.D8EEE528@space.mit.edu> <39C7B569.371DFD0F@moene.indiv.nluug.nl>
Toon Moene wrote:
> Duncan Galloway wrote:
> > I'm trying to compile a program and include the c code listed in the
> > document
> >
> > http://gcc.gnu.org/onlinedocs/g77_20.html#SEC604
> >
> > This is supposed to trap floating point errors, overflows, underflows
> IIRC, this solution only worked with glibc-2.0.x; with glibc-2.1 and
> higher you need to do this in a different way. Unfortunately, I do not
> have a glibc-2.1 system handy, so I can't explain how.
>
> Hope someone else on this list can help,
I got the following suggestion from Alfredo Ferrari INFN - Milan in
private email:
<QUOTE>
the following piece of code works nicely on my RedHat systems with both
Glibc2.0 and 2.1 in order to trap floating point exceptions in fortran
codes.
I hope it is useful... It is a long time I am not trying it on a
Glibc2.0
system, I hope it is still ok, with RH6.0,6.1 and 6.2 it works like a
charm.
Cheers
Alfredo Ferrari
#include <features.h>
#if __GLIBC_MINOR__ > 0
#include <fenv.h>
#else
#include <fpu_control.h>
#endif
static void __attribute__ ((constructor))
trapfpe ()
{
#if __GLIBC_MINOR__ > 0
feenabletraps ((FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW));
#else
__setfpucw (_FPU_DEFAULT &
~(_FPU_MASK_IM | _FPU_MASK_ZM | _FPU_MASK_OM));
#endif
}
void fpenab_ () {
trapfpe();
}
/*
Enable exception traps for the exceptions specified by traps.
*/
int feenabletraps(int traps)
{
unsigned short cw;
asm volatile ("fnstcw %0":"=m" (cw));
cw &= ~traps;
asm volatile ("fldcw %0"::"m" (cw));
return 0;
}
</QUOTE>
Hope this helps,
--
Toon Moene - mailto:toon@moene.indiv.nluug.nl - phoneto: +31 346 214290
Saturnushof 14, 3738 XG Maartensdijk, The Netherlands
GNU Fortran 77: http://gcc.gnu.org/onlinedocs/g77_news.html
GNU Fortran 95: http://g95.sourceforge.net/ (under construction)