[GHC] #1819: x86 native codegen implements float2int# incorrectly
GHC
trac at galois.com
Thu Nov 1 08:13:13 EDT 2007
#1819: x86 native codegen implements float2int# incorrectly
-------------------------------+--------------------------------------------
Reporter: simonmar | Owner:
Type: bug | Status: new
Priority: normal | Milestone: 6.8 branch
Component: Compiler (NCG) | Version: 6.8.1
Severity: normal | Keywords:
Difficulty: Unknown | Testcase: arith005 (see description)
Architecture: x86 | Os: Unknown
-------------------------------+--------------------------------------------
`float2Int#` is supposed to truncate towards zero, but the x86 native
codegen implements it as rounding (or rather, whatever rounding mode the
x87 FPU is set to, which is normally rounding). The right thing appears
to be to save/restore the x87 control word around the conversion
operation, and temporarily set it to truncation. This is what gcc does.
Code generated by gcc for truncation:
{{{
fnstcw -2(%ebp)
movw -2(%ebp), %ax
movb $12, %ah
movw %ax, -4(%ebp)
flds 8(%ebp)
fldcw -4(%ebp)
fistpl -8(%ebp)
fldcw -2(%ebp)
movl -8(%ebp), %eax
}}}
Test case:
{{{
import GHC.Exts
main = print (f2i (-2.9))
{-# NOINLINE f2i #-}
f2i :: Float -> Int
f2i (F# x) = I# (float2Int# x)
}}}
Also `arith005` triggers this, when -O is on and the libraries were
compiled with `-fasm`.
Related: #1254
--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/1819>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
More information about the Glasgow-haskell-bugs
mailing list