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 "strcpy.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 strcpy  (optimized assembler version for the 80960K series)
43	procedure strcat  (optimized assembler version for the 80960K series)
44
45	dest_addr = strcpy (dest_addr, src_addr)
46
47	copy the null terminated string pointed to by src_addr to
48	the string space pointed to by dest_addr.  Return the original
49	dest_addr.
50
51	This routine will fail if the source and destination string
52	overlap (in particular, if the end of the source is overlapped
53	by the beginning of the destination).  The behavior is undefined.
54	This is acceptable according to the draft C standard.
55
56	Undefined behavior will also occur if the end of the source string
57	(i.e. the terminating null byte) is in the last two words of the
58	program's allocated memory space.  This is so because strcpy fetches
59	ahead.  Disallowing the fetch ahead would impose a severe performance
60	penalty.
61
62	Strategy:
63
64	Fetch the source string and store the destination string by words
65	until the null byte is encountered.  When the word with the null
66	byte is reached, store it by bytes up through the null byte only.
67
68	Tactics:
69
70	1) Do NOT try to fetch and store the words in a word aligned manner
71	because, in my judgement, the performance degradation experienced due
72 	to non-aligned accesses does NOT outweigh the time and complexity added
73	by the preamble and convoluted body that would be necessary to assure
74	alignment.  This is supported by the intuition that most source and
75	destination strings will be word aligned to begin with.
76
77
78	procedure strcat
79
80	dest_addr = strcat (dest_addr, src_addr)
81
82	Appends the string pointed to by src_addr to the string pointed
83	to by dest_addr.  The first character of the source string is
84	copied to the location initially occupied by the trailing null
85	byte of the destination string.  Thereafter, characters are copied
86	from the source to the destination up thru the null byte that
87	trails the source string.
88
89	See the strcpy routine, above, for its caveats, as they apply here too.
90
91	Strategy:
92
93	Skip to the end (null byte) of the destination string, and then drop
94	into the strcpy code.
95
96	Tactics:
97
98	Skipping to the null byte is Ldone by reading the destination string
99	in long-words and scanbyte'ing them, then examining the bytes of the
100	word that contains the null byte, until the address of the null byte is
101	known.  Then we drop into the strcpy routine.  It is probable (approx.
102	three out of four times) that the destination string as strcpy sees
103	it will NOT be word aligned (i.e. that the null byte won't be the
104	last byte of a word).  But it is not worth the complication to that
105	routine to force word aligned memory accesses to be gaurenteed.
106*/
107	.globl _strcpy, _strcat
108	.globl __strcpy, __strcat
109	.leafproc _strcpy,__strcpy
110	.leafproc _strcat,__strcat
111	.align    2
112_strcat:
113#ifndef __PIC
114 	lda	Lrett,g14
115#else
116 	lda	Lrett-(.+8)(ip),g14
117#endif
118__strcat:
119	mov	g14,g13		# preserve return address
120	ldl	(g0),g4		# fetch first two words
121	addo	8,g0,g2		# post-increment src word pointer
122	lda	0xff,g3		# byte extraction mask
123
124Lsearch_for_word_with_null_byte:
125	scanbyte 0,g4		# check for null byte
126	mov	g5,g7		# copy second word
127	bo.f	Lsearch_for_null	# branch if null found
128	scanbyte 0,g7		# check for null byte
129	ldl	(g2),g4		# fetch next pair of word of src
130	addo	8,g2,g2		# post-increment src word pointer
131	bno	Lsearch_for_word_with_null_byte	# branch if null not found yet
132
133	subo	4,g2,g2		# back up the byte pointer
134	mov	g7,g4		# move word with null to search word
135Lsearch_for_null:
136	subo	9,g2,g5		# back up the byte pointer
137Lsearch_for_null.a:
138	and	g4,g3,g6	# extract byte
139	cmpo	0,g6		# is it null?
140	addo	1,g5,g5		# bump src byte ptr
141	shro	8,g4,g4		# shift word to position next byte
142	bne	Lsearch_for_null.a
143	b	Lend_of_dest_found
144
145_strcpy:
146#ifndef __PIC
147 	lda	Lrett,g14
148#else
149 	lda	Lrett-(.+8)(ip),g14
150#endif
151__strcpy:
152	mov	g0, g5
153Lend_of_dest_found:
154	ld	(g1), g2	# fetch first word of source
155	mov	g14,g6		# preserve return address
156	lda	0xff, g3	# byte extraction mask = 0xff;
157Lwloop:				# word copying loop
158	addo	4, g1, g1	# post-increment source ptr
159	scanbyte 0, g2		# does source word contain null byte?
160	mov	g2, g4		# save a copy of the source word
161	be	Lcloop		# branch if null present
162	ld	(g1), g2	# pre-fetch next word of source
163	st	g4, (g5)	# store current word
164	addo	4, g5, g5	# post-increment dest ptr
165	b	Lwloop
166
167Lcloop:				# character copying loop
168	and	g3, g4, g14	# extract next char
169	shro	8, g4, g4	# position word for next byte extraction
170	cmpo	0, g14 		# is it null?
171	stob	g14, (g5)	# store the byte
172	addo	1, g5, g5	# post-increment dest ptr
173	bne	Lcloop		# quit if null encountered
174
175	bx	(g6)		# g0 = dest string address; g14 = 0
176Lrett:
177	ret
178