luapower / dynasm

DynASM with Lua mode
http://luapower.com/dynasm
103 stars 10 forks source link

Use bitops instead of div/mod #1

Closed thotypous closed 10 years ago

thotypous commented 10 years ago

Tested with the following synthetic benchmark ran with luajit -jdump. The new code produces a inner loop with less instructions and without the calls to lj_carith_modu64 and lj_carith_divu64 which are produced by current code.

local switch_this = true
local ffi=require'ffi'
ffi.cdef[[int printf(const char *format, ...);]]
local a, b
for i=1,100 do
    if switch_this then
        a = ffi.cast("uint32_t", ffi.cast("uintptr_t", ffi.C.printf) + i)
        b = bit.rshift(ffi.cast("uintptr_t", ffi.C.printf) + i, 32)
    else
        a = (ffi.cast("uintptr_t", ffi.C.printf) + i) % 2^32
        b = (ffi.cast("uintptr_t", ffi.C.printf) + i) / 2^32
    end
end

The patch appears to work. Tested with dynasm_demo.lua and with bf.lua.

capr commented 10 years ago

Ok, I will use bit ops for 32bit, I still have to use div/mod for 64bit -- until bitops will work with 64bit too.

thotypous commented 10 years ago

Ah sorry, I'm using luajit 2.1-alpha. Forgot that it doesn't work with 2.0.