iPXE
x86_string.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 /** @file
00025  *
00026  * Optimised string operations
00027  *
00028  */
00029 
00030 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00031 
00032 #include <string.h>
00033 
00034 /**
00035  * Copy memory area
00036  *
00037  * @v dest              Destination address
00038  * @v src               Source address
00039  * @v len               Length
00040  * @ret dest            Destination address
00041  */
00042 void * __attribute__ (( noinline )) __memcpy ( void *dest, const void *src,
00043                                                size_t len ) {
00044         void *edi = dest;
00045         const void *esi = src;
00046         int discard_ecx;
00047 
00048         /* We often do large dword-aligned and dword-length block
00049          * moves.  Using movsl rather than movsb speeds these up by
00050          * around 32%.
00051          */
00052         __asm__ __volatile__ ( "rep movsl"
00053                                : "=&D" ( edi ), "=&S" ( esi ),
00054                                  "=&c" ( discard_ecx )
00055                                : "0" ( edi ), "1" ( esi ), "2" ( len >> 2 )
00056                                : "memory" );
00057         __asm__ __volatile__ ( "rep movsb"
00058                                : "=&D" ( edi ), "=&S" ( esi ),
00059                                  "=&c" ( discard_ecx )
00060                                : "0" ( edi ), "1" ( esi ), "2" ( len & 3 )
00061                                : "memory" );
00062         return dest;
00063 }
00064 
00065 /**
00066  * Copy memory area backwards
00067  *
00068  * @v dest              Destination address
00069  * @v src               Source address
00070  * @v len               Length
00071  * @ret dest            Destination address
00072  */
00073 void * __attribute__ (( noinline )) __memcpy_reverse ( void *dest,
00074                                                        const void *src,
00075                                                        size_t len ) {
00076         void *edi = ( dest + len - 1 );
00077         const void *esi = ( src + len - 1 );
00078         int discard_ecx;
00079 
00080         /* Assume memmove() is not performance-critical, and perform a
00081          * bytewise copy for simplicity.
00082          */
00083         __asm__ __volatile__ ( "std\n\t"
00084                                "rep movsb\n\t"
00085                                "cld\n\t"
00086                                : "=&D" ( edi ), "=&S" ( esi ),
00087                                  "=&c" ( discard_ecx )
00088                                : "0" ( edi ), "1" ( esi ),
00089                                  "2" ( len )
00090                                : "memory" );
00091         return dest;
00092 }
00093 
00094 
00095 /**
00096  * Copy (possibly overlapping) memory area
00097  *
00098  * @v dest              Destination address
00099  * @v src               Source address
00100  * @v len               Length
00101  * @ret dest            Destination address
00102  */
00103 void * __memmove ( void *dest, const void *src, size_t len ) {
00104 
00105         if ( dest <= src ) {
00106                 return __memcpy ( dest, src, len );
00107         } else {
00108                 return __memcpy_reverse ( dest, src, len );
00109         }
00110 }