不是在普通的 Lua 中。您当然可以通过编写来优化求幂和 if 语句:
local MAXINT, SUBT = math.pow(2, 31), math.pow(2, 32)
function convert(n)
-- Like C's ternary operator
return (n >= MAXINT and n - SUBT) or n
end
我不知道优化 if 语句是否会对解释器有很大帮助;我认为不适合 LuaJIT;但可能是普通的 Lua?
如果您真的想避免比较,请选择 C,例如(未经测试的代码!):
int convert(lua_State *L)
{
lua_pushinteger(L, (int) ((unsigned int) luaL_checklong(L, 1)));
return 1;
}
但是,堆栈开销可能会破坏目的。
微优化有什么具体原因吗?
编辑:我一直在考虑这个,在普通 Lua 中实际上是可能的:
local DIV, SUBT = math.pow(2, 31) + 1, math.pow(2, 32)
-- n MUST be an integer!
function convert(n)
-- the math.floor() evaluates to 0 for integers 0 through 2^31;
-- else it is 1 and SUBT is subtracted.
return n - (math.floor(n / DIV) * SUBT)
end
我不确定它是否会提高性能;除法必须比条件跳转快。
然而,从技术上讲,这回答了问题并避免了比较。