include/jhash.h (55 lines of code) (raw):

/* * Copyright (C) 2016, Facebook, Inc. * * Lifted from 4.4 Linux kernel source. Alterations for netconsd: * - Pulled in rol32() from linux/bitops.h * - Use stdint fixed-width types instead of kernel shorthand types * - Deleted unaligned jhash() because we don't use it and C++ hates it. */ #ifndef _LINUX_JHASH_H #define _LINUX_JHASH_H /* jhash.h: Jenkins hash support. * * Copyright (C) 2006. Bob Jenkins (bob_jenkins@burtleburtle.net) * * http://burtleburtle.net/bob/hash/ * * These are the credits from Bob's sources: * * lookup3.c, by Bob Jenkins, May 2006, Public Domain. * * These are functions for producing 32-bit hashes for hash table lookup. * hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final() * are externally useful functions. Routines to test the hash are included * if SELF_TEST is defined. You can use this free for any purpose. It's in * the public domain. It has no warranty. * * Copyright (C) 2009-2010 Jozsef Kadlecsik (kadlec@blackhole.kfki.hu) * * I've modified Bob's hash to be useful in the Linux kernel, and * any bugs present are my fault. * Jozsef */ #include <stdint.h> static inline uint32_t rol32(uint32_t word, unsigned int shift) { return (word << shift) | (word >> (32 - shift)); } /* Best hash sizes are of power of two */ #define jhash_size(n) ((uint32_t)1<<(n)) /* Mask the hash value, i.e (value & jhash_mask(n)) instead of (value % n) */ #define jhash_mask(n) (jhash_size(n)-1) /* __jhash_mix -- mix 3 32-bit values reversibly. */ #define __jhash_mix(a, b, c) \ { \ a -= c; a ^= rol32(c, 4); c += b; \ b -= a; b ^= rol32(a, 6); a += c; \ c -= b; c ^= rol32(b, 8); b += a; \ a -= c; a ^= rol32(c, 16); c += b; \ b -= a; b ^= rol32(a, 19); a += c; \ c -= b; c ^= rol32(b, 4); b += a; \ } /* __jhash_final - final mixing of 3 32-bit values (a,b,c) into c */ #define __jhash_final(a, b, c) \ { \ c ^= b; c -= rol32(b, 14); \ a ^= c; a -= rol32(c, 11); \ b ^= a; b -= rol32(a, 25); \ c ^= b; c -= rol32(b, 16); \ a ^= c; a -= rol32(c, 4); \ b ^= a; b -= rol32(a, 14); \ c ^= b; c -= rol32(b, 24); \ } /* * Arbitrary initial parameters */ #define JHASH_INITVAL 0xdeadbeef #define LISTEN_SEED 0xfaceb00c #define WORKER_SEED 0xb00cface /* jhash2 - hash an array of uint32_t's * @k: the key which must be an array of uint32_t's * @length: the number of uint32_t's in the key * @initval: the previous hash, or an arbitray value * * Returns the hash value of the key. */ static inline __attribute__((pure)) uint32_t jhash2(const uint32_t *k, uint32_t length, uint32_t initval) { uint32_t a, b, c; /* Set up the internal state */ a = b = c = JHASH_INITVAL + (length<<2) + initval; /* Handle most of the key */ while (length > 3) { a += k[0]; b += k[1]; c += k[2]; __jhash_mix(a, b, c); length -= 3; k += 3; } /* Handle the last 3 uint32_t's: all the case statements fall through */ switch (length) { case 3: c += k[2]; /* fall through */ case 2: b += k[1]; /* fall through */ case 1: a += k[0]; __jhash_final(a, b, c); case 0: /* Nothing left to add */ break; } return c; } #endif /* _LINUX_JHASH_H */