1/* memcpy with REP MOVSB/STOSB
2   Copyright (C) 2015-2021 Free Software Foundation, Inc.
3   This file is part of the GNU C Library.
4
5   The GNU C Library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9
10   The GNU C Library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14
15   You should have received a copy of the GNU Lesser General Public
16   License along with the GNU C Library; if not, see
17   <https://www.gnu.org/licenses/>.  */
18
19#include <sysdep.h>
20#include "asm-syntax.h"
21
22#ifndef MEMCPY
23# define MEMCPY		memcpy
24# define MEMCPY_CHK	__memcpy_chk
25#endif
26
27#ifdef USE_AS_BCOPY
28# define STR2		12
29# define STR1		STR2+4
30# define N     		STR1+4
31#else
32# define STR1		12
33# define STR2		STR1+4
34# define N     		STR2+4
35#endif
36
37#define CFI_PUSH(REG)						\
38  cfi_adjust_cfa_offset (4);					\
39  cfi_rel_offset (REG, 0)
40
41#define CFI_POP(REG)						\
42  cfi_adjust_cfa_offset (-4);					\
43  cfi_restore (REG)
44
45#define PUSH(REG)	pushl REG; CFI_PUSH (REG)
46#define POP(REG)	popl REG; CFI_POP (REG)
47
48	.text
49#if defined SHARED && IS_IN (libc) && !defined USE_AS_BCOPY
50ENTRY (MEMCPY_CHK)
51	movl	12(%esp), %eax
52	cmpl	%eax, 16(%esp)
53	jb	HIDDEN_JUMPTARGET (__chk_fail)
54END (MEMCPY_CHK)
55#endif
56ENTRY (MEMCPY)
57	PUSH	(%esi)
58	PUSH	(%edi)
59	movl	N(%esp), %ecx
60	movl	STR1(%esp), %edi
61	movl	STR2(%esp), %esi
62	mov	%edi, %eax
63#ifdef USE_AS_MEMPCPY
64	add	%ecx, %eax
65#endif
66
67#ifdef USE_AS_MEMMOVE
68	cmp	%esi, %edi
69	ja	L(copy_backward)
70	je	L(bwd_write_0bytes)
71#endif
72
73	rep	movsb
74	POP	(%edi)
75	POP	(%esi)
76	ret
77
78#ifdef USE_AS_MEMMOVE
79L(copy_backward):
80	lea	-1(%edi,%ecx), %edi
81	lea	-1(%esi,%ecx), %esi
82	std
83	rep	movsb
84	cld
85L(bwd_write_0bytes):
86	POP	(%edi)
87	POP	(%esi)
88	ret
89#endif
90
91END (MEMCPY)
92
93#ifndef USE_AS_BCOPY
94libc_hidden_builtin_def (MEMCPY)
95#endif
96