-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpysmhasher.cpp
176 lines (155 loc) · 4.85 KB
/
pysmhasher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <Python.h>
#include <stdint.h>
#include "MurmurHash2.h"
#include "MurmurHash3.h"
#include "City.h"
PyDoc_STRVAR(module_doc, "Python Extensions for SMHasher");
static PyObject *PY_MurmurHash2(PyObject *self, PyObject *args) {
char *key;
unsigned len;
unsigned seed = 0;
if (!PyArg_ParseTuple(args, "s#|I", &key, &len, &seed)) {
return NULL;
}
uint32_t h = MurmurHash2(key, len, seed);
return PyLong_FromUnsignedLong(h);
}
static PyObject *PY_MurmurHash2A(PyObject *self, PyObject *args) {
char *key;
unsigned len;
unsigned seed = 0;
if (!PyArg_ParseTuple(args, "s#|I", &key, &len, &seed)) {
return NULL;
}
uint32_t h = MurmurHash2A(key, len, seed);
return PyLong_FromUnsignedLong(h);
}
static PyObject *PY_MurmurHash64B(PyObject *self, PyObject *args) {
char *key;
unsigned len;
unsigned seed = 0;
if (!PyArg_ParseTuple(args, "s#|I", &key, &len, &seed)) {
return NULL;
}
uint64_t h = MurmurHash64B(key, len, seed);
#if defined(__x86_64__)
return PyLong_FromUnsignedLong(h);
#else
return PyLong_FromUnsignedLongLong(h);
#endif
}
static PyObject *PY_MurmurHash3_x86_32(PyObject *self, PyObject *args) {
const char *key;
Py_ssize_t len;
uint32_t seed = 0;
unsigned char out[4];
if (!PyArg_ParseTuple(args, "s#|I", &key, &len, &seed)) {
return NULL;
}
MurmurHash3_x86_32((void *)key, len, seed, &out);
return _PyLong_FromByteArray((const unsigned char *)&out, 4, 0, 0);
}
static PyObject *PY_MurmurHash3_x86_128(PyObject *self, PyObject *args) {
const char *key;
Py_ssize_t len;
uint32_t seed = 0;
unsigned char out[16];
if (!PyArg_ParseTuple(args, "s#|I", &key, &len, &seed)) {
return NULL;
}
MurmurHash3_x86_128((void *)key, len, seed, &out);
return _PyLong_FromByteArray((const unsigned char *)&out, 16, 0, 0);
}
// static PyObject *PY_CityHash32(PyObject *self, PyObject *args) {
// char *key;
// unsigned len;
// unsigned seed = 0;
// if (!PyArg_ParseTuple(args, "s#|I", &key, &len, &seed)) {
// return NULL;
// }
// uint32_t h = CityHash32WithSeed(key, len, seed);
// return PyLong_FromUnsignedLong(h);
// }
static PyObject *PY_CityHash64(PyObject *self, PyObject *args) {
char *key;
unsigned len;
if (!PyArg_ParseTuple(args, "s#|I", &key, &len)) {
return NULL;
}
uint64_t h = CityHash64(key, len);
#if defined(__x86_64__)
return PyLong_FromUnsignedLong(h);
#else
return PyLong_FromUnsignedLongLong(h);
#endif
}
// 32-bit end
// 64-bit
#if defined(__x86_64__)
static PyObject *PY_MurmurHash64A(PyObject *self, PyObject *args) {
char *key;
unsigned len;
unsigned seed = 0;
if (!PyArg_ParseTuple(args, "s#|I", &key, &len, &seed)) {
return NULL;
}
uint64_t h = MurmurHash64A(key, len, seed);
return PyLong_FromUnsignedLong(h);
}
static PyObject *PY_MurmurHash3_x64_128(PyObject *self, PyObject *args) {
const char *key;
Py_ssize_t len;
uint32_t seed = 0;
unsigned char out[16];
if (!PyArg_ParseTuple(args, "s#|I", &key, &len, &seed)) {
return NULL;
}
MurmurHash3_x64_128((void *)key, len, seed, &out);
return _PyLong_FromByteArray((const unsigned char *)&out, 16, 0, 0);
}
#endif
#if defined(__x86_64__)
static PyMethodDef methods[] = {
{"murmurhash2", PY_MurmurHash2, METH_VARARGS, "32-bit hash"},
{"murmurhash2A", PY_MurmurHash2A, METH_VARARGS,
"32-bit hash, a variant of murmurhash2"},
{"murmurhash64A", PY_MurmurHash64A, METH_VARARGS,
"64-bit hash for 64-bit platform"},
{"murmurhash64B", PY_MurmurHash64B, METH_VARARGS,
"64-bit hash for 32-bit platform"},
{"murmurhash3_x86_32", PY_MurmurHash3_x86_32, METH_VARARGS, "32-bit hash"},
{"murmurhash3_x64_128", PY_MurmurHash3_x64_128, METH_VARARGS,
"128-bit hash"},
// {"cityhash32", PY_CityHash32, METH_VARARGS, "city hash 32"},
{"cityhash64", PY_CityHash64, METH_VARARGS, "city hash 64"},
{NULL, NULL, 0, NULL}}; // sentinel
#else
static PyMethodDef methods[] = {
{"murmurhash2", PY_MurmurHash2, METH_VARARGS, "32-bit hash"},
{"murmurhash2A", PY_MurmurHash2A, METH_VARARGS,
"32-bit hash, a variant of murmurhash2"},
{"murmurhash64B", PY_MurmurHash64B, METH_VARARGS,
"64-bit hash for 32-bit platform"},
{"murmurhash3_x86_32", PY_MurmurHash3_x86_32, METH_VARARGS, "32-bit hash"},
{"murmurhash3_x86_128", PY_MurmurHash3_x86_128, METH_VARARGS,
"128-bit hash"},
// {"cityhash32", PY_CityHash32, METH_VARARGS, "city hash 32"},
{"cityhash64", PY_CityHash64, METH_VARARGS, "city hash 64"},
{NULL, NULL, 0, NULL}}; // sentinel
#endif
static struct PyModuleDef pysmhasher_module = {
PyModuleDef_HEAD_INIT, module_doc,
"smhasher python extensions", // doc
-1, methods};
#ifdef __cplusplus
extern "C" {
#endif
PyMODINIT_FUNC PyInit_pysmhasher(void) {
PyObject *m;
m = PyModule_Create(&pysmhasher_module);
PyModule_AddStringConstant(m, "__version__", MODULE_VERSION);
return m;
}
#ifdef __cplusplus
}
#endif