[Haskell-cafe] RealFrac methods for Double and Float

Daniel Fischer daniel.is.fischer at web.de
Sun Oct 10 20:02:28 EDT 2010


I have put together a package to test possible implementations of the 
RealFrac methods for Double and Float (base-2 IEEE754) and uploaded a 
.tar.gz bundle to http://hackage.haskell.org/trac/ghc/ticket/2271 .

On the one hand, pure Haskell implementations, on the other hand 
implementations calling out to rint[f], trunc[f], floor[f] and ceil[f] from 

Both ways go via Integer by default, with a specialised faster 
implementation for Int (and narrower types, but those RULES haven't yet 
been written) enabled by a rewrite rule.

Overall, the pure Haskell implementations don't fare badly on my computer. 
All give a speedup compared to the current implementation, for most 
conversions, pure Haskell is on par with or faster than the C-call 
(although that would probably change if the C functions were made primops).

The FFI calls are significantly faster for
properFraction :: Double -> (Integer, Double)
and for round (except round :: Integral a => Float -> a when compiled
via C, then native and FFI are on par).

Sample results for the speedups against the current implementation (note:
for truncate :: x -> Int, the Prelude value is fst . properFraction, not 
the rewritten float2Int or double2Int) are included in the tarball.

I would appreciate feedback from your tests/benchmarks on other platforms,
especially 64-bit platforms (mine is x86 linux, 32 bit).

To run the QuickCheck tests, you need QuickCheck-2.*, to run the 
benchmarks, criterion.

More instructions in the README.


More information about the Haskell-Cafe mailing list