Ticket #4092 (new feature request)

Opened 3 years ago

Last modified 8 months ago

Floating point manipulation : ulp and coerce IEEE-754 Double# into Word64#

Reported by: malosh Owned by:
Priority: normal Milestone: 7.6.2
Component: Compiler Version: 6.12.2
Keywords: Cc: daniel.is.fischer@…, midfield@…, dterei, iridcode@…, johan.tibell@…, acfoltzer@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Difficulty:
Test Case: Blocked By:
Blocking: Related Tickets:

Description

There are currently two ways to compute the ulp of double numbers with GHC :

  • Calling a C function, which requires to allocate a pointer. This is way too expensive when using interval arithmetic (for instance), that compute two ULPs at each arithmetic operations.
  • Programming it by hand in haskell with GHC primitive operations, which requires using unsafeCoerce# : this does not work in GHC 6.12.2.

unsafeCoerce# should work, and there should be a primitive ulp# function in GHC, operating on Doubles at least. By the way, here is my haskell code using C for computing it :

foreign import ccall unsafe "math.h frexp" c_frexp::CDouble->(Ptr CInt)->IO ()
foreign import ccall unsafe "math.h ldexp" c_ldexp::CDouble->CInt->IO CDouble
ulp::Double->Double
ulp x=unsafePerformIO $ do
  expon<-alloca (\e->do
                    c_frexp (realToFrac x) e
                    peek e)
  (c_ldexp 0.5 $ expon-52) >>= return.realToFrac

Attachments

partial-4092.patch Download (8.4 KB) - added by simonmar 23 months ago.

Change History

Changed 3 years ago by simonmar

  • milestone set to 6.14.1

What I had in mind was something like:

coerceDoubleToWord64# :: Double# -> Word64#
coerceFloatToWord32#  :: Float#  -> Word#

(note we don't have a Word32# type)

By the way, you can do this without the FFI right now, by poking the Double into memory, and then peeking it out as a Word64. Not terribly efficient, but better than using the FFI.

Changed 3 years ago by daniel.is.fischer

  • cc daniel.is.fischer@… added

I would like to see those coercions and their inverses, too.

Changed 3 years ago by midfield

  • cc midfield@… added

Changed 2 years ago by igloo

  • milestone changed from 7.0.1 to 7.0.2

Changed 2 years ago by igloo

  • milestone changed from 7.0.2 to 7.2.1

Changed 2 years ago by dterei

  • cc dterei added

Changed 2 years ago by SimonMeier

  • cc iridcode@… added

I would like to see those coercions and their inverses, too.

Changed 23 months ago by tibbe

  • cc johan.tibell@… added

Changed 23 months ago by simonmar

Changed 23 months ago by simonmar

partial patch for this attached. I ran into difficulty because there's no way to allocate temporary memory for the conversion in the native code generator. There are various ways to hack around this (use a spare slot in StgReg?, allocate a new word in the data segment) but none are very nice. We really just want to allocate a slot from the spill area, but that requires cooperation from the register allocator.

Changed 22 months ago by acfoltzer

  • cc acfoltzer@… added

Changed 20 months ago by igloo

  • milestone changed from 7.2.1 to 7.4.1

Changed 16 months ago by igloo

  • milestone changed from 7.4.1 to 7.6.1

Changed 8 months ago by igloo

  • milestone changed from 7.6.1 to 7.6.2
Note: See TracTickets for help on using tickets.