wasm2lua icon indicating copy to clipboard operation
wasm2lua copied to clipboard

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

Open SwadicalRag opened this issue 6 years ago • 2 comments

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 avatar Jun 15 '19 03:06 SwadicalRag

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 avatar Jun 15 '19 03:06 SwadicalRag


    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

SwadicalRag avatar Jun 15 '19 03:06 SwadicalRag