Back to home page

OSCL-LXR

 
 

    


0001 #ifndef _LINUX_JHASH_H
0002 #define _LINUX_JHASH_H
0003 
0004 /* jhash.h: Jenkins hash support.
0005  *
0006  * Copyright (C) 2006. Bob Jenkins (bob_jenkins@burtleburtle.net)
0007  *
0008  * https://burtleburtle.net/bob/hash/
0009  *
0010  * These are the credits from Bob's sources:
0011  *
0012  * lookup3.c, by Bob Jenkins, May 2006, Public Domain.
0013  *
0014  * These are functions for producing 32-bit hashes for hash table lookup.
0015  * hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
0016  * are externally useful functions.  Routines to test the hash are included
0017  * if SELF_TEST is defined.  You can use this free for any purpose.  It's in
0018  * the public domain.  It has no warranty.
0019  *
0020  * Copyright (C) 2009-2010 Jozsef Kadlecsik (kadlec@netfilter.org)
0021  *
0022  * I've modified Bob's hash to be useful in the Linux kernel, and
0023  * any bugs present are my fault.
0024  * Jozsef
0025  */
0026 #include <linux/bitops.h>
0027 #include <linux/unaligned/packed_struct.h>
0028 
0029 /* Best hash sizes are of power of two */
0030 #define jhash_size(n)   ((u32)1<<(n))
0031 /* Mask the hash value, i.e (value & jhash_mask(n)) instead of (value % n) */
0032 #define jhash_mask(n)   (jhash_size(n)-1)
0033 
0034 /* __jhash_mix -- mix 3 32-bit values reversibly. */
0035 #define __jhash_mix(a, b, c)            \
0036 {                       \
0037     a -= c;  a ^= rol32(c, 4);  c += b; \
0038     b -= a;  b ^= rol32(a, 6);  a += c; \
0039     c -= b;  c ^= rol32(b, 8);  b += a; \
0040     a -= c;  a ^= rol32(c, 16); c += b; \
0041     b -= a;  b ^= rol32(a, 19); a += c; \
0042     c -= b;  c ^= rol32(b, 4);  b += a; \
0043 }
0044 
0045 /* __jhash_final - final mixing of 3 32-bit values (a,b,c) into c */
0046 #define __jhash_final(a, b, c)          \
0047 {                       \
0048     c ^= b; c -= rol32(b, 14);      \
0049     a ^= c; a -= rol32(c, 11);      \
0050     b ^= a; b -= rol32(a, 25);      \
0051     c ^= b; c -= rol32(b, 16);      \
0052     a ^= c; a -= rol32(c, 4);       \
0053     b ^= a; b -= rol32(a, 14);      \
0054     c ^= b; c -= rol32(b, 24);      \
0055 }
0056 
0057 /* An arbitrary initial parameter */
0058 #define JHASH_INITVAL       0xdeadbeef
0059 
0060 /* jhash - hash an arbitrary key
0061  * @k: sequence of bytes as key
0062  * @length: the length of the key
0063  * @initval: the previous hash, or an arbitray value
0064  *
0065  * The generic version, hashes an arbitrary sequence of bytes.
0066  * No alignment or length assumptions are made about the input key.
0067  *
0068  * Returns the hash value of the key. The result depends on endianness.
0069  */
0070 static inline u32 jhash(const void *key, u32 length, u32 initval)
0071 {
0072     u32 a, b, c;
0073     const u8 *k = key;
0074 
0075     /* Set up the internal state */
0076     a = b = c = JHASH_INITVAL + length + initval;
0077 
0078     /* All but the last block: affect some 32 bits of (a,b,c) */
0079     while (length > 12) {
0080         a += __get_unaligned_cpu32(k);
0081         b += __get_unaligned_cpu32(k + 4);
0082         c += __get_unaligned_cpu32(k + 8);
0083         __jhash_mix(a, b, c);
0084         length -= 12;
0085         k += 12;
0086     }
0087     /* Last block: affect all 32 bits of (c) */
0088     switch (length) {
0089     case 12: c += (u32)k[11]<<24;   fallthrough;
0090     case 11: c += (u32)k[10]<<16;   fallthrough;
0091     case 10: c += (u32)k[9]<<8; fallthrough;
0092     case 9:  c += k[8];     fallthrough;
0093     case 8:  b += (u32)k[7]<<24;    fallthrough;
0094     case 7:  b += (u32)k[6]<<16;    fallthrough;
0095     case 6:  b += (u32)k[5]<<8; fallthrough;
0096     case 5:  b += k[4];     fallthrough;
0097     case 4:  a += (u32)k[3]<<24;    fallthrough;
0098     case 3:  a += (u32)k[2]<<16;    fallthrough;
0099     case 2:  a += (u32)k[1]<<8; fallthrough;
0100     case 1:  a += k[0];
0101          __jhash_final(a, b, c);
0102          break;
0103     case 0: /* Nothing left to add */
0104         break;
0105     }
0106 
0107     return c;
0108 }
0109 
0110 /* jhash2 - hash an array of u32's
0111  * @k: the key which must be an array of u32's
0112  * @length: the number of u32's in the key
0113  * @initval: the previous hash, or an arbitray value
0114  *
0115  * Returns the hash value of the key.
0116  */
0117 static inline u32 jhash2(const u32 *k, u32 length, u32 initval)
0118 {
0119     u32 a, b, c;
0120 
0121     /* Set up the internal state */
0122     a = b = c = JHASH_INITVAL + (length<<2) + initval;
0123 
0124     /* Handle most of the key */
0125     while (length > 3) {
0126         a += k[0];
0127         b += k[1];
0128         c += k[2];
0129         __jhash_mix(a, b, c);
0130         length -= 3;
0131         k += 3;
0132     }
0133 
0134     /* Handle the last 3 u32's */
0135     switch (length) {
0136     case 3: c += k[2];  fallthrough;
0137     case 2: b += k[1];  fallthrough;
0138     case 1: a += k[0];
0139         __jhash_final(a, b, c);
0140         break;
0141     case 0: /* Nothing left to add */
0142         break;
0143     }
0144 
0145     return c;
0146 }
0147 
0148 
0149 /* __jhash_nwords - hash exactly 3, 2 or 1 word(s) */
0150 static inline u32 __jhash_nwords(u32 a, u32 b, u32 c, u32 initval)
0151 {
0152     a += initval;
0153     b += initval;
0154     c += initval;
0155 
0156     __jhash_final(a, b, c);
0157 
0158     return c;
0159 }
0160 
0161 static inline u32 jhash_3words(u32 a, u32 b, u32 c, u32 initval)
0162 {
0163     return __jhash_nwords(a, b, c, initval + JHASH_INITVAL + (3 << 2));
0164 }
0165 
0166 static inline u32 jhash_2words(u32 a, u32 b, u32 initval)
0167 {
0168     return __jhash_nwords(a, b, 0, initval + JHASH_INITVAL + (2 << 2));
0169 }
0170 
0171 static inline u32 jhash_1word(u32 a, u32 initval)
0172 {
0173     return __jhash_nwords(a, 0, 0, initval + JHASH_INITVAL + (1 << 2));
0174 }
0175 
0176 #endif /* _LINUX_JHASH_H */