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 | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 15 | void randhash_32 ( const void *, int, uint32_t, void * out )
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 16 | {
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 17 | ((uint32_t*)out)[0] = rand_u32();
|
| 18 | }
|
| 19 |
|
| 20 | void randhash_64 ( const void *, int, uint32_t, void * out )
|
| 21 | {
|
| 22 | ((uint32_t*)out)[0] = rand_u32();
|
| 23 | ((uint32_t*)out)[1] = rand_u32();
|
| 24 | }
|
| 25 |
|
| 26 | void randhash_128 ( const void *, int, uint32_t, void * out )
|
| 27 | {
|
| 28 | ((uint32_t*)out)[0] = rand_u32();
|
| 29 | ((uint32_t*)out)[1] = rand_u32();
|
| 30 | ((uint32_t*)out)[2] = rand_u32();
|
| 31 | ((uint32_t*)out)[3] = rand_u32();
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 32 | }
|
| 33 |
|
| 34 | void BadHash ( const void * key, int len, uint32_t seed, void * out )
|
| 35 | {
|
| 36 | uint32_t h = seed;
|
| 37 |
|
| 38 | const uint8_t * data = (const uint8_t*)key;
|
| 39 |
|
| 40 | for(int i = 0; i < len; i++)
|
| 41 | {
|
| 42 | h ^= h >> 3;
|
| 43 | h ^= h << 5;
|
| 44 | h ^= data[i];
|
| 45 | }
|
| 46 |
|
| 47 | *(uint32_t*)out = h;
|
| 48 | }
|
| 49 |
|
| 50 | void sumhash ( const void * key, int len, uint32_t seed, void * out )
|
| 51 | {
|
| 52 | uint32_t h = seed;
|
| 53 |
|
| 54 | const uint8_t * data = (const uint8_t*)key;
|
| 55 |
|
| 56 | for(int i = 0; i < len; i++)
|
| 57 | {
|
| 58 | h += data[i];
|
| 59 | }
|
| 60 |
|
| 61 | *(uint32_t*)out = h;
|
| 62 | }
|
| 63 |
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 64 | void sumhash32 ( const void * key, int len, uint32_t seed, void * out )
|
| 65 | {
|
| 66 | uint32_t h = seed;
|
| 67 |
|
| 68 | const uint32_t * data = (const uint32_t*)key;
|
| 69 |
|
| 70 | for(int i = 0; i < len/4; i++)
|
| 71 | {
|
| 72 | h += data[i];
|
| 73 | }
|
| 74 |
|
| 75 | *(uint32_t*)out = h;
|
| 76 | }
|
| 77 |
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 78 | void DoNothingHash ( const void *, int, uint32_t, void * )
|
| 79 | {
|
| 80 | return;
|
| 81 | }
|
| 82 |
|
| 83 | //-----------------------------------------------------------------------------
|
| 84 | // One-byte-at-a-time hash based on Murmur's mix
|
| 85 |
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 86 | 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] | 87 | {
|
| 88 | const uint8_t * data = (const uint8_t*)key;
|
| 89 |
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 90 | uint32_t h = seed ^ len;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 91 |
|
| 92 | for(int i = 0; i < len; i++)
|
| 93 | {
|
| 94 | h ^= data[i];
|
| 95 | h *= 0x5bd1e995;
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 96 | h ^= h >> 15;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 97 | }
|
| 98 |
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 99 | h *= 0x5bd1e995;
|
| 100 | h ^= h >> 15;
|
| 101 |
|
| 102 | *(uint32_t*)out = h;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 103 | }
|
| 104 |
|
| 105 | //----------------------------------------------------------------------------
|
| 106 |
|
| 107 | void FNV ( const void * key, int len, uint32_t seed, void * out )
|
| 108 | {
|
| 109 | unsigned int h = seed;
|
| 110 |
|
| 111 | const uint8_t * data = (const uint8_t*)key;
|
| 112 |
|
tanjent@gmail.com | 2aa29c3 | 2011-03-19 08:53:53 +0000 | [diff] [blame] | 113 | h ^= BIG_CONSTANT(2166136261);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 114 |
|
| 115 | for(int i = 0; i < len; i++)
|
| 116 | {
|
| 117 | h ^= data[i];
|
| 118 | h *= 16777619;
|
| 119 | }
|
| 120 |
|
| 121 | *(uint32_t*)out = h;
|
| 122 | }
|
| 123 |
|
| 124 | //-----------------------------------------------------------------------------
|
| 125 |
|
| 126 | uint32_t x17 ( const void * key, int len, uint32_t h )
|
| 127 | {
|
| 128 | const uint8_t * data = (const uint8_t*)key;
|
| 129 |
|
| 130 | for(int i = 0; i < len; ++i)
|
| 131 | {
|
| 132 | h = 17 * h + (data[i] - ' ');
|
| 133 | }
|
| 134 |
|
| 135 | return h ^ (h >> 16);
|
| 136 | }
|
| 137 |
|
| 138 | //-----------------------------------------------------------------------------
|
| 139 |
|
| 140 | uint32_t Bernstein ( const void * key, int len, uint32_t h )
|
| 141 | {
|
| 142 | const uint8_t * data = (const uint8_t*)key;
|
| 143 |
|
| 144 | for(int i = 0; i < len; ++i)
|
| 145 | {
|
| 146 | h = 33 * h + data[i];
|
| 147 | }
|
| 148 |
|
| 149 | return h;
|
| 150 | }
|
| 151 |
|
| 152 | //-----------------------------------------------------------------------------
|