• Rise from your gwave!

fast memset

Discussion in 'Saturn Dev' started by vbt, Jul 29, 2012.

  1. vbt

    vbt Staff Member

    I'm still playing to find speed on emulators and i find memset slow, anybody tried to write a fast memset, i only found this :

    _memclrwh: ! terulet torlo: memclrwh(byte* buf, long width, long height, long step)

    ! step = map width - brush width

    xor r2,r2


    mov r5,r1


    mov.b r2,@r4

    add #1,r4

    dt r1

    bf lswh_2

    add r7,r4

    dt r6

    bf lswh_1



    i'd like to replace such thing :

    memset(SpriteOnScreenMap, 255, 0x10000);
  2. mrkotfw

    mrkotfw Member

    I'm not sire if that's as fast as you think it is. It writes in units of 1 byte and doesn't even look to eliminate some trivial pipeline stalls. Newlib's implementation might be faster.
  3. antime

    antime Extra Hard Mid Boss

    If possible, use DMA. If you have to use CPU transfers, try the existing library code first. Smart people have spent a lot of time optimizing it. Other than that, write in larger chunks, to reduce the amount of time spent waiting on the (S)DRAM. Use all the normal optimizing tricks, like unrolling your loops and taking advantage of the instruction set where possible. Also test on real hardware if you can, emulators rarely take things like memory wait states or pipeline stalls into account.

    A naive attempt at a 2-way unrolled version (still writing only a single byte at a time):

        .align  4
        mov     #0, r1
        cmp/eq  r1, r5
        bt      leave
        cmp/eq  r1, r6
        bt      leave
        mov     r5, r0
        mov     r4, r3
        add     #1, r3
        add     r5, r4
        tst     #1, r0
        bf/s    odd
        mov     r4, r2
        mov.b   r1, @-r2
        cmp/hi  r3, r2
        bt/s    inner
        mov.b   r1, @-r2
        add     r7, r3
        dt      r6
        bf/s    outer
        add     r7, r4

Share This Page