Complete nginx source code annotation (6) Core/murmurhash

Source: Internet
Author: User

The following is from murmurhash2 on the open-source project home page of the murmurhash project of Google Code, which is used by nginx.

uint32_t MurmurHash2 ( const void * key, int len, uint32_t seed ){  // ‘m‘ and ‘r‘ are mixing constants generated offline.  // They‘re not really ‘magic‘, they just happen to work well.  const uint32_t m = 0x5bd1e995;  const int r = 24;  // Initialize the hash to a ‘random‘ value  uint32_t h = seed ^ len;  // Mix 4 bytes at a time into the hash  const unsigned char * data = (const unsigned char *)key;  while(len >= 4)  {    uint32_t k = *(uint32_t*)data;    k *= m;    k ^= k >> r;    k *= m;    h *= m;    h ^= k;    data += 4;    len -= 4;  }  // Handle the last few bytes of the input array  switch(len)  {  case 3: h ^= data[2] << 16;  case 2: h ^= data[1] << 8;  case 1: h ^= data[0];      h *= m;  };  // Do a few final mixes of the hash to ensure the last few  // bytes are well-incorporated.  h ^= h >> 13;  h *= m;  h ^= h >> 15;  return h;} 

Below is the source code of murmurhash in nginx, which is basically the same as above.

uint32_tngx_murmur_hash2(u_char *data, size_t len){    uint32_t  h, k;    h = 0 ^ len;    while (len >= 4) {        k  = data[0];        k |= data[1] << 8;        k |= data[2] << 16;        k |= data[3] << 24;        k *= 0x5bd1e995;        k ^= k >> 24;        k *= 0x5bd1e995;        h *= 0x5bd1e995;        h ^= k;        data += 4;        len -= 4;    }    switch (len) {    case 3:        h ^= data[2] << 16;    case 2:        h ^= data[1] << 8;    case 1:        h ^= data[0];        h *= 0x5bd1e995;    }    h ^= h >> 13;    h *= 0x5bd1e995;    h ^= h >> 15;    return h;}
Reference
  1. Murmurhash2

Complete nginx source code annotation (6) Core/murmurhash

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.