These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / arch / x86 / core / x86_string.c
1 /*
2  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  *
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.
22  */
23
24 /** @file
25  *
26  * Optimised string operations
27  *
28  */
29
30 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
31
32 #include <string.h>
33
34 /**
35  * Copy memory area
36  *
37  * @v dest              Destination address
38  * @v src               Source address
39  * @v len               Length
40  * @ret dest            Destination address
41  */
42 void * __attribute__ (( noinline )) __memcpy ( void *dest, const void *src,
43                                                size_t len ) {
44         void *edi = dest;
45         const void *esi = src;
46         int discard_ecx;
47
48         /* We often do large dword-aligned and dword-length block
49          * moves.  Using movsl rather than movsb speeds these up by
50          * around 32%.
51          */
52         __asm__ __volatile__ ( "rep movsl"
53                                : "=&D" ( edi ), "=&S" ( esi ),
54                                  "=&c" ( discard_ecx )
55                                : "0" ( edi ), "1" ( esi ), "2" ( len >> 2 )
56                                : "memory" );
57         __asm__ __volatile__ ( "rep movsb"
58                                : "=&D" ( edi ), "=&S" ( esi ),
59                                  "=&c" ( discard_ecx )
60                                : "0" ( edi ), "1" ( esi ), "2" ( len & 3 )
61                                : "memory" );
62         return dest;
63 }
64
65 /**
66  * Copy memory area backwards
67  *
68  * @v dest              Destination address
69  * @v src               Source address
70  * @v len               Length
71  * @ret dest            Destination address
72  */
73 void * __attribute__ (( noinline )) __memcpy_reverse ( void *dest,
74                                                        const void *src,
75                                                        size_t len ) {
76         void *edi = ( dest + len - 1 );
77         const void *esi = ( src + len - 1 );
78         int discard_ecx;
79
80         /* Assume memmove() is not performance-critical, and perform a
81          * bytewise copy for simplicity.
82          */
83         __asm__ __volatile__ ( "std\n\t"
84                                "rep movsb\n\t"
85                                "cld\n\t"
86                                : "=&D" ( edi ), "=&S" ( esi ),
87                                  "=&c" ( discard_ecx )
88                                : "0" ( edi ), "1" ( esi ),
89                                  "2" ( len )
90                                : "memory" );
91         return dest;
92 }
93
94
95 /**
96  * Copy (possibly overlapping) memory area
97  *
98  * @v dest              Destination address
99  * @v src               Source address
100  * @v len               Length
101  * @ret dest            Destination address
102  */
103 void * __memmove ( void *dest, const void *src, size_t len ) {
104
105         if ( dest <= src ) {
106                 return __memcpy ( dest, src, len );
107         } else {
108                 return __memcpy_reverse ( dest, src, len );
109         }
110 }