下面是摘自 Google Code 的 Murmurhash 开源项目主页上的 Murmurhash2,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;
}

下面是 Nginx 中 Murmurhash 的源码,基本与上面无异。


uint32_t
ngx_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

Nginx源码完全注释(6)core/murmurhash的更多相关文章

  1. Nginx 源码完全注释(11)ngx_spinlock

    Nginx 是多进程模式的,一个 master 与多个 workers,一般工作在多核 CPU 上,所以自旋锁就是必须用到的.Nginx 中的自旋锁的定义,位于 ngx_spinlock.c 中,如下 ...

  2. Nginx源码完全注释(5)core/ngx_cpuinfo.c

    /* * Copyright (C) Igor Sysoev * Copyright (C) Nginx, Inc. */ #include <ngx_config.h> #include ...

  3. Nginx 源码完全注释(10)ngx_radix_tree

    ngx_radix_tree.h // 未被使用的节点 #define NGX_RADIX_NO_VALUE (uintptr_t) -1 typedef struct ngx_radix_node_ ...

  4. Nginx源码完全注释(9)nginx.c: ngx_get_options

    本文分析 ngxin.c 中的 ngx_get_options 函数,其影响: nginx.c 中的: static ngx_uint_t ngx_show_help; static ngx_uint ...

  5. Nginx源码完全注释(8)ngx_errno.c

    errno.h中的strerror(int errno)可以确定指定的errno的错误的提示信息.在 Nginx 中,将所有错误提示信息预先存储在一个数组里,而预先确定这个数组的大小,是在自动化脚本中 ...

  6. Nginx源码完全注释(2)ngx_array.h / ngx_array.c

    数组头文件 ngx_array.h #include <ngx_config.h> #include <ngx_core.h> struct ngx_array_s { voi ...

  7. nginx源码完全注释(1)ngx_alloc.h / ngx_alloc.c

    首先看 ngx_alloc.h 文件,主要声明或宏定义了 ngx_alloc,ngx_calloc,ngx_memalign,ngx_free. /* * Copyright (C) Igor Sys ...

  8. Nginx源码完全注释(7)ngx_palloc.h/ngx_palloc.c

    ngx_palloc.h /* * NGX_MAX_ALLOC_FROM_POOL should be (ngx_pagesize - 1), i.e. 4095 on x86. * On Windo ...

  9. Nginx源码完全注释(4)ngx_queue.h / ngx_queue.c

    队列头文件ngx_queue.h #include <ngx_config.h> #include <ngx_core.h> #ifndef _NGX_QUEUE_H_INCL ...

随机推荐

  1. Spring AOP表达式报错:Pointcut is not well-formed: expecting 'name pattern' at character position

    问题现象: java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test ...

  2. java的日期

    直接看例子: import java.text.DateFormatSymbols; import java.util.Calendar; import java.util.GregorianCale ...

  3. Android 系统四大组件

    Android 系统四大组件分别是活动(Activity).服务(Service).广播接收器(Broadcast Receiver)和内容提供器(Content Provider). 活动是所有 A ...

  4. php 数据库类

    <?php /** * 对Yii2数据封装 * @author nike@youfumama.com * @date 2017-03-08 * 抽象类不能被实例化 * eg1: select u ...

  5. MVC自定义错误页面

    MVC异常处理主要有三种方案:1.基于HandleErrorAttribute重写OnException方法:2.基于Global.apsx添加Application_Error方法:3.直接在Web ...

  6. FPGA设计中的float

    在通常的设计中,不建议使用浮点数计算,因为浮点数计算会消耗FPGA大量的逻辑单元.但很多情况下,又需要使用浮点数进行计算提高精度. 所以需要有合适的方法计算浮点运算. 正常情况下FPGA只能以整形数据 ...

  7. 转-SpringMVC——之 国际化

    原文地址:http://www.cnblogs.com/liukemng/p/3750117.html 在系列(7)中我们讲了数据的格式化显示,Spring在做格式化展示的时候已经做了国际化处理,那么 ...

  8. cookie跨域问题汇总

    一.通过nginx反向代理 通过nginx反向代理后,使得浏览器认为访问的资源都是属于相同协议,域名和端口的. 详细见:<nginx实现跨域访问> 二.jsonp方式请求 v jquery ...

  9. 慢日志之二:ERROR 1146 (42S02): Table 'mysql.slow_log' doesn't exist,分析诊断工具之四

    去查看最新的slow log,发现没有最新的记录,上去检查slow log是否开启了. MySQL> show variables like '%slow%'; +--------------- ...

  10. ConditionObject分析

      ConditionObject是AQS中的内部类,提供了条件锁的同步实现,实现了Condition接口,并且实现了其中的await(),signal(),signalALL()等方法. Condi ...