PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

2,5x faster memcpy routine for MIPS devices

Here is a fast memcpy routine that is quite similar to what I use in Pocket Divx Decoder for
PocketMVP player (althought PDD uses 64bits mode).

Any developer who is still working with MIPS devices or wants to improve the performance of his app for them can use it for free.

The benefits of this code are great so long you take into consideration the following:

- It works about 2.5x faster if you´re copying a large buffer. However, while it will work on tiny buffers, it´ll be a lot slower than standard WCE memcpy on any movement of less than 32 bytes(with more than 32 will be much faster too). So it´s more suited for games and multimedia rather than 'boring' stuff.

- It won´t work with an uncached buffer. For the standard developer that means you can´t use this to drawn into the GAPI DRAM buffer** of the cassiopeia or directly into its framebuffer. Any buffer allocated with malloc or Virtualalloc (unless you have set the NOCACHE flag in the last function) will work flawlessly. Anyways I´d never use memcpy to copy to those kind of buffers.

** Unless you´re using the cached memory area of that GAPI buffer, that is, 0x8000D000. Due to conflicts with the Casio´s DMA controller, I wouldn´t do that. GAPI always return the non-cached memory address: 0xA000D000

Code:

#ifdef MIPS
#define MEMCPY MIPS_memcpy
#else
#define MEMCPY memcpy
#endif

static void MIPS_memcpy(void *dst, const void *src, int size)
{
__asm(
"addi $8,$6,-32;" // Size-64
"bltz $8,bucle3;" // If it´s a small buffer, it´s not worth using cache ops.

"andi $9,$4,0xF;"
"beq $9,$0,bucle2;"

"bucle1:" // Loop until Dst buffer is 16-byte aligned
"lb $8,0($5);"
"sb $8,0($4);"
"addi $4,$4,1;"
"addi $5,$5,1;"
"addi $6,$6,-1;"
"andi $8,$4,0xF;"
"bne $8,$0,bucle1;"

"bucle2:" // Loop while nbytes to copy > 16
"ulw $8,0($5);"
"ulw $9,4($5);"
"ulw $10,8($5);"
"ulw $11,12($5);"
".set noreorder;"
"cache 13,0($4);" // Create a new cache block
".set reorder;"
"sw $8,0($4);"
"sw $9,4($4);"
"sw $10,8($4);"
"sw $11,12($4);"
"addi $4,$4,16;"
"addi $5,$5,16;"
"addi $6,$6,-16;"
"addi $8,$6,-16;"
"bgtz $8,bucle2;"

"bucle3:" // Loop until all the bytes have been copied
"lb $8,0($5);"
"sb $8,0($4);"
"addi $4,$4,1;"
"addi $5,$5,1;"
"addi $6,$6,-1;"
"bgtz $6,bucle3;",dst,src,size);
}


I hope it helps.

Pedro Mateu - Pocket Divx Decoder developer[/code]
angedelamort · 06 Jan 2003, 17:55 UTC
Really interesting. Thanks for sharing this code with us :)
Great!
Do you have something similar for ARM/StrongARM/XScale? That would be really cool.
Deje wrote:
Great!
Do you have something similar for ARM/StrongARM/XScale? That would be really cool.


Yes, althought it´s not mine, but from Paul Meyer; that´s why I wrote this post:

http://forums.pocketmatrix.com/viewtopic.php?t=9228

Paul´s code works way faster than C in xScale, but it makes no difference in SA1110. It´s not really just a memcpy routine but many 'motion compensation' functions wrotten in ARM ASM. Anyone with some knowledge of ARM ASM should be able to make a memcpy with these functions. All his code can be found in Pocket Divx Decoder code, which can be downloaded here:www.pocketmvp.com (Pocket Divx Decoder 0.62 source code is included inside PocketMVP player source).

PS: I´ve finished a version of that MIPS ASM that also works faster if size<32bytes (however, in that case it´ll work just a bit faster). If anyone is going to use this in his project just post a message either here or in projectmayo forum.
Moreover both this and the newer code seems to work with uncached buffers.

Pedro Mateu - Pocket Divx Decoder developer
I may use it in countersheep if it ever ends up on MIPS devices, infact this could be the code that allows it to run on MIPS devices ;-)

-B
I would be interested to see benchmarks comparing these routines to the standard C-runtime memcpy. Do you have such results that you could post here? Thanks.

BTW, I would hope the C compiler would generate inline code (ie. an intrinsic) for a memcpy operation of less than 32 bytes.
Thank you. I will have a look at this.

Digby, yes. I tried this too (intrinsic memcpy < 32bytes) with no luck. :(
Sergey Chaban · 07 Jan 2003, 08:01 UTC
I don't know about MIPS, but ARM & SH compilers indeed inline the function if count param for memcpy is constant and small enough (and intrinsic option is enabled).
"Small enough" is like this:
For ARM: count <= 8
For SH: count < 16
But generated code copies data byte by byte (ldrb/strb or mov.b), i.e. compiler makes no assumptions about src/dest alignment.

SC
another trick is to hardcode the methods for copying and clearing your graphicsbuffer (ie. fixed size of 320x240) - i've done this for my former 3d-engine which was optimized for arm-devices, here comes the code:
Code:
AREA BUF, CODE, READONLY
EXPORT arm_clear
EXPORT arm_copy

;extern "C" inline void arm_clear(unsigned __int16 *dst, unsigned __int16 c)
arm_clear

stmfd sp!, {r4 - r11, lr}

add r1, r1, r1, lsl #16
mov r2, r1
mov r3, r1
mov r4, r1
mov r5, r1
mov r6, r1
mov r7, r1
mov r8, r1
mov r9, r1
mov r10, r1
mov r11, r1
mov r12, r1

mov r14, #320

arm_clear_inner

stmia r0!, {r1 - r12}
stmia r0!, {r1 - r12}
stmia r0!, {r1 - r12}
stmia r0!, {r1 - r12}
stmia r0!, {r1 - r12}
stmia r0!, {r1 - r12}
stmia r0!, {r1 - r12}
stmia r0!, {r1 - r12}
stmia r0!, {r1 - r12}
stmia r0!, {r1 - r12}

subs r14, r14, #1
bne arm_clear_inner

ldmfd sp!, {r4 - r11, pc}

;extern "C" inline void arm_copy(unsigned __int16 *dst, unsigned __int16 *src)
arm_copy

stmfd sp!, {r4 - r11, lr}

mov r14, #320

arm_copy_inner

ldmia r1!, {r2 - r12}
stmia r0!, {r2 - r12}
ldmia r1!, {r2 - r12}
stmia r0!, {r2 - r12}
ldmia r1!, {r2 - r12}
stmia r0!, {r2 - r12}
ldmia r1!, {r2 - r12}
stmia r0!, {r2 - r12}
ldmia r1!, {r2 - r12}
stmia r0!, {r2 - r12}
ldmia r1!, {r2 - r12}
stmia r0!, {r2 - r12}
ldmia r1!, {r2 - r12}
stmia r0!, {r2 - r12}
ldmia r1!, {r2 - r12}
stmia r0!, {r2 - r12}
ldmia r1!, {r2 - r12}
stmia r0!, {r2 - r12}
ldmia r1!, {r2 - r12}
stmia r0!, {r2 - r12}
ldmia r1!, {r2 - r11}
stmia r0!, {r2 - r11}

subs r14, r14, #1
bne arm_copy_inner

ldmfd sp!, {r4 - r11, pc}

END

but now i own a pocket loox (pxa250 - xscale) and although these routines are still a little bit faster than memcpy/clear, i found something better from one of the intel pdf's (works on arm4/sa1100 processors too but doesn't increment speed so you'll have to compile one ARM4 and one PXA version), anyway, here we go:
Code:
AREA memcpy_PXA250_v4, CODE, READONLY
EXPORT memcpy__PXA250

;// memcpy_PXA250_v4
;// This is an optimized memcopy developed to be compatible with an ARM4 processor
;// and ARM4 compiler
;// PLD is added to using DCD constants. PLD behaves as a NOP on an ARM4 processor
;// int memcpy__PXA250(unsigned char *d, unsigned char *s, size_t c);
;// R0 = d, R1 = s, R2 = count
;// Returns original d address in R0.

memcpy__PXA250
;//Save registers onto the stack
;//R3 should be OK to destroy. If not, we stack it off too.
stmfd sp!, {r0,r3-r12, lr}
prefetch_setup
;//Prefetch the source.
;//Have to align source register with word boundary first
mov r5, r1
and r5, r5, #~0x3
;//The PLD instruction just happens to be a Never Execute on ARM V4,
;//so we can in-line the PLD instruction and still maintain V4 compatibility
;// 0x0000000c: f5d5f000 .... PLD [r5,#0]
;// 0x00000010: f5d5f020 ... PLD [r5,#0x20]
;// 0x00000014: f5d5f040 @... PLD [r5,#0x40]
DCD 0xf5d5f000
DCD 0xf5d5f020
DCD 0xf5d5f040
;//If there are 4 or less bytes to copy, we just jump to the end
;//and do a straight byte copy.
cmp r2, #4
bls finish
;//Align the destination to a word boundary.
rsb r4, r0, #0 ;//Figure out how many bytes
ands r4, r4, #0x2 ;//See if we need to do 2 copies
ldrneb r5, [r1], #1 ;//Read the two bytes
ldrneb r6, [r1], #1
subne r2, r2, #2 ;//Decrement count by 2
strneb r5, [r0], #1 ;//Now store the two bytes
strneb r6, [r0], #1 ;//Have to do two separate byte stores
;//because of possible address misalignment
ands r4, r0, #0x1 ;//See if we need to do 1 copy
ldrneb r5, [r1], #1 ;//Load the single byte
subne r2, r2, #1 ;//Decrement count by 1
strneb r5, [r0], #1 ;//Store the single byte
;//We need to choose which memcpy we use based
;//on how the source is now aligned. If the destination and source
;//are both aligned, then we fall through to the aligned copy
;//Check the byte alignment of the source
;//We do it in reverse order just because. If most memcopies are
;//expected to be off by a certain #, that should be placed first.
and r3, r1, #3
cmp r3, #3 ;//If both bits are set, go do case 3, off by 3 bytes
beq memcpyoffby3 ;//Goto case 3
cmp r3, #2 ;//Check for case 2, off by 2 bytes
beq memcpyoffby2 ;//Goto case 2
cmp r3, #1 ;//Check for case 1, off by 1 byte
beq memcpyoffby1 ;//Goto case 1
;//The source and destination are word aligned. We get an easy job.
memcpyoffby0
;//Now we need to align the destination to a cache line boundary
;//We need to figure out how many words are needed to align it.
;//If the number of words to align it are less than the number of words
;//we’re asked to copy, just copy the required number of words.
and r4, r0, #0x1C ;//Grab the low bits of the destination
rsb r4, r4, #32 ;//Negate them and
;//add 32 to the low bits (this is
;//how many we need to move to get aligned)
and r5, r2, #0x1C ;//Check only the number of words from count
cmp r4, r2 ;//Compare low bits to align against the words from count
movhi r4, r5 ;//If words to align is greater than the count, then
;//use the words from count instead
cmp r4, #0
beq offby0mainloop
;//r4 now contains the number of times we need to do a word load/store
;//So we need to sortof back-calculate how many of the word load/stores to
;//skip in memcpyoffby0cachelinealignload/store
rsb r3, r4, #32
and r3, r3, #0x1C
;//r3 now contains the number of *instructions* to skip over.
;//Deduct words from size
sub r2, r2, r4
;//Because the & 0x1C corresponds to words, we don’t have to shift anything
;//when we jump into load table
;//Qsing two jump tables is faster because it gives the processor a chance to load
;//data before we try to store it out.
adr r12, offby0cachelinealignload
add pc, r12, r3
offby0cachelinealignload ;//Need to have up to 8 words (1 cache line)
ldr r4, [r1], #4 ;//Could also do load/store pairs, and shift
ldr r5, [r1], #4 ;//r3 left 1 bit to calculate jump address
ldr r6, [r1], #4
ldr r7, [r1], #4
ldr r8, [r1], #4
ldr r9, [r1], #4
ldr r10,[r1], #4
ldr r11,[r1], #4
;//Now jump into the store table
adr r12, offby0cachelinealignstore
add pc, r12, r3
offby0cachelinealignstore
str r4, [r0], #4
str r5, [r0], #4
str r6, [r0], #4
str r7, [r0], #4
str r8, [r0], #4
str r9, [r0], #4
str r10,[r0], #4
str r11,[r0], #4
;//We are now cache line aligned.
;//We loop around doing prefetches and copies based on how far ahead we want to look
offby0mainloop
cmp r2, #(32*3 + 32) ;//Only keep looking ahead by 4 cache lines
bmi offby0endofmainloop
;//Preload the data
;// 0x000000f4: f5d1f060 ‘... PLD [r1,#0x60]
;// 0x000000f8: f5d1f080 .... PLD [r1,#0x80]
DCD 0xf5d1f060
DCD 0xf5d1f080
;//Here is the main loop that handles pipelining the loads
ldmia r1!, {r4-r11}
stmia r0!, {r4-r11}
ldmia r1!, {r4-r11}
stmia r0!, {r4-r11}
sub r2, r2, #64 ;//Take 64 bytes off of count
b offby0mainloop
offby0endofmainloop
;//If we still have more than 32*4 words to move, do one more preload
cmp r2, #32*4
bls offby0nopreload
;// 0x0000011c: f5d1f080 .... PLD [r1,#0x80]
DCD 0xf5d1f080
offby0nopreload
;//Now we finish up the copy without any preloads. The data should have already
;//been loaded into the caches
;//Copy 32 bytes at a time
offby0finishcachelines
cmp r2, #32
bmi offby0endoffinishcachelines
ldmia r1!, {r4-r11}
stmia r0!, {r4-r11}
sub r2, r2, #32 ;//Take 32 bytes off of count
b offby0finishcachelines
offby0endoffinishcachelines
;//Now we need to finish off any partial cache lines that may be left. We do a similar
;//algorithm to the cachelinealign loop above.
ands r3, r2, #0x1C ;//Get number of words left
beq finish ;//If words left==0, then branch to finish
sub r2, r2, r3 ;//Subtract words left from count
rsb r3, r3, #32 ;//Get 32-number of words left
adr r12, offby0finishload ;//That’s the instructions to skip
add pc, r12, r3
offby0finishload ;//Need to have up to 8 words (1 cache line)
ldr r4, [r1], #4 ;//Could also do load/store pairs, and shift
ldr r5, [r1], #4 ;//r3 left 1 bit to calculate jump address
ldr r6, [r1], #4
ldr r7, [r1], #4
ldr r8, [r1], #4
ldr r9, [r1], #4
ldr r10,[r1], #4
ldr r11,[r1], #4
;//Now jump into the store table
adr r12, offby0finishstore
add pc, r12, r3
offby0finishstore
str r4, [r0], #4
str r5, [r0], #4
str r6, [r0], #4
str r7, [r0], #4
str r8, [r0], #4
str r9, [r0], #4
str r10,[r0], #4
str r11,[r0], #4
;//Copy the last 4 bytes, if necessary
rsb r2, r2, #4 ;//Find how many bytes to copy (0, 1,2,3, or 4)
adr r12, finishloadby0
add pc, r12, r2, LSL #2 ;//Need to shift r2 left by 2 bits to jump instructions
finishloadby0
ldrb r3, [r1], #1
ldrb r4, [r1], #1
ldrb r5, [r1], #1
ldrb r6, [r1], #1
adr r12, finishstoreby0
add pc, r12, r2, LSL #2
finishstoreby0
strb r3, [r0], #1
strb r4, [r0], #1
strb r5, [r0], #1
strb r6, [r0], #1
;//Return to calling function
ldmfd sp!, {r0,r3-r12, pc}
;//The source and destination are not aligned. We’re going to have
;//to load and shift data from a temporary buffer. Stuff needs to be
;//shifted to the right by 8 bits to align properly
memcpyoffby1
;//First we need to word align the source
and r3, r1, #~0x3
;//Load the first value into the holding buffer (lr)
ldr lr, [r3], #4
mov lr, lr, LSR #8
;//Now we need to align the destination to a cache line boundary
;//We need to figure out how many words are needed to align it.
;//If the number of words to align it are less than the number of words
;//we’re asked to copy, just copy the required number of words.
and r4, r0, #0x1C ;//Grab the low bits of the destination
rsb r4, r4, #32 ;//Negate them
;//Add 32 to the low bits (this is
;//how many we need to move to get aligned)
and r5, r2, #0x1C ;//Check only the number of words from count
cmp r4, r2 ;//Compare low bits to align against the words from count
movhi r4, r5 ;//If words to align is greater than the count, then
;//use the words from count instead
cmp r4, #0
beq offby1mainloop
;//r4 now contains the number of times we need to do a word load/store
;//So we need to sortof back-calculate how many of the word load/stores to
;//skip in memcpyoffby1cachelinealignload
rsb r6, r4, #32
and r6, r6, #0x1C
;//r3 now contains the number of *words* to skip over.
;//Deduct words from size
sub r2, r2, r4
;//Because the & 0x1C corresponds to words, we DO need to shift this time around
;//when we jump into load table
adr r12, offby1cachelinealignload
add pc, r12, r6, LSL #2 ;//Allows 4 instructions per byteblit
;//Because there is no convenient way to split the load/store into multiples of 2
;//unless we keep them together, for misaligned data we leave them together.
offby1cachelinealignload ;//Need to have up to 8 words (1 cache line)
ldr r4, [r3], #4
orr r12,lr, r4, LSL #24
str r12,[r0], #4
mov lr, r4, LSR #8
ldr r4, [r3], #4
orr r12,lr, r4, LSL #24
str r12,[r0], #4
mov lr, r4, LSR #8
ldr r4, [r3], #4
orr r12,lr, r4, LSL #24
str r12,[r0], #4
mov lr, r4, LSR #8
ldr r4, [r3], #4
orr r12,lr, r4, LSL #24
str r12,[r0], #4
mov lr, r4, LSR #8
ldr r4, [r3], #4
orr r12,lr, r4, LSL #24
str r12,[r0], #4
mov lr, r4, LSR #8
ldr r4, [r3], #4
orr r12,lr, r4, LSL #24
str r12,[r0], #4
mov lr, r4, LSR #8
ldr r4, [r3], #4
orr r12,lr, r4, LSL #24
str r12,[r0], #4
mov lr, r4, LSR #8
ldr r4, [r3], #4
orr r12,lr, r4, LSL #24
str r12,[r0], #4
mov lr, r4, LSR #8
;//We are now cache line aligned.
;//We loop around doing prefetches and copies based on how far ahead we want to look
offby1mainloop
cmp r2, #(32*4 + 32);//Only keep looking ahead by 4 cache lines
bmi offby1endofmainloop
;//Preload
;// 0x00000264: f5d3f060 ‘... PLD [r3,#0x60]
;// 0x00000268: f5d3f080 .... PLD [r3,#0x80]
DCD 0xf5d3f060
DCD 0xf5d3f080
;//Here is the main loop that handles pipelining the loads for off by 1
ldmia r3!, {r4, r5, r6, r7, r8, r9, r10, r11}
orr r1,lr, r4, LSL #24
mov lr, r4, LSR #8
orr r4, lr, r5, LSL #24
mov lr, r5, LSR #8
orr r5, lr, r6, LSL #24
mov lr, r6, LSR #8
orr r6, lr, r7, LSL #24
mov lr, r7, LSR #8
orr r7, lr, r8, LSL #24
mov lr, r8, LSR #8
orr r8, lr, r9, LSL #24
mov lr, r9, LSR #8
orr r9, lr, r10, LSL #24
mov lr, r10, LSR #8
orr r10, lr, r11, LSL #24
mov lr, r11, LSR #8
stmia r0!, {r1, r4, r5, r6, r7, r8, r9, r10}
ldmia r3!, {r4, r5, r6, r7, r8, r9, r10, r11}
orr r1,lr, r4, LSL #24
mov lr, r4, LSR #8
orr r4, lr, r5, LSL #24
mov lr, r5, LSR #8
orr r5, lr, r6, LSL #24
mov lr, r6, LSR #8
orr r6, lr, r7, LSL #24
mov lr, r7, LSR #8
orr r7, lr, r8, LSL #24
mov lr, r8, LSR #8
orr r8, lr, r9, LSL #24
mov lr, r9, LSR #8
orr r9, lr, r10, LSL #24
mov lr, r10, LSR #8
orr r10, lr, r11, LSL #24
mov lr, r11, LSR #8
stmia r0!, {r1, r4, r5, r6, r7, r8, r9, r10}
sub r2, r2, #64 ;//Take 64 bytes off of count
b offby1mainloop
offby1endofmainloop
;//If we still have more than 32*4 words to move, do one more preload
cmp r2, #32*4
bls offby1nopreload
;// 0x00000338: f5d3f080 .... PLD [r3,#0x80]
DCD 0xf5d3f080
offby1nopreload
;//Now we finish up the copy without any preloads. The data should have already
;//been loaded into the caches
;//Copy 32 bytes at a time
offby1finishcachelines
cmp r2, #32
bmi offby1endoffinishcachelines
ldmia r3!, {r4, r5, r6, r7, r8, r9, r10, r11}
orr r1,lr, r4, LSL #24
mov lr, r4, LSR #8
orr r4, lr, r5, LSL #24
mov lr, r5, LSR #8
orr r5, lr, r6, LSL #24
mov lr, r6, LSR #8
orr r6, lr, r7, LSL #24
mov lr, r7, LSR #8
orr r7, lr, r8, LSL #24
mov lr, r8, LSR #8
orr r8, lr, r9, LSL #24
mov lr, r9, LSR #8
orr r9, lr, r10, LSL #24
mov lr, r10, LSR #8
orr r10, lr, r11, LSL #24
mov lr, r11, LSR #8
stmia r0!, {r1, r4, r5, r6, r7, r8, r9, r10}
sub r2, r2, #32;//Take 32 bytes off of count
b offby1finishcachelines
offby1endoffinishcachelines
;//Now we need to finish off any partial cache lines that may be left. We do a similar
;//algorithm to the cachelinealign loop above.
ands r6, r2, #0x1C ;//Get number of words left
subeq r1, r3, #3 ;//Realign source on exact byte if need to branch
beq finish ;//If words left==0, then branch to finish
sub r2, r2, r6 ;//Subtract words left from count
rsb r6, r6, #32 ;//Get 32-number of words left
adr r12, offby1finishload;//That’s the copies to skip
add pc, r12, r6, LSL #2 ;//.but need to multiply by 4 to get instructions
offby1finishload ;//Need to have up to 8 words (1 cache line)
ldr r4, [r3], #4
orr r12,lr, r4, LSL #24
str r12,[r0], #4
mov lr, r4, LSR #8
ldr r4, [r3], #4
orr r12,lr, r4, LSL #24
str r12,[r0], #4
mov lr, r4, LSR #8
ldr r4, [r3], #4
orr r12,lr, r4, LSL #24
str r12,[r0], #4
mov lr, r4, LSR #8
ldr r4, [r3], #4
orr r12,lr, r4, LSL #24
str r12,[r0], #4
mov lr, r4, LSR #8
ldr r4, [r3], #4
orr r12,lr, r4, LSL #24
str r12,[r0], #4
mov lr, r4, LSR #8
ldr r4, [r3], #4
orr r12,lr, r4, LSL #24
str r12,[r0], #4
mov lr, r4, LSR #8
ldr r4, [r3], #4
orr r12,lr, r4, LSL #24
str r12,[r0], #4
mov lr, r4, LSR #8
ldr r4, [r3], #4
orr r12,lr, r4, LSL #24
str r12,[r0], #4
mov lr, r4, LSR #8
sub r1, r3, #3 ;//Realign source on exact byte
;//Copy the last 4 bytes, if necessary
rsb r2, r2, #4 ;//Find how many bytes to copy (1,2,3, or 4)
adr r12, finishloadby1
add pc, r12, r2, LSL #2 ;//Need to shift r2 left by 2 bits to jump instructions
finishloadby1
ldrb r3, [r1], #1
ldrb r4, [r1], #1
ldrb r5, [r1], #1
ldrb r6, [r1], #1
adr r12, finishstoreby1
add pc, r12, r2, LSL #2
finishstoreby1
strb r3, [r0], #1
strb r4, [r0], #1
strb r5, [r0], #1
strb r6, [r0], #1
;//Return to calling function
ldmfd sp!, {r0,r3-r12, pc}
;//The source and destination are not aligned. We’re going to have to load
;//and shift data from a temporary buffer. Stuff needs to be shifted to the
;//right by 16 bits to align properly
memcpyoffby2
;//First we need to word align the source
and r3, r1, #~0x3
;//Load the first value into the holding buffer (lr)
ldr lr, [r3], #4
mov lr, lr, LSR #16
;//Now we need to align the destination to a cache line boundary
;//We need to figure out how many words are needed to align it.
;//If the number of words to align it are less than the number of words
;//we’re asked to copy, just copy the required number of words.
and r4, r0, #0x1C ;//Grab the low bits of the destination
rsb r4, r4, #32 ;//Negate them
;//Add 32 to the low bits (this is
;//how many we need to move to get aligned)
and r5, r2, #0x1C ;//Check only the number of words from count
cmp r4, r2 ;//Compare low bits to align against the words from count
movhi r4, r5 ;//If words to align is greater than the count, then
;//use the words from count instead
cmp r4, #0
beq offby2mainloop
;//r4 now contains the number of times we need to do a word load/store
;//So we need to sortof back-calculate how many of the word load/stores to
;//skip in memcpyoffby2cachelinealignload
rsb r6, r4, #32
and r6, r6, #0x1C
;//r3 now contains the number of *words* to skip over.
;//Deduct words from size
sub r2, r2, r4
;//Because the & 0x1C corresponds to words, we DO need to shift this time around
;//when we jump into load table
adr r12, offby2cachelinealignload
add pc, r12, r6, LSL #2 ;//Allows 4 instructions per byteblit
;//Because there is no convenient way to split the load/store into multiples of 2
;//unless we keep them together, for misaligned data we leave them together.
offby2cachelinealignload ;//Need to have up to 8 words (1 cache line)
ldr r4, [r3], #4
orr r12,lr, r4, LSL #16
str r12,[r0], #4
mov lr, r4, LSR #16
ldr r4, [r3], #4
orr r12,lr, r4, LSL #16
str r12,[r0], #4
mov lr, r4, LSR #16
ldr r4, [r3], #4
orr r12,lr, r4, LSL #16
str r12,[r0], #4
mov lr, r4, LSR #16
ldr r4, [r3], #4
orr r12,lr, r4, LSL #16
str r12,[r0], #4
mov lr, r4, LSR #16
ldr r4, [r3], #4
orr r12,lr, r4, LSL #16
str r12,[r0], #4
mov lr, r4, LSR #16
ldr r4, [r3], #4
orr r12,lr, r4, LSL #16
str r12,[r0], #4
mov lr, r4, LSR #16
ldr r4, [r3], #4
orr r12,lr, r4, LSL #16
str r12,[r0], #4
mov lr, r4, LSR #16
ldr r4, [r3], #4
orr r12,lr, r4, LSL #16
str r12,[r0], #4
mov lr, r4, LSR #16
;//So in theory we should now be cache line aligned.
;//We loop around doing prefetches and copies based on how far ahead we want to look
offby2mainloop
cmp r2, #(32*4 + 32) ;//Only keep looking ahead by 4 cache lines
bmi offby2endofmainloop
;//Preload
;// 0x00000514: f5d3f060 ‘... PLD [r3,#0x60]
;// 0x00000518: f5d3f080 .... PLD [r3,#0x80]
DCD 0xf5d3f060
DCD 0xf5d3f080
;//Here is the main loop that handles pipelining the loads for off by 2
ldmia r3!, {r4, r5, r6, r7, r8, r9, r10, r11}
orr r1,lr, r4, LSL #16
mov lr, r4, LSR #16
orr r4, lr, r5, LSL #16
mov lr, r5, LSR #16
orr r5, lr, r6, LSL #16
mov lr, r6, LSR #16
orr r6, lr, r7, LSL #16
mov lr, r7, LSR #16
orr r7, lr, r8, LSL #16
mov lr, r8, LSR #16
orr r8, lr, r9, LSL #16
mov lr, r9, LSR #16
orr r9, lr, r10, LSL #16
mov lr, r10, LSR #16
orr r10, lr, r11, LSL #16
mov lr, r11, LSR #16
stmia r0!, {r1, r4, r5, r6, r7, r8, r9, r10}
ldmia r3!, {r4, r5, r6, r7, r8, r9, r10, r11}
orr r1,lr, r4, LSL #16
mov lr, r4, LSR #16
orr r4, lr, r5, LSL #16
mov lr, r5, LSR #16
orr r5, lr, r6, LSL #16
mov lr, r6, LSR #16
orr r6, lr, r7, LSL #16
mov lr, r7, LSR #16
orr r7, lr, r8, LSL #16
mov lr, r8, LSR #16
orr r8, lr, r9, LSL #16
mov lr, r9, LSR #16
orr r9, lr, r10, LSL #16
mov lr, r10, LSR #16
orr r10, lr, r11, LSL #16
mov lr, r11, LSR #16
stmia r0!, {r1, r4, r5, r6, r7, r8, r9, r10}
sub r2, r2, #64;//Take 64 bytes off of count
b offby2mainloop
offby2endofmainloop
;//If we still have more than 32*4 words to move, do one more preload
cmp r2, #32*4
bls offby2nopreload
;// 0x000005e8: f5d3f080 .... PLD [r3,#0x80]
DCD 0xf5d3f080
offby2nopreload
;//Now we finish up the copy without any preloads. The data should have already
;//been loaded into the caches
;//Copy 32 bytes at a time
offby2finishcachelines
cmp r2, #32
bmi offby2endoffinishcachelines
ldmia r3!, {r4, r5, r6, r7, r8, r9, r10, r11}
orr r1,lr, r4, LSL #16
mov lr, r4, LSR #16
orr r4, lr, r5, LSL #16
mov lr, r5, LSR #16
orr r5, lr, r6, LSL #16
mov lr, r6, LSR #16
orr r6, lr, r7, LSL #16
mov lr, r7, LSR #16
orr r7, lr, r8, LSL #16
mov lr, r8, LSR #16
orr r8, lr, r9, LSL #16
mov lr, r9, LSR #16
orr r9, lr, r10, LSL #16
mov lr, r10, LSR #16
orr r10, lr, r11, LSL #16
mov lr, r11, LSR #16
stmia r0!, {r1, r4, r5, r6, r7, r8, r9, r10}
sub r2, r2, #32;//Take 32 bytes off of count
b offby2finishcachelines
offby2endoffinishcachelines
;//Now we need to finish off any partial cache lines that may be left. We do a similar
;//algorithm to the cachelinealign loop above.
ands r6, r2, #0x1C ;//Get number of words left
subeq r1, r3, #2 ;//Realign source on exact byte if need to branch
beq finish ;//If words left==0, then branch to finish
sub r2, r2, r6 ;//Subtract words left from count
rsb r6, r6, #32 ;//Get 32-number of words left
adr r12, offby2finishload ;//That’s the copies to skip
add pc, r12, r6, LSL #2 ;//..but need to multiply by 4 to get instructions
offby2finishload ;//Need to have up to 8 words (1 cache line)
ldr r4, [r3], #4
orr r12,lr, r4, LSL #16
str r12,[r0], #4
mov lr, r4, LSR #16
ldr r4, [r3], #4
orr r12,lr, r4, LSL #16
str r12,[r0], #4
mov lr, r4, LSR #16
ldr r4, [r3], #4
orr r12,lr, r4, LSL #16
str r12,[r0], #4
mov lr, r4, LSR #16
ldr r4, [r3], #4
orr r12,lr, r4, LSL #16
str r12,[r0], #4
mov lr, r4, LSR #16
ldr r4, [r3], #4
orr r12,lr, r4, LSL #16
str r12,[r0], #4
mov lr, r4, LSR #16
ldr r4, [r3], #4
orr r12,lr, r4, LSL #16
str r12,[r0], #4
mov lr, r4, LSR #16
ldr r4, [r3], #4
orr r12,lr, r4, LSL #16
str r12,[r0], #4
mov lr, r4, LSR #16
ldr r4, [r3], #4
orr r12,lr, r4, LSL #16
str r12,[r0], #4
mov lr, r4, LSR #16
sub r1, r3, #2 ;//Realign source on exact byte
;//Copy the last 4 bytes, if necessary
rsb r2, r2, #4 ;//Find how many bytes to copy (1,2,3, or 4)
adr r12, finishloadby2
add pc, r12, r2, LSL #2;//Need to shift r2 left by 2 bits to jump instructions
finishloadby2
ldrb r3, [r1], #1
ldrb r4, [r1], #1
ldrb r5, [r1], #1
ldrb r6, [r1], #1
adr r12, finishstoreby2
add pc, r12, r2, LSL #2
finishstoreby2
strb r3, [r0], #1
strb r4, [r0], #1
strb r5, [r0], #1
strb r6, [r0], #1
;//Return to calling function
ldmfd sp!, {r0,r3-r12, pc}
;//The source and destination are not aligned. We’re going to have to load
;//and shift data from a temporary buffer. Stuff needs to be shifted to the
;//right by 24 bits to align properly
memcpyoffby3
;//First we need to word align the source
and r3, r1, #~0x3
;//Load the first value into the holding buffer (lr)
ldr lr, [r3], #4
mov lr, lr, LSR #24
;//Now we need to align the destination to a cache line boundary
;//We need to figure out how many words are needed to align it.
;//If the number of words to align it are less than the number of words
;//we’re asked to copy, just copy the required number of words.
and r4, r0, #0x1C ;//Grab the low bits of the destination
rsb r4, r4, #32 ;//Negate them
;//Add 32 to the low bits (this is
;//how many we need to move to get aligned)
and r5, r2, #0x1C ;//Check only the number of words from count
cmp r4, r2 ;//Compare low bits to align against the words from count
movhi r4, r5 ;//If words to align is greater than the count, then
;//use the words from count instead
cmp r4, #0
beq offby3mainloop
;//r4 now contains the number of times we need to do a word load/store
;//So we need to sortof back-calculate how many of the word load/stores to
;//skip in memcpyoffby3cachelinealignload
rsb r6, r4, #32
and r6, r6, #0x1C
;//r3 now contains the number of *words* to skip over.
;//Deduct words from size
sub r2, r2, r4
;//Because the & 0x1C corresponds to words, we DO need to shift this time around
;//when we jump into load table
adr r12, offby3cachelinealignload
add pc, r12, r6, LSL #2 ;//Allows 4 instructions per byteblit
;//Because there is no convenient way to split the load/store into multiples of 2
;//unless we keep them together, for misaligned data we leave them together.
offby3cachelinealignload ;//Need to have up to 8 words (1 cache line)
ldr r4, [r3], #4
orr r12,lr, r4, LSL #8
str r12,[r0], #4
mov lr, r4, LSR #24
ldr r4, [r3], #4
orr r12,lr, r4, LSL #8
str r12,[r0], #4
mov lr, r4, LSR #24
ldr r4, [r3], #4
orr r12,lr, r4, LSL #8
str r12,[r0], #4
mov lr, r4, LSR #24
ldr r4, [r3], #4
orr r12,lr, r4, LSL #8
str r12,[r0], #4
mov lr, r4, LSR #24
ldr r4, [r3], #4
orr r12,lr, r4, LSL #8
str r12,[r0], #4
mov lr, r4, LSR #24
ldr r4, [r3], #4
orr r12,lr, r4, LSL #8
str r12,[r0], #4
mov lr, r4, LSR #24
ldr r4, [r3], #4
orr r12,lr, r4, LSL #8
str r12,[r0], #4
mov lr, r4, LSR #24
ldr r4, [r3], #4
orr r12,lr, r4, LSL #8
str r12,[r0], #4
mov lr, r4, LSR #24
;//So in theory we should now be cache line aligned.
;//We loop around doing prefetches and copies based on how far ahead we want to look
offby3mainloop
cmp r2, #(32*4 + 32) ;//Only keep looking ahead by 4 cache lines
bmi offby3endofmainloop
;//Preload
;// 0x000007c4: f5d3f060 ‘... PLD [r3,#0x60]
;// 0x000007c8: f5d3f080 .... PLD [r3,#0x80]
DCD 0xf5d3f060
DCD 0xf5d3f080
;//Here is the main loop that handles pipelining the loads for off by 1
ldmia r3!, {r4, r5, r6, r7, r8, r9, r10, r11}
orr r1,lr, r4, LSL #8
mov lr, r4, LSR #24
orr r4, lr, r5, LSL #8
mov lr, r5, LSR #24
orr r5, lr, r6, LSL #8
mov lr, r6, LSR #24
orr r6, lr, r7, LSL #8
mov lr, r7, LSR #24
orr r7, lr, r8, LSL #8
mov lr, r8, LSR #24
orr r8, lr, r9, LSL #8
mov lr, r9, LSR #24
orr r9, lr, r10, LSL #8
mov lr, r10, LSR #24
orr r10, lr, r11, LSL #8
mov lr, r11, LSR #24
stmia r0!, {r1, r4, r5, r6, r7, r8, r9, r10}
ldmia r3!, {r4, r5, r6, r7, r8, r9, r10, r11}
orr r1,lr, r4, LSL #8
mov lr, r4, LSR #24
orr r4, lr, r5, LSL #8
mov lr, r5, LSR #24
orr r5, lr, r6, LSL #8
mov lr, r6, LSR #24
orr r6, lr, r7, LSL #8
mov lr, r7, LSR #24
orr r7, lr, r8, LSL #8
mov lr, r8, LSR #24
orr r8, lr, r9, LSL #8
mov lr, r9, LSR #24
orr r9, lr, r10, LSL #8
mov lr, r10, LSR #24
orr r10, lr, r11, LSL #8
mov lr, r11, LSR #24
stmia r0!, {r1, r4, r5, r6, r7, r8, r9, r10}
sub r2, r2, #64 ;//Take 64 bytes off of count
b offby3mainloop
offby3endofmainloop
;//If we still have more than 32*4 words to move, do one more preload
cmp r2, #32*4
bls offby3nopreload
;// 0x00000898: f5d3f080 .... PLD [r3,#0x80]
DCD 0xf5d3f080
offby3nopreload
;//Now we finish up the copy without any preloads. The data should have already
;//been loaded into the caches
;//Copy 32 bytes at a time
offby3finishcachelines
cmp r2, #32
bmi offby3endoffinishcachelines
ldmia r3!, {r4, r5, r6, r7, r8, r9, r10, r11}
orr r1,lr, r4, LSL #8
mov lr, r4, LSR #24
orr r4, lr, r5, LSL #8
mov lr, r5, LSR #24
orr r5, lr, r6, LSL #8
mov lr, r6, LSR #24
orr r6, lr, r7, LSL #8
mov lr, r7, LSR #24
orr r7, lr, r8, LSL #8
mov lr, r8, LSR #24
orr r8, lr, r9, LSL #8
mov lr, r9, LSR #24
orr r9, lr, r10, LSL #8
mov lr, r10, LSR #24
orr r10, lr, r11, LSL #8
mov lr, r11, LSR #24
stmia r0!, {r1, r4, r5, r6, r7, r8, r9, r10}
sub r2, r2, #32 ;//Take 32 bytes off of count
b offby3finishcachelines
offby3endoffinishcachelines
;//Now we need to finish off any partial cache lines that may be left. We do a similar
;//algorithm to the cachelinealign loop above.
ands r6, r2, #0x1C ;//Get number of words left
subeq r1, r3, #1 ;//Realign source on exact byte if need to branch
beq finish ;//If words left==0, then branch to finish
sub r2, r2, r6 ;//Subtract words left from count
rsb r6, r6, #32 ;//Get 32-number of words left
adr r12, offby3finishload ;//That’s the copies to skip
add pc, r12, r6, LSL #2 ;//..but need to multiply by 4 to get instructions
offby3finishload ;//Need to have up to 8 words (1 cache line)
ldr r4, [r3], #4
orr r12,lr, r4, LSL #8
str r12,[r0], #4
mov lr, r4, LSR #24
ldr r4, [r3], #4
orr r12,lr, r4, LSL #8
str r12,[r0], #4
mov lr, r4, LSR #24
ldr r4, [r3], #4
orr r12,lr, r4, LSL #8
str r12,[r0], #4
mov lr, r4, LSR #24
ldr r4, [r3], #4
orr r12,lr, r4, LSL #8
str r12,[r0], #4
mov lr, r4, LSR #24
ldr r4, [r3], #4
orr r12,lr, r4, LSL #8
str r12,[r0], #4
mov lr, r4, LSR #24
ldr r4, [r3], #4
orr r12,lr, r4, LSL #8
str r12,[r0], #4
mov lr, r4, LSR #24
ldr r4, [r3], #4
orr r12,lr, r4, LSL #8
str r12,[r0], #4
mov lr, r4, LSR #24
ldr r4, [r3], #4
orr r12,lr, r4, LSL #8
str r12,[r0], #4
mov lr, r4, LSR #24
sub r1, r3, #1 ;//Realign source on exact byte
;// b finish ;//Not needed, just fall through
;//Copy the last 4 bytes, if necessary
finish ;//This finish also used in < 4 bytes case
rsb r2, r2, #4 ;//Find how many bytes to copy (1,2,3, or 4)
adr r12, finishloadby3
add pc, r12, r2, LSL #2 ;//Need to shift r2 left by 2 bits to jump instructions
finishloadby3
ldrb r3, [r1], #1
ldrb r4, [r1], #1
ldrb r5, [r1], #1
ldrb r6, [r1], #1
adr r12, finishstoreby3
add pc, r12, r2, LSL #2
finishstoreby3
strb r3, [r0], #1
strb r4, [r0], #1
strb r5, [r0], #1
strb r6, [r0], #1
;//Return to calling function
ldmfd sp!, {r0,r3-r12, pc}
END

have phun ;)
Sergey Chaban · 07 Jan 2003, 11:37 UTC
And yet another approach is to generate binary code at runtime.
That is, you have a lot more info when the program is running - actual CPU model, buffer alignment and datasize, so you don't have to check for these things explicitly in the code.
For ARM I wrote a library to emit machine code, it's available from go-mono.com CVS/snapshots or as a part of AMX VM port for CE (see my page). I also have a newer version that supports V5/XS, Thumb & WMMX - but it's not released yet.

SC
Digby wrote:
I would be interested to see benchmarks comparing these routines to the standard C-runtime memcpy. Do you have such results that you could post here? Thanks.


Of course I did some (few) benchmarks before posting. Today I´ve been doing (and this time copying) some further tests:

Buffers:

Src -> 1000000 bytes (cached)
Dst -> 1000000 bytes (cached)

Device:

EM505S @ 180 Mhz

Test1:

Src, Dst alignment -> 1K memory-aligned*

*By default, WCE aligns large buffers to default page size (1K in MIPS, 4K in ARM).

MIPS_memcpy(Dst,Src,1000000); -> 42ms

memcpy(Dst,Src,1000000); -> 109ms

Result -> 2,59x



Test2:

Src, Dst alignment -> 2 byte (halfword)- aligned

for (i=0;i<1000;i++)
MIPS_memcpy(Dst+2,Src+2,100000);-> 4168ms

for (i=0;i<1000;i++)
memcpy(Dst+2,Src+2,100000); -> 10920ms

Result -> 2,61x



Test3:

Src -> not-aligned
Dst -> 16 byte Aligned

for (i=0;i<1000;i++)
MIPS_memcpy(Dst+16,Src+1,100000);-> 6078ms

for (i=0;i<1000;i++)
memcpy(Dst+16,Src+1,100000); -> 6910ms

Result -> 1,13x (yes it´s seems there are problems with some alignments)


Another test for 'Games':

Buffers:

Src -> 320*240 bytes (cached)
Dst -> 320*240 bytes (cached)

Test4:

Operation:

for (i=0;i<10000;i++)
MIPS_memcpy(Dst+1,Src+1,160*100);-> 6391ms

for (i=0;i<10000;i++)
memcpy(Dst+1,Src+1,160*100); -> 9058ms


Result -> 1,47x


I´ve done all the tests with 'GetTickCount()' function.

However, I´ve got some weird results that by now I don´t understand. It seems that if the buffer is small(not the number of bytes to copy(?)), my routines doesn´t perform well; while they perform much better so long the buffer is large... So your question about benchmarks has been very useful Digby.

Hence if you´re going to use this memcpy please check the results or let me do some further testing and finish another version I´ve in mind. It can take a bit as I´ve now some problems with the lastest PDD and PocketMVP 0.8o.


BTW, I would hope the C compiler would generate inline code (ie. an intrinsic) for a memcpy operation of less than 32 bytes.


At least in MIPS the quality of the EVC inlined code is really poor. Larry Banks did extensive tests many time ago and he realized there is no benefit doing so in this type of devices.
Ah the golden days of computing are back :D

* Smiles from ear to ear *

The perfect antidote to all this Java programming ;)
Thanks for posting the results.

Just for fun, how does generic C code like this compare to the performance of memcpy on your MIPS device? Does the MIPS compiler with EVC support /QMmips64?

Code:


typedef double DATATYPE;

int simplecpy (void *pvDest, void *pvSrc, int nBytes)
{
DATATYPE *pSrc, *pDest, *pEnd;

// must be DATATYPE-aligned and evenly multiple xfer!
if ((long)pvDest & (sizeof(DATATYPE) - 1) ||
(long)pvSrc & (sizeof(DATATYPE) - 1) ||
nBytes & (sizeof(DATATYPE) - 1))
return 0;

pSrc = (DATATYPE *)pvSrc;
pDest = (DATATYPE *)pvDest;
pEnd = pDest + (nBytes / sizeof(DATATYPE));

while (pEnd > pDest)
*pSrc++ = *pDest++;

return 1;
}

Is the code posted only compatable with Visual Studio 6+ ?, because Evc++ 3.0 does not like the "__asm" command (not supported in this product) apparently

-B
You aren't trying to do inline assembly for an ARM build are you?

Dan East
nope, just std.....