Perl and integer overflows
Shlomi Fish
shlomif at iglu.org.il
Sun Jun 20 15:28:11 IDT 2010
On Sunday 20 Jun 2010 13:25:00 Shachar Shemesh wrote:
> 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.
You can compile Perl with 64-bit integers, even on 32-bit platforms. That or
use a big-integer module such as Math::BigInt :
http://perldoc.perl.org/Math/BigInt.html
See its lib => 'GMP' option if you want much better speed.
Regards,
Shlomi Fish
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
What does "Zionism" mean? - http://shlom.in/def-zionism
God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
More information about the Linux-il
mailing list