[plt-scheme] Bit manipulation in PLT Scheme

From: Paulo J. Matos (pocmatos at gmail.com)
Date: Fri Mar 10 18:37:25 EST 2006

On 09/03/06, Matthew Flatt <mflatt at cs.utah.edu> wrote:
> At Thu, 9 Mar 2006 01:10:54 +0000, "Paulo J. Matos" wrote:
> > I'm having the need to work in a somewhat low-level with bits and
> > bytes, which I've never done in Scheme. I'm curious about if there's
> > any performance or memory penalty by using bitfields in Scheme
> > (SRFI-60) (is there any alternative to this in PLT-Scheme?). Will a
> > bitfield of 64 bits occupy 64bits in memory or will I have a penalty?
> > Will a logical 'and' between two bitfields have any performance
> > penalty when compared to the same operation in C?
>
> SRFI 60 is implemented in terms of MzScheme exact integers, MzScheme's
> `bitwise-and', etc.
>
> On a 32-bit platform, a 64-bit number will take at least 128 bits,
> because 64 bites are used for header and length information in a
> bignum. Operations will be much more expensive than in C, because a
> bignum allocation will be required.
>
> On a 64-bit platforms, many of the numbers will turn out to fit in 62
> bits, and so they'll be unboxed fixnums. The operations will be a
> little more expensive than in C, because there will be a type test to
> check that the number is a fixnum (but this is small compared to boxing
> overhead).
>
> When I need fast 32-bit numbers with bit operations, I use a pair of
> 16-but numbers, and I write functions that return the pair through
> multiple values (to avoid even allocating a cons cell for the pair).
> Managing the pair can be tedious, and to handle 64 bits this way, you'd
> need a triple of numbers (on a 32-bit platform).
>

Hi Matthew,

After answering Danny, I rechecked your reply and your last suggestion
stroke my mind. At the time I read this I didn't get it.
It seems that MzScheme is then able to differentiate between a 16-bit
number and every larger number. So, it'll only use bignum operations
and structures on number bigger than 16 bit? Is this correct?

Cheers,

Paulo Matos

> Matthew
>
>


--
Paulo Jorge Matos - pocm at sat inesc-id pt
Web: http://sat.inesc-id.pt/~pocm
Computer and Software Engineering
INESC-ID - SAT Group


Posted on the users mailing list.