-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.cpp
348 lines (275 loc) · 6.92 KB
/
utils.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include "utils.h"
#include "lua_compat.h"
af_dtype GetDataType (lua_State * L, int index)
{
af_dtype types[] = {
f32, c32, f64, c64, b8, s32, u32, u8, s64, u64,
#if AF_API_VERSION >= 32
s16, u16
#endif
};
const char * names[] = {
"f32", "c32", "f64", "c64", "b8", "s32", "u32", "u8", "s64", "u64",
#if AF_API_VERSION >= 32
"s16", "u16"
#endif
};
return types[luaL_checkoption(L, index, "f32", names)];
}
#define RESULT_CODE(what) case what: \
lua_pushliteral(L, #what); \
break;
void PushResult (lua_State * L, af_err err)
{
switch (err)
{
RESULT_CODE(AF_SUCCESS)
RESULT_CODE(AF_ERR_NO_MEM)
RESULT_CODE(AF_ERR_DRIVER)
RESULT_CODE(AF_ERR_RUNTIME)
RESULT_CODE(AF_ERR_INVALID_ARRAY)
RESULT_CODE(AF_ERR_ARG)
RESULT_CODE(AF_ERR_SIZE)
RESULT_CODE(AF_ERR_TYPE)
RESULT_CODE(AF_ERR_DIFF_TYPE)
RESULT_CODE(AF_ERR_BATCH)
RESULT_CODE(AF_ERR_NOT_SUPPORTED)
RESULT_CODE(AF_ERR_NOT_CONFIGURED)
#if AF_API_VERSION >= 32
RESULT_CODE(AF_ERR_NONFREE)
#endif
RESULT_CODE(AF_ERR_NO_DBL)
RESULT_CODE(AF_ERR_NO_GFX)
#if AF_API_VERSION >= 32
RESULT_CODE(AF_ERR_LOAD_LIB)
RESULT_CODE(AF_ERR_LOAD_SYM)
RESULT_CODE(AF_ERR_ARR_BKND_MISMATCH)
#endif
RESULT_CODE(AF_ERR_INTERNAL)
RESULT_CODE(AF_ERR_UNKNOWN)
}
}
#undef RESULT_CODE
int PushErr(lua_State * L, af_err err, int nret)
{
lua_pushinteger(L, err);// ..., ret1, [ret2, ...], err
lua_insert(L, -(nret + 1)); // ..., err, ret1[, ret2, ...]
return nret + 1;
}
void * GetMemory (lua_State * L, int index)
{
if (lua_type(L, index) == LUA_TSTRING) return (void *)lua_tostring(L, index);
else return lua_touserdata(L, index);
}
af_array GetArray (lua_State * L, int index)
{
af_array * ptr_to = (af_array *)lua_touserdata(L, index);
#ifndef NDEBUG
// Assert? (error to access after Clear...)
#endif
return *ptr_to;
}
af_features GetFeatures (lua_State * L, int index)
{
af_features * ptr_to = (af_features *)lua_touserdata(L, index);
#ifndef NDEBUG
// Assert? (ditto)
#endif
return *ptr_to;
}
af_index_t * GetIndexer (lua_State * L, int index)
{
af_index_t ** ptr_to = (af_index_t **)lua_touserdata(L, index);
#ifndef NDEBUG
// Assert? (error to access after Clear...)
#endif
return *ptr_to;
}
static void AddMetatable (lua_State * L, const char * name, lua_CFunction func)
{
if (luaL_newmetatable(L, name)) // ..., object, mt
{
lua_pushcfunction(L, func); // ..., object, mt, func
lua_setfield(L, -2, "__gc");// ..., object, mt = { __gc = func }
}
lua_setmetatable(L, -2);// ..., object
}
af_array * NewArray (lua_State * L)
{
void * ptr = lua_newuserdata(L, sizeof(af_array)); // ..., array
AddMetatable(L, "af_array", [](lua_State * L)
{
lua_settop(L, 1); // array
af_array ptr = GetArray(L, 1);
if (ptr) af_release_array(ptr);
ClearArray(L, 1);
return 0;
});
*(af_array*)ptr = nullptr;
return (af_array *)ptr;
}
af_features * NewFeatures (lua_State * L)
{
void * ptr = lua_newuserdata(L, sizeof(af_features)); // ..., features
AddMetatable(L, "af_features", [](lua_State * L)
{
lua_settop(L, 1); // features
af_features ptr = GetFeatures(L, 1);
if (ptr) af_release_features(ptr);
ClearFeatures(L, 1);
return 0;
});
*(af_features*)ptr = nullptr;
return (af_features *)ptr;
}
af_index_t ** NewIndexer (lua_State * L)
{
void * ptr = lua_newuserdata(L, sizeof(af_index_t *)); // ..., indexer
AddMetatable(L, "af_index_t", [](lua_State * L)
{
lua_settop(L, 1); // indexer
af_index_t * ptr = GetIndexer(L, 1);
if (ptr) af_release_indexers(ptr);
ClearIndexer(L, 1);
return 0;
});
*(af_index_t **)ptr = nullptr;
return (af_index_t **)ptr;
}
void ClearArray (lua_State * L, int index)
{
*(af_array *)lua_touserdata(L, index) = nullptr;
}
void ClearFeatures (lua_State * L, int index)
{
*(af_features *)lua_touserdata(L, index) = nullptr;
}
void ClearIndexer (lua_State * L, int index)
{
*(af_index_t **)lua_touserdata(L, index) = nullptr;
}
LuaDims::LuaDims (lua_State * L, int first)
{
luaL_checktype(L, first + 1, LUA_TTABLE);
int n = (int)luaL_checkinteger(L, first);
for (int i = 0; i < n; ++i)
{
lua_rawgeti(L, first + 1, i + 1); // ..., n, t, [type,] ..., dim
mDims.push_back(lua_tointeger(L, -1));
lua_pop(L, 1); // ..., n, t, [type,] ...
}
}
template<typename T> void AddToVector (std::vector<char> & arr, T value)
{
const char * bytes = (const char *)&value;
for (size_t i = 0; i < sizeof(T); ++i) arr.push_back(*bytes++);
}
template<typename T> void AddFloat (std::vector<char> & arr, lua_State * L, int index, int pos)
{
lua_rawgeti(L, index, pos + 1); // ..., arr, ..., float
AddToVector(arr, Arg<T>(L, -1));
lua_pop(L, 1); // ..., arr, ...
}
template<typename T> void AddInt (std::vector<char> & arr, lua_State * L, int index, int pos)
{
lua_rawgeti(L, index, pos + 1); // ..., arr, ..., int
AddToVector(arr, Arg<T>(L, -1));
lua_pop(L, 1); // ..., arr, ...
}
template<typename T, typename C> void AddComplex (std::vector<char> & arr, lua_State * L, int index, int pos)
{
lua_rawgeti(L, index, pos + 1); // ..., arr, ... complex
lua_rawgeti(L, -1, 1); // ..., arr, ..., complex, real
lua_rawgeti(L, -2, 2); // ..., arr, ..., complex, real, imag
C comp(Arg<T>(L, -2), Arg<T>(L, -1));
AddToVector(arr, comp);
lua_pop(L, 3); // ..., arr, ...
}
LuaData::LuaData (lua_State * L, int index, int type_index, bool copy) : mDataPtr(0)
{
mType = Arg<af_dtype>(L, type_index);
// Non-string: build up from Lua array.
if (!lua_isstring(L, index))
{
int count = lua_objlen(L, index);
switch (mType)
{
case b8:
case u8:
mData.reserve(count);
break;
#if AF_API_VERSION >= 32
case s16:
case u16:
mData.reserve(count * 2);
break;
#endif
case f32:
case s32:
case u32:
mData.reserve(count * 4);
break;
case c32:
case f64:
case s64:
case u64:
mData.reserve(count * 8);
break;
case c64:
mData.reserve(count * 16);
break;
}
for (int i = 0; i < count; ++i)
{
switch (mType)
{
case f32:
AddFloat<float>(mData, L, index, i);
break;
case c32:
AddComplex<float, af::af_cfloat>(mData, L, index, i);
break;
case f64:
AddFloat<double>(mData, L, index, i);
break;
case c64:
AddComplex<double, af::af_cdouble>(mData, L, index, i);
break;
case b8:
AddInt<char>(mData, L, index, i);
break;
#if AF_API_VERSION >= 32
case s16:
AddInt<short>(mData, L, index, i);
break;
case u16:
AddInt<unsigned short>(mData, L, index, i);
break;
#endif
case s32:
AddInt<int>(mData, L, index, i);
break;
case u32:
AddInt<unsigned int>(mData, L, index, i);
break;
case u8:
AddInt<unsigned char>(mData, L, index, i);
break;
case s64:
AddInt<intl>(mData, L, index, i);
break;
case u64:
AddInt<uintl>(mData, L, index, i);
break;
}
}
}
// Already a Lua string: make a copy or simply point to it.
else
{
const char * str = lua_tostring(L, index);
if (copy) mData.assign(str, str + lua_objlen(L, index));
else mDataPtr = str;
}
if (!mDataPtr) mDataPtr = &mData.front();
}