-
Notifications
You must be signed in to change notification settings - Fork 4
/
libhash.c
220 lines (197 loc) · 6.62 KB
/
libhash.c
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <lua.h>
#include <lauxlib.h>
#include "luaT.h"
#include "TH.h"
#include "hash.h"
#define LH_MAX_MOD 9007199254740992L
#define IMPLEMENT_THTENSOR_HASH(TYPE, CTYPE) \
static void TH##TYPE##Tensor_hashUpdate(TH##TYPE##Tensor *tensor, LHHash *hash) \
{ \
TH_TENSOR_APPLY(CTYPE, tensor, \
LHHash_update(hash, tensor_data, tensor_size*sizeof(CTYPE)); break;); \
}
IMPLEMENT_THTENSOR_HASH(Byte, unsigned char);
IMPLEMENT_THTENSOR_HASH(Char, char);
IMPLEMENT_THTENSOR_HASH(Short, short);
IMPLEMENT_THTENSOR_HASH(Int, int);
IMPLEMENT_THTENSOR_HASH(Long, long);
IMPLEMENT_THTENSOR_HASH(Float, float);
IMPLEMENT_THTENSOR_HASH(Double, double);
static void libhash_updatehash(lua_State *L, LHHash *state, int idx)
{
if(lua_type(L, idx) == LUA_TSTRING) {
size_t len = 0;
const char *str = lua_tolstring(L, idx, &len);
LHHash_update(state, str, len);
}
else if(lua_type(L, idx) == LUA_TNUMBER) {
lua_Number num = lua_tonumber(L, idx);
LHHash_update(state, &num, sizeof(lua_Number));
}
else if(luaT_isudata(L, idx, "torch.ByteTensor")) {
THByteTensor_hashUpdate(luaT_toudata(L, idx, "torch.ByteTensor"), state);
}
else if(luaT_isudata(L, idx, "torch.CharTensor")) {
THCharTensor_hashUpdate(luaT_toudata(L, idx, "torch.CharTensor"), state);
}
else if(luaT_isudata(L, idx, "torch.ShortTensor")) {
THShortTensor_hashUpdate(luaT_toudata(L, idx, "torch.ShortTensor"), state);
}
else if(luaT_isudata(L, idx, "torch.IntTensor")) {
THIntTensor_hashUpdate(luaT_toudata(L, idx, "torch.IntTensor"), state);
}
else if(luaT_isudata(L, idx, "torch.LongTensor")) {
THLongTensor_hashUpdate(luaT_toudata(L, idx, "torch.LongTensor"), state);
}
else if(luaT_isudata(L, idx, "torch.FloatTensor")) {
THFloatTensor_hashUpdate(luaT_toudata(L, idx, "torch.FloatTensor"), state);
}
else if(luaT_isudata(L, idx, "torch.DoubleTensor")) {
THDoubleTensor_hashUpdate(luaT_toudata(L, idx, "torch.DoubleTensor"), state);
}
else {
luaL_error(L, "string, number, or tensor [number] expected");
}
}
/*
stuff [seed] [mod]
stuff name [seed] [mod]
stuff hash [seed] [mod]
*/
static int libhash_hash(lua_State *L)
{
int nopt = lua_gettop(L);
LHHash *state = NULL;
unsigned long long seed = 0;
int freestate = 0;
unsigned long long mod = 0;
unsigned long long res = 0;
if(nopt == 1 || (nopt >= 2 && nopt <= 3 && lua_isnumber(L, 2))) {
state = LHXXH64_new();
seed = (unsigned long long)luaL_optlong(L, 2, 0);
mod = (unsigned long long)luaL_optlong(L, 3, LH_MAX_MOD);
freestate = 1;
} else if((nopt >= 2 && nopt <= 4) && lua_tostring(L, 2)) {
const char *hashtype = lua_tostring(L, 2);
seed = (unsigned long long)luaL_optlong(L, 3, 0);
mod = (unsigned long long)luaL_optlong(L, 4, LH_MAX_MOD);
if(!strcmp(hashtype, "XXH64"))
state = LHXXH64_new();
else if(!strcmp(hashtype, "FNV64"))
state = LHFNV64_new();
else
luaL_error(L, "invalid hash type (XXH64 || FNV64 expected)");
freestate = 1;
} else if((nopt >= 2 && nopt <= 4) && luaT_toudata(L, 2, "torch.Hash")) {
state = luaT_toudata(L, 2, "torch.Hash");
seed = (unsigned long long)luaL_optlong(L, 3, 0);
mod = (unsigned long long)luaL_optlong(L, 4, LH_MAX_MOD);
} else {
luaL_error(L, "invalid arguments: stuff [seed] [mod] || stuff name [seed] [mod] || stuff hash [seed] [mod] expected");
}
if(!state)
luaL_error(L, "invalid Hash state");
LHHash_reset(state, seed);
libhash_updatehash(L, state, 1);
res = LHHash_digest(state);
res = res % mod;
if(freestate)
LHHash_free(state);
lua_pushnumber(L, res);
return 1;
}
static int libhash_LHXXH64_new(lua_State *L)
{
LHHash *state = LHXXH64_new();
unsigned long long seed = (unsigned long long)luaL_optlong(L, 1, 0);
LHHash_reset(state, seed);
luaT_pushudata(L, state, "torch.Hash");
return 1;
}
static int libhash_LHFNV64_new(lua_State *L)
{
LHHash *state = LHFNV64_new();
unsigned long long seed = (unsigned long long)luaL_optlong(L, 1, 0);
LHHash_reset(state, seed);
luaT_pushudata(L, state, "torch.Hash");
return 1;
}
static int libhash_LHHash_reset(lua_State *L)
{
LHHash *state = luaT_checkudata(L, 1, "torch.Hash");
unsigned long long seed = (unsigned long long)luaL_optlong(L, 2, 0);
LHHash_reset(state, seed);
return 1; /* self */
}
static int libhash_LHHash_update(lua_State *L)
{
LHHash *state = luaT_checkudata(L, 1, "torch.Hash");
libhash_updatehash(L, state, 2);
return 1; /* self */
}
static int libhash_LHHash_hash(lua_State *L)
{
LHHash *state = luaT_checkudata(L, 1, "torch.Hash");
unsigned long long seed = (unsigned long long)luaL_optlong(L, 3, 0);
unsigned long long mod = (unsigned long long)luaL_optlong(L, 4, LH_MAX_MOD);
unsigned long long res = 0;
luaL_argcheck(L, mod > 0, 4, "modulo should be positive");
LHHash_reset(state, seed);
libhash_updatehash(L, state, 2);
res = LHHash_digest(state);
res %= mod;
lua_pushnumber(L, res);
return 1; /* self */
}
static int libhash_LHHash_digest(lua_State *L)
{
LHHash *state = luaT_checkudata(L, 1, "torch.Hash");
unsigned long long mod = (unsigned long long)luaL_optlong(L, 2, LH_MAX_MOD);
luaL_argcheck(L, mod > 0, 2, "modulo should be positive");
unsigned long long res = LHHash_digest(state) % mod;
lua_pushinteger(L, (long)res);
return 1;
}
static int libhash_LHHash_clone(lua_State *L)
{
LHHash *state = luaT_checkudata(L, 1, "torch.Hash");
LHHash *newstate = LHHash_clone(state);
luaT_pushudata(L, newstate, "torch.Hash");
return 1;
}
static int libhash_LHHash_free(lua_State *L)
{
LHHash *state = luaT_checkudata(L, 1, "torch.Hash");
LHHash_free(state);
return 0;
}
static const struct luaL_Reg libhash_LHHash__ [] = {
{"hash", libhash_LHHash_hash},
{"reset", libhash_LHHash_reset},
{"update", libhash_LHHash_update},
{"digest", libhash_LHHash_digest},
{"clone", libhash_LHHash_clone},
{NULL, NULL}
};
static const struct luaL_Reg libhash__ [] = {
{"XXH64", libhash_LHXXH64_new},
{"FNV64", libhash_LHFNV64_new},
{"hash", libhash_hash},
{NULL, NULL}
};
int luaopen_libhash(lua_State *L)
{
lua_getglobal(L, "require");
if(!lua_isfunction(L, -1))
luaL_error(L, "require seems not be a function");
lua_pushstring(L, "torch");
lua_call(L, 1, 1);
if(!lua_istable(L, -1))
luaL_error(L, "could not load torch");
luaT_newmetatable(L, "torch.Hash", NULL, NULL, libhash_LHHash_free, NULL);
luaL_register(L, NULL, libhash_LHHash__);
lua_pop(L, 1);
lua_newtable(L);
luaL_register(L, NULL, libhash__);
return 1; /* hash */
}