aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/luajit-2.0/src/lj_carith.c
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/luajit-2.0/src/lj_carith.c')
-rw-r--r--libraries/luajit-2.0/src/lj_carith.c314
1 files changed, 314 insertions, 0 deletions
diff --git a/libraries/luajit-2.0/src/lj_carith.c b/libraries/luajit-2.0/src/lj_carith.c
new file mode 100644
index 0000000..b0f4c65
--- /dev/null
+++ b/libraries/luajit-2.0/src/lj_carith.c
@@ -0,0 +1,314 @@
1/*
2** C data arithmetic.
3** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h
4*/
5
6#include "lj_obj.h"
7
8#if LJ_HASFFI
9
10#include "lj_gc.h"
11#include "lj_err.h"
12#include "lj_tab.h"
13#include "lj_meta.h"
14#include "lj_ctype.h"
15#include "lj_cconv.h"
16#include "lj_cdata.h"
17#include "lj_carith.h"
18
19/* -- C data arithmetic --------------------------------------------------- */
20
21/* Binary operands of an operator converted to ctypes. */
22typedef struct CDArith {
23 uint8_t *p[2];
24 CType *ct[2];
25} CDArith;
26
27/* Check arguments for arithmetic metamethods. */
28static int carith_checkarg(lua_State *L, CTState *cts, CDArith *ca)
29{
30 TValue *o = L->base;
31 int ok = 1;
32 MSize i;
33 if (o+1 >= L->top)
34 lj_err_argt(L, 1, LUA_TCDATA);
35 for (i = 0; i < 2; i++, o++) {
36 if (tviscdata(o)) {
37 GCcdata *cd = cdataV(o);
38 CTypeID id = (CTypeID)cd->typeid;
39 CType *ct = ctype_raw(cts, id);
40 uint8_t *p = (uint8_t *)cdataptr(cd);
41 if (ctype_isptr(ct->info)) {
42 p = (uint8_t *)cdata_getptr(p, ct->size);
43 if (ctype_isref(ct->info)) ct = ctype_rawchild(cts, ct);
44 } else if (ctype_isfunc(ct->info)) {
45 p = (uint8_t *)*(void **)p;
46 ct = ctype_get(cts,
47 lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|id), CTSIZE_PTR));
48 }
49 ca->ct[i] = ct;
50 ca->p[i] = p;
51 } else if (tvisint(o)) {
52 ca->ct[i] = ctype_get(cts, CTID_INT32);
53 ca->p[i] = (uint8_t *)&o->i;
54 } else if (tvisnum(o)) {
55 ca->ct[i] = ctype_get(cts, CTID_DOUBLE);
56 ca->p[i] = (uint8_t *)&o->n;
57 } else if (tvisnil(o)) {
58 ca->ct[i] = ctype_get(cts, CTID_P_VOID);
59 ca->p[i] = (uint8_t *)0;
60 } else {
61 ca->ct[i] = NULL;
62 ca->p[i] = NULL;
63 ok = 0;
64 }
65 }
66 return ok;
67}
68
69/* Pointer arithmetic. */
70static int carith_ptr(lua_State *L, CTState *cts, CDArith *ca, MMS mm)
71{
72 CType *ctp = ca->ct[0];
73 uint8_t *pp = ca->p[0];
74 ptrdiff_t idx;
75 CTSize sz;
76 CTypeID id;
77 GCcdata *cd;
78 if (ctype_isptr(ctp->info) || ctype_isrefarray(ctp->info)) {
79 if ((mm == MM_sub || mm == MM_eq || mm == MM_lt || mm == MM_le) &&
80 (ctype_isptr(ca->ct[1]->info) || ctype_isrefarray(ca->ct[1]->info))) {
81 uint8_t *pp2 = ca->p[1];
82 if (mm == MM_eq) { /* Pointer equality. Incompatible pointers are ok. */
83 setboolV(L->top-1, (pp == pp2));
84 return 1;
85 }
86 if (!lj_cconv_compatptr(cts, ctp, ca->ct[1], CCF_IGNQUAL))
87 return 0;
88 if (mm == MM_sub) { /* Pointer difference. */
89 intptr_t diff;
90 sz = lj_ctype_size(cts, ctype_cid(ctp->info)); /* Element size. */
91 if (sz == 0 || sz == CTSIZE_INVALID)
92 return 0;
93 diff = ((intptr_t)pp - (intptr_t)pp2) / (int32_t)sz;
94 /* All valid pointer differences on x64 are in (-2^47, +2^47),
95 ** which fits into a double without loss of precision.
96 */
97 setintptrV(L->top-1, (int32_t)diff);
98 return 1;
99 } else if (mm == MM_lt) { /* Pointer comparison (unsigned). */
100 setboolV(L->top-1, ((uintptr_t)pp < (uintptr_t)pp2));
101 return 1;
102 } else {
103 lua_assert(mm == MM_le);
104 setboolV(L->top-1, ((uintptr_t)pp <= (uintptr_t)pp2));
105 return 1;
106 }
107 }
108 if (!((mm == MM_add || mm == MM_sub) && ctype_isnum(ca->ct[1]->info)))
109 return 0;
110 lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT_PSZ), ca->ct[1],
111 (uint8_t *)&idx, ca->p[1], 0);
112 if (mm == MM_sub) idx = -idx;
113 } else if (mm == MM_add && ctype_isnum(ctp->info) &&
114 (ctype_isptr(ca->ct[1]->info) || ctype_isrefarray(ca->ct[1]->info))) {
115 /* Swap pointer and index. */
116 ctp = ca->ct[1]; pp = ca->p[1];
117 lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT_PSZ), ca->ct[0],
118 (uint8_t *)&idx, ca->p[0], 0);
119 } else {
120 return 0;
121 }
122 sz = lj_ctype_size(cts, ctype_cid(ctp->info)); /* Element size. */
123 if (sz == CTSIZE_INVALID)
124 return 0;
125 pp += idx*(int32_t)sz; /* Compute pointer + index. */
126 id = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(ctp->info)),
127 CTSIZE_PTR);
128 cd = lj_cdata_new(cts, id, CTSIZE_PTR);
129 *(uint8_t **)cdataptr(cd) = pp;
130 setcdataV(L, L->top-1, cd);
131 lj_gc_check(L);
132 return 1;
133}
134
135/* 64 bit integer arithmetic. */
136static int carith_int64(lua_State *L, CTState *cts, CDArith *ca, MMS mm)
137{
138 if (ctype_isnum(ca->ct[0]->info) && ca->ct[0]->size <= 8 &&
139 ctype_isnum(ca->ct[1]->info) && ca->ct[1]->size <= 8) {
140 CTypeID id = (((ca->ct[0]->info & CTF_UNSIGNED) && ca->ct[0]->size == 8) ||
141 ((ca->ct[1]->info & CTF_UNSIGNED) && ca->ct[1]->size == 8)) ?
142 CTID_UINT64 : CTID_INT64;
143 CType *ct = ctype_get(cts, id);
144 GCcdata *cd;
145 uint64_t u0, u1, *up;
146 lj_cconv_ct_ct(cts, ct, ca->ct[0], (uint8_t *)&u0, ca->p[0], 0);
147 if (mm != MM_unm)
148 lj_cconv_ct_ct(cts, ct, ca->ct[1], (uint8_t *)&u1, ca->p[1], 0);
149 switch (mm) {
150 case MM_eq:
151 setboolV(L->top-1, (u0 == u1));
152 return 1;
153 case MM_lt:
154 setboolV(L->top-1,
155 id == CTID_INT64 ? ((int64_t)u0 < (int64_t)u1) : (u0 < u1));
156 return 1;
157 case MM_le:
158 setboolV(L->top-1,
159 id == CTID_INT64 ? ((int64_t)u0 <= (int64_t)u1) : (u0 <= u1));
160 return 1;
161 default: break;
162 }
163 cd = lj_cdata_new(cts, id, 8);
164 up = (uint64_t *)cdataptr(cd);
165 setcdataV(L, L->top-1, cd);
166 switch (mm) {
167 case MM_add: *up = u0 + u1; break;
168 case MM_sub: *up = u0 - u1; break;
169 case MM_mul: *up = u0 * u1; break;
170 case MM_div:
171 if (id == CTID_INT64)
172 *up = (uint64_t)lj_carith_divi64((int64_t)u0, (int64_t)u1);
173 else
174 *up = lj_carith_divu64(u0, u1);
175 break;
176 case MM_mod:
177 if (id == CTID_INT64)
178 *up = (uint64_t)lj_carith_modi64((int64_t)u0, (int64_t)u1);
179 else
180 *up = lj_carith_modu64(u0, u1);
181 break;
182 case MM_pow:
183 if (id == CTID_INT64)
184 *up = (uint64_t)lj_carith_powi64((int64_t)u0, (int64_t)u1);
185 else
186 *up = lj_carith_powu64(u0, u1);
187 break;
188 case MM_unm: *up = (uint64_t)-(int64_t)u0; break;
189 default: lua_assert(0); break;
190 }
191 lj_gc_check(L);
192 return 1;
193 }
194 return 0;
195}
196
197/* Handle ctype arithmetic metamethods. */
198static int lj_carith_meta(lua_State *L, CTState *cts, CDArith *ca, MMS mm)
199{
200 cTValue *tv = NULL;
201 if (tviscdata(L->base))
202 tv = lj_ctype_meta(cts, cdataV(L->base)->typeid, mm);
203 if (!tv && L->base+1 < L->top && tviscdata(L->base+1))
204 tv = lj_ctype_meta(cts, cdataV(L->base+1)->typeid, mm);
205 if (!tv) {
206 const char *repr[2];
207 int i;
208 for (i = 0; i < 2; i++) {
209 if (ca->ct[i])
210 repr[i] = strdata(lj_ctype_repr(L, ctype_typeid(cts, ca->ct[i]), NULL));
211 else
212 repr[i] = typename(&L->base[i]);
213 }
214 lj_err_callerv(L, mm == MM_len ? LJ_ERR_FFI_BADLEN :
215 mm == MM_concat ? LJ_ERR_FFI_BADCONCAT :
216 mm < MM_add ? LJ_ERR_FFI_BADCOMP : LJ_ERR_FFI_BADARITH,
217 repr[0], repr[1]);
218 }
219 return lj_meta_tailcall(L, tv);
220}
221
222/* Arithmetic operators for cdata. */
223int lj_carith_op(lua_State *L, MMS mm)
224{
225 CTState *cts = ctype_cts(L);
226 CDArith ca;
227 if (carith_checkarg(L, cts, &ca)) {
228 if (carith_int64(L, cts, &ca, mm) || carith_ptr(L, cts, &ca, mm)) {
229 copyTV(L, &G(L)->tmptv2, L->top-1); /* Remember for trace recorder. */
230 return 1;
231 }
232 }
233 return lj_carith_meta(L, cts, &ca, mm);
234}
235
236/* -- 64 bit integer arithmetic helpers ----------------------------------- */
237
238#if LJ_32 && LJ_HASJIT
239/* Signed/unsigned 64 bit multiplication. */
240int64_t lj_carith_mul64(int64_t a, int64_t b)
241{
242 return a * b;
243}
244#endif
245
246/* Unsigned 64 bit division. */
247uint64_t lj_carith_divu64(uint64_t a, uint64_t b)
248{
249 if (b == 0) return U64x(80000000,00000000);
250 return a / b;
251}
252
253/* Signed 64 bit division. */
254int64_t lj_carith_divi64(int64_t a, int64_t b)
255{
256 if (b == 0 || (a == (int64_t)U64x(80000000,00000000) && b == -1))
257 return U64x(80000000,00000000);
258 return a / b;
259}
260
261/* Unsigned 64 bit modulo. */
262uint64_t lj_carith_modu64(uint64_t a, uint64_t b)
263{
264 if (b == 0) return U64x(80000000,00000000);
265 return a % b;
266}
267
268/* Signed 64 bit modulo. */
269int64_t lj_carith_modi64(int64_t a, int64_t b)
270{
271 if (b == 0) return U64x(80000000,00000000);
272 if (a == (int64_t)U64x(80000000,00000000) && b == -1) return 0;
273 return a % b;
274}
275
276/* Unsigned 64 bit x^k. */
277uint64_t lj_carith_powu64(uint64_t x, uint64_t k)
278{
279 uint64_t y;
280 if (k == 0)
281 return 1;
282 for (; (k & 1) == 0; k >>= 1) x *= x;
283 y = x;
284 if ((k >>= 1) != 0) {
285 for (;;) {
286 x *= x;
287 if (k == 1) break;
288 if (k & 1) y *= x;
289 k >>= 1;
290 }
291 y *= x;
292 }
293 return y;
294}
295
296/* Signed 64 bit x^k. */
297int64_t lj_carith_powi64(int64_t x, int64_t k)
298{
299 if (k == 0)
300 return 1;
301 if (k < 0) {
302 if (x == 0)
303 return U64x(7fffffff,ffffffff);
304 else if (x == 1)
305 return 1;
306 else if (x == -1)
307 return (k & 1) ? -1 : 1;
308 else
309 return 0;
310 }
311 return (int64_t)lj_carith_powu64((uint64_t)x, (uint64_t)k);
312}
313
314#endif