[Haskell-cafe] -O2 bug in GHC 6.8.1?

Bertram Felgenhauer bertram.felgenhauer at googlemail.com
Tue Nov 20 12:05:58 EST 2007


Ian Lynagh wrote:
> 
> Hi Brad,
> 
> On Tue, Nov 20, 2007 at 09:50:02PM +1000, Brad Clow wrote:
> > 
> > $ ./test
> > 23
> > 24
> 
> I can't reproduce this. Can you please tell us what platform you are on
> (e.g. x86_64 Linux) and what gcc --version says?

I see a bug that only affects x86_32.

The native code generator (compiler/nativeGen/PprMach.hs) generates a
simple fistp instruction for converting floats or doubles to int. with
the usual default rounding mode (round) that means numbers between n+1/2
and n are rounded up instead of truncated. (relevant Code:

pprInstr g@(GDTOI src dst) 
   = pprG g (hcat [gtab, text "subl $4, %esp ; ", 
                   gpush src 0, gsemi, text "fistpl 0(%esp) ; popl ", 
                   pprReg I32 dst])

)

What's missing is the FPU control word manipulation to set the
rounding mode appropriately, say

pprinstr g@(GDTOI src dst)
   = pprG g (hcat [gtab, gpush src 0,
                   text " ; subl $4, %esp ; fstcw 0(%esp) ; movl $0xC00, ",
                   reg, text " ; orl 0(%esp), ", reg,
                   text " ; pushl ", reg,
                   text " ; fldcw 0(%esp) ; fistpl 0(%esp) ; popl ", reg,
                   text " ; addl $4, %esp"])
   where
     reg = pprReg I32 dst

For reference, see http://www.eglibc.org/cgi-bin/viewcvs.cgi/fsf/glibc-2_3-branch/libc/sysdeps/i386/fpu/s_truncf.S?rev=10&sortby=date&view=markup

HTH,

Bertram


More information about the Haskell-Cafe mailing list