2007-01-18 16:18:02 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2012-07-20 20:55:45 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301, USA.
|
2007-01-18 16:18:02 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* Optimised string operations
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-05-01 16:41:06 +02:00
|
|
|
FILE_LICENCE ( GPL2_OR_LATER );
|
|
|
|
|
2007-01-18 16:18:02 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy memory area
|
|
|
|
*
|
|
|
|
* @v dest Destination address
|
|
|
|
* @v src Source address
|
|
|
|
* @v len Length
|
|
|
|
* @ret dest Destination address
|
|
|
|
*/
|
2012-11-04 23:43:59 +01:00
|
|
|
void * __attribute__ (( noinline )) __memcpy ( void *dest, const void *src,
|
|
|
|
size_t len ) {
|
2007-01-18 16:18:02 +01:00
|
|
|
void *edi = dest;
|
|
|
|
const void *esi = src;
|
|
|
|
int discard_ecx;
|
|
|
|
|
|
|
|
/* We often do large dword-aligned and dword-length block
|
|
|
|
* moves. Using movsl rather than movsb speeds these up by
|
|
|
|
* around 32%.
|
|
|
|
*/
|
2012-06-26 13:42:24 +02:00
|
|
|
__asm__ __volatile__ ( "rep movsl"
|
|
|
|
: "=&D" ( edi ), "=&S" ( esi ),
|
|
|
|
"=&c" ( discard_ecx )
|
|
|
|
: "0" ( edi ), "1" ( esi ), "2" ( len >> 2 )
|
|
|
|
: "memory" );
|
|
|
|
__asm__ __volatile__ ( "rep movsb"
|
|
|
|
: "=&D" ( edi ), "=&S" ( esi ),
|
|
|
|
"=&c" ( discard_ecx )
|
|
|
|
: "0" ( edi ), "1" ( esi ), "2" ( len & 3 )
|
|
|
|
: "memory" );
|
2007-01-18 16:18:02 +01:00
|
|
|
return dest;
|
|
|
|
}
|
2012-11-04 23:43:59 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy memory area backwards
|
|
|
|
*
|
|
|
|
* @v dest Destination address
|
|
|
|
* @v src Source address
|
|
|
|
* @v len Length
|
|
|
|
* @ret dest Destination address
|
|
|
|
*/
|
|
|
|
void * __attribute__ (( noinline )) __memcpy_reverse ( void *dest,
|
|
|
|
const void *src,
|
|
|
|
size_t len ) {
|
|
|
|
void *edi = ( dest + len - 1 );
|
|
|
|
const void *esi = ( src + len - 1 );
|
|
|
|
int discard_ecx;
|
|
|
|
|
|
|
|
/* Assume memmove() is not performance-critical, and perform a
|
|
|
|
* bytewise copy for simplicity.
|
|
|
|
*/
|
|
|
|
__asm__ __volatile__ ( "std\n\t"
|
|
|
|
"rep movsb\n\t"
|
|
|
|
"cld\n\t"
|
|
|
|
: "=&D" ( edi ), "=&S" ( esi ),
|
|
|
|
"=&c" ( discard_ecx )
|
|
|
|
: "0" ( edi ), "1" ( esi ),
|
|
|
|
"2" ( len )
|
|
|
|
: "memory" );
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy (possibly overlapping) memory area
|
|
|
|
*
|
|
|
|
* @v dest Destination address
|
|
|
|
* @v src Source address
|
|
|
|
* @v len Length
|
|
|
|
* @ret dest Destination address
|
|
|
|
*/
|
|
|
|
void * __memmove ( void *dest, const void *src, size_t len ) {
|
|
|
|
|
|
|
|
if ( dest <= src ) {
|
|
|
|
return __memcpy ( dest, src, len );
|
|
|
|
} else {
|
|
|
|
return __memcpy_reverse ( dest, src, len );
|
|
|
|
}
|
|
|
|
}
|