Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SwadicalRag opened this issue Jun 15, 2019 · 2 comments
Labels
optimisation Implement this to make things run faster

Comments

@SwadicalRag
Copy link
Owner

SwadicalRag commented Jun 15, 2019

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 SwadicalRag added the optimisation Implement this to make things run faster label Jun 15, 2019
@SwadicalRag SwadicalRag changed the title Override wasm-implementations of libc malloc/memset with lua-implementations Override wasm-implementations of frequently used libc functions with lua-implementations Jun 15, 2019
@SwadicalRag
Copy link
Owner Author

SwadicalRag commented Jun 15, 2019

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
Copy link
Owner Author

    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 SwadicalRag changed the title Override wasm-implementations of frequently used libc functions with lua-implementations Override and inline wasm-implementations of frequently used libc functions with lua-implementations Jun 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
optimisation Implement this to make things run faster
Projects
None yet
Development

No branches or pull requests

1 participant