Hacker.xcc
Benoit Dupont de Dinechin (Benoit.Dupont-de-Dinechin@st.com)
Copyright 2007 STMicroelectronics. Copyright 1995 - 1998 Commissariat a l'Energie Atomique.
This program is free software; you can redistribute it and/or modify it under the terms of either (at your option): the GNU General Public License (GPL) version 2; the GNU Lesser General Public License (LGPL) version 2.1; any later version of these licences as published by the Free Software Foundation.
HackerU16_CLP2— Macro to round a uint16_t to the nearest upper power of 2.
From ``Hacker's Delight'', H. S. Warren, Addison Wesley Professional 2003. Due to the large size of the expanded macro, only apply to constant arguments.
#define HackerU16_CLP2a(x) ((x) - 1) #define HackerU16_CLP2b(x) ((x) | ((x)>>1)) #define HackerU16_CLP2c(x) ((x) | ((x)>>2)) #define HackerU16_CLP2d(x) ((x) | ((x)>>4)) #define HackerU16_CLP2e(x) ((x) | ((x)>>8)) #define uint16_CLP2(x) \ (HackerU16_CLP2e(HackerU16_CLP2d(HackerU16_CLP2c(HackerU16_CLP2b(HackerU16_CLP2a(x))))) + 1)
HackerU32_popc— Count the number of bits set in a uint32_t.
static inline int HackerU32_popc(uint32_t word) { uint32_t v = word; uint32_t w = v - ((v >> 1) & 0x55555555); uint32_t x = (w & 0x33333333) + ((w >> 2) & 0x33333333); uint32_t y = (x + (x >> 4)) & 0x0F0F0F0F; uint16_t z = (y * 0x01010101) >> 24; return z; }
HackerU32_flp2— Round down to the nearest power of 2.
static inline int HackerU32_flp2(uint32_t word) { uint32_t x = word; x |= (x >> 1); x |= (x >> 2); x |= (x >> 4); x |= (x >> 8); x |= (x >> 16); x -= (x >> 1); return x; }
HackerU32_clp2— Round up to the nearest power of 2.
static inline int HackerU32_clp2(uint32_t word) { uint32_t x = word - 1; x |= (x >> 1); x |= (x >> 2); x |= (x >> 4); x |= (x >> 8); x |= (x >> 16); return x + 1; }
HackerU64_popc— Count the number of bits set in a uint64_t.
See http://www.caam.rice.edu/~dougm/twiddle/HackerU64_popc.html.
static inline int HackerU64_popc(uint64_t word) { uint32_t word0 = word, word1 = word >> 32; int popc = HackerU32_popc(word0) + HackerU32_popc(word1); Except_CHECK(popc == HackerU64_popcBasic(word)); return popc; }
Hacker_hashELF— The published hash algorithm used in the UNIX ELF format.
Accepts a pointer to a string to be hashed and returns an 32-bit unsigned.
uint32_t Hacker_hashELF(const char *name);
Hacker_hashFNV32— 32-bit Fowler / Noll / Vo (FNV) hash algorithm.
See http://www.isthe.com/chongo/tech/comp/fnv/.
#define Hacker_hashFNV32_PRIME 16777619U // FNV prime for 32-bit hash. #define Hacker_hashFNV32_OFFSET 2166136261U // FNV offset for 32-bit hash. uint32_t Hacker_hashFNV32(const char *name);
Hacker_mixJenkins— mix 3 32-bit values reversibly.
By Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this code any way you wish, private, educational, or commercial. It's free.
For every delta with one or two bits set, and the deltas of all three high bits or all three low bits, whether the original value of a,b,c is almost all zero or is uniformly distributed,
* If Hacker_mixJenkins is run forward or backward, at least 32 bits in a,b,c have at least 1/4 probability of changing.
* If Hacker_mixJenkins is run forward, every bit of c will change between 1/3 and 2/3 of the time. (Well, 22/100 and 78/100 for some 2-bit deltas.)
Hacker_mixJenkins was built out of 36 single-cycle latency instructions in a structure that could supported 2x parallelism, like so:
a -= b; a -= c; x = (c>>13); b -= c; a ^= x; b -= a; x = (a<<8); c -= a; b ^= x; c -= b; x = (b>>13); ...
Unfortunately, superscalar Pentiums and Sparcs can't take advantage of that parallelism. They've also turned some of those single-cycle latency instructions into multi-cycle latency instructions. Still, this is the fastest good hash I could find. There were about 2^^68 to choose from. I only looked at a billion or so.
#define Hacker_mixJenkins(a,b,c) \ { \ a -= b; a -= c; a ^= (c>>13); \ b -= c; b -= a; b ^= (a<<8); \ c -= a; c -= b; c ^= (b>>13); \ a -= b; a -= c; a ^= (c>>12); \ b -= c; b -= a; b ^= (a<<16); \ c -= a; c -= b; c ^= (b>>5); \ a -= b; a -= c; a ^= (c>>3); \ b -= c; b -= a; b ^= (a<<10); \ c -= a; c -= b; c ^= (b>>15); \ }
Hacker_hashJenkins— Hash a variable-length key into a 32-bit value.
By Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this code any way you wish, private, educational, or commercial. It's free.
- key
- The key (the unaligned variable-length array of bytes).
- length
- The length of the key, counting by bytes.
- initval
- Can be any 4-byte value.
- Return
- a 32-bit value.
Every bit of the key affects every bit of the return value. Every 1-bit and 2-bit delta achieves avalanche. About 6*len+35 instructions.
The best hash table sizes are powers of 2. There is no need to do mod a prime (mod is sooo slow!). If you need less than 32 bits, use a bitmask. For example, if you need only 10 bits, do
h = (h & hashmask(10));
In which case, the hash table should have hashsize(10) elements.
If you are hashing n strings (ub1 **)k, do it like this:
@verbatim
for (i See http://burtleburtle.net/bob/hash/evahash.html Use for hash table lookup, or anything where one collision in 2^^32 is
acceptable. Do NOT use for cryptographic purposes.0, h
0; iuint32_t
Hacker_hashJenkins(const uint8_t *key, uint32_t length, uint32_t initval);
HackerPtr_Sort— Function used to merge sort a HackerPtr array.
void
HackerPtr_Sort(HackerPtr array[], int32_t first, int32_t last, HackerPtrCompare cmp);