diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-12-05 14:51:58 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-12-05 14:51:58 -0300 |
commit | 95735bda46278a4bc0966d8a3c9079dd0072c51e (patch) | |
tree | a295309dfba9d375a8c970f7717975003b9f690b | |
parent | d30569c06407529cc6e99f4a35ae5f9bfe6fa940 (diff) | |
download | lua-95735bda46278a4bc0966d8a3c9079dd0072c51e.tar.gz lua-95735bda46278a4bc0966d8a3c9079dd0072c51e.tar.bz2 lua-95735bda46278a4bc0966d8a3c9079dd0072c51e.zip |
Simplifications in 'op_arith*' family of macros
-rw-r--r-- | lvm.c | 74 |
1 files changed, 27 insertions, 47 deletions
@@ -885,38 +885,21 @@ void luaV_finishOp (lua_State *L) { | |||
885 | 885 | ||
886 | 886 | ||
887 | /* | 887 | /* |
888 | ** Auxiliary macro for arithmetic operations over floats and others | ||
889 | ** with immediate operand. 'fop' is the float operation; 'tm' is the | ||
890 | ** corresponding metamethod. | ||
891 | */ | ||
892 | #define op_arithfI_aux(L,v1,imm,fop,tm) { \ | ||
893 | lua_Number nb; \ | ||
894 | if (tonumberns(v1, nb)) { \ | ||
895 | lua_Number fimm = cast_num(imm); \ | ||
896 | pc++; setfltvalue(s2v(ra), fop(L, nb, fimm)); \ | ||
897 | }} | ||
898 | |||
899 | |||
900 | /* | ||
901 | ** Arithmetic operations over floats and others with immediate operand. | ||
902 | */ | ||
903 | #define op_arithfI(L,fop,tm) { \ | ||
904 | TValue *v1 = vRB(i); \ | ||
905 | int imm = GETARG_sC(i); \ | ||
906 | op_arithfI_aux(L, v1, imm, fop, tm, 0); } | ||
907 | |||
908 | /* | ||
909 | ** Arithmetic operations with immediate operands. 'iop' is the integer | 888 | ** Arithmetic operations with immediate operands. 'iop' is the integer |
910 | ** operation. | 889 | ** operation, 'fop' is the float operation. |
911 | */ | 890 | */ |
912 | #define op_arithI(L,iop,fop,tm) { \ | 891 | #define op_arithI(L,iop,fop) { \ |
913 | TValue *v1 = vRB(i); \ | 892 | TValue *v1 = vRB(i); \ |
914 | int imm = GETARG_sC(i); \ | 893 | int imm = GETARG_sC(i); \ |
915 | if (ttisinteger(v1)) { \ | 894 | if (ttisinteger(v1)) { \ |
916 | lua_Integer iv1 = ivalue(v1); \ | 895 | lua_Integer iv1 = ivalue(v1); \ |
917 | pc++; setivalue(s2v(ra), iop(L, iv1, imm)); \ | 896 | pc++; setivalue(s2v(ra), iop(L, iv1, imm)); \ |
918 | } \ | 897 | } \ |
919 | else op_arithfI_aux(L, v1, imm, fop, tm); } | 898 | else if (ttisfloat(v1)) { \ |
899 | lua_Number nb = fltvalue(v1); \ | ||
900 | lua_Number fimm = cast_num(imm); \ | ||
901 | pc++; setfltvalue(s2v(ra), fop(L, nb, fimm)); \ | ||
902 | }} | ||
920 | 903 | ||
921 | 904 | ||
922 | /* | 905 | /* |
@@ -940,11 +923,18 @@ void luaV_finishOp (lua_State *L) { | |||
940 | 923 | ||
941 | 924 | ||
942 | /* | 925 | /* |
943 | ** Arithmetic operations with register operands. | 926 | ** Arithmetic operations with K operands for floats. |
944 | */ | 927 | */ |
945 | #define op_arith(L,iop,fop) { \ | 928 | #define op_arithfK(L,fop) { \ |
946 | TValue *v1 = vRB(i); \ | 929 | TValue *v1 = vRB(i); \ |
947 | TValue *v2 = vRC(i); \ | 930 | TValue *v2 = KC(i); \ |
931 | op_arithf_aux(L, v1, v2, fop); } | ||
932 | |||
933 | |||
934 | /* | ||
935 | ** Arithmetic operations over integers and floats. | ||
936 | */ | ||
937 | #define op_arith_aux(L,v1,v2,iop,fop) { \ | ||
948 | if (ttisinteger(v1) && ttisinteger(v2)) { \ | 938 | if (ttisinteger(v1) && ttisinteger(v2)) { \ |
949 | lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \ | 939 | lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \ |
950 | pc++; setivalue(s2v(ra), iop(L, i1, i2)); \ | 940 | pc++; setivalue(s2v(ra), iop(L, i1, i2)); \ |
@@ -953,32 +943,21 @@ void luaV_finishOp (lua_State *L) { | |||
953 | 943 | ||
954 | 944 | ||
955 | /* | 945 | /* |
956 | ** Arithmetic operations with K operands. | 946 | ** Arithmetic operations with register operands. |
957 | */ | 947 | */ |
958 | #define op_arithK(L,iop,fop) { \ | 948 | #define op_arith(L,iop,fop) { \ |
959 | TValue *v1 = vRB(i); \ | 949 | TValue *v1 = vRB(i); \ |
960 | TValue *v2 = KC(i); \ | 950 | TValue *v2 = vRC(i); \ |
961 | if (ttisinteger(v1) && ttisinteger(v2)) { \ | 951 | op_arith_aux(L, v1, v2, iop, fop); } |
962 | lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \ | ||
963 | pc++; setivalue(s2v(ra), iop(L, i1, i2)); \ | ||
964 | } \ | ||
965 | else { \ | ||
966 | lua_Number n1; lua_Number n2; \ | ||
967 | if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \ | ||
968 | pc++; setfltvalue(s2v(ra), fop(L, n1, n2)); \ | ||
969 | }}} | ||
970 | 952 | ||
971 | 953 | ||
972 | /* | 954 | /* |
973 | ** Arithmetic operations with K operands for floats. | 955 | ** Arithmetic operations with K operands. |
974 | */ | 956 | */ |
975 | #define op_arithfK(L,fop) { \ | 957 | #define op_arithK(L,iop,fop) { \ |
976 | TValue *v1 = vRB(i); \ | 958 | TValue *v1 = vRB(i); \ |
977 | TValue *v2 = KC(i); \ | 959 | TValue *v2 = KC(i); \ |
978 | lua_Number n1; lua_Number n2; \ | 960 | op_arith_aux(L, v1, v2, iop, fop); } |
979 | if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \ | ||
980 | pc++; setfltvalue(s2v(ra), fop(L, n1, n2)); \ | ||
981 | }} | ||
982 | 961 | ||
983 | 962 | ||
984 | /* | 963 | /* |
@@ -1025,7 +1004,8 @@ void luaV_finishOp (lua_State *L) { | |||
1025 | 1004 | ||
1026 | 1005 | ||
1027 | /* | 1006 | /* |
1028 | ** Order operations with immediate operand. | 1007 | ** Order operations with immediate operand. (Immediate operand is |
1008 | ** always small enough to have an exact representation as a float.) | ||
1029 | */ | 1009 | */ |
1030 | #define op_orderI(L,opi,opf,inv,tm) { \ | 1010 | #define op_orderI(L,opi,opf,inv,tm) { \ |
1031 | int cond; \ | 1011 | int cond; \ |
@@ -1364,7 +1344,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { | |||
1364 | vmbreak; | 1344 | vmbreak; |
1365 | } | 1345 | } |
1366 | vmcase(OP_ADDI) { | 1346 | vmcase(OP_ADDI) { |
1367 | op_arithI(L, l_addi, luai_numadd, TM_ADD); | 1347 | op_arithI(L, l_addi, luai_numadd); |
1368 | vmbreak; | 1348 | vmbreak; |
1369 | } | 1349 | } |
1370 | vmcase(OP_ADDK) { | 1350 | vmcase(OP_ADDK) { |