1# Copyright (C) 2005, 2006, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
2# Copyright (C) 2006 Anders Carlsson <andersca@mac.com>
3# Copyright (C) 2006, 2007 Samuel Weinig <sam@webkit.org>
4# Copyright (C) 2006 Alexey Proskuryakov <ap@webkit.org>
5# Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
6# Copyright (C) 2009 Cameron McCormack <cam@mcc.id.au>
7# Copyright (C) Research In Motion Limited 2010. All rights reserved.
8# Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
9# Copyright (C) 2011 Patrick Gansterer <paroga@webkit.org>
10# Copyright (C) 2012 Ericsson AB. All rights reserved.
11#
12# This library is free software; you can redistribute it and/or
13# modify it under the terms of the GNU Library General Public
14# License as published by the Free Software Foundation; either
15# version 2 of the License, or (at your option) any later version.
16#
17# This library is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20# Library General Public License for more details.
21#
22# You should have received a copy of the GNU Library General Public License
23# along with this library; see the file COPYING.LIB.  If not, write to
24# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25# Boston, MA 02110-1301, USA.
26
27package Hasher;
28
29use strict;
30
31sub leftShift($$) {
32    my ($value, $distance) = @_;
33    return (($value << $distance) & 0xFFFFFFFF);
34}
35
36# Paul Hsieh's SuperFastHash
37# http://www.azillionmonkeys.com/qed/hash.html
38sub GenerateHashValue
39{
40    my @chars = split(/ */, $_[0]);
41
42    # This hash is designed to work on 16-bit chunks at a time. But since the normal case
43    # (above) is to hash UTF-16 characters, we just treat the 8-bit chars as if they
44    # were 16-bit chunks, which should give matching results
45
46    my $EXP2_32 = 4294967296;
47
48    my $hash = 0x9e3779b9;
49    my $l    = scalar @chars; #I wish this was in Ruby --- Maks
50    my $rem  = $l & 1;
51    $l = $l >> 1;
52
53    my $s = 0;
54
55    # Main loop
56    for (; $l > 0; $l--) {
57        $hash   += ord($chars[$s]);
58        my $tmp = leftShift(ord($chars[$s+1]), 11) ^ $hash;
59        $hash   = (leftShift($hash, 16)% $EXP2_32) ^ $tmp;
60        $s += 2;
61        $hash += $hash >> 11;
62        $hash %= $EXP2_32;
63    }
64
65    # Handle end case
66    if ($rem != 0) {
67        $hash += ord($chars[$s]);
68        $hash ^= (leftShift($hash, 11)% $EXP2_32);
69        $hash += $hash >> 17;
70    }
71
72    # Force "avalanching" of final 127 bits
73    $hash ^= leftShift($hash, 3);
74    $hash += ($hash >> 5);
75    $hash = ($hash% $EXP2_32);
76    $hash ^= (leftShift($hash, 2)% $EXP2_32);
77    $hash += ($hash >> 15);
78    $hash = $hash% $EXP2_32;
79    $hash ^= (leftShift($hash, 10)% $EXP2_32);
80
81    # Save 8 bits for StringImpl to use as flags.
82    $hash &= 0xffffff;
83
84    # This avoids ever returning a hash code of 0, since that is used to
85    # signal "hash not computed yet". Setting the high bit maintains
86    # reasonable fidelity to a hash code of 0 because it is likely to yield
87    # exactly 0 when hash lookup masks out the high bits.
88    $hash = (0x80000000 >> 8) if ($hash == 0);
89
90    return $hash;
91}
92
931;
94