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