2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
26 * Optimised string operations
30 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
37 * @v dest Destination address
38 * @v src Source address
40 * @ret dest Destination address
42 void * __attribute__ (( noinline )) __memcpy ( void *dest, const void *src,
45 const void *esi = src;
48 /* We often do large dword-aligned and dword-length block
49 * moves. Using movsl rather than movsb speeds these up by
52 __asm__ __volatile__ ( "rep movsl"
53 : "=&D" ( edi ), "=&S" ( esi ),
55 : "0" ( edi ), "1" ( esi ), "2" ( len >> 2 )
57 __asm__ __volatile__ ( "rep movsb"
58 : "=&D" ( edi ), "=&S" ( esi ),
60 : "0" ( edi ), "1" ( esi ), "2" ( len & 3 )
66 * Copy memory area backwards
68 * @v dest Destination address
69 * @v src Source address
71 * @ret dest Destination address
73 void * __attribute__ (( noinline )) __memcpy_reverse ( void *dest,
76 void *edi = ( dest + len - 1 );
77 const void *esi = ( src + len - 1 );
80 /* Assume memmove() is not performance-critical, and perform a
81 * bytewise copy for simplicity.
83 __asm__ __volatile__ ( "std\n\t"
86 : "=&D" ( edi ), "=&S" ( esi ),
88 : "0" ( edi ), "1" ( esi ),
96 * Copy (possibly overlapping) memory area
98 * @v dest Destination address
99 * @v src Source address
101 * @ret dest Destination address
103 void * __memmove ( void *dest, const void *src, size_t len ) {
106 return __memcpy ( dest, src, len );
108 return __memcpy_reverse ( dest, src, len );