tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 1 | #include "Hashes.h"
|
| 2 |
|
| 3 | #include "Random.h"
|
| 4 |
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 5 |
|
| 6 | #include <stdlib.h>
|
| 7 | //#include <stdint.h>
|
| 8 | #include <assert.h>
|
tanjent@gmail.com | 2aa29c3 | 2011-03-19 08:53:53 +0000 | [diff] [blame] | 9 | //#include <emmintrin.h>
|
| 10 | //#include <xmmintrin.h>
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 11 |
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 12 | //----------------------------------------------------------------------------
|
| 13 | // fake / bad hashes
|
| 14 |
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 15 | void BadHash ( const void * key, int len, uint32_t seed, void * out )
|
| 16 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 17 | uint32_t h = seed;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 18 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 19 | const uint8_t * data = (const uint8_t*)key;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 20 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 21 | for(int i = 0; i < len; i++)
|
| 22 | {
|
| 23 | h ^= h >> 3;
|
| 24 | h ^= h << 5;
|
| 25 | h ^= data[i];
|
| 26 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 27 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 28 | *(uint32_t*)out = h;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 29 | }
|
| 30 |
|
| 31 | void sumhash ( const void * key, int len, uint32_t seed, void * out )
|
| 32 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 33 | uint32_t h = seed;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 34 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 35 | const uint8_t * data = (const uint8_t*)key;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 36 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 37 | for(int i = 0; i < len; i++)
|
| 38 | {
|
| 39 | h += data[i];
|
| 40 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 41 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 42 | *(uint32_t*)out = h;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 43 | }
|
| 44 |
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 45 | void sumhash32 ( const void * key, int len, uint32_t seed, void * out )
|
| 46 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 47 | uint32_t h = seed;
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 48 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 49 | const uint32_t * data = (const uint32_t*)key;
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 50 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 51 | for(int i = 0; i < len/4; i++)
|
| 52 | {
|
| 53 | h += data[i];
|
| 54 | }
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 55 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 56 | *(uint32_t*)out = h;
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 57 | }
|
| 58 |
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 59 | void DoNothingHash ( const void *, int, uint32_t, void * )
|
| 60 | {
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 61 | }
|
| 62 |
|
| 63 | //-----------------------------------------------------------------------------
|
| 64 | // One-byte-at-a-time hash based on Murmur's mix
|
| 65 |
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 66 | void MurmurOAAT ( const void * key, int len, uint32_t seed, void * out )
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 67 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 68 | const uint8_t * data = (const uint8_t*)key;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 69 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 70 | uint32_t h = seed ^ len;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 71 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 72 | for(int i = 0; i < len; i++)
|
| 73 | {
|
| 74 | h ^= data[i];
|
| 75 | h *= 0x5bd1e995;
|
| 76 | h ^= h >> 15;
|
| 77 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 78 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 79 | h *= 0x5bd1e995;
|
| 80 | h ^= h >> 15;
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 81 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 82 | *(uint32_t*)out = h;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 83 | }
|
| 84 |
|
| 85 | //----------------------------------------------------------------------------
|
| 86 |
|
| 87 | void FNV ( const void * key, int len, uint32_t seed, void * out )
|
| 88 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 89 | unsigned int h = seed;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 90 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 91 | const uint8_t * data = (const uint8_t*)key;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 92 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 93 | h ^= BIG_CONSTANT(2166136261);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 94 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 95 | for(int i = 0; i < len; i++)
|
| 96 | {
|
| 97 | h ^= data[i];
|
| 98 | h *= 16777619;
|
| 99 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 100 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 101 | *(uint32_t*)out = h;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 102 | }
|
| 103 |
|
| 104 | //-----------------------------------------------------------------------------
|
| 105 |
|
| 106 | uint32_t x17 ( const void * key, int len, uint32_t h )
|
| 107 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 108 | const uint8_t * data = (const uint8_t*)key;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 109 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 110 | for(int i = 0; i < len; ++i)
|
| 111 | {
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 112 | h = 17 * h + (data[i] - ' ');
|
| 113 | }
|
| 114 |
|
| 115 | return h ^ (h >> 16);
|
| 116 | }
|
| 117 |
|
| 118 | //-----------------------------------------------------------------------------
|
| 119 |
|
| 120 | uint32_t Bernstein ( const void * key, int len, uint32_t h )
|
| 121 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 122 | const uint8_t * data = (const uint8_t*)key;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 123 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 124 | for(int i = 0; i < len; ++i)
|
| 125 | {
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 126 | h = 33 * h + data[i];
|
| 127 | }
|
| 128 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 129 | return h;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 130 | }
|
| 131 |
|
| 132 | //-----------------------------------------------------------------------------
|