[fpc-pascal]Double-precision recursion

Sebastian G√ľnther sguenther at gmx.de
Fri Mar 30 00:00:30 CEST 2001

Rich Pasco wrote:
> I didn't know there a separate stack in the FPU.  How can I learn more
> about how Free Pascal utilizes it?

AFAIK, FPC works this way:

If you have a FPU operation which uses function results as arguments,
such as
  a := b + c(...);    (c is a function)
then FPC will push 'b' on the FPU stack, call c() (which returns the
result in the top-most FPU stack element), and add the both top-most
In the case of a recursion, you will get serious problems with this
approach, of course.

> > If you reduce the line "p := get+cof(n);" just to "p := cof(n);", then
> > the program works. (ehmm, it doesn't crash anymore...)
> Yes, but in the "real" program from which this trivial example was
> derived, this is not an option.

Of course.... :)

> > But I'm somewhat surprised that BP sufferes from the same problem.
> Apparently both BP and FP use the FPU stack (if there is one) the same
> way.  In this case, I'm surprised that the C version is not also subject
> to the same bug.

I think most other compilers (including Delphi?) just don't push
operands on the stack if there are still functions to be executed,
before the operation is finished. As Marco pointed out, this results in
much slower code - but on the other hand, this is the only solution to
get code which works correctly in all cases.

A workaround for  a := b + c(...);  is this: (might not work with
optimizations enabled)

    d: Double;
    d := c(...);
    a := b + d;

- Sebastian

More information about the fpc-pascal mailing list