aboutsummaryrefslogtreecommitdiff
path: root/src/lj_carith.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lj_carith.c')
-rw-r--r--src/lj_carith.c260
1 files changed, 260 insertions, 0 deletions
diff --git a/src/lj_carith.c b/src/lj_carith.c
new file mode 100644
index 00000000..a7d92983
--- /dev/null
+++ b/src/lj_carith.c
@@ -0,0 +1,260 @@
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_ctype.h"
13#include "lj_cconv.h"
14#include "lj_cdata.h"
15#include "lj_carith.h"
16
17/* -- C data arithmetic --------------------------------------------------- */
18
19/* Binary operands of an operator converted to ctypes. */
20typedef struct CDArith {
21 uint8_t *p[2];
22 CType *ct[2];
23} CDArith;
24
25/* Check arguments for arithmetic metamethods. */
26static int carith_checkarg(lua_State *L, CTState *cts, CDArith *ca)
27{
28 TValue *o = L->base;
29 int ok = 1;
30 MSize i;
31 if (o+1 >= L->top)
32 lj_err_argt(L, 1, LUA_TCDATA);
33 for (i = 0; i < 2; i++, o++) {
34 if (tviscdata(o)) {
35 GCcdata *cd = cdataV(o);
36 CType *ct = ctype_raw(cts, (CTypeID)cd->typeid);
37 uint8_t *p = (uint8_t *)cdataptr(cd);
38 if (ctype_isptr(ct->info)) {
39 p = (uint8_t *)cdata_getptr(p, ct->size);
40 if (ctype_isref(ct->info)) ct = ctype_rawchild(cts, ct);
41 }
42 ca->ct[i] = ct;
43 ca->p[i] = p;
44 } else if (tvisnum(o)) {
45 ca->ct[i] = ctype_get(cts, CTID_DOUBLE);
46 ca->p[i] = (uint8_t *)&o->n;
47 } else if (tvisnil(o)) {
48 ca->ct[i] = ctype_get(cts, CTID_P_VOID);
49 ca->p[i] = (uint8_t *)0;
50 } else {
51 ca->ct[i] = NULL;
52 ca->p[i] = NULL;
53 ok = 0;
54 }
55 }
56 return ok;
57}
58
59/* Pointer arithmetic. */
60static int carith_ptr(lua_State *L, CTState *cts, CDArith *ca, MMS mm)
61{
62 CType *ctp = ca->ct[0];
63 uint8_t *pp = ca->p[0];
64 ptrdiff_t idx;
65 CTSize sz;
66 CTypeID id;
67 GCcdata *cd;
68 if (ctype_isptr(ctp->info) || ctype_isrefarray(ctp->info)) {
69 if ((mm == MM_sub || mm == MM_eq || mm == MM_lt || mm == MM_le) &&
70 (ctype_isptr(ca->ct[1]->info) || ctype_isrefarray(ca->ct[1]->info))) {
71 uint8_t *pp2 = ca->p[1];
72 if (mm == MM_eq) { /* Pointer equality. Incompatible pointers are ok. */
73 setboolV(L->top-1, (pp == pp2));
74 return 1;
75 }
76 if (!lj_cconv_compatptr(cts, ctp, ca->ct[1], CCF_IGNQUAL))
77 return 0;
78 if (mm == MM_sub) { /* Pointer difference. */
79 intptr_t diff;
80 sz = lj_ctype_size(cts, ctype_cid(ctp->info)); /* Element size. */
81 if (sz == 0 || sz == CTSIZE_INVALID)
82 return 0;
83 diff = ((intptr_t)pp - (intptr_t)pp2) / (int32_t)sz;
84 /* All valid pointer differences on x64 are in (-2^47, +2^47),
85 ** which fits into a double without loss of precision.
86 */
87 setnumV(L->top-1, (lua_Number)diff);
88 return 1;
89 } else if (mm == MM_lt) { /* Pointer comparison (unsigned). */
90 setboolV(L->top-1, ((uintptr_t)pp < (uintptr_t)pp2));
91 return 1;
92 } else {
93 lua_assert(mm == MM_le);
94 setboolV(L->top-1, ((uintptr_t)pp <= (uintptr_t)pp2));
95 return 1;
96 }
97 }
98 if (!((mm == MM_add || mm == MM_sub) && ctype_isnum(ca->ct[1]->info)))
99 return 0;
100 lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT_PSZ), ca->ct[1],
101 (uint8_t *)&idx, ca->p[1], 0);
102 if (mm == MM_sub) idx = -idx;
103 } else if (mm == MM_add && ctype_isnum(ctp->info) &&
104 (ctype_isptr(ca->ct[1]->info) || ctype_isrefarray(ca->ct[1]->info))) {
105 /* Swap pointer and index. */
106 ctp = ca->ct[1]; pp = ca->p[1];
107 lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT_PSZ), ca->ct[0],
108 (uint8_t *)&idx, ca->p[0], 0);
109 } else {
110 return 0;
111 }
112 sz = lj_ctype_size(cts, ctype_cid(ctp->info)); /* Element size. */
113 if (sz == CTSIZE_INVALID)
114 return 0;
115 pp += idx*(int32_t)sz; /* Compute pointer + index. */
116 id = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(ctp->info)),
117 CTSIZE_PTR);
118 cd = lj_cdata_new(cts, id, CTSIZE_PTR);
119 *(uint8_t **)cdataptr(cd) = pp;
120 setcdataV(L, L->top-1, cd);
121 lj_gc_check(L);
122 return 1;
123}
124
125/* 64 bit integer arithmetic. */
126static int carith_int64(lua_State *L, CTState *cts, CDArith *ca, MMS mm)
127{
128 if (ctype_isnum(ca->ct[0]->info) && ca->ct[0]->size <= 8 &&
129 ctype_isnum(ca->ct[1]->info) && ca->ct[1]->size <= 8) {
130 CTypeID id = (((ca->ct[0]->info & CTF_UNSIGNED) && ca->ct[0]->size == 8) ||
131 ((ca->ct[1]->info & CTF_UNSIGNED) && ca->ct[1]->size == 8)) ?
132 CTID_UINT64 : CTID_INT64;
133 CType *ct = ctype_get(cts, id);
134 GCcdata *cd;
135 uint64_t u0, u1, *up;
136 lj_cconv_ct_ct(cts, ct, ca->ct[0], (uint8_t *)&u0, ca->p[0], 0);
137 if (mm != MM_unm)
138 lj_cconv_ct_ct(cts, ct, ca->ct[1], (uint8_t *)&u1, ca->p[1], 0);
139 switch (mm) {
140 case MM_eq:
141 setboolV(L->top-1, (u0 == u1));
142 return 1;
143 case MM_lt:
144 setboolV(L->top-1,
145 id == CTID_INT64 ? ((int64_t)u0 < (int64_t)u1) : (u0 < u1));
146 return 1;
147 case MM_le:
148 setboolV(L->top-1,
149 id == CTID_INT64 ? ((int64_t)u0 <= (int64_t)u1) : (u0 <= u1));
150 return 1;
151 case MM_div: case MM_mod:
152 if (u1 == 0) { /* Division by zero. */
153 if (u0 == 0)
154 setnanV(L->top-1);
155 else if (id == CTID_INT64 && (int64_t)u0 < 0)
156 setminfV(L->top-1);
157 else
158 setpinfV(L->top-1);
159 return 1;
160 } else if (id == CTID_INT64 && (int64_t)u1 == -1 &&
161 u0 == U64x(80000000,00000000)) { /* MIN64 / -1. */
162 if (mm == MM_div) id = CTID_UINT64; else u0 = 0;
163 mm = MM_unm; /* Result is 0x8000000000000000ULL or 0LL. */
164 }
165 break;
166 default: break;
167 }
168 cd = lj_cdata_new(cts, id, 8);
169 up = (uint64_t *)cdataptr(cd);
170 setcdataV(L, L->top-1, cd);
171 switch (mm) {
172 case MM_add: *up = u0 + u1; break;
173 case MM_sub: *up = u0 - u1; break;
174 case MM_mul: *up = u0 * u1; break;
175 case MM_div:
176 if (id == CTID_INT64)
177 *up = (uint64_t)((int64_t)u0 / (int64_t)u1);
178 else
179 *up = u0 / u1;
180 break;
181 case MM_mod:
182 if (id == CTID_INT64)
183 *up = (uint64_t)((int64_t)u0 % (int64_t)u1);
184 else
185 *up = u0 % u1;
186 break;
187 case MM_pow: *up = lj_carith_powi64(u0, u1, (id == CTID_UINT64)); 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/* Arithmetic operators for cdata. */
198int lj_carith_op(lua_State *L, MMS mm)
199{
200 CTState *cts = ctype_cts(L);
201 CDArith ca;
202 if (carith_checkarg(L, cts, &ca)) {
203 if (carith_int64(L, cts, &ca, mm) || carith_ptr(L, cts, &ca, mm)) {
204 copyTV(L, &G(L)->tmptv2, L->top-1); /* Remember for trace recorder. */
205 return 1;
206 }
207 }
208 /* NYI: per-cdata metamethods. */
209 {
210 const char *repr[2];
211 int i;
212 for (i = 0; i < 2; i++) {
213 if (ca.ct[i])
214 repr[i] = strdata(lj_ctype_repr(L, ctype_typeid(cts, ca.ct[i]), NULL));
215 else
216 repr[i] = typename(&L->base[i]);
217 }
218 lj_err_callerv(L, mm == MM_len ? LJ_ERR_FFI_BADLEN :
219 mm == MM_concat ? LJ_ERR_FFI_BADCONCAT :
220 mm < MM_add ? LJ_ERR_FFI_BADCOMP : LJ_ERR_FFI_BADARITH,
221 repr[0], repr[1]);
222 }
223 return 0; /* unreachable */
224}
225
226/* -- 64 bit integer arithmetic helpers ----------------------------------- */
227
228/* 64 bit integer x^k. */
229uint64_t lj_carith_powi64(uint64_t x, uint64_t k, int isunsigned)
230{
231 uint64_t y = 0;
232 if (k == 0)
233 return 1;
234 if (!isunsigned) {
235 if ((int64_t)k < 0) {
236 if (x == 0)
237 return U64x(7fffffff,ffffffff);
238 else if (x == 1)
239 return 1;
240 else if ((int64_t)x == -1)
241 return (k & 1) ? -1 : 1;
242 else
243 return 0;
244 }
245 }
246 for (; (k & 1) == 0; k >>= 1) x *= x;
247 y = x;
248 if ((k >>= 1) != 0) {
249 for (;;) {
250 x *= x;
251 if (k == 1) break;
252 if (k & 1) y *= x;
253 k >>= 1;
254 }
255 y *= x;
256 }
257 return y;
258}
259
260#endif