about summary refs log tree commit diff
path: root/sysdeps/x86_64/memcpy.S
blob: 00c17a42875a57c2b46c9192e06a05cea4d59c28 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* Optimized memcpy for x86-64
   Copyright (C) 2013-2015 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#include <sysdep.h>

#include "asm-syntax.h"

#if defined SHARED && IS_IN (libc)
# if !defined USE_AS_MEMPCPY && !defined USE_MULTIARCH
#  define memcpy	__memcpy
#  undef libc_hidden_builtin_def
#  define libc_hidden_builtin_def(name) \
	.globl __GI_memcpy; __GI_memcpy = __memcpy
# endif

ENTRY_CHK (__memcpy_chk)

	cmpq	%rdx, %rcx
	jb	HIDDEN_JUMPTARGET (__chk_fail)

END_CHK (__memcpy_chk)
#endif

ENTRY(memcpy)
	movq	%rsi, %rax
#ifdef USE_AS_MEMPCPY
	leaq	(%rdi,%rdx), %r11
#endif
	leaq	(%rdx,%rdx), %rcx
	subq	%rdi, %rax
	subq	%rdx, %rax
	cmpq	%rcx, %rax
	jb	L(overlapping)
	cmpq	$16, %rdx
	jbe	L(less_16)
	movdqu	(%rsi), %xmm8
	cmpq	$32, %rdx
	movdqu	%xmm8, (%rdi)
	movdqu	-16(%rsi,%rdx), %xmm8
	movdqu	%xmm8, -16(%rdi,%rdx)
	ja	.L31
L(return):
#ifdef USE_AS_MEMPCPY
	movq	%r11, %rax
#else
	movq	%rdi, %rax
#endif
	ret
	.p2align 4,,10
	.p2align 4
.L31:
	movdqu	16(%rsi), %xmm8
	cmpq	$64, %rdx
	movdqu	%xmm8, 16(%rdi)
	movdqu	-32(%rsi,%rdx), %xmm8
	movdqu	%xmm8, -32(%rdi,%rdx)
	jbe	L(return)
	movdqu	32(%rsi), %xmm8
	cmpq	$128, %rdx
	movdqu	%xmm8, 32(%rdi)
	movdqu	-48(%rsi,%rdx), %xmm8
	movdqu	%xmm8, -48(%rdi,%rdx)
	movdqu	48(%rsi), %xmm8
	movdqu	%xmm8, 48(%rdi)
	movdqu	-64(%rsi,%rdx), %xmm8
	movdqu	%xmm8, -64(%rdi,%rdx)
	jbe	L(return)
	leaq	64(%rdi), %rcx
	addq	%rdi, %rdx
	andq	$-64, %rdx
	andq	$-64, %rcx
	movq	%rcx, %rax
	subq	%rdi, %rax
	addq	%rax, %rsi
	cmpq	%rdx, %rcx
	je	L(return)
	movq	%rsi, %r10
	subq	%rcx, %r10
	leaq	16(%r10), %r9
	leaq	32(%r10), %r8
	leaq	48(%r10), %rax
	.p2align 4,,10
	.p2align 4
L(loop):
	movdqu	(%rcx,%r10), %xmm8
	movdqa	%xmm8, (%rcx)
	movdqu	(%rcx,%r9), %xmm8
	movdqa	%xmm8, 16(%rcx)
	movdqu	(%rcx,%r8), %xmm8
	movdqa	%xmm8, 32(%rcx)
	movdqu	(%rcx,%rax), %xmm8
	movdqa	%xmm8, 48(%rcx)
	addq	$64, %rcx
	cmpq	%rcx, %rdx
	jne	L(loop)
	jmp	L(return)
L(overlapping):
	cmpq	%rsi, %rdi
	jae	.L3
	testq	%rdx, %rdx
	.p2align 4,,5
	je	L(return)
	movq	%rdx, %r9
	leaq	16(%rsi), %rcx
	leaq	16(%rdi), %r8
	shrq	$4, %r9
	movq	%r9, %rax
	salq	$4, %rax
	cmpq	%rcx, %rdi
	setae	%cl
	cmpq	%r8, %rsi
	setae	%r8b
	orl	%r8d, %ecx
	cmpq	$15, %rdx
	seta	%r8b
	testb	%r8b, %cl
	je	.L16
	testq	%rax, %rax
	je	.L16
	xorl	%ecx, %ecx
	xorl	%r8d, %r8d
.L7:
	movdqu	(%rsi,%rcx), %xmm8
	addq	$1, %r8
	movdqu	%xmm8, (%rdi,%rcx)
	addq	$16, %rcx
	cmpq	%r8, %r9
	ja	.L7
	cmpq	%rax, %rdx
	je	L(return)
.L21:
	movzbl	(%rsi,%rax), %ecx
	movb	%cl, (%rdi,%rax)
	addq	$1, %rax
	cmpq	%rax, %rdx
	ja	.L21
	jmp	L(return)
L(less_16):
	testb	$24, %dl
	jne	L(between_9_16)
	testb	$4, %dl
	.p2align 4,,5
	jne	L(between_5_8)
	testq	%rdx, %rdx
	.p2align 4,,2
	je	L(return)
	movzbl	(%rsi), %eax
	testb	$2, %dl
	movb	%al, (%rdi)
	je	L(return)
	movzwl	-2(%rsi,%rdx), %eax
	movw	%ax, -2(%rdi,%rdx)
	jmp	L(return)
.L3:
	leaq	-1(%rdx), %rax
	.p2align 4,,10
	.p2align 4
.L11:
	movzbl	(%rsi,%rax), %edx
	movb	%dl, (%rdi,%rax)
	subq	$1, %rax
	jmp	.L11
L(between_9_16):
	movq	(%rsi), %rax
	movq	%rax, (%rdi)
	movq	-8(%rsi,%rdx), %rax
	movq	%rax, -8(%rdi,%rdx)
	jmp	L(return)
.L16:
	xorl	%eax, %eax
	jmp	.L21
L(between_5_8):
	movl	(%rsi), %eax
	movl	%eax, (%rdi)
	movl	-4(%rsi,%rdx), %eax
	movl	%eax, -4(%rdi,%rdx)
	jmp	L(return)
END(memcpy)

#ifndef USE_AS_MEMPCPY
libc_hidden_builtin_def (memcpy)
# if defined SHARED && !defined USE_MULTIARCH && IS_IN (libc)
#  undef memcpy
#  include <shlib-compat.h>
versioned_symbol (libc, __memcpy, memcpy, GLIBC_2_14);
# endif
#endif