-
Notifications
You must be signed in to change notification settings - Fork 7
/
int64.c
906 lines (729 loc) · 19.4 KB
/
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
#include <ctype.h>
#include <math.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "int64.h"
/* 64-bit int printf format specifiers { */
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
#include <inttypes.h>
typedef long long strtoll_t;
typedef unsigned long long strtoull_t;
#elif defined(_MSC_VER)
#define __PRI_64 "I64"
#define strtoll _strtoi64
#define strtoull _strtoui64
typedef __int64 strtoll_t;
typedef unsigned __int64 strtoull_t;
#else
#define __PRI_64 "ll"
typedef long long strtoll_t;
typedef unsigned long long strtoull_t;
#endif
#ifndef PRId64
#define PRId64 __PRI_64 "d"
#endif
#ifndef PRIu64
#define PRIu64 __PRI_64 "u"
#endif
#ifndef PRIx64
#define PRIx64 __PRI_64 "x"
#endif /* } */
#undef abs
#define abs(n) ((n) < 0 ? -(n) : (n))
#define MAX_DBL_INT 9007199254740992LL /* 2**53 */
#define LUA_INTEGER_64 (sizeof(lua_Integer) >= sizeof(int64_t))
#define LUA_NUMBER_64 (sizeof(lua_Number) >= sizeof(int64_t))
#define LUA_POINTER_64 (sizeof(void *) >= sizeof(int64_t))
#define ITOA_BUFSIZ 32
#define SUFFIX_SIGNED "LL"
#define SUFFIX_UNSIGNED "ULL"
#define LTYPE "INT64"
#define SUCCESS 1
#define FAILURE 0
typedef struct obj64 {
int _unsigned;
union {
int64_t i;
uint64_t u;
} v;
} obj64;
typedef union dbl64 {
volatile double d;
volatile int64_t i;
volatile uint64_t u;
} dbl64;
typedef struct str_info {
const char *s;
size_t l;
int base;
} str_info;
typedef struct stack_info {
lua_State *L;
int idx;
} stack_info;
/*
** {=================================================================
** Lua 5.1 compatibility
** ==================================================================
*/
#if LUA_VERSION_NUM < 502 /* { */
/* lua-5.2.2/src/lua.h:88 */
#define LUA_NUMTAGS 9
/* lua-5.2.2/src/lua.h:184-196 */
#define LUA_OPADD 0
#define LUA_OPSUB 1
#define LUA_OPMUL 2
#define LUA_OPDIV 3
#define LUA_OPMOD 4
#define LUA_OPPOW 5
#define LUA_OPUNM 6
#define LUA_OPEQ 0
#define LUA_OPLT 1
#define LUA_OPLE 2
static lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum) {
lua_Integer n = lua_tointeger(L, idx);
if (isnum) *isnum = (n != 0 || lua_type(L, idx) == LUA_TNUMBER);
return n;
}
/* lua-5.2.2/src/lauxlib.c:284-287 */
static void luaL_setmetatable (lua_State *L, const char *tname) {
luaL_getmetatable(L, tname);
lua_setmetatable(L, -2);
}
/* lua-5.2.2/src/lauxlib.c:290-302 */
static void *luaL_testudata (lua_State *L, int ud, const char *tname) {
void *p = lua_touserdata(L, ud);
if (p != NULL) { /* value is a userdata? */
if (lua_getmetatable(L, ud)) { /* does it have a metatable? */
luaL_getmetatable(L, tname); /* get correct metatable */
if (!lua_rawequal(L, -1, -2)) /* not the same? */
p = NULL; /* value is a userdata with wrong metatable */
lua_pop(L, 2); /* remove both metatables */
return p;
}
}
return NULL; /* value is not a userdata with a metatable */
}
#endif /* } */
/* }================================================================= */
/*
** {=================================================================
** Test whether types are at least 64 bits wide.
** ==================================================================
*/
LUALIB_API int luaL_integeris64 (lua_State *L) {
(void)L;
return LUA_INTEGER_64;
}
LUALIB_API int luaL_numberis64 (lua_State *L) {
(void)L;
return LUA_NUMBER_64;
}
LUALIB_API int luaL_pointeris64 (lua_State *L) {
(void)L;
return LUA_POINTER_64;
}
/* }================================================================= */
/*
** {=================================================================
** Lua type => (u)int64_t
** ==================================================================
*/
#define ltag_to_ltoi_error(tt) ((tt) + 2)
#define LTOI_ERRMSG(type) "cannot convert " type " to int64"
static const char *ltoi_errmsg[] = {
"ok",
LTOI_ERRMSG("none"),
LTOI_ERRMSG("nil"),
LTOI_ERRMSG("boolean"),
LTOI_ERRMSG("address"), /* light userdata */
LTOI_ERRMSG("number"),
LTOI_ERRMSG("string"),
LTOI_ERRMSG("table"),
LTOI_ERRMSG("function"),
"userdata is not an int64 boxed type object",
LTOI_ERRMSG("thread"),
"not a valid type"
};
LUALIB_API void luaL_i64pushstrerror (lua_State *L, int errcode) {
if (errcode < 0 || errcode > ltag_to_ltoi_error(LUA_NUMTAGS))
errcode = ltag_to_ltoi_error(LUA_NUMTAGS);
lua_pushstring(L, ltoi_errmsg[errcode]);
}
static int str2i64 (const void *data, int64_t *n) {
const str_info *info = data;
char *endptr;
strtoll_t x = strtoll(info->s, &endptr, info->base);
if (info->s == endptr) return FAILURE;
if (*endptr == 'L' || *endptr == 'l') {
endptr++;
if (*endptr == 'L' || *endptr == 'l') endptr++;
else return FAILURE;
}
while (isspace((unsigned char)*endptr)) endptr++;
if (endptr != info->s + info->l) return FAILURE;
*n = (int64_t)x;
return SUCCESS;
}
static int str2u64 (const void *data, uint64_t *n) {
const str_info *info = data;
char *endptr;
strtoull_t x = strtoull(info->s, &endptr, info->base);
if (info->s == endptr) return FAILURE;
if (*endptr == 'U' || *endptr == 'u') endptr++;
if (*endptr == 'L' || *endptr == 'l') {
endptr++;
if (*endptr == 'L' || *endptr == 'l') endptr++;
else return FAILURE;
}
while (isspace((unsigned char)*endptr)) endptr++;
if (endptr != info->s + info->l) return FAILURE;
*n = (uint64_t)x;
return SUCCESS;
}
static int num2i64 (const void *data, int64_t *n) {
const stack_info *info = data;
int64_t i = (int64_t)lua_tointeger(info->L, info->idx);
lua_Number x = lua_tonumber(info->L, info->idx);
if (isnormal(x) && i == x && (!LUA_NUMBER_64
|| (!LUA_INTEGER_64 || abs(i) <= MAX_DBL_INT))) {
*n = i;
}
else if (LUA_NUMBER_64) {
/* !!! XXX: BIT HACKS -- NOT GUARANTEED !!! */
dbl64 d;
d.d = x;
*n = d.i;
}
else return FAILURE;
return SUCCESS;
}
static int num2u64 (const void *data, uint64_t *n) {
const stack_info *info = data;
uint64_t i = (uint64_t)lua_tointeger(info->L, info->idx);
lua_Number x = lua_tonumber(info->L, info->idx);
if (isnormal(x) && (!LUA_NUMBER_64
|| (!LUA_INTEGER_64 || abs(i) <= MAX_DBL_INT))) {
*n = i;
}
else if (LUA_NUMBER_64) {
/* !!! XXX: BIT HACKS -- NOT GUARANTEED !!! */
dbl64 d;
d.d = x;
*n = d.u;
}
else return FAILURE;
return SUCCESS;
}
static int addr2i64 (const void *data, int64_t *n) {
if (LUA_POINTER_64) {
*n = (int64_t)data;
return SUCCESS;
}
else return FAILURE;
}
static int addr2u64 (const void *data, uint64_t *n) {
if (LUA_POINTER_64) {
*n = (uint64_t)data;
return SUCCESS;
}
else return FAILURE;
}
static int hvud2i64 (const void *data, int64_t *n) {
const stack_info *info = data;
const obj64 *o = luaL_testudata(info->L, info->idx, LTYPE);
if (o) {
*n = o->v.i;
return SUCCESS;
}
else return FAILURE;
}
static int hvud2u64 (const void *data, uint64_t *n) {
const stack_info *info = data;
const obj64 *o = luaL_testudata(info->L, info->idx, LTYPE);
if (o) {
*n = o->v.u;
return SUCCESS;
}
else return FAILURE;
}
LUALIB_API int64_t luaL_toi64be (lua_State *L, int idx, int base,
int *errcode) {
int64_t n;
int success;
const int tt = lua_type(L, idx);
switch (tt) {
case LUA_TNUMBER: {
stack_info info = {L, idx};
success = num2i64(&info, &n);
break;
}
case LUA_TSTRING: {
str_info info;
info.s = lua_tolstring(L, idx, &info.l);
info.base = base;
success = str2i64(&info, &n);
break;
}
case LUA_TLIGHTUSERDATA: {
const void *ptr = lua_touserdata(L, idx);
success = addr2i64(ptr, &n);
break;
}
case LUA_TUSERDATA: {
stack_info info = {L, idx};
success = hvud2i64(&info, &n);
break;
}
default: {
success = 1;
break;
}
}
if (errcode) *errcode = success ? 0 : ltag_to_ltoi_error(tt);
return success ? n : 0;
}
LUALIB_API uint64_t luaL_tou64be (lua_State *L, int idx, int base,
int *errcode) {
uint64_t n;
int success;
const int tt = lua_type(L, idx);
switch (tt) {
case LUA_TNUMBER: {
stack_info info = {L, idx};
success = num2u64(&info, &n);
break;
}
case LUA_TSTRING: {
str_info info;
info.s = lua_tolstring(L, idx, &info.l);
info.base = base;
success = str2u64(&info, &n);
break;
}
case LUA_TLIGHTUSERDATA: {
const void *ptr = lua_touserdata(L, idx);
success = addr2u64(ptr, &n);
break;
}
case LUA_TUSERDATA: {
stack_info info = {L, idx};
success = hvud2u64(&info, &n);
break;
}
default: {
success = 1;
break;
}
}
if (errcode) *errcode = success ? 0 : ltag_to_ltoi_error(tt);
return success ? n : 0;
}
LUALIB_API int64_t luaL_toi64bx (lua_State *L, int idx, int base,
int *isnum) {
int errcode;
int64_t n = luaL_toi64be(L, idx, base, &errcode);
if (isnum) *isnum = (errcode == 0);
return n;
}
LUALIB_API uint64_t luaL_tou64bx (lua_State *L, int idx, int base,
int *isnum) {
int errcode;
uint64_t n = luaL_tou64be(L, idx, base, &errcode);
if (isnum) *isnum = (errcode == 0);
return n;
}
static void i64pushfstr (lua_State *L, void *n, const char *fmt,
int _unsigned) {
char buf[ITOA_BUFSIZ];
int64_t i;
int offset = 0;
if (strchr(fmt, 'x') && !_unsigned) {
i = *(int64_t *)n;
if (i < 0) {
buf[offset++] = '-';
i = -i;
n = &i;
}
}
sprintf(&buf[offset], fmt, _unsigned ? *(uint64_t *)n : *(int64_t *)n);
lua_pushstring(L, buf);
}
LUALIB_API void luaL_i64pushstring (lua_State *L, int64_t n) {
i64pushfstr(L, &n, "%"PRId64 SUFFIX_SIGNED, 0);
}
LUALIB_API void luaL_u64pushstring (lua_State *L, uint64_t n) {
i64pushfstr(L, &n, "%"PRIu64 SUFFIX_UNSIGNED, 1);
}
LUALIB_API void luaL_i64pushhex (lua_State *L, int64_t n) {
i64pushfstr(L, &n, "%#"PRIx64 SUFFIX_SIGNED, 0);
}
LUALIB_API void luaL_u64pushhex (lua_State *L, uint64_t n) {
i64pushfstr(L, &n, "%#"PRIx64 SUFFIX_UNSIGNED, 1);
}
/* }================================================================= */
/*
** {=================================================================
** If the absolute value of the int fits in 2**53, this works fine.
**
** OTHERWISE:
** This is incredibly hacky and may not even work.
** Fit a 64-bit into the bit field of a double, if this Lua
** defined lua_Number as double.
** !!! UNDEFINED BEHAVIOR / IMPLEMENTATION-DEFINED / NOT GUARANTEED !!!
** ==================================================================
*/
#define ERR_LNUM "cannot convert int64 to number"
LUALIB_API void luaL_i64pushnumber (lua_State *L, int64_t n) {
if (LUA_NUMBER_64) {
if (abs(n) <= MAX_DBL_INT) {
if (LUA_INTEGER_64)
lua_pushinteger(L, n);
else
lua_pushnumber(L, n);
}
else {
/* !!! XXX: BIT MAGIC / NOT GUARANTEED !!! */
dbl64 x;
x.i = n;
lua_pushnumber(L, x.d);
}
}
else luaL_error(L, ERR_LNUM);
}
LUALIB_API void luaL_u64pushnumber (lua_State *L, uint64_t n) {
if (LUA_NUMBER_64) {
if (n <= MAX_DBL_INT) {
lua_pushnumber(L, n);
}
else {
/* !!! XXX: BIT MAGIC / NOT GUARANTEED !!! */
dbl64 x;
x.u = n;
lua_pushnumber(L, x.d);
}
}
else luaL_error(L, ERR_LNUM);
}
/* }================================================================= */
/*
** {=================================================================
** Throw error if can't fit 64-bit int into light userdatum.
** ==> Throw error on 32-bit systems.
** ==================================================================
*/
#define ERR_ADDR "cannot convert int64 to address"
LUALIB_API void luaL_i64pushaddress (lua_State *L, int64_t n) {
if (LUA_POINTER_64)
lua_pushlightuserdata(L, (void *)n);
else luaL_error(L, ERR_ADDR);
}
LUALIB_API void luaL_u64pushaddress (lua_State *L, uint64_t n) {
if (LUA_POINTER_64)
lua_pushlightuserdata(L, (void *)n);
else luaL_error(L, ERR_ADDR);
}
/* }================================================================= */
/*
** {=================================================================
** Lua interface to 64-bit types.
** ==================================================================
*/
#define toInt64(L) luaL_checkudata(L, 1, LTYPE)
static obj64 *obj64_pushraw (lua_State *L) {
obj64 *o = lua_newuserdata(L, sizeof *o);
luaL_setmetatable(L, LTYPE);
return o;
}
static obj64 *obj64_pushint (lua_State *L, int64_t n) {
obj64 *o = obj64_pushraw(L);
o->_unsigned = 0;
o->v.i = n;
return o;
}
static obj64 *obj64_pushunsigned (lua_State *L, uint64_t n) {
obj64 *o = obj64_pushraw(L);
o->_unsigned = 1;
o->v.u = n;
return o;
}
static int obj64_unew (lua_State *L) {
int base = luaL_optint(L, 2, 0);
int errcode;
uint64_t n = luaL_tou64be(L, 1, base, &errcode);
if (errcode != 0) {
lua_pushnil(L);
luaL_i64pushstrerror(L, errcode);
return 2;
}
obj64_pushunsigned(L, n);
return 1;
}
static int obj64_inew (lua_State *L) {
int base = luaL_optint(L, 2, 0);
int errcode;
int64_t n = luaL_tou64be(L, 1, base, &errcode);
if (errcode != 0) {
lua_pushnil(L);
luaL_i64pushstrerror(L, errcode);
return 2;
}
obj64_pushint(L, n);
return 1;
}
// FIXME: remove module self at index 1
static int obj64_xnew (lua_State *L) {
int isnum;
int base = (int)lua_tointegerx(L, 2, &isnum); /* zero by default */
int top = isnum ? 3 : 2;
lua_settop(L, top);
lua_pushcfunction(L, lua_toboolean(L, top) ? obj64_unew : obj64_inew);
lua_pushvalue(L, 1);
lua_pushinteger(L, base);
lua_call(L, 2, 1);
return 1;
}
static int obj64_tostring (lua_State *L) {
obj64 *o = toInt64(L);
if (o->_unsigned)
luaL_u64pushstring(L, o->v.u);
else
luaL_i64pushstring(L, o->v.i);
return 1;
}
static int obj64_unsigned (lua_State *L) {
obj64 *o = toInt64(L);
lua_pushboolean(L, o->_unsigned);
return 1;
}
static int obj64_toaddr (lua_State *L) {
obj64 *o = toInt64(L);
if (o->_unsigned)
luaL_u64pushaddress(L, o->v.u);
else
luaL_i64pushaddress(L, o->v.i);
return 1;
}
static int obj64_tonumber (lua_State *L) {
obj64 *o = toInt64(L);
if (o->_unsigned)
luaL_u64pushnumber(L, o->v.u);
else
luaL_u64pushnumber(L, o->v.i);
return 1;
}
static int obj64_tohex (lua_State *L) {
obj64 *o = toInt64(L);
if (o->_unsigned)
luaL_u64pushhex(L, o->v.u);
else
luaL_i64pushhex(L, o->v.i);
return 1;
}
static int i_compare (int op, int64_t a, int64_t b) {
switch (op) {
case LUA_OPEQ: return a == b;
case LUA_OPLT: return a < b;
case LUA_OPLE: return a <= b;
default: return 0;
}
}
static int u_compare (int op, uint64_t a, uint64_t b) {
switch (op) {
case LUA_OPEQ: return a == b;
case LUA_OPLT: return a < b;
case LUA_OPLE: return a <= b;
default: return 0;
}
}
static int obj64_compare (lua_State *L, int op) {
obj64 *o;
int other;
int errcode;
switch (lua_type(L, 1)) {
case LUA_TUSERDATA: {
other = 2;
o = luaL_checkudata(L, 1, LTYPE);
}
default: {
other = 1;
o = luaL_checkudata(L, 2, LTYPE);
}
}
if (o->_unsigned) {
uint64_t u = luaL_tou64be(L, other, 0, &errcode);
if (errcode == 0) {
uint64_t a, b;
if (other == 2) { a = o->v.u; b = u; }
else { b = o->v.u; a = u; }
lua_pushboolean(L, u_compare(op, a, b));
return 1;
}
}
else {
int64_t i = luaL_toi64be(L, other, 0, &errcode);
if (errcode == 0) {
int64_t a, b;
if (other == 2) { a = o->v.i; b = i; }
else { b = o->v.i; a = i; }
lua_pushboolean(L, i_compare(op, a, b));
return 1;
}
}
lua_pushnil(L);
luaL_i64pushstrerror(L, errcode);
return 2;
}
static int obj64_eq (lua_State *L) {
return obj64_compare(L, LUA_OPEQ);
}
static int obj64_lt (lua_State *L) {
return obj64_compare(L, LUA_OPLT);
}
static int obj64_le (lua_State *L) {
return obj64_compare(L, LUA_OPLE);
}
static int64_t i_arith (int op, int64_t a, int64_t b) {
switch (op) {
case LUA_OPADD: return a + b;
case LUA_OPSUB: return a - b;
case LUA_OPMUL: return a * b;
case LUA_OPDIV: return a / b;
case LUA_OPMOD: return a % b;
case LUA_OPPOW: {
if (b < 0) return 0;
else if (b == 0) return 1;
else if (b == 1) return a;
if (a == 2) {
if (b >= 0)
return 1 << b;
else
return 1 >> -b;
}
else {
/* exponentiation by squaring */
/* http://stackoverflow.com/a/101613 */
int64_t r = 1;
while (b) {
if (b & 1) r *= a;
b >>= 1;
a *= a;
}
return r;
}
}
default: return 0;
}
}
static uint64_t u_arith (int op, uint64_t a, uint64_t b) {
switch (op) {
case LUA_OPADD: return a + b;
case LUA_OPSUB: return a - b;
case LUA_OPMUL: return a * b;
case LUA_OPDIV: return a / b;
case LUA_OPMOD: return a % b;
case LUA_OPPOW: {
if (b == 0) return 1;
else if (b == 1) return a;
if (a == 2) return 1 << b;
else {
/* exponentiation by squaring */
/* http://stackoverflow.com/a/101613 */
uint64_t r = 1;
while (b) {
if (b & 1) r *= a;
b >>= 1;
a *= a;
}
return r;
}
}
default: return 0;
}
}
static int obj64_arith (lua_State *L, int op) {
obj64 *o;
int errcode;
o = toInt64(L);
if (o->_unsigned) {
uint64_t u = luaL_tou64be(L, 2, 0, &errcode);
if (errcode == 0) {
obj64_pushunsigned(L, u_arith(op, o->v.u, u));
return 1;
}
}
else {
int64_t i = luaL_toi64be(L, 2, 0, &errcode);
if (errcode == 0) {
obj64_pushint(L, i_arith(op, o->v.i, i));
return 1;
}
}
lua_pushnil(L);
luaL_i64pushstrerror(L, errcode);
return 2;
}
static int obj64_add (lua_State *L) {
return obj64_arith(L, LUA_OPADD);
}
static int obj64_sub (lua_State *L) {
return obj64_arith(L, LUA_OPSUB);
}
static int obj64_mul (lua_State *L) {
return obj64_arith(L, LUA_OPMUL);
}
static int obj64_div (lua_State *L) {
return obj64_arith(L, LUA_OPDIV);
}
static int obj64_mod (lua_State *L) {
return obj64_arith(L, LUA_OPMOD);
}
static int obj64_pow (lua_State *L) {
return obj64_arith(L, LUA_OPPOW);
}
static int obj64_unm (lua_State *L) {
obj64 *o = toInt64(L);
int64_t n = o->_unsigned ? (int64_t)o->v.u : o->v.i;
obj64_pushint(L, -n);
return 1;
}
static const luaL_Reg lib[] = {
{"new_signed", obj64_inew},
{"new_unsigned", obj64_unew},
{"__call", obj64_xnew},
{NULL, NULL}
};
static const luaL_Reg meta[] = {
{"__tostring", obj64_tostring},
{"__eq", obj64_eq},
{"__lt", obj64_lt},
{"__le", obj64_le},
{"__add", obj64_add},
{"__sub", obj64_sub},
{"__mul", obj64_mul},
{"__div", obj64_div},
{"__mod", obj64_mod},
{"__pow", obj64_pow},
{"__unm", obj64_unm},
{"unsigned", obj64_unsigned},
{"tohex", obj64_tohex},
{"tonumber", obj64_tonumber},
{"toaddress", obj64_toaddr},
{NULL, NULL}
};
#if LUA_VERSION_NUM == 502
#define luaL_register(L,n,l) ((n) ? luaL_setfuncs(L,l,0) : luaL_newlib(L,l))
#endif
int luaopen_int64 (lua_State *L) {
luaL_newmetatable(L, LTYPE);
luaL_register(L, NULL, meta);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
luaL_register(L, "int64", lib);
lua_pushvalue(L, -1);
lua_setmetatable(L, -2);
return 1;
}
/* }================================================================= */