Intel Floating Pt Error 
Author Message
 Intel Floating Pt Error

Hi folks,

I get a Floating point unit(FPU) data register stack overflow
exception(it CRAHES
my application. The application does a lot of FP operations.
I am not an expert of the Intel FPU but my guess is - some FP
push data into the stack, and others pop them out. In a perfect
world, eventually the stack should be empty(I think) assuming
the compiler generating the right code.

Do you know what may go wrong for me?
Is it a compiler problem?
Is it a Intel FPU problem?
Is it my application itself?

What I did now is manually put some embedded assembly in my
C code to clean the stack explicitly in various places. It fixes the
but does not seem like a good long term solution.

My environment is : NT 4.0 w/sp 3(or maybe 4), PII-400(dell box), VC5.0.

Pls help and reply soon.


Sun, 12 Aug 2001 03:00:00 GMT  
 Intel Floating Pt Error

> >In a perfect
> >world, eventually the stack should be empty(I think) assuming
> >the compiler generating the right code.

> Yes.

The general rule for VC++ is that whenever a function is called, the FPU
stack must be empty. Whenever a function returns, the FPU stack must
be empty _unless_ the function is returning a float or double, in which
case it is returned on the FPU stack.

This rule is great for avoiding problems because it means that each
function can assume that has the entire stack to itself. However it is
terrible for optimizing, and negates the entire purpose of having an
FPU stack. However, with such a tiny stack, they really have no
choice. If the stack was larger, or could easily be streamed to memory,
then the FPU stack would be useful and better code could be
generated. Alas, we don't live in that universe.

> >Do you know what may go wrong for me?
> >Is it a compiler problem?

I'd bet that the problem is a missing header file. For instance, in some
(as yet not understood situations) the compiler will allow you to use
sqrt() without including math.h. When this happens it generates some
wacky inline integer code, and leaves the FPU stack alone. At some
later point the caller will try to pop the non-existent result from the
FPU stack - BOOM.

Equivalently, an inaccurate prototype for any function that returns a
float or double can cause this.

To diagnose the problem, step through your code watching the
floating point registers (specifically - TAGS) to find out when
the stack gets misaligned - you should be able to narrow in on the
problematic function call, and find the bad prototype.

> If you call a maths function in the middle of a complicated FP
> expression, there will, on entry to the function, already be one or two,
> or maybe even three, numbers on the stack, so the function has access
> only to about six stack slots.  If it does the same, the second function
> has about four slots, and maybe as few as two.  I'm sure that you can
> see where this is leading.

In my experience this isn't true - at least not with VC++. See my first
paragraph. They could have designed the code generator that way,
but they chose not to (last time I checked), presumably because it
would be far too prone to stack overflow.

.Bruce Dawson, Cavedog Entertainment.
Makers of Total Annihilation -

P.S. Here's a VC++ 5.0 app that shows a potential FPU stack
misalignment problem you can get if you don't include math.h.
This code should not compile, and under VC++ 6.0 it doesn't.

This program demonstrates a VC++ header or compiler bug. This code
should not compile, but it does. However the code generated is
rather lousy - it does not calculate sqrt(x), it never puts anything
on the FPU stack, so when main() tries to pop something off, the
results are rather ill-defined.
I'm not really sure why this code compiles.
This only causes problems on VC++ 5.0.
#ifndef _DLL
#error This program only fails properly with the DLL C run time.

#include <iosfwd>
using namespace std;

double DoIt(double x)
 return sqrt(x);


int main(int argc, char *argv[])
 double x = 2.0;
 x = DoIt(x);
 return 0;

Mon, 13 Aug 2001 03:00:00 GMT  
 [ 2 post ] 

 Relevant Pages 

1. floating pt errors..

2. turbo C floating pt error quetion

3. MFC Floating Pt Error

4. Conversion: Motorola float to Intel float

5. Are there std Floating Pt. Exts.?

6. floating pt. cast as int

7. floating pt.: roundoff or truncate?

8. floating pt in TC

9. Minus Sign and Float. Pt. Edit Control

10. Floating Point Precision in VC++ between Intel/AMD

11. intel floating point a.. part 2

12. intel floating point architecture


Powered by phpBB® Forum Software