💾 Archived View for gmi.noulin.net › gitRepositories › sodiumTest › file › murmurhash2.c.gmi captured on 2024-07-09 at 02:45:31. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-01-29)

-=-=-=-=-=-=-

sodiumTest

Log

Files

Refs

README

murmurhash2.c (1344B)

     1 //-----------------------------------------------------------------------------
     2 // MurmurHash2, by Austin Appleby
     3 
     4 // Note - This code makes a few assumptions about how your machine behaves -
     5 
     6 // 1. We can read a 4-byte value from any address without crashing
     7 // 2. sizeof(int) == 4
     8 
     9 // And it has a few limitations -
    10 
    11 // 1. It will not work incrementally.
    12 // 2. It will not produce the same results on little-endian and big-endian
    13 //    machines.
    14 
    15 unsigned int murmurhash2(const void * key, int len, const unsigned int seed)
    16 {
    17         // 'm' and 'r' are mixing constants generated offline.
    18         // They're not really 'magic', they just happen to work well.
    19 
    20         const unsigned int m = 0x5bd1e995;
    21         const int r = 24;
    22 
    23         // Initialize the hash to a 'random' value
    24 
    25         unsigned int h = seed ^ len;
    26 
    27         // Mix 4 bytes at a time into the hash
    28 
    29         const unsigned char * data = (const unsigned char *)key;
    30 
    31         while(len >= 4)
    32         {
    33                 unsigned int k = *(unsigned int *)data;
    34 
    35                 k *= m;
    36                 k ^= k >> r;
    37                 k *= m;
    38 
    39                 h *= m;
    40                 h ^= k;
    41 
    42                 data += 4;
    43                 len -= 4;
    44         }
    45 
    46         // Handle the last few bytes of the input array
    47 
    48         switch(len)
    49         {
    50         case 3: h ^= data[2] << 16;
    51         case 2: h ^= data[1] << 8;
    52         case 1: h ^= data[0];
    53                 h *= m;
    54         };
    55 
    56         // Do a few final mixes of the hash to ensure the last few
    57         // bytes are well-incorporated.
    58 
    59         h ^= h >> 13;
    60         h *= m;
    61         h ^= h >> 15;
    62 
    63         return h;
    64 }