Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: LGPL-2.1
0002  *
0003  * Based on Paul Hsieh's (LGPG 2.1) hash function
0004  * From: http://www.azillionmonkeys.com/qed/hash.html
0005  */
0006 
0007 #define get16bits(d) (*((const __u16 *) (d)))
0008 
0009 static __always_inline
0010 __u32 SuperFastHash (const char *data, int len, __u32 initval) {
0011     __u32 hash = initval;
0012     __u32 tmp;
0013     int rem;
0014 
0015     if (len <= 0 || data == NULL) return 0;
0016 
0017     rem = len & 3;
0018     len >>= 2;
0019 
0020     /* Main loop */
0021 #pragma clang loop unroll(full)
0022     for (;len > 0; len--) {
0023         hash  += get16bits (data);
0024         tmp    = (get16bits (data+2) << 11) ^ hash;
0025         hash   = (hash << 16) ^ tmp;
0026         data  += 2*sizeof (__u16);
0027         hash  += hash >> 11;
0028     }
0029 
0030     /* Handle end cases */
0031     switch (rem) {
0032         case 3: hash += get16bits (data);
0033                 hash ^= hash << 16;
0034                 hash ^= ((signed char)data[sizeof (__u16)]) << 18;
0035                 hash += hash >> 11;
0036                 break;
0037         case 2: hash += get16bits (data);
0038                 hash ^= hash << 11;
0039                 hash += hash >> 17;
0040                 break;
0041         case 1: hash += (signed char)*data;
0042                 hash ^= hash << 10;
0043                 hash += hash >> 1;
0044     }
0045 
0046     /* Force "avalanching" of final 127 bits */
0047     hash ^= hash << 3;
0048     hash += hash >> 5;
0049     hash ^= hash << 4;
0050     hash += hash >> 17;
0051     hash ^= hash << 25;
0052     hash += hash >> 6;
0053 
0054     return hash;
0055 }