Real.maxFinite bug
Stephen Weeks
MLton@sourcelight.com
Mon, 1 Oct 2001 17:31:01 -0700
I have tracked a bug in MLton's implementation of Real.maxFinite down to the
following C example. (If you try using Real.maxFinite in *any* program, the
resulting executable will fail to terminate.)
The code below runs as I would expect when compiled gcc -O0, but fails to
terminate when compiled gcc -O2. Furthermore, if you uncomment the fprintf and
compile gcc -O2, the program terminates as expected. I'm guessing the problem
has something to do with extended precision, but I can't see how, since my
understanding of the C calling convention is that the float argument is passed
in 64 bits on the stack. Thus I can't understand how uncommenting the fprintf
changes behavior.
Any ideas?
--------------------------------------------------------------------------------
#include <stdio.h>
typedef double Double;
typedef int Int;
typedef unsigned int Word;
#define EXPONENT_MASK 0x7FF00000
Int Real_isFinite(Double d) {
Word word1;
/* fprintf(stderr, "Real_isFinite(%.20e)\n", d); */
word1 = ((Word *)&d)[1];
return !((word1 & EXPONENT_MASK) == EXPONENT_MASK);
}
int main ()
{
Double x, y;
x = 1.0;
loop:
y = x * 2.0;
if (Real_isFinite(y)) {
x = y;
goto loop;
}
fprintf(stderr, "max is %.20e\n", x);
return 0;
}