[libc] Simplify memcpy() implementation

The "rep" prefix can be used with an iteration count of zero, which
allows the variable-length memcpy() to be implemented without using
any conditional jumps.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown
2012-06-26 12:42:24 +01:00
parent 80cdf6acc7
commit 6a4ff519c8

View File

@@ -43,21 +43,15 @@ void * __memcpy ( void *dest, const void *src, size_t len ) {
* moves. Using movsl rather than movsb speeds these up by * moves. Using movsl rather than movsb speeds these up by
* around 32%. * around 32%.
*/ */
if ( len >> 2 ) { __asm__ __volatile__ ( "rep movsl"
__asm__ __volatile__ ( "rep movsl" : "=&D" ( edi ), "=&S" ( esi ),
: "=&D" ( edi ), "=&S" ( esi ), "=&c" ( discard_ecx )
"=&c" ( discard_ecx ) : "0" ( edi ), "1" ( esi ), "2" ( len >> 2 )
: "0" ( edi ), "1" ( esi ), : "memory" );
"2" ( len >> 2 ) __asm__ __volatile__ ( "rep movsb"
: "memory" ); : "=&D" ( edi ), "=&S" ( esi ),
} "=&c" ( discard_ecx )
if ( len & 0x02 ) { : "0" ( edi ), "1" ( esi ), "2" ( len & 3 )
__asm__ __volatile__ ( "movsw" : "=&D" ( edi ), "=&S" ( esi ) : "memory" );
: "0" ( edi ), "1" ( esi ) : "memory" );
}
if ( len & 0x01 ) {
__asm__ __volatile__ ( "movsb" : "=&D" ( edi ), "=&S" ( esi )
: "0" ( edi ), "1" ( esi ) : "memory" );
}
return dest; return dest;
} }