aboutsummaryrefslogtreecommitdiff
path: root/lvm.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2018-11-23 12:23:45 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2018-11-23 12:23:45 -0200
commit84e32ad2ebd6bd160c1320456743a5b1d8f233e9 (patch)
treec5a32eaf7577c99e7598e9de1385a6b53522f04b /lvm.c
parent35296e1fde705b3ac8356a4f4ce426497cf7b64c (diff)
downloadlua-84e32ad2ebd6bd160c1320456743a5b1d8f233e9.tar.gz
lua-84e32ad2ebd6bd160c1320456743a5b1d8f233e9.tar.bz2
lua-84e32ad2ebd6bd160c1320456743a5b1d8f233e9.zip
Added opcodes for arithmetic with K operands
Added opcodes for all seven arithmetic operators with K operands (that is, operands that are numbers in the array of constants of the function). They cover the cases of constant float operands (e.g., 'x + .0.0', 'x^0.5') and large integer operands (e.g., 'x % 10000').
Diffstat (limited to 'lvm.c')
-rw-r--r--lvm.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/lvm.c b/lvm.c
index ece7d77a..27ef68cf 100644
--- a/lvm.c
+++ b/lvm.c
@@ -856,6 +856,39 @@ void luaV_finishOp (lua_State *L) {
856 856
857 857
858/* 858/*
859** Arithmetic operations with K operands.
860*/
861#define op_arithK(L,iop,fop,tm,flip) { \
862 TValue *v1 = vRB(i); \
863 TValue *v2 = KC(i); \
864 if (ttisinteger(v1) && ttisinteger(v2)) { \
865 lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \
866 setivalue(s2v(ra), iop(L, i1, i2)); \
867 } \
868 else { \
869 lua_Number n1; lua_Number n2; \
870 if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \
871 setfltvalue(s2v(ra), fop(L, n1, n2)); \
872 } \
873 else \
874 Protect(luaT_trybinassocTM(L, v1, v2, ra, flip, tm)); } }
875
876
877/*
878** Arithmetic operations with K operands for floats.
879*/
880#define op_arithfK(L,fop,tm) { \
881 TValue *v1 = vRB(i); \
882 TValue *v2 = KC(i); \
883 lua_Number n1; lua_Number n2; \
884 if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \
885 setfltvalue(s2v(ra), fop(L, n1, n2)); \
886 } \
887 else \
888 Protect(luaT_trybinTM(L, v1, v2, ra, tm)); }
889
890
891/*
859** Bitwise operations with constant operand. 892** Bitwise operations with constant operand.
860*/ 893*/
861#define op_bitwiseK(L,op,tm) { \ 894#define op_bitwiseK(L,op,tm) { \
@@ -1219,6 +1252,34 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1219 op_arithI(L, luaV_idiv, luai_numidiv, TM_IDIV, 0); 1252 op_arithI(L, luaV_idiv, luai_numidiv, TM_IDIV, 0);
1220 vmbreak; 1253 vmbreak;
1221 } 1254 }
1255 vmcase(OP_ADDK) {
1256 op_arithK(L, l_addi, luai_numadd, TM_ADD, GETARG_k(i));
1257 vmbreak;
1258 }
1259 vmcase(OP_SUBK) {
1260 op_arithK(L, l_subi, luai_numsub, TM_SUB, 0);
1261 vmbreak;
1262 }
1263 vmcase(OP_MULK) {
1264 op_arithK(L, l_muli, luai_nummul, TM_MUL, GETARG_k(i));
1265 vmbreak;
1266 }
1267 vmcase(OP_MODK) {
1268 op_arithK(L, luaV_mod, luaV_modf, TM_MOD, 0);
1269 vmbreak;
1270 }
1271 vmcase(OP_POWK) {
1272 op_arithfK(L, luai_numpow, TM_POW);
1273 vmbreak;
1274 }
1275 vmcase(OP_DIVK) {
1276 op_arithfK(L, luai_numdiv, TM_DIV);
1277 vmbreak;
1278 }
1279 vmcase(OP_IDIVK) {
1280 op_arithK(L, luaV_idiv, luai_numidiv, TM_IDIV, 0);
1281 vmbreak;
1282 }
1222 vmcase(OP_ADD) { 1283 vmcase(OP_ADD) {
1223 op_arith(L, l_addi, luai_numadd, TM_ADD); 1284 op_arith(L, l_addi, luai_numadd, TM_ADD);
1224 vmbreak; 1285 vmbreak;