1/*******************************************************************************
2 *
3 * Copyright (c) 1993 Intel Corporation
4 *
5 * Intel hereby grants you permission to copy, modify, and distribute this
6 * software and its documentation.  Intel grants this permission provided
7 * that the above copyright notice appears in all copies and that both the
8 * copyright notice and this permission notice appear in supporting
9 * documentation.  In addition, Intel grants this permission provided that
10 * you prominently mark as "not part of the original" any modifications
11 * made to this software or documentation, and that the name of Intel
12 * Corporation not be used in advertising or publicity pertaining to
13 * distribution of the software or the documentation without specific,
14 * written prior permission.
15 *
16 * Intel Corporation provides this AS IS, WITHOUT ANY WARRANTY, EXPRESS OR
17 * IMPLIED, INCLUDING, WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY
18 * OR FITNESS FOR A PARTICULAR PURPOSE.  Intel makes no guarantee or
19 * representations regarding the use of, or the results of the use of,
20 * the software and documentation in terms of correctness, accuracy,
21 * reliability, currentness, or otherwise; and you rely on the software,
22 * documentation and results solely at your own risk.
23 *
24 * IN NO EVENT SHALL INTEL BE LIABLE FOR ANY LOSS OF USE, LOSS OF BUSINESS,
25 * LOSS OF PROFITS, INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES
26 * OF ANY KIND.  IN NO EVENT SHALL INTEL'S TOTAL LIABILITY EXCEED THE SUM
27 * PAID TO INTEL FOR THE PRODUCT LICENSED HEREUNDER.
28 *
29 ******************************************************************************/
30
31	.file "memcpy.s"
32#ifdef	__PIC
33	.pic
34#endif
35#ifdef	__PID
36	.pid
37#endif
38/*
39 * (c) copyright 1988,1993 Intel Corp., all rights reserved
40 */
41/*
42	procedure memmove  (optimized assembler version for the 80960K series)
43	procedure memcpy   (optimized assembler version for the 80960K series)
44
45	dest_addr = memmove (dest_addr, src_addr, len)
46	dest_addr = memcpy  (dest_addr, src_addr, len)
47
48	copy len bytes pointed to by src_addr to the space pointed to by
49	dest_addr.  Return the original dest_addr.
50
51	These routines will work even if the arrays overlap.  The standard
52	requires this of memmove, but memcpy is allowed to fail if overlap
53	is present.  Nevertheless, it is implemented the same as memmove
54	because the overhead is trifling.
55
56	Undefined behavior will occur if the end of the source array is in
57	the last two words of the program's allocated memory space.  This
58	is so because the routine fetches ahead.  Disallowing the fetch
59	ahead would impose a severe performance penalty.
60
61	Strategy:
62
63	Fetch the source array by words and store them by words to the
64	destination array, until there are fewer than three bytes left
65	to copy.  Then, using the last word of the source (the one that
66	contains the remaining 0, 1, 2, or 3 bytes to be copied), store
67	a byte at a time until Ldone.
68
69	Tactics:
70
71	1) Do NOT try to fetch and store the words in a word aligned manner
72	because, in my judgement, the performance degradation experienced due
73	to non-aligned accesses does NOT outweigh the time and complexity added
74	by the preamble and convoluted body that would be necessary to assure
75	alignment.  This is supported by the intuition that most source and
76	destination arrays (even more true of most big source arrays) will
77	be word aligned to begin with.
78
79	2) For non-overlapping arrays, rather than decrementing len to zero,
80	I calculate the address of the byte after the last byte of the
81	destination array, and quit when the destination byte pointer passes
82	that.
83
84	3) For overlapping arrays where the source starts at a lower address
85	than the destination the move is performed in reverse order.
86
87	4) Overlapping arrays where the source starts at a higher address
88	are treated like non-overlapping case.  Where the two arrays exactly
89	coincide, the routine is short-circuited;  no move is Ldone at all.
90	This costs only one cycle.
91*/
92
93	.globl _memcpy, _memmove
94	.globl __memcpy, __memmove
95	.leafproc _memmove, __memmove
96	.leafproc _memcpy, __memcpy
97	.align    2
98_memmove:
99_memcpy:
100#ifndef __PIC
101 	lda	Lrett,g14
102#else
103 	lda	Lrett-(.+8)(ip),g14
104#endif
105__memmove:
106__memcpy:
107	mov	g14, g13	# preserve return address
108	cmpibge	0,g2,Lexit	# exit if number of bytes to move is <= zero.
109	cmpo	g0,g1		# does start of dest overlap end of src?
110	addo	g2,g1,g3
111	be	Lexit		# no move necessary if src and dest are same
112	concmpo	g3,g0
113	addo	g2, g0, g6
114	bg	Lbackwards	# if overlap, then do move backwards
115
116	ld	(g1), g7	# fetch first word of source
117	mov	g0, g5
118	b	Lwloop_b
119
120Lwloop_a:
121	ld	(g1), g7	# fetch ahead next word of source
122	st	g4, (g5)	# store word to dest
123	addo	4, g5, g5	# post-increment dest pointer
124Lwloop_b:			# word copying loop
125	addo	4, g1, g1	# pre-increment src pointer
126	cmpo	g3, g1		# is len <= 3 ?
127	mov	g7, g4		# keep a copy of the current word
128	bge	Lwloop_a		# loop if more than 3 bytes to move
129	cmpobe	g6, g5, Lexit    # quit if no more bytes to move
130
131Lcloop_a:			# character copying loop (len < 3)
132	stob	g4, (g5)	# store a byte
133	shro	8, g4, g4	# position next byte for storing
134	addo	1, g5, g5
135	cmpobne	g6, g5, Lcloop_a	# quit if no more bytes to move
136
137Lexit:
138	mov	0, g14
139	bx	(g13)		# g0 = dest array address; g14 = 0
140Lrett:
141	ret
142
143Lwloop.a:
144	subo	4, g6, g6	# pre-decrement dest pointer
145	st	g7, (g6)	# store word to dest
146Lbackwards:			# word copying loop
147	subo	4, g3, g3	# pre-decrement src pointer
148	cmpo	g1, g3		# is len <= 3?
149	ld	(g3), g7	# fetch ahead next word of source
150	ble	Lwloop.a		# loop if more than 3 bytes to move
151	cmpobe	g6, g0, Lexit	# quit if no more bytes to move
152
153Lcloop.a:
154	subo	1, g6, g6
155	rotate	8, g7, g7	# position byte for storing
156	stob	g7, (g6)	# store byte
157	cmpobne	g6, g0, Lcloop.a	# quit if no more bytes to move
158	b	Lexit
159
160/* end of memmove */
161