SwadicalRag / wasm2lua

wasm2lua: converting WASM into Lua
MIT License
194 stars 11 forks source link

Override and inline wasm-implementations of frequently used libc functions with lua-implementations #27

Open SwadicalRag opened 5 years ago

SwadicalRag commented 5 years ago

i.e. we roll our own optimised code that plays nice with our custom memory design e.g. (naiive memcpy example that's optimised for luajit)

    function __FUNCS__.memcpy(dest, src, len)
        local fastLen = bit.band(len,-4) -- bit.bnot(3)
        for i=0,fastLen-1,4 do
            __MEMORY_WRITE_32__(mem_0,dest+i,__MEMORY_READ_32__(mem_0,src+i))
        end

        for i=(len-fastLen),len-1,1 do
            __MEMORY_WRITE_8__(mem_0,dest+i,__MEMORY_READ_8__(mem_0,src+i))
        end

        return dest
    end

Before: image After: image

This can be optimised further in the pure-lua memory version by allowing memset to copy over fpMap too (so that memory type hints are preserved, and speed is therefore preserved without having to convert between floats and ints)

SwadicalRag commented 5 years ago

These ones should be inlined to assist the LuaJIT tracer


__FUNCS__.sinf = math.sin
__FUNCS__.asinf = math.asin
__FUNCS__.cosf = math.cos
__FUNCS__.acosf = math.acos
__FUNCS__.atanf = math.atan
__FUNCS__.atan2f = math.atan2
__FUNCS__.sqrtf = math.sqrt
__FUNCS__.powf = math.pow
__FUNCS__.fmodf = math.fmod
__FUNCS__.floor = math.floor
__FUNCS__.fabsf = math.abs
__FUNCS__.frexp = function(x,pExp) local mul,exp = math_frexp(x) __MEMORY_WRITE_32__(module.memory,pExp,exp) return mul end
SwadicalRag commented 5 years ago

    function __FUNCS__.memset(dest, byte, len)
        local fastLen = bit.band(len,-4) -- bit.bnot(3)
        local fwByte = bit.bor(
            byte,
            bit.lshift(byte,4),
            bit.lshift(byte,8),
            bit.lshift(byte,12)
        )
        for i=0,fastLen-1,4 do
            __MEMORY_WRITE_32__(mem_0,dest+i,fwByte)
        end

        for i=(len-fastLen),len-1,1 do
            __MEMORY_WRITE_8__(mem_0,dest+i,byte)
        end

        return dest
    end