Opened 5 years ago

Last modified 5 months ago

#4092 new feature request

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

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

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 (1)

partial-4092.patch (8.4 KB) - added by simonmar 4 years ago.

Download all attachments as: .zip

Change History (20)

comment:1 Changed 5 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.

comment:2 Changed 5 years ago by daniel.is.fischer

  • Cc daniel.is.fischer@… added

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

comment:3 Changed 5 years ago by midfield

  • Cc midfield@… added

comment:4 Changed 4 years ago by igloo

  • Milestone changed from 7.0.1 to 7.0.2

comment:5 Changed 4 years ago by igloo

  • Milestone changed from 7.0.2 to 7.2.1

comment:6 Changed 4 years ago by dterei

  • Cc dterei added

comment:7 Changed 4 years ago by SimonMeier

  • Cc iridcode@… added

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

comment:8 Changed 4 years ago by tibbe

  • Cc johan.tibell@… added

Changed 4 years ago by simonmar

comment:9 Changed 4 years 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.

comment:10 Changed 4 years ago by acfoltzer

  • Cc acfoltzer@… added

comment:11 Changed 4 years ago by igloo

  • Milestone changed from 7.2.1 to 7.4.1

comment:12 Changed 3 years ago by igloo

  • Milestone changed from 7.4.1 to 7.6.1

comment:13 Changed 3 years ago by igloo

  • Milestone changed from 7.6.1 to 7.6.2

comment:14 Changed 22 months ago by lelf

  • Cc me@… added
  • difficulty set to Unknown

comment:15 Changed 22 months ago by carter

Wait, unsafecoerce# doesn't work here? Oh, is this because we (in general) need to move things between the registers used for floating point vs word values

Last edited 22 months ago by carter (previous) (diff)

comment:16 Changed 13 months ago by nh2

  • Cc mail@… added

Would be very useful.

comment:17 Changed 11 months ago by thoughtpolice

  • Milestone changed from 7.6.2 to 7.10.1

Moving to 7.10.1.

comment:18 Changed 8 months ago by jystic

  • Cc jystic@… added

comment:19 Changed 5 months ago by thoughtpolice

  • Milestone changed from 7.10.1 to 7.12.1

Moving to 7.12.1 milestone; if you feel this is an error and should be addressed sooner, please move it back to the 7.10.1 milestone.

Note: See TracTickets for help on using tickets.