Subject: | Wrong results on 64 bit system |
Function djb returns wrong results on a 64bit system. For example, on a
32 bit machine:
577 % perl -MDigest::DJB -le 'print Digest::DJB::djb("abc123")'
4048022465 (correct)
but on a 64 bit machine:
bash-2.05b# /usr/local/nf/bin/perl -I./blib/lib -I./blib/arch
-MDigest::DJB -le 'print Digest::DJB::djb("abc123");'
6953305107393
The reason for this is that in DBI.xs the arithmetic is being done on a
value of type size_t. The djb hash expect the arithmetic to be done on
values of type uint32 (i.e., unsigned long).
Detail of the 64 bit system follow:
FreeBSD admn6 6.2-RELEASE-p7 FreeBSD 6.2-RELEASE-p7 #1: Fri Aug 31
11:58:46 EDT 2007 root@admn6:/usr/obj/usr/src/sys/NF_GENERIC amd64
The perl version on it is
Summary of my perl5 (revision 5 version 8 subversion 8) configuration:
Platform:
osname=freebsd, osvers=6.2-release-p7, archname=amd64-freebsd
uname='freebsd admn6 6.2-release-p7 freebsd 6.2-release-p7 #1: fri
aug 31 11:58:46 edt 2007 root@admn6:usrobjusrsrcsysnf_generic amd64 '
config_args='-de -Dprefix=/usr/local/nf'
hint=recommended, useposix=true, d_sigaction=define
usethreads=undef use5005threads=undef useithreads=undef
usemultiplicity=undef
useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
use64bitint=define use64bitall=define uselongdouble=undef
usemymalloc=n, bincompat5005=undef
Compiler:
cc='cc', ccflags ='-DHAS_FPSETMASK -DHAS_FLOATINGPOINT_H
-fno-strict-aliasing -pipe -Wdeclaration-after-statement
-I/usr/local/include',
optimize='-O',
cppflags='-DHAS_FPSETMASK -DHAS_FLOATINGPOINT_H -fno-strict-aliasing
-pipe -Wdeclaration-after-statement -I/usr/local/include'
ccversion='', gccversion='3.4.6 [FreeBSD] 20060305', gccosandvers=''
intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678
d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t',
lseeksize=8
alignbytes=8, prototype=define
Linker and Libraries:
ld='cc', ldflags ='-Wl,-E -L/usr/local/lib'
libpth=/usr/lib /usr/local/lib
libs=-lm -lcrypt -lutil -lc
perllibs=-lm -lcrypt -lutil -lc
libc=, so=so, useshrplib=false, libperl=libperl.a
gnulibc_version=''
Dynamic Linking:
dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags=' '
cccdlflags='-DPIC -fPIC', lddlflags='-shared -L/usr/local/lib'
Characteristics of this binary (from libperl):
Compile-time options: PERL_MALLOC_WRAP USE_64_BIT_ALL USE_64_BIT_INT
USE_LARGE_FILES USE_PERLIO
Built under freebsd
Compiled at Aug 9 2008 22:37:57
@INC:
/usr/local/nf/lib/perl5/5.8.8/amd64-freebsd
/usr/local/nf/lib/perl5/5.8.8
/usr/local/nf/lib/perl5/site_perl/5.8.8/amd64-freebsd
/usr/local/nf/lib/perl5/site_perl/5.8.8
/usr/local/nf/lib/perl5/site_perl
.
Lastly, a Pureperl implementation that works for me is:
#!/usr/local/nf/bin/perl
use strict;
use warnings;
print djbhash($ARGV[0]), "\n";
sub djbhash {
my ($str) = @_;
my $hash = 5381;
my $c;
foreach $c (split(//, $str)) {
use integer;
$hash = ((((($hash << 5)& 0xFFFFFFFF) + $hash) & 0xFFFFFFFF)
+ ord($c)) & 0xFFFFFFFF;
}
$hash = $hash & 0xFFFFFFFF;
return $hash
}