Skip to content

Commit 55a4344

Browse files
m6w6weltling
authored andcommitted
murmur: port to C
1 parent 8289ddd commit 55a4344

File tree

6 files changed

+123
-157
lines changed

6 files changed

+123
-157
lines changed

ext/hash/config.m4

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,12 @@ fi
3535

3636
EXT_HASH_SOURCES="hash.c hash_md.c hash_sha.c hash_ripemd.c hash_haval.c \
3737
hash_tiger.c hash_gost.c hash_snefru.c hash_whirlpool.c hash_adler32.c \
38-
hash_crc32.c hash_fnv.c hash_joaat.c $EXT_HASH_SHA3_SOURCES"
38+
hash_crc32.c hash_fnv.c hash_joaat.c $EXT_HASH_SHA3_SOURCES
39+
murmur/PMurHash.c murmur/PMurHash128.c hash_murmur.c"
3940
EXT_HASH_HEADERS="php_hash.h php_hash_md.h php_hash_sha.h php_hash_ripemd.h \
4041
php_hash_haval.h php_hash_tiger.h php_hash_gost.h php_hash_snefru.h \
4142
php_hash_whirlpool.h php_hash_adler32.h php_hash_crc32.h \
4243
php_hash_fnv.h php_hash_joaat.h php_hash_sha3.h php_hash_murmur.h"
4344

44-
MURMUR_DIR=murmur
45-
PHP_HASH_CXX_FLAGS="-Iext/hash/$MURMUR_DIR"
46-
4745
PHP_NEW_EXTENSION(hash, $EXT_HASH_SOURCES, 0,,$PHP_HASH_CFLAGS)
4846
PHP_INSTALL_HEADERS(ext/hash, $EXT_HASH_HEADERS)
49-
50-
PHP_REQUIRE_CXX()
51-
PHP_ADD_SOURCES(PHP_EXT_DIR(hash), $MURMUR_DIR/PMurHash.cpp $MURMUR_DIR/PMurHash128.cpp hash_murmur.cpp, $PHP_HASH_CXX_FLAGS)
52-
53-
PHP_ADD_BUILD_DIR($ext_builddir/$MURMUR_DIR)

ext/hash/hash_murmur.cpp renamed to ext/hash/hash_murmur.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@
1414
+----------------------------------------------------------------------+
1515
*/
1616

17-
extern "C" {
1817
#include "php_hash.h"
1918
#include "php_hash_murmur.h"
20-
}
2119

22-
#include "PMurHash.h"
23-
#include "PMurHash128.h"
20+
#include "murmur/PMurHash.h"
21+
#include "murmur/PMurHash128.h"
2422
#define FORCE_INLINE zend_always_inline
25-
#include "endianness.h"
23+
#include "murmur/endianness.h"
2624

2725

2826
const php_hash_ops php_hash_murmur3a_ops = {/*{{{*/
@@ -188,4 +186,3 @@ PHP_HASH_API int PHP_MURMUR3FCopy(const php_hash_ops *ops, PHP_MURMUR3F_CTX *ori
188186
copy_context->len = orig_context->len;
189187
return SUCCESS;
190188
}/*}}}*/
191-

ext/hash/murmur/PMurHash.cpp renamed to ext/hash/murmur/PMurHash.c

Lines changed: 27 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010

1111
/*-----------------------------------------------------------------------------
12-
12+
1313
If you want to understand the MurmurHash algorithm you would be much better
1414
off reading the original source. Just point your browser at:
1515
http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp
@@ -69,18 +69,18 @@ on big endian machines, or a byte-by-byte read if the endianess is unknown.
6969

7070
/* Find best way to ROTL */
7171
#if defined(_MSC_VER)
72-
#define FORCE_INLINE __forceinline
72+
#define FORCE_INLINE static __forceinline
7373
#include <stdlib.h> /* Microsoft put _rotl declaration in here */
7474
#define ROTL32(x,y) _rotl(x,y)
7575
#else
76-
#define FORCE_INLINE inline __attribute__((always_inline))
76+
#define FORCE_INLINE static inline __attribute__((always_inline))
7777
/* gcc recognises this code and generates a rotate instruction for CPUs with one */
7878
#define ROTL32(x,r) (((uint32_t)x << r) | ((uint32_t)x >> (32 - r)))
7979
#endif
8080

8181
#include "endianness.h"
8282

83-
#define READ_UINT32(ptr) getblock32((uint32_t *)ptr)
83+
#define READ_UINT32(ptr) getblock32((uint32_t *)ptr, 0)
8484

8585
/*-----------------------------------------------------------------------------
8686
* Core murmurhash algorithm macros */
@@ -90,31 +90,31 @@ static const uint32_t kC2 = 0x1b873593;
9090

9191
/* This is the main processing body of the algorithm. It operates
9292
* on each full 32-bits of input. */
93-
FORCE_INLINE void doblock(uint32_t &h1, uint32_t &k1)
94-
{
95-
k1 *= kC1;
96-
k1 = ROTL32(k1,15);
97-
k1 *= kC2;
98-
99-
h1 ^= k1;
100-
h1 = ROTL32(h1,13);
101-
h1 = h1*5+0xe6546b64;
102-
}
93+
#define doblock(h1, k1) \
94+
do {\
95+
k1 *= kC1;\
96+
k1 = ROTL32(k1,15);\
97+
k1 *= kC2;\
98+
\
99+
h1 ^= k1;\
100+
h1 = ROTL32(h1,13);\
101+
h1 = h1*5+0xe6546b64;\
102+
} while(0)
103103

104104
/* Append unaligned bytes to carry, forcing hash churn if we have 4 bytes */
105105
/* cnt=bytes to process, h1=name of h1 var, c=carry, n=bytes in c, ptr/len=payload */
106-
FORCE_INLINE void dobytes(int cnt, uint32_t &h1, uint32_t &c, int &n,
107-
const uint8_t *&ptr, int &len)
108-
{
109-
while(cnt--) {
110-
c = c>>8 | (uint32_t)*ptr++<<24;
111-
n++; len--;
112-
if(n==4) {
113-
doblock(h1, c);
114-
n = 0;
115-
}
116-
}
117-
}
106+
#define dobytes(cnt, h1, c, n, ptr, len) \
107+
do {\
108+
unsigned __cnt = cnt;\
109+
while(__cnt--) {\
110+
c = c>>8 | (uint32_t)*ptr++<<24;\
111+
n++; len--;\
112+
if(n==4) {\
113+
doblock(h1, c);\
114+
n = 0;\
115+
}\
116+
}\
117+
} while(0)
118118

119119
/*---------------------------------------------------------------------------*/
120120

@@ -200,7 +200,7 @@ void PMurHash32_Process(uint32_t *ph1, uint32_t *pcarry, const void *key, int le
200200
/* Copy out new running hash and carry */
201201
*ph1 = h1;
202202
*pcarry = (c & ~0xff) | n;
203-
}
203+
}
204204

205205
/*---------------------------------------------------------------------------*/
206206

@@ -224,23 +224,3 @@ uint32_t PMurHash32_Result(uint32_t h, uint32_t carry, uint32_t total_length)
224224

225225
return h;
226226
}
227-
228-
229-
/*---------------------------------------------------------------------------*/
230-
231-
/* All in one go */
232-
233-
uint32_t PMurHash32(const void * key, int len, uint32_t seed)
234-
{
235-
uint32_t carry = 0;
236-
PMurHash32_Process(&seed, &carry, key, len);
237-
return PMurHash32_Result(seed, carry, (uint32_t) len);
238-
}
239-
240-
/* MurmurHash3_x86_32 api */
241-
void PMurHash32(const void * key, int len, uint32_t seed, void * out)
242-
{
243-
uint32_t carry = 0;
244-
PMurHash32_Process(&seed, &carry, key, len);
245-
*(uint32_t*)out = PMurHash32_Result(seed, carry, (uint32_t) len);
246-
}

ext/hash/murmur/PMurHash.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,3 @@ typedef unsigned int uint32_t;
2929

3030
void PMurHash32_Process(uint32_t *ph1, uint32_t *pcarry, const void *key, int len);
3131
uint32_t PMurHash32_Result(uint32_t h1, uint32_t carry, uint32_t total_length);
32-
uint32_t PMurHash32(const void * key, int len, uint32_t seed);
33-
void PMurHash32(const void * key, int len, uint32_t seed, void * out);

0 commit comments

Comments
 (0)