1ad8684de3b7dcda3454ae6892a8a7197e070cc2
[platform/upstream/glibc.git] / sysdeps / i386 / i586 / memcpy.S
1 /* Highly optimized version for i586.
2    Copyright (C) 1997-2013 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, see
18    <http://www.gnu.org/licenses/>.  */
19
20 #include <sysdep.h>
21 #include "asm-syntax.h"
22 #include "bp-sym.h"
23 #include "bp-asm.h"
24
25 /* BEWARE: `#ifdef memcpy' means that memcpy is redefined as `mempcpy',
26    and the return value is the byte after the last one copied in
27    the destination. */
28 #define MEMPCPY_P (defined memcpy)
29
30 #define PARMS   LINKAGE+8       /* space for 2 saved regs */
31 #define RTN     PARMS
32 #define DEST    RTN+RTN_SIZE
33 #define SRC     DEST+PTR_SIZE
34 #define LEN     SRC+PTR_SIZE
35
36         .text
37 #if defined PIC && !defined NOT_IN_libc
38 ENTRY (__memcpy_chk)
39         movl    12(%esp), %eax
40         cmpl    %eax, 16(%esp)
41         jb      HIDDEN_JUMPTARGET (__chk_fail)
42 END (__memcpy_chk)
43 #endif
44 ENTRY (BP_SYM (memcpy))
45
46         pushl   %edi
47         cfi_adjust_cfa_offset (4)
48         pushl   %esi
49         cfi_adjust_cfa_offset (4)
50
51         movl    DEST(%esp), %edi
52         cfi_rel_offset (edi, 4)
53         movl    SRC(%esp), %esi
54         cfi_rel_offset (esi, 0)
55         movl    LEN(%esp), %ecx
56         movl    %edi, %eax
57
58         /* We need this in any case.  */
59         cld
60
61         /* Cutoff for the big loop is a size of 32 bytes since otherwise
62            the loop will never be entered.  */
63         cmpl    $32, %ecx
64         jbe     L(1)
65
66         negl    %eax
67         andl    $3, %eax
68         subl    %eax, %ecx
69         xchgl   %eax, %ecx
70
71         rep; movsb
72
73         movl    %eax, %ecx
74         subl    $32, %ecx
75         js      L(2)
76
77         /* Read ahead to make sure we write in the cache since the stupid
78            i586 designers haven't implemented read-on-write-miss.  */
79         movl    (%edi), %eax
80 L(3):   movl    28(%edi), %edx
81
82         /* Now correct the loop counter.  Please note that in the following
83            code the flags are not changed anymore.  */
84         subl    $32, %ecx
85
86         movl    (%esi), %eax
87         movl    4(%esi), %edx
88         movl    %eax, (%edi)
89         movl    %edx, 4(%edi)
90         movl    8(%esi), %eax
91         movl    12(%esi), %edx
92         movl    %eax, 8(%edi)
93         movl    %edx, 12(%edi)
94         movl    16(%esi), %eax
95         movl    20(%esi), %edx
96         movl    %eax, 16(%edi)
97         movl    %edx, 20(%edi)
98         movl    24(%esi), %eax
99         movl    28(%esi), %edx
100         movl    %eax, 24(%edi)
101         movl    %edx, 28(%edi)
102
103         leal    32(%esi), %esi
104         leal    32(%edi), %edi
105
106         jns     L(3)
107
108         /* Correct extra loop counter modification.  */
109 L(2):   addl    $32, %ecx
110 #if !MEMPCPY_P
111         movl    DEST(%esp), %eax
112 #endif
113
114 L(1):   rep; movsb
115
116 #if MEMPCPY_P
117         movl    %edi, %eax
118 #endif
119
120         popl    %esi
121         cfi_adjust_cfa_offset (-4)
122         cfi_restore (esi)
123         popl    %edi
124         cfi_adjust_cfa_offset (-4)
125         cfi_restore (edi)
126
127         RET_PTR
128 END (BP_SYM (memcpy))
129 #if !MEMPCPY_P
130 libc_hidden_builtin_def (memcpy)
131 #endif