jhash.h

Go to the documentation of this file.
00001 /* jhash.h: Jenkins hash support.
00002  *
00003  * Copyright (C) 1996 Bob Jenkins (bob_jenkins@burtleburtle.net)
00004  *
00005  * $Id: jhash.h 2738 2007-02-17 13:59:56Z regit $
00006  *
00007  * http://burtleburtle.net/bob/hash/
00008  *
00009  * These are the credits from Bob's sources:
00010  *
00011  * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
00012  * hash(), hash2(), hash3, and mix() are externally useful functions.
00013  * Routines to test the hash are included if SELF_TEST is defined.
00014  * You can use this free for any purpose.  It has no warranty.
00015  *
00016  * Copyright (C) 2003 David S. Miller (davem@redhat.com)
00017  *
00018  * I've modified Bob's hash to be useful in the Linux kernel, and
00019  * any bugs present are surely my fault.  -DaveM
00020  */
00021 
00022 #ifndef _JHASH_HEADER
00023 #define _JHASH_HEADER
00024 
00025 #include <stdint.h>
00026 
00027 /* NOTE: Arguments are modified. */
00028 #define __jhash_mix(a, b, c) \
00029 { \
00030   a -= b; a -= c; a ^= (c>>13); \
00031   b -= c; b -= a; b ^= (a<<8); \
00032   c -= a; c -= b; c ^= (b>>13); \
00033   a -= b; a -= c; a ^= (c>>12);  \
00034   b -= c; b -= a; b ^= (a<<16); \
00035   c -= a; c -= b; c ^= (b>>5); \
00036   a -= b; a -= c; a ^= (c>>3);  \
00037   b -= c; b -= a; b ^= (a<<10); \
00038   c -= a; c -= b; c ^= (b>>15); \
00039 }
00040 /* The golden ration: an arbitrary value */
00041 #define JHASH_GOLDEN_RATIO      0x9e3779b9
00042 
00043 #ifdef USE_JHASH3
00044 
00051 static uint32_t jhash_3words(uint32_t a, uint32_t b, uint32_t c,
00052                              uint32_t initval)
00053 {
00054         a += JHASH_GOLDEN_RATIO;
00055         b += JHASH_GOLDEN_RATIO;
00056         c += initval;
00057 
00058         __jhash_mix(a, b, c);
00059 
00060         return c;
00061 }
00062 #endif                          /* USE_JHASH3 */
00063 
00064 #ifdef USE_JHASH2
00065 
00066 /* A special optimized version that handles 1 or more of uint32_ts.
00067  * The length parameter here is the number of uint32_ts in the key.
00068  */
00069 static inline uint32_t jhash2(uint32_t * k, uint32_t length,
00070                               uint32_t initval)
00071 {
00072         uint32_t a, b, c, len;
00073 
00074         a = b = JHASH_GOLDEN_RATIO;
00075         c = initval;
00076         len = length;
00077 
00078         while (len >= 3) {
00079                 a += k[0];
00080                 b += k[1];
00081                 c += k[2];
00082                 __jhash_mix(a, b, c);
00083                 k += 3;
00084                 len -= 3;
00085         }
00086 
00087         c += length * 4;
00088 
00089         switch (len) {
00090         case 2:
00091                 b += k[1];
00092         case 1:
00093                 a += k[0];
00094         };
00095 
00096         __jhash_mix(a, b, c);
00097 
00098         return c;
00099 }
00100 #endif                          /* USE_JHASH2 */
00101 
00102 #endif

Generated on Sat Nov 22 04:00:37 2008 for NuFW by  doxygen 1.4.7