Opened 4 years ago

Last modified 9 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.6.2
Component: Compiler Version: 6.12.2
Keywords: Cc: daniel.is.fischer@…, midfield@…, dterei, iridcode@…, johan.tibell@…, acfoltzer@…, me@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Difficulty: Unknown
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 (1)

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

Download all attachments as: .zip

Change History (16)

comment:1 Changed 4 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 4 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 4 years ago by midfield

  • Cc midfield@… added

comment:4 Changed 3 years ago by igloo

  • Milestone changed from 7.0.1 to 7.0.2

comment:5 Changed 3 years ago by igloo

  • Milestone changed from 7.0.2 to 7.2.1

comment:6 Changed 3 years ago by dterei

  • Cc dterei added

comment:7 Changed 3 years ago by SimonMeier

  • Cc iridcode@… added

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

comment:8 Changed 3 years ago by tibbe

  • Cc johan.tibell@… added

Changed 3 years ago by simonmar

comment:9 Changed 3 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 3 years ago by acfoltzer

  • Cc acfoltzer@… added

comment:11 Changed 3 years ago by igloo

  • Milestone changed from 7.2.1 to 7.4.1

comment:12 Changed 2 years ago by igloo

  • Milestone changed from 7.4.1 to 7.6.1

comment:13 Changed 19 months ago by igloo

  • Milestone changed from 7.6.1 to 7.6.2

comment:14 Changed 9 months ago by lelf

  • Cc me@… added
  • Difficulty set to Unknown

comment:15 Changed 9 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 9 months ago by carter (previous) (diff)
Note: See TracTickets for help on using tickets.