source: trunk/target/linux/generic-2.6/patches-2.6.28/201-jhash3.patch @ 15128

Last change on this file since 15128 was 15128, checked in by nbd, 7 years ago

kernel: replace jhash2 with the faster jhash3 algorithm

File size: 6.2 KB
  • include/linux/jhash.h

    a b  
    33 
    44/* jhash.h: Jenkins hash support. 
    55 * 
    6  * Copyright (C) 1996 Bob Jenkins (bob_jenkins@burtleburtle.net) 
     6 * Copyright (C) 2006. Bob Jenkins (bob_jenkins@burtleburtle.net) 
    77 * 
    88 * http://burtleburtle.net/bob/hash/ 
    99 * 
    1010 * These are the credits from Bob's sources: 
    1111 * 
    12  * lookup2.c, by Bob Jenkins, December 1996, Public Domain. 
    13  * hash(), hash2(), hash3, and mix() are externally useful functions. 
    14  * Routines to test the hash are included if SELF_TEST is defined. 
    15  * You can use this free for any purpose.  It has no warranty. 
     12 * lookup3.c, by Bob Jenkins, May 2006, Public Domain. 
    1613 * 
    17  * Copyright (C) 2003 David S. Miller (davem@redhat.com) 
     14 * These are functions for producing 32-bit hashes for hash table lookup. 
     15 * hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()  
     16 * are externally useful functions.  Routines to test the hash are included  
     17 * if SELF_TEST is defined.  You can use this free for any purpose.  It's in 
     18 * the public domain.  It has no warranty. 
     19 * 
     20 * Copyright (C) 2009 Jozsef Kadlecsik (kadlec@blackhole.kfki.hu) 
    1821 * 
    1922 * I've modified Bob's hash to be useful in the Linux kernel, and 
    20  * any bugs present are surely my fault.  -DaveM 
     23 * any bugs present are my fault.  Jozsef 
    2124 */ 
    2225 
    23 /* NOTE: Arguments are modified. */ 
    24 #define __jhash_mix(a, b, c) \ 
     26#define __rot(x,k) (((x)<<(k)) | ((x)>>(32-(k)))) 
     27 
     28/* __jhash_mix - mix 3 32-bit values reversibly. */ 
     29#define __jhash_mix(a,b,c) \ 
     30{ \ 
     31  a -= c;  a ^= __rot(c, 4);  c += b; \ 
     32  b -= a;  b ^= __rot(a, 6);  a += c; \ 
     33  c -= b;  c ^= __rot(b, 8);  b += a; \ 
     34  a -= c;  a ^= __rot(c,16);  c += b; \ 
     35  b -= a;  b ^= __rot(a,19);  a += c; \ 
     36  c -= b;  c ^= __rot(b, 4);  b += a; \ 
     37} 
     38 
     39/* __jhash_final - final mixing of 3 32-bit values (a,b,c) into c */ 
     40#define __jhash_final(a,b,c) \ 
    2541{ \ 
    26   a -= b; a -= c; a ^= (c>>13); \ 
    27   b -= c; b -= a; b ^= (a<<8); \ 
    28   c -= a; c -= b; c ^= (b>>13); \ 
    29   a -= b; a -= c; a ^= (c>>12);  \ 
    30   b -= c; b -= a; b ^= (a<<16); \ 
    31   c -= a; c -= b; c ^= (b>>5); \ 
    32   a -= b; a -= c; a ^= (c>>3);  \ 
    33   b -= c; b -= a; b ^= (a<<10); \ 
    34   c -= a; c -= b; c ^= (b>>15); \ 
     42  c ^= b; c -= __rot(b,14); \ 
     43  a ^= c; a -= __rot(c,11); \ 
     44  b ^= a; b -= __rot(a,25); \ 
     45  c ^= b; c -= __rot(b,16); \ 
     46  a ^= c; a -= __rot(c,4);  \ 
     47  b ^= a; b -= __rot(a,14); \ 
     48  c ^= b; c -= __rot(b,24); \ 
    3549} 
    3650 
    37 /* The golden ration: an arbitrary value */ 
    38 #define JHASH_GOLDEN_RATIO      0x9e3779b9 
     51/* An arbitrary initial parameter */ 
     52#define JHASH_INIT_PARAM        0xdeadbeef 
    3953 
    4054/* The most generic version, hashes an arbitrary sequence 
    4155 * of bytes.  No alignment or length assumptions are made about 
    42  * the input key. 
     56 * the input key. The result depends on endianness. 
    4357 */ 
    4458static inline u32 jhash(const void *key, u32 length, u32 initval) 
    4559{ 
    46         u32 a, b, c, len; 
     60        u32 a,b,c; 
    4761        const u8 *k = key; 
    4862 
    49         len = length; 
    50         a = b = JHASH_GOLDEN_RATIO; 
    51         c = initval; 
    52  
    53         while (len >= 12) { 
    54                 a += (k[0] +((u32)k[1]<<8) +((u32)k[2]<<16) +((u32)k[3]<<24)); 
    55                 b += (k[4] +((u32)k[5]<<8) +((u32)k[6]<<16) +((u32)k[7]<<24)); 
    56                 c += (k[8] +((u32)k[9]<<8) +((u32)k[10]<<16)+((u32)k[11]<<24)); 
    57  
    58                 __jhash_mix(a,b,c); 
     63        /* Set up the internal state */ 
     64        a = b = c = JHASH_INIT_PARAM + length + initval; 
    5965 
     66        /* all but the last block: affect some 32 bits of (a,b,c) */ 
     67        while (length > 12) { 
     68                a += (k[0] + ((u32)k[1]<<8) + ((u32)k[2]<<16) + ((u32)k[3]<<24)); 
     69                b += (k[4] + ((u32)k[5]<<8) + ((u32)k[6]<<16) + ((u32)k[7]<<24)); 
     70                c += (k[8] + ((u32)k[9]<<8) + ((u32)k[10]<<16) + ((u32)k[11]<<24)); 
     71                __jhash_mix(a, b, c); 
     72                length -= 12; 
    6073                k += 12; 
    61                 len -= 12; 
    6274        } 
    6375 
    64         c += length; 
    65         switch (len) { 
    66         case 11: c += ((u32)k[10]<<24); 
    67         case 10: c += ((u32)k[9]<<16); 
    68         case 9 : c += ((u32)k[8]<<8); 
    69         case 8 : b += ((u32)k[7]<<24); 
    70         case 7 : b += ((u32)k[6]<<16); 
    71         case 6 : b += ((u32)k[5]<<8); 
     76        /* last block: affect all 32 bits of (c) */ 
     77        /* all the case statements fall through */ 
     78        switch (length) { 
     79        case 12: c += (u32)k[11]<<24; 
     80        case 11: c += (u32)k[10]<<16; 
     81        case 10: c += (u32)k[9]<<8; 
     82        case 9 : c += k[8]; 
     83        case 8 : b += (u32)k[7]<<24; 
     84        case 7 : b += (u32)k[6]<<16; 
     85        case 6 : b += (u32)k[5]<<8; 
    7286        case 5 : b += k[4]; 
    73         case 4 : a += ((u32)k[3]<<24); 
    74         case 3 : a += ((u32)k[2]<<16); 
    75         case 2 : a += ((u32)k[1]<<8); 
     87        case 4 : a += (u32)k[3]<<24; 
     88        case 3 : a += (u32)k[2]<<16; 
     89        case 2 : a += (u32)k[1]<<8; 
    7690        case 1 : a += k[0]; 
    77         }; 
    78  
    79         __jhash_mix(a,b,c); 
     91                __jhash_final(a, b, c); 
     92        case 0 : 
     93                break; 
     94        } 
    8095 
    8196        return c; 
    8297} 
    static inline u32 jhash(const void *key, 
    86101 */ 
    87102static inline u32 jhash2(const u32 *k, u32 length, u32 initval) 
    88103{ 
    89         u32 a, b, c, len; 
     104        u32 a, b, c; 
    90105 
    91         a = b = JHASH_GOLDEN_RATIO; 
    92         c = initval; 
    93         len = length; 
     106        /* Set up the internal state */ 
     107        a = b = c = JHASH_INIT_PARAM + (length<<2) + initval; 
    94108 
    95         while (len >= 3) { 
     109        /* handle most of the key */ 
     110        while (length > 3) { 
    96111                a += k[0]; 
    97112                b += k[1]; 
    98113                c += k[2]; 
    99114                __jhash_mix(a, b, c); 
    100                 k += 3; len -= 3; 
     115                length -= 3; 
     116                k += 3; 
    101117        } 
    102118 
    103         c += length * 4; 
    104  
    105         switch (len) { 
    106         case 2 : b += k[1]; 
    107         case 1 : a += k[0]; 
    108         }; 
    109  
    110         __jhash_mix(a,b,c); 
     119        /* handle the last 3 u32's */ 
     120        /* all the case statements fall through */  
     121        switch (length) { 
     122        case 3: c += k[2]; 
     123        case 2: b += k[1]; 
     124        case 1: a += k[0]; 
     125                __jhash_final(a, b, c); 
     126        case 0:     /* case 0: nothing left to add */ 
     127                break; 
     128        } 
    111129 
    112130        return c; 
    113131} 
    114132 
    115  
    116133/* A special ultra-optimized versions that knows they are hashing exactly 
    117134 * 3, 2 or 1 word(s). 
    118  * 
    119  * NOTE: In partilar the "c += length; __jhash_mix(a,b,c);" normally 
    120  *       done at the end is not done here. 
    121135 */ 
    122136static inline u32 jhash_3words(u32 a, u32 b, u32 c, u32 initval) 
    123137{ 
    124         a += JHASH_GOLDEN_RATIO; 
    125         b += JHASH_GOLDEN_RATIO; 
    126         c += initval; 
     138        a += JHASH_INIT_PARAM + initval; 
     139        b += JHASH_INIT_PARAM + initval; 
     140        c += JHASH_INIT_PARAM + initval; 
    127141 
    128         __jhash_mix(a, b, c); 
     142        __jhash_final(a, b, c); 
    129143 
    130144        return c; 
    131145} 
    132146 
    133147static inline u32 jhash_2words(u32 a, u32 b, u32 initval) 
    134148{ 
    135         return jhash_3words(a, b, 0, initval); 
     149        return jhash_3words(0, a, b, initval); 
    136150} 
    137151 
    138152static inline u32 jhash_1word(u32 a, u32 initval) 
    139153{ 
    140         return jhash_3words(a, 0, 0, initval); 
     154        return jhash_3words(0, 0, a, initval); 
    141155} 
    142156 
    143157#endif /* _LINUX_JHASH_H */ 
Note: See TracBrowser for help on using the repository browser.