From 01eb83dcfe30368633f18161a85414daf3fc407c Mon Sep 17 00:00:00 2001 From: Dibyendu Majumdar Date: Sun, 16 Dec 2018 12:41:21 +0000 Subject: [PATCH] issue #161 avoid taking address of math functions in the fast call scenario, instead assign unique codes to functions that may be inlined --- include/lua.h | 4 +++- ravi-tests/ravi_tests1.ravi | 10 ++++++++++ src/ldo.c | 19 +++++++++++++++---- src/ravijit.cpp | 4 ++-- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/include/lua.h b/include/lua.h index bd3eaa7..0e98d05 100644 --- a/include/lua.h +++ b/include/lua.h @@ -549,7 +549,9 @@ struct lua_Debug { LUA_API void (ravi_pushcfastcall)(lua_State *L, void *ptr, int tag); -#define RAVI_TFCF_D_D 1 +#define RAVI_TFCF_EXP 1 +#define RAVI_TFCF_LOG 2 +#define RAVI_TFCF_D_D 3 /* Create an integer array (specialization of Lua table) * of given size and initialize array with supplied initial value diff --git a/ravi-tests/ravi_tests1.ravi b/ravi-tests/ravi_tests1.ravi index 36719e9..00e0ce8 100644 --- a/ravi-tests/ravi_tests1.ravi +++ b/ravi-tests/ravi_tests1.ravi @@ -1695,6 +1695,16 @@ check(x, 'NEW_IARRAY', 'LOADK', 'SETLIST', 'TEST', 'RETURN', 'RETURN') print 'Test 74 OK' +function x() + assert(math.exp(2) == ravi.exp(2)) + assert(math.log(2) == ravi.ln(2)) + assert(math.exp(math.log(2)) == ravi.exp(ravi.ln(2))) +end +x() +compile(x) +x() +print 'Test 75 OK' + for k,v in pairs(opcodes_coverage) do print(k, v) diff --git a/src/ldo.c b/src/ldo.c index 1c5147c..51fbd76 100644 --- a/src/ldo.c +++ b/src/ldo.c @@ -13,6 +13,7 @@ #include #include #include +#include #include "lua.h" @@ -514,10 +515,12 @@ int luaD_precall (lua_State *L, StkId func, int nresults, int op_call) { return 0; } case RAVI_TFCF: { - int tt = rttype(func); int nargs = L->top - func - 1; - int sig = tt >> 8; + int tt = rttype(func); + int sig = tt >> 8; /* Extract the function signature*/ switch (sig) { + case RAVI_TFCF_LOG: + case RAVI_TFCF_EXP: case RAVI_TFCF_D_D: { if (nargs == 1) { TValue *arg1 = func + 1; @@ -529,8 +532,16 @@ int luaD_precall (lua_State *L, StkId func, int nresults, int op_call) { else nargs = 0; if (nargs) { - double (*f)(double) = fcfvalue(func); - double v = f(arg); + double v; + switch (sig) { + case RAVI_TFCF_EXP: v = exp(arg); break; + case RAVI_TFCF_LOG: v = log(arg); break; + default: { + double(*f)(double) = fcfvalue(func); + double v = f(arg); + break; + } + } setfltvalue(func, v); L->top = func + 1; /* top points after the last result */ return 1; diff --git a/src/ravijit.cpp b/src/ravijit.cpp index 93b3e3d..6fbbc99 100644 --- a/src/ravijit.cpp +++ b/src/ravijit.cpp @@ -272,9 +272,9 @@ static const luaL_Reg ravilib[] = {{"iscompiled", ravi_is_compiled}, LUAMOD_API int raviopen_llvmjit(lua_State *L) { luaL_newlib(L, ravilib); /* faster calls some maths functions */ - ravi_pushcfastcall(L, (void *)exp, RAVI_TFCF_D_D); + ravi_pushcfastcall(L, NULL, RAVI_TFCF_EXP); lua_setfield(L, -2, "exp"); - ravi_pushcfastcall(L, (void *)log, RAVI_TFCF_D_D); + ravi_pushcfastcall(L, NULL, RAVI_TFCF_LOG); lua_setfield(L, -2, "ln"); return 1; }