[Haskell-cafe] Re: FFI and C99 "complex"

Aaron Denney wnoise at ofb.net
Sun Jan 16 18:17:02 EST 2005


On 2005-01-16, Aaron Denney <wnoise at ofb.net> wrote:
> Basically, what's the best way to deal with C99's complex types?
> Pragmatically, how do I deal with them in function definitions now?
>
> Is there any chance they'll get added to a later version of the
> FFI addendum?

So, I have heard claims that C 99 specifies that a variable v
of type "complex t" is stored as if declared as t v[2], with
v[0] the real part and v[1] the imaginary part.

I don't have a copy of the spec at hand.  Could someone who does verify
this?

If this is true, then the following should be a valid Storable instance:

-- {poke,peek}{Elem,Byte}Off defaulted
instance (RealFloat a, Storable a) => Storable (Complex a) where
    sizeOf    (a :+ b)   = 2 * sizeOf a
    alignment (a :+ b)   = alignment a
    poke      x (a :+ b) = do let y = castPtr x
                              poke y a
                              pokeElemOff y 1 b
    peek      x          = do let y = castPtr x
                              a <- peek y
                              b <- peekElemOff y 1
                              return (a :+ b)

Would providing the the ElemOff or the ByteOff methods instead be any
more efficient?

-- 
Aaron Denney
-><-



More information about the Haskell-Cafe mailing list