memmove.S revision 76d05dc695b06c4e987bb8078f78032441e1430c
1/* ----------------------------------------------------------------------- *
2 *
3 *   Copyright 2008 H. Peter Anvin - All Rights Reserved
4 *   Copyright 2010 Intel Corporation; author: H. Peter Anvin
5 *
6 *   Permission is hereby granted, free of charge, to any person
7 *   obtaining a copy of this software and associated documentation
8 *   files (the "Software"), to deal in the Software without
9 *   restriction, including without limitation the rights to use,
10 *   copy, modify, merge, publish, distribute, sublicense, and/or
11 *   sell copies of the Software, and to permit persons to whom
12 *   the Software is furnished to do so, subject to the following
13 *   conditions:
14 *
15 *   The above copyright notice and this permission notice shall
16 *   be included in all copies or substantial portions of the Software.
17 *
18 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 *   OTHER DEALINGS IN THE SOFTWARE.
26 *
27 * ----------------------------------------------------------------------- */
28
29/*
30 * memmove.S
31 *
32 * Reasonably efficient memmove, using aligned transfers at least
33 * for the destination operand.
34 */
35
36	.globl	memmove
37	.type	memmove,@function
38	.text
39memmove:
40	movl	0xc(%esp),%ecx
41	movl	0x8(%esp),%edx
42	movl	0x4(%esp),%eax
43
44	jecxz	4f
45
46	pushl	%esi
47	pushl	%edi
48	pushl	%eax		/* Return value */
49
50	movl	%eax,%edi
51	movl	%edx,%esi
52
53	cmpl	%edi,%esi
54	jb	2f
55
56	/* source >= dest, forwards move */
57
58	/* Initial alignment */
591:
60	movl	%edi,%edx
61	shrl	$1,%edx
62	jnc	11f
63	movsb
64	decl	%ecx
6511:
66	movb	%cl,%al
67	cmpl	$2,%ecx
68	jb	13f
69
70	shrl	$1,%edx
71	jnc	12f
72	movsw
73	subl	$2,%ecx
7412:
75	/* Bulk transfer */
76	movb	%cl,%al
77	shrl	$2,%ecx
78	rep; movsl
79
80	/* Final alignment */
81	testb	$2,%al
82	jz	14f
83	movsw
8413:
8514:
86	testb	$1,%al
87	jz	15f
88	movsb
8915:
90	/* Common exit stub */
913:
92	popl	%eax		/* Return value */
93	popl	%edi
94	popl	%esi
954:
96	ret
97
98
992:
100	/* source < dest, backwards move if overlap */
101	leal	-1(%ecx,%esi),%eax
102	cmpl	%eax,%edi
103	ja	1b			/* No overlap, after all... */
104
105	std
106	leal	-1(%ecx,%edi),%edi
107	movl	%eax,%esi
108
109	/* Initial alignment */
110	movl	%edi,%edx
111	shrl	$1,%edx
112	jc	21f
113	movsb
114	decl	%ecx
11521:
116	decl	%esi
117	decl	%edi
118	movb	%cl,%al
119	cmpl	$2,%ecx
120	jb	23f
121	shrl	$1,%edx
122	jc	22f
123	movsw
124	subl	$2,%ecx
12522:
126	/* Bulk transfer */
127	subl	$2,%esi
128	subl	$2,%edi
129	movb	%cl,%al
130	shrl	$2,%ecx
131	rep; movsl
132
133	/* Final alignment */
134	addl	$2,%esi
135	addl	$2,%edi
136	testb	$2,%al
137	jz	24f
138	movsw
13923:
14024:
141	incl	%esi
142	incl	%edi
143	testb	$1,%al
144	jz	25f
145	movsb
14625:
147	cld
148	jmp	3b
149
150	.size	memmove, .-memmove
151