[Haskell-cafe] Looking for largest power of 2 <= Integer

Dan Piponi dpiponi at gmail.com
Tue Dec 4 17:23:47 EST 2007

There's a bit of work required to make this code good enough for
general consumption, and I don't know much about Haskell internals.

(1) What is the "official" way to find the size of a word? A quick
look at 6.8.1's base/GHC/Num.lhs reveals that it uses a #defined

(2) Is it safe to assume an underlying implementation based on GMP?
(In Num.lhs there is an alternative definition for .NET. Is that ever
used?) Is it safe to assume the size of a GMP "limb" is the same as
the word size? (I'm assuming it is for now.)

David said:

> I've attached a program with two QuickCheck properties.  Unfortunately
> they fail on negative Integers.  I can't figure out why.

Judging by Num.lhs, the sign of the Integer is stored as the sign of
the length of the Integer in "limbs". The code needs to deal with that
explicitly, as well as doing some bounds checking on the index.

Anyway, if someone can help with those questions I can try to write a
bulletproof version of the code.

More information about the Haskell-Cafe mailing list