1/* memcpy with REP MOVSB/STOSB
2 Copyright (C) 2015-2024 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#define STR1 12
28#define STR2 STR1+4
29#define N STR2+4
30
31#define CFI_PUSH(REG) \
32 cfi_adjust_cfa_offset (4); \
33 cfi_rel_offset (REG, 0)
34
35#define CFI_POP(REG) \
36 cfi_adjust_cfa_offset (-4); \
37 cfi_restore (REG)
38
39#define PUSH(REG) pushl REG; CFI_PUSH (REG)
40#define POP(REG) popl REG; CFI_POP (REG)
41
42 .text
43#if defined SHARED && IS_IN (libc)
44ENTRY (MEMCPY_CHK)
45 movl 12(%esp), %eax
46 cmpl %eax, 16(%esp)
47 jb HIDDEN_JUMPTARGET (__chk_fail)
48END (MEMCPY_CHK)
49libc_hidden_builtin_def (MEMCPY_CHK)
50#endif
51ENTRY (MEMCPY)
52 PUSH (%esi)
53 PUSH (%edi)
54 movl N(%esp), %ecx
55 movl STR1(%esp), %edi
56 movl STR2(%esp), %esi
57 mov %edi, %eax
58#ifdef USE_AS_MEMPCPY
59 add %ecx, %eax
60#endif
61
62#ifdef USE_AS_MEMMOVE
63 cmp %esi, %edi
64 ja L(copy_backward)
65 je L(bwd_write_0bytes)
66#endif
67
68 rep movsb
69 POP (%edi)
70 POP (%esi)
71 ret
72
73#ifdef USE_AS_MEMMOVE
74L(copy_backward):
75 lea -1(%edi,%ecx), %edi
76 lea -1(%esi,%ecx), %esi
77 std
78 rep movsb
79 cld
80L(bwd_write_0bytes):
81 POP (%edi)
82 POP (%esi)
83 ret
84#endif
85
86END (MEMCPY)
87
88libc_hidden_builtin_def (MEMCPY)
89

source code of glibc/sysdeps/i386/memcpy.S