bug in constant folding

Andrea Cilio A.Cilio@dutepp0.et.tudelft.nl
Mon, 15 Mar 1999 19:17:26 +0100


/* I took the following code from the sources of a BSD version of libm.
 * The original function computes floor(x) where x is double.
 *
 * This code exercises a bug in porky1 -fold.
 * The faulty code probably resides in libuseful/eval.cc, in
fold_constants()
 * or one of its callees.
 */

#include<stdio.h>
#include<math.h>

double Large;
double x = 72.0310;

int main()
{
        double y;
        Large = pow(2,52);

        &y;
        y = x + Large;
        y -= Large;

        printf (" rounded: %f    original:  %f\n", y, x);
}

/*
 * First I compiled this codfe into a .spd file, then I ran:
 * porky1 -forward-prop fold_bug.spd fold_bug.sfp
 * then:
 * porky1 -fold fold_bug.sfp fold_bug.sfl
 *
 * The expression involving y,x,L is then simplified, while it should
not,
 * given the precision changes due to floating-point representation.
 *
 * This is the resulting c code, totally erroneous!

...
extern int main()
  {
    double y;

    Large = pow(2.0, 52.0);
    y = x + Large;
    y = x;
    printf(" rounded: %f    original:  %f\n", x, x);
    return;
  }

 */


-- Andrea

 CARDIT Labs - Faculty of Information Technology and Systems
 Delft University                      A.Cilio@et.tudelft.nl