下面是摘自 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. openresty 几个插件使用

    1. jwt    opm get SkyLothar/lua-resty-jwt   2. cookie   opm get p0pr0ck5/lua-resty-cookie   3. http ...

  2. 模态对话框中的window.close关闭时会打开新页面

    在模态对话框的页面的<head></head>加上<base target="_self"> 就不会打开新页面了.

  3. Cluster的日记体系

    Cluster的日志体系 Cluster的日志体系: Oracle cluster不像数据库那样,具有丰富的视图.工具可以用来辅助诊断,他的日志和trace文件时唯一的选择.但不想oracle只有al ...

  4. 洛谷1527(bzoj2738)矩阵乘法——二维树状数组+整体二分

    题目:https://www.luogu.org/problemnew/show/P1527 不难想到(?)可以用二维树状数组.但维护什么?怎么查询是难点. 因为求第k小,可以考虑记权值树状数组,把比 ...

  5. 【备忘】windows环境下20行php代码搞定音频裁剪

    先上图,由于最近的需求需要对语音文件进行处理,所以抽空研究了下php处理音/视频文件的处理,简单的demo处理,截取一个音频文件的前20秒,并保存新的媒体文件. 操作步骤: ①在此站点下载所需的辅助程 ...

  6. Juicer自定义函数

    首先,先写自定义的方法: function (sex) { ; ; var Range = Max - Min; var Rand = Math.random(); var res = (Min + ...

  7. Hibernate学习11——Hibernate 高级配置(连接池、log4j)

    第一节:配置数据库连接池 这里配置c3p0连接池,需要的jar包: jar包位于hibernate压缩包的:hibernate-release-4.3.5.Final\lib\optional\c3p ...

  8. java web 程序--注册页面/HashMap的用法。。要懂啊

    思路:1.一个form表单,用户输入后,提交 2.第二个是注册页面,主要是用Map.先假设往map里面拿东西,然后判断是否为空  若为空,new 一个HashMap它的子类,然后通过map.conta ...

  9. 模拟admin组件自己开发stark组件之创建篇

    admin组件 admin组件为我们提供了针对django管理页面 我们先简短来看下django的admin组件的启动流程,注册流程,url匹配过程 启动注册 1. 扫描所有应用下的注册了应用中的ad ...

  10. javascript数组的申明方式以及常用方法

    数组的定义: 方法1. var mycars=new Array()mycars[0]="Saab"mycars[1]="Volvo"mycars[2]=&qu ...