[Haskell-cafe] Re: Float instance of Data.Bits

Sam Martin sam.martin at geomerics.com
Fri Jul 9 10:32:23 EDT 2010

> Some operations wouldn't make much sense with Float, for instance the
> 'complement' function.  What should it return?  Also note that bit
> manipulation functions could cover only a small window of the value
> range.  So it could happen that x .|. y = x, even though y is nonzero.
> Also rotation would be a destructive operation.

Perhaps I can illustrate this with an example. It's very common with SSE
code to interpret a float as both a mask and a number. You can exchange
the two freely.

For instance, in c-like pseudo code, you can write:
float mask = myval == 0 ? 0x00000000 : 0xffffffff
float temp = 5 / myval
float result = mask .&. temp

Which returns 0 or the result of 5 / myval. Each line above turns into a
single sse instruction, and there are no branches. Bit wise operations
on Floats should operate as if you had reinterpreted the Float as an
unsigned integer.

There are plenty of other examples of bit twiddling floats. Floats have
a well defined bit representation (if a slightly complex one) so it's
perfectly reasonable to be able to manipulate it. A complement operation
on a float returns a float, and is well defined, if the output bit
pattern is one you want.

An alternative way to model this would be to provide ways to reinterpret
a float as a word32, which helps enforce static typing. I don't know of
any way of doing this in Haskell though.

Does that make more sense?


More information about the Haskell-Cafe mailing list