Changeset 52768 in webkit for trunk/JavaScriptCore/wtf/HashFunctions.h
- Timestamp:
- Jan 4, 2010, 4:13:21 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/wtf/HashFunctions.h
r52761 r52768 23 23 24 24 #include "RefPtr.h" 25 #include "Unicode.h"26 25 #include <stdint.h> 27 26 … … 179 178 static const unsigned stringHashingStartValue = 0x9e3779b9U; 180 179 181 // stringHash methods based on Paul Hsieh's SuperFastHash.182 // http://www.azillionmonkeys.com/qed/hash.html183 // 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 loop192 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 case201 if (rem) {202 hash += data[0];203 hash ^= hash << 11;204 hash += hash >> 17;205 }206 207 // Force "avalanching" of final 127 bits208 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 to215 // signal "hash not computed yet", using a value that is likely to be216 // effectively the same as 0 when the low bits are masked217 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 loop230 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 case239 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 bits246 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 to253 // signal "hash not computed yet", using a value that is likely to be254 // effectively the same as 0 when the low bits are masked255 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 loop266 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 to292 // signal "hash not computed yet", using a value that is likely to be293 // effectively the same as 0 when the low bits are masked.294 if (hash == 0)295 hash = 0x80000000;296 297 return hash;298 }299 300 180 } // namespace WTF 301 181
Note:
See TracChangeset
for help on using the changeset viewer.