1/* ----------------------------------------------------------------------- *
2 *
3 *   Copyright 2008 H. Peter Anvin - All Rights Reserved
4 *
5 *   Permission is hereby granted, free of charge, to any person
6 *   obtaining a copy of this software and associated documentation
7 *   files (the "Software"), to deal in the Software without
8 *   restriction, including without limitation the rights to use,
9 *   copy, modify, merge, publish, distribute, sublicense, and/or
10 *   sell copies of the Software, and to permit persons to whom
11 *   the Software is furnished to do so, subject to the following
12 *   conditions:
13 *
14 *   The above copyright notice and this permission notice shall
15 *   be included in all copies or substantial portions of the Software.
16 *
17 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 *   OTHER DEALINGS IN THE SOFTWARE.
25 *
26 * ----------------------------------------------------------------------- */
27
28/*
29 * memmove.S
30 *
31 * Reasonably efficient memmove, using aligned transfers at least
32 * for the destination operand.
33 */
34
35	.globl	memmove
36	.type	memmove,@function
37	.text
38memmove:
39	jecxz	3f
40
41	pushl	%esi
42	pushl	%edi
43	pushl	%eax		/* Return value */
44
45	movl	%eax,%edi
46	movl	%edx,%esi
47
48	cmpl	%edi,%esi
49	jb	1f
50
51	/* source >= dest, forwards move */
52
53	/* Initial alignment */
54	movl	%edi,%edx
55	shrl	$1,%edx
56	jnc	11f
57	movsb
58	decl	%ecx
5911:
60	movb	%cl,%al
61	cmpl	$2,%ecx
62	jb	13f
63
64	shrl	$1,%edx
65	jnc	12f
66	movsw
67	subl	$2,%ecx
6812:
69	/* Bulk transfer */
70	movb	%cl,%al
71	shrl	$2,%ecx
72	rep; movsl
73
74	/* Final alignment */
75	testb	$2,%al
76	jz	14f
77	movsw
7813:
7914:
80	testb	$1,%al
81	jz	15f
82	movsb
8315:
84	jmp	2f
85
86
871:
88	/* source < dest, backwards move */
89	std
90	leal	-1(%ecx,%esi),%esi
91	leal	-1(%ecx,%edi),%edi
92
93	/* Initial alignment */
94	movl	%edi,%edx
95	shrl	$1,%edx
96	jc	21f
97	movsb
98	decl	%ecx
9921:
100	decl	%esi
101	decl	%edi
102	movb	%cl,%al
103	cmpl	$2,%ecx
104	jb	23f
105	shrl	$1,%edx
106	jc	22f
107	movsw
108	subl	$2,%ecx
10922:
110	/* Bulk transfer */
111	subl	$2,%esi
112	subl	$2,%edi
113	movb	%cl,%al
114	shrl	$2,%ecx
115	rep; movsl
116
117	/* Final alignment */
118	addl	$2,%esi
119	addl	$2,%edi
120	testb	$2,%al
121	jz	24f
122	movsw
12323:
12424:
125	incl	%esi
126	incl	%edi
127	testb	$1,%al
128	jz	25f
129	movsb
13025:
131	cld
1322:
133	popl	%eax		/* Return value */
134	popl	%edi
135	popl	%esi
1363:
137	ret
138
139	.size	memmove, .-memmove
140