Re: FPU, i386

From: Richard B. Johnson (root@chaos.analogic.com)
Date: Wed Apr 17 2002 - 09:26:03 EST


On Wed, 17 Apr 2002, Andrey Ulanov wrote:

> Look at this:
>
> $ cat test.c
> #include <stdio.h>
>
> main()
> {
> double h = 0.2;
>
> if(1/h == 5.0)
> printf("1/h == 5.0\n");
>
> if(1/h < 5.0)
> printf("1/h < 5.0\n");
> return 0;
> }
> $ gcc test.c
> $ ./a.out
> 1/h < 5.0
> $
>
> I also ran same a.out under FreeBSD. It says "1/h == 5.0".
> It seems there is difference somewhere in FPU
> initialization code. And I think it should be fixed.
>

No. No. No. Read something about basic programming of floating-point
operations before complaining about something you obviously know
nothing about. "==" has no use in floating-point operations. Any
code that uses "==", referencing floating-point numbers is broken.

If the BSD 'C' runtime library fortuously says that 1.0/0.2 == 5, than
you just got lucky. Time to play the lottery. FYI, you can set the
per-process FPU initialization anyway you want. FYI, by default it
ignores 1/0 errors:

Cheers,
Dick Johnson

Penguin : Linux version 2.4.18 on an i686 machine (797.90 BogoMips).

                 Windows-2000/Professional isn't.


/*
 * Note FPU control only exists per process. Therefore, you have
 * to set up the FPU before you use it in any program.
 */
#include <i386/fpu_control.h>

#define FPU_MASK (_FPU_MASK_IM |\
                  _FPU_MASK_DM |\
                  _FPU_MASK_ZM |\
                  _FPU_MASK_OM |\
                  _FPU_MASK_UM |\
                  _FPU_MASK_PM)

void fpu()
{
    __setfpucw(_FPU_DEFAULT & ~FPU_MASK);
}


main() {
   double zero=0.0;
   double one=1.0;
   fpu();

   one /=zero;
}

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Tue Apr 23 2002 - 22:00:18 EST