Ignore:
Timestamp:
Jan 4, 2010, 2:00:38 PM (15 years ago)
Author:
[email protected]
Message:

JavaScriptCore: https://bugs.webkit.org/show_bug.cgi?id=33163
Add string hashing functions to WTF.
Use WTF's string hashing functions from UStringImpl.

Reviewed by Sam Weinig.

(JSC::UStringImpl::computeHash):

  • wtf/HashFunctions.h:

(WTF::stringHash):

WebCore: https://bugs.webkit.org/show_bug.cgi?id=33163
Use WTF's string hashing functions from StringImpl.

Reviewed by Sam Weinig.

  • platform/text/StringImpl.h:

(WebCore::StringImpl::computeHash):

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/wtf/HashFunctions.h

    r39563 r52758  
    2323
    2424#include "RefPtr.h"
     25#include "wtf/unicode/Unicode.h"
    2526#include <stdint.h>
    2627
     
    178179    static const unsigned stringHashingStartValue = 0x9e3779b9U;
    179180
     181    // stringHash methods based on Paul Hsieh's SuperFastHash.
     182    // http://www.azillionmonkeys.com/qed/hash.html
     183    // char* data is interpreted as latin-encoded (zero extended to 16 bits).
     184
     185    inline unsigned stringHash(const UChar* data, unsigned length)
     186    {
     187        unsigned hash = WTF::stringHashingStartValue;
     188        unsigned rem = length & 1;
     189        length >>= 1;
     190
     191        // Main loop
     192        for (; length > 0; length--) {
     193            hash += data[0];
     194            unsigned tmp = (data[1] << 11) ^ hash;
     195            hash = (hash << 16) ^ tmp;
     196            data += 2;
     197            hash += hash >> 11;
     198        }
     199
     200        // Handle end case
     201        if (rem) {
     202            hash += data[0];
     203            hash ^= hash << 11;
     204            hash += hash >> 17;
     205        }
     206
     207        // Force "avalanching" of final 127 bits
     208        hash ^= hash << 3;
     209        hash += hash >> 5;
     210        hash ^= hash << 2;
     211        hash += hash >> 15;
     212        hash ^= hash << 10;
     213
     214        // this avoids ever returning a hash code of 0, since that is used to
     215        // signal "hash not computed yet", using a value that is likely to be
     216        // effectively the same as 0 when the low bits are masked
     217        if (hash == 0)
     218            hash = 0x80000000;
     219
     220        return hash;
     221    }
     222
     223    inline unsigned stringHash(const char* data, unsigned length)
     224    {
     225        unsigned hash = WTF::stringHashingStartValue;
     226        unsigned rem = length & 1;
     227        length >>= 1;
     228
     229        // Main loop
     230        for (; length > 0; length--) {
     231            hash += static_cast<unsigned char>(data[0]);
     232            unsigned tmp = (static_cast<unsigned char>(data[1]) << 11) ^ hash;
     233            hash = (hash << 16) ^ tmp;
     234            data += 2;
     235            hash += hash >> 11;
     236        }
     237
     238        // Handle end case
     239        if (rem) {
     240            hash += static_cast<unsigned char>(data[0]);
     241            hash ^= hash << 11;
     242            hash += hash >> 17;
     243        }
     244
     245        // Force "avalanching" of final 127 bits
     246        hash ^= hash << 3;
     247        hash += hash >> 5;
     248        hash ^= hash << 2;
     249        hash += hash >> 15;
     250        hash ^= hash << 10;
     251
     252        // this avoids ever returning a hash code of 0, since that is used to
     253        // signal "hash not computed yet", using a value that is likely to be
     254        // effectively the same as 0 when the low bits are masked
     255        if (hash == 0)
     256            hash = 0x80000000;
     257
     258        return hash;
     259    }
     260
     261    inline unsigned stringHash(const char* data)
     262    {
     263        unsigned hash = WTF::stringHashingStartValue;
     264       
     265        // Main loop
     266        for (;;) {
     267            unsigned char b0 = data[0];
     268            if (!b0)
     269                break;
     270            unsigned char b1 = data[1];
     271            if (!b1) {
     272                hash += b0;
     273                hash ^= hash << 11;
     274                hash += hash >> 17;
     275                break;
     276            }
     277            hash += b0;
     278            unsigned tmp = (b1 << 11) ^ hash;
     279            hash = (hash << 16) ^ tmp;
     280            data += 2;
     281            hash += hash >> 11;
     282        }
     283       
     284        // Force "avalanching" of final 127 bits.
     285        hash ^= hash << 3;
     286        hash += hash >> 5;
     287        hash ^= hash << 2;
     288        hash += hash >> 15;
     289        hash ^= hash << 10;
     290
     291        // This avoids ever returning a hash code of 0, since that is used to
     292        // signal "hash not computed yet", using a value that is likely to be
     293        // effectively the same as 0 when the low bits are masked.
     294        if (hash == 0)
     295            hash = 0x80000000;
     296       
     297        return hash;
     298    }
     299
    180300} // namespace WTF
    181301
Note: See TracChangeset for help on using the changeset viewer.