xref: /titanic_51/usr/src/lib/libc/i386/gen/memcpy.s (revision 36615d24946b849e48cedbbafa9adfb4a02b590c)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27	.ident	"%Z%%M%	%I%	%E% SMI"
28
29	.file	"%M%"
30
31#include <sys/asm_linkage.h>
32
33	ANSI_PRAGMA_WEAK(memmove,function)
34	ANSI_PRAGMA_WEAK(memcpy,function)
35
36#include "SYS.h"
37
38	ENTRY(memcpy)
39	movl	%edi,%edx	/ save register variables
40	pushl	%esi
41	movl	8(%esp),%edi	/ %edi = dest address
42	movl	12(%esp),%esi	/ %esi = source address
43	movl	16(%esp),%ecx	/ %ecx = length of string
44	movl	%edi,%eax	/ return value from the call
45
46	shrl	$2,%ecx		/ %ecx = number of words to move
47	rep ; smovl		/ move the words
48
49	movl	16(%esp),%ecx	/ %ecx = number of bytes to move
50	andl	$0x3,%ecx	/ %ecx = number of bytes left to move
51	rep ; smovb		/ move the bytes
52
53	popl	%esi		/ restore register variables
54	movl	%edx,%edi
55	ret
56	SET_SIZE(memcpy)
57
58
59	ENTRY(memmove)
60	pushl	%edi		/ save off %edi, %esi and move destination
61	movl	4+12(%esp),%ecx	/ get number of bytes to move
62	pushl	%esi
63	testl	%ecx,%ecx	/ if (n == 0)
64	je	.CleanupReturn	/    return(s);
65	movl	8+ 4(%esp),%edi	/ destination buffer address
66	movl	8+ 8(%esp),%esi	/ source buffer address
67.Common:
68	movl	$3,%eax		/ heavily used constant
69	cmpl	%esi,%edi	/ if (source addr > dest addr)
70	leal	-1(%esi,%ecx),%edx
71	jle	.CopyRight	/
72	cmpl	%edx,%edi
73	jle	.CopyLeft
74.CopyRight:
75	cmpl	$8,%ecx		/    if (size < 8 bytes)
76	jbe	.OneByteCopy	/        goto fast short copy loop
77.FourByteCopy:
78	movl	%ecx,%edx	/    save count
79	movl	%esi,%ecx	/    get source buffer 4 byte aligned
80	andl	%eax,%ecx
81	jz	.SkipAlignRight
82	subl	%ecx,%edx
83	rep;	smovb		/    do the byte part of copy
84.SkipAlignRight:
85	movl	%edx,%ecx
86	shrl	$2,%ecx
87	rep;	smovl		/    do the long word part
88	movl	%edx,%ecx	/    compute bytes left to move
89	andl	%eax,%ecx	/    complete copy of remaining bytes
90	jz	.CleanupReturn
91.OneByteCopy:
92	rep;	smovb		/    do the byte part of copy
93.CleanupReturn:
94	popl	%esi		/  }
95	popl	%edi		/  restore registers
96	movl	4(%esp),%eax	/  set up return value
97.Return:
98	ret			/  return(dba);
99
100.CopyLeft:
101	std				/ reverse direction bit (RtoL)
102	cmpl	$12,%ecx		/ if (size < 12)
103	ja	.BigCopyLeft		/ {
104	movl	%edx,%esi		/     src = src + size - 1
105	leal	-1(%ecx,%edi),%edi	/     dst = dst + size - 1
106	rep;	smovb			/    do the byte copy
107	cld				/    reset direction flag to LtoR
108	popl	%esi			/  }
109	popl	%edi			/  restore registers
110	movl	4(%esp),%eax		/  set up return value
111	ret				/  return(dba);
112.BigCopyLeft:				/ } else {
113	xchgl	%edx,%ecx
114	movl	%ecx,%esi		/ align source w/byte copy
115	leal	-1(%edx,%edi),%edi
116	andl	%eax,%ecx
117	jz	.SkipAlignLeft
118	addl	$1, %ecx		/ we need to insure that future
119	subl	%ecx,%edx		/ copy is done on aligned boundary
120	rep;	smovb
121.SkipAlignLeft:
122	movl	%edx,%ecx
123	subl	%eax,%esi
124	shrl	$2,%ecx			/ do 4 byte copy RtoL
125	subl	%eax,%edi
126	rep;	smovl
127	andl	%eax,%edx		/ do 1 byte copy whats left
128	jz	.CleanupReturnLeft
129	movl	%edx,%ecx
130	addl	%eax,%esi		/ rep; smovl instruction will decrement
131	addl	%eax,%edi		/ %edi, %esi by four after each copy
132					/ adding 3 will restore pointers to byte
133					/ before last double word copied
134					/ which is where they are expected to
135					/ be for the single byte copy code
136	rep;	smovb
137.CleanupReturnLeft:
138	cld				/ reset direction flag to LtoR
139	popl	%esi
140	popl	%edi			/ restore registers
141	movl	4(%esp),%eax		/ set up return value
142	ret				/ return(dba);
143	SET_SIZE(memmove)
144