Skip to content

LLVM/optimized code for sqrt incorrect for negative values

The LLVM sqrt intrinsic is undefined below values of -0.0

See http://llvm.org/docs/LangRef.html#llvm-sqrt-intrinsic

In some versions of LLVM it returns 0.0 in this case, but in newer versions the return value is undefined.

This causes the result of Haskell's sqrt to vary at negative values based on the codegen and optimization flags chosen.

main = print (sqrt (-7 :: Double))
$ ghc -O -fllvm Sqrt.hs
[1 of 1] Compiling Main             ( Sqrt.hs, Sqrt.o )
Linking Sqrt ...
$ ./Sqrt 
0.0
$ ghc -O  Sqrt.hs
[1 of 1] Compiling Main             ( Sqrt.hs, Sqrt.o )
Linking Sqrt ...
$ ./Sqrt 
NaN
Trac metadata
Trac field Value
Version 7.8.4
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler (LLVM)
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information