Changeset 52758 in webkit for trunk/JavaScriptCore/runtime/UStringImpl.cpp
- Timestamp:
- Jan 4, 2010, 2:00:38 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/runtime/UStringImpl.cpp
r52472 r52758 82 82 } 83 83 84 // Golden ratio - arbitrary start value to avoid mapping all 0's to all 0's85 // or anything like that.86 const unsigned PHI = 0x9e3779b9U;87 88 // Paul Hsieh's SuperFastHash89 // http://www.azillionmonkeys.com/qed/hash.html90 unsigned UStringImpl::computeHash(const UChar* s, int len)91 {92 unsigned l = len;93 uint32_t hash = PHI;94 uint32_t tmp;95 96 int rem = l & 1;97 l >>= 1;98 99 // Main loop100 for (; l > 0; l--) {101 hash += s[0];102 tmp = (s[1] << 11) ^ hash;103 hash = (hash << 16) ^ tmp;104 s += 2;105 hash += hash >> 11;106 }107 108 // Handle end case109 if (rem) {110 hash += s[0];111 hash ^= hash << 11;112 hash += hash >> 17;113 }114 115 // Force "avalanching" of final 127 bits116 hash ^= hash << 3;117 hash += hash >> 5;118 hash ^= hash << 2;119 hash += hash >> 15;120 hash ^= hash << 10;121 122 // this avoids ever returning a hash code of 0, since that is used to123 // signal "hash not computed yet", using a value that is likely to be124 // effectively the same as 0 when the low bits are masked125 if (hash == 0)126 hash = 0x80000000;127 128 return hash;129 84 } 130 131 // Paul Hsieh's SuperFastHash132 // http://www.azillionmonkeys.com/qed/hash.html133 unsigned UStringImpl::computeHash(const char* s, int l)134 {135 // This hash is designed to work on 16-bit chunks at a time. But since the normal case136 // (above) is to hash UTF-16 characters, we just treat the 8-bit chars as if they137 // were 16-bit chunks, which should give matching results138 139 uint32_t hash = PHI;140 uint32_t tmp;141 142 size_t rem = l & 1;143 l >>= 1;144 145 // Main loop146 for (; l > 0; l--) {147 hash += static_cast<unsigned char>(s[0]);148 tmp = (static_cast<unsigned char>(s[1]) << 11) ^ hash;149 hash = (hash << 16) ^ tmp;150 s += 2;151 hash += hash >> 11;152 }153 154 // Handle end case155 if (rem) {156 hash += static_cast<unsigned char>(s[0]);157 hash ^= hash << 11;158 hash += hash >> 17;159 }160 161 // Force "avalanching" of final 127 bits162 hash ^= hash << 3;163 hash += hash >> 5;164 hash ^= hash << 2;165 hash += hash >> 15;166 hash ^= hash << 10;167 168 // this avoids ever returning a hash code of 0, since that is used to169 // signal "hash not computed yet", using a value that is likely to be170 // effectively the same as 0 when the low bits are masked171 if (hash == 0)172 hash = 0x80000000;173 174 return hash;175 }176 177 }
Note:
See TracChangeset
for help on using the changeset viewer.