Perl and integer overflows

Perl and integer overflows

Shachar Shemesh shachar at shemesh.biz
Sun Jun 20 13:25:00 IDT 2010


Hi all,

I'm trying to calculate the result of a string hash function in perl. I 
need an explicit function, as the perl output is used to create a C 
program, that will then use that very same hash function. Please, do not 
direct me to perl's excellent hash handling. I know it's there. I use it 
where applicable. This is not one of those cases.

The hash function I'm using is sdbm hash function 
(http://www.cse.yorku.ca/~oz/hash.html). To calculate it, I need to 
perform integer arithmetics, knowing full well it may overflow. It is 
okay for it to overflow, as I only need the bottom 32 bits. In fact, my 
code ANDs the result with 0xffffffff, to make sure that, on 64 bit 
platforms, I only get the bottom 32 bits.

Here's the problem. When the integer gets too big, Perl replaces it with 
MAXINT. On 64 bit platforms, that's not too bad - I can simply truncate 
to 32 bits after each operation. On 32 bit platforms, I'm a bit at a 
loss as to what to do.

Any suggestions on how to get the C integer semantics out of perl?

Shachar

-- 
Shachar Shemesh
Lingnu Open Source Consulting Ltd.
http://www.lingnu.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.cs.huji.ac.il/pipermail/linux-il/attachments/20100620/be560037/attachment.html>


More information about the Linux-il mailing list