Add required PHIs for implicit conversions (via XREF fwd).
[luajit-2.0.git] / src / lj_carith.c
blob56708bf67dc68bdcc7c131f8b4908ce4819cf2c0
1 /*
2 ** C data arithmetic.
3 ** Copyright (C) 2005-2012 Mike Pall. See Copyright Notice in luajit.h
4 */
6 #include "lj_obj.h"
8 #if LJ_HASFFI
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"
19 /* -- C data arithmetic --------------------------------------------------- */
21 /* Binary operands of an operator converted to ctypes. */
22 typedef struct CDArith {
23 uint8_t *p[2];
24 CType *ct[2];
25 } CDArith;
27 /* Check arguments for arithmetic metamethods. */
28 static int carith_checkarg(lua_State *L, CTState *cts, CDArith *ca)
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));
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;
66 return ok;
69 /* Pointer arithmetic. */
70 static int carith_ptr(lua_State *L, CTState *cts, CDArith *ca, MMS mm)
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;
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.
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;
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;
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;
135 /* 64 bit integer arithmetic. */
136 static int carith_int64(lua_State *L, CTState *cts, CDArith *ca, MMS mm)
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;
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;
191 lj_gc_check(L);
192 return 1;
194 return 0;
197 /* Handle ctype arithmetic metamethods. */
198 static int lj_carith_meta(lua_State *L, CTState *cts, CDArith *ca, MMS mm)
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]);
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]);
219 return lj_meta_tailcall(L, tv);
222 /* Arithmetic operators for cdata. */
223 int lj_carith_op(lua_State *L, MMS mm)
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;
233 return lj_carith_meta(L, cts, &ca, mm);
236 /* -- 64 bit integer arithmetic helpers ----------------------------------- */
238 #if LJ_32 && LJ_HASJIT
239 /* Signed/unsigned 64 bit multiplication. */
240 int64_t lj_carith_mul64(int64_t a, int64_t b)
242 return a * b;
244 #endif
246 /* Unsigned 64 bit division. */
247 uint64_t lj_carith_divu64(uint64_t a, uint64_t b)
249 if (b == 0) return U64x(80000000,00000000);
250 return a / b;
253 /* Signed 64 bit division. */
254 int64_t lj_carith_divi64(int64_t a, int64_t b)
256 if (b == 0 || (a == (int64_t)U64x(80000000,00000000) && b == -1))
257 return U64x(80000000,00000000);
258 return a / b;
261 /* Unsigned 64 bit modulo. */
262 uint64_t lj_carith_modu64(uint64_t a, uint64_t b)
264 if (b == 0) return U64x(80000000,00000000);
265 return a % b;
268 /* Signed 64 bit modulo. */
269 int64_t lj_carith_modi64(int64_t a, int64_t b)
271 if (b == 0) return U64x(80000000,00000000);
272 if (a == (int64_t)U64x(80000000,00000000) && b == -1) return 0;
273 return a % b;
276 /* Unsigned 64 bit x^k. */
277 uint64_t lj_carith_powu64(uint64_t x, uint64_t k)
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;
291 y *= x;
293 return y;
296 /* Signed 64 bit x^k. */
297 int64_t lj_carith_powi64(int64_t x, int64_t k)
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;
311 return (int64_t)lj_carith_powu64((uint64_t)x, (uint64_t)k);
314 #endif