forked from pllua/pllua-deprecated
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lua_int64.c
366 lines (312 loc) · 7.8 KB
/
lua_int64.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**
Copyright (c) 2012-2013 codingow.com
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
source:https://github.com/idning/lua-int64.git
*/
#include "lua_int64.h"
#include <stdint.h>
#include <math.h>
#include <stdlib.h>
#ifndef _MSC_VER
#include <stdbool.h>
#include <inttypes.h>
#endif
#include "pllua.h"
#include "pllua_errors.h"
static const char int64_type_name[] = "int64";
static int64_t check_int64(lua_State* L, int idx) {\
int64_t* p;
luaL_checktype(L,idx,LUA_TUSERDATA);
p = (int64_t*)luaL_checkudata(L, idx, int64_type_name);
return p ? *p : 0;
}
#define get_ab_values \
int64_t a;\
int64_t b;\
if(lua_isnil(L,1) || lua_isnil(L,2)) \
return luaL_error(L, "attempt to perform arithmetic on a nil value"); \
a = get_int64(L,1); \
b = get_int64(L,2)
static int64_t
get_int64(lua_State *L, int index) {
int type = lua_type(L,index);
int64_t value = 0;
switch(type) {
case LUA_TNUMBER: {
return (int64_t)(luaL_checknumber(L,index));
}
case LUA_TSTRING: {
#ifdef _MSC_VER
return(int64_t)_strtoi64(lua_tostring(L, index), NULL, 0);
#else
return(int64_t)strtoll(lua_tostring(L, index), NULL, 0);
#endif
}
case LUA_TUSERDATA:
value = check_int64(L, index);
break;
default:
return luaL_error(L, "argument %d error type %s", index, lua_typename(L,type));
}
return value;
}
static inline void
_pushint64(lua_State *L, int64_t n) {
int64_t * udata = (int64_t *)lua_newuserdata(L, sizeof(int64_t ));
*udata = n;
luaL_getmetatable(L, int64_type_name);
lua_setmetatable(L, -2);
}
static int
int64_add(lua_State *L) {
get_ab_values;
_pushint64(L, a+b);
return 1;
}
static int
int64_new(lua_State *L) {
int top = lua_gettop(L);
int64_t n;
switch(top) {
case 0 :
_pushint64(L,0);
break;
case 1 :
n = get_int64(L,1);
lua_pop(L, 1);
_pushint64(L,n);
break;
default: {
const char * str;
int base = luaL_checkinteger(L,2);
if (base < 2) {
luaL_error(L, "base must be >= 2");
}
str = luaL_checkstring(L, 1);
#ifdef _MSC_VER
n = _strtoi64(str, NULL, base);
#else
n = strtoll(str, NULL, base);
#endif
_pushint64(L,n);
break;
}
}
return 1;
}
static int
int64_sub(lua_State *L) {
get_ab_values;
_pushint64(L, a-b);
return 1;
}
static int
int64_mul(lua_State *L) {
get_ab_values;
_pushint64(L, a * b);
return 1;
}
static int
int64_div(lua_State *L) {
get_ab_values;
if (b == 0) {
return luaL_error(L, "div by zero");
}
_pushint64(L, a / b);
return 1;
}
static int
int64_mod(lua_State *L) {
get_ab_values;
if (b == 0) {
return luaL_error(L, "mod by zero");
}
_pushint64(L, a % b);
return 1;
}
static int64_t
_pow64(int64_t a, int64_t b) {
int64_t a2;
if (b == 1) {
return a;
}
a2 = a * a;
if (b % 2 == 1) {
return _pow64(a2, b/2) * a;
} else {
return _pow64(a2, b/2);
}
}
static int
int64_pow(lua_State *L) {
int64_t p;
get_ab_values;
if (b > 0) {
p = _pow64(a,b);
} else if (b == 0) {
p = 1;
} else {
return luaL_error(L, "pow by nagtive number %d",(int)b);
}
_pushint64(L, p);
return 1;
}
static int
int64_unm(lua_State *L) {
int64_t a = get_int64(L,1);
_pushint64(L, -a);
return 1;
}
static int
int64_eq(lua_State *L) {
get_ab_values;
lua_pushboolean(L,a == b);
return 1;
}
static int
int64_lt(lua_State *L) {
get_ab_values;
lua_pushboolean(L,a < b);
return 1;
}
static int
int64_le(lua_State *L) {
get_ab_values;
lua_pushboolean(L,a <= b);
return 1;
}
static int
int64_len(lua_State *L) {
int64_t a = get_int64(L,1);
lua_pushnumber(L,(lua_Number)a);
return 1;
}
static int
tostring(lua_State *L) {
static char hex[16] = "0123456789ABCDEF";
int64_t n = check_int64(L,1);
if (lua_gettop(L) == 1) {
char str[24];
#ifndef _MSC_VER
snprintf(str, sizeof(str), "%" PRId64, n);
#else
snprintf(str, sizeof(str), "%10lld", (long long)n);
#endif
lua_pushstring(L,str);
} else {
int i;
char buffer[64];
int base = luaL_checkinteger(L,2);
int shift = 1;
int mask = 2;
switch(base) {
case 0: {
unsigned char buffer[8];
int i;
for (i=0;i<8;i++) {
buffer[i] = (n >> (i*8)) & 0xff;
}
lua_pushlstring(L,(const char *)buffer, 8);
return 1;
}
case 10: {
int buffer[32], i;
int64_t dec = (int64_t)n;
luaL_Buffer b;
#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 502
luaL_buffinit(L, &b);
#else
luaL_buffinitsize(L , &b , 28);
#endif
if (dec<0) {
luaL_addchar(&b, '-');
dec = -dec;
}
for (i=0;i<32;i++) {
buffer[i] = dec%10;
dec /= 10;
if (dec == 0)
break;
}
while (i>=0) {
luaL_addchar(&b, hex[buffer[i]]);
--i;
}
luaL_pushresult(&b);
return 1;
}
case 2:
shift = 1;
mask = 1;
break;
case 8:
shift = 3;
mask = 7;
break;
case 16:
shift = 4;
mask = 0xf;
break;
default:
luaL_error(L, "Unsupport base %d",base);
break;
}
for (i=0;i<64;i+=shift) {
buffer[i/shift] = hex[(n>>(64-shift-i)) & mask];
}
lua_pushlstring(L, buffer, 64 / shift);
}
return 1;
}
#undef get_ab_values
void register_int64(lua_State *L)
{
if (sizeof(long long int)==sizeof(int64_t)) {
luaL_Reg regs[] =
{
{ "new", int64_new },
{ "tostring", tostring },
{ "__add", int64_add },
{ "__sub", int64_sub },
{ "__mul", int64_mul },
{ "__div", int64_div },
{ "__mod", int64_mod },
{ "__unm", int64_unm },
{ "__pow", int64_pow },
{ "__eq", int64_eq },
{ "__lt", int64_lt },
{ "__le", int64_le },
{ "__len", int64_len },
{ "__tostring", tostring },
{ NULL, NULL }
};
luaL_newmetatable(L, int64_type_name);
luaL_setfuncs(L, regs, 0);
lua_pushvalue(L, -1);
lua_setfield(L, -1, "__index");
lua_setglobal(L, int64_type_name);
}
}
int64 get64lua(lua_State *L, int index)
{
return get_int64(L,index);
}
void setInt64lua(lua_State *L, int64 value)
{
_pushint64(L,value);
}