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 "mccpy_ca.s"
32#ifdef	__PIC
33	.pic
34#endif
35#ifdef	__PID
36	.pid
37#endif
38/*
39 * (c) copyright 1989,1993 Intel Corp., all rights reserved
40 */
41
42/*
43	procedure memccpy  (optimized assembler version for the 80960CA)
44
45	dest_addr = memccpy (dest_addr, src_addr, char, len)
46
47	copy len bytes pointed to by src_addr to the space pointed to by
48        dest_addr, stopping if char is copied.  If char is copied,
49        return address of byte after char in dest string;  else null.
50
51	Undefined behavior will occur if the end of the source array is in
52	the last two words of the program's allocated memory space.  This
53	is so because the routine fetches ahead.  Disallowing the fetch
54	ahead would impose a severe performance penalty.
55
56        Undefined behavior will also occur if the source and destination
57        strings overlap.
58
59
60	This program handles five cases:
61
62	1) both arguments start on a word boundary
63	2) neither are word aligned, but they are offset by the same amount
64	3) source is word aligned, destination is not
65	4) destination is word aligned, source is not
66	5) neither is word aligned, and they are offset by differing amounts
67
68	At the time of this writing, only g0 thru g7 and g13 are available
69	for use in this leafproc;  other registers would have to be saved and
70	restored.  These nine registers, plus tricky use of g14 are sufficient
71	to implement the routine.
72*/
73
74#if __i960_BIG_ENDIAN__
75#define MSW g6
76#define LSW g7
77#else
78#define LSW g6
79#define MSW g7
80#endif
81
82	.globl	_memccpy
83	.leafproc	_memccpy, __memccpy
84	.align	2
85_memccpy:
86#ifndef __PIC
87	lda 	Lrett,g14
88#else
89	lda 	Lrett-(.+8)(ip),g14
90#endif
91__memccpy:
92	notand	g1,3,g5		# extract word addr of start of src
93	 lda	(g14),g13	# preserve return address
94	cmpibge.f 0,g3,Lexit_char_not_found # Lexit if # of bytes to move is <= 0
95	cmpo	g5,g1		# check alignment of src
96	 ld	(g5),LSW	# fetch word containing at least first byte
97	notand	g0,3,g4		# extract word addr of start of dest
98	 lda	4(g5),g5	# advance src word addr
99	shlo	24,g2,g2	# reduce char to single byte
100	 bne.f	Lcase_245	# branch if src is NOT word aligned
101
102Lcase_13:
103	cmpobe.t g0,g4,Lcase_1_setup	# branch if dest word aligned
104
105Lcase_3:				# src is word aligned; dest is not
106	mov	LSW,MSW		# make copy of first word of src
107	addo	4,g4,g1		# move dest word ptr to first word boundary
108	 lda	32,g14		# initialize shift count to zero
109
110Lcase_25:
111Lcase_3_cloop_at_start:		# character copying loop for start of dest str
112	cmpdeci	0,g3,g3		# is max_bytes exhausted?
113#if __i960_BIG_ENDIAN__
114	 lda	-8(g14),g14	# augment the shift counter
115#else
116	 lda	8(g14),g14	# augment the shift counter
117#endif
118	be.f	Lexit_char_not_found	# Lexit if max_bytes is exhausted
119#if __i960_BIG_ENDIAN__
120	rotate	8,MSW,MSW	# move next byte into position for extraction
121#endif
122	shlo	24,MSW,g4
123	 stob	MSW,(g0)	# store the byte in dest
124	cmpo	g4,g2
125	 lda	1(g0),g0	# post-increment dest ptr
126#if ! __i960_BIG_ENDIAN__
127	shro	8,MSW,MSW	# move next byte into position for extraction
128#endif
129	 be.f	Lexit_char_found	# Lexit if char found
130	cmpobne.t g1,g0,Lcase_3_cloop_at_start # branch if reached word boundary
131
132	ld	(g5),MSW	# fetch msw of operand for double shift
133
134Lcase_4:
135	shro	8,g2,g4
136	or	g4,g2,g1
137	shro	16,g1,g4
138	or	g4,g1,g4
139
140#if __i960_BIG_ENDIAN__
141	cmpobne 0,g14,Lcase_3_wloop
142
143Lcase_3_wloop2:
144	cmpi	g3,4		# less than four bytes to move?
145	 lda	4(g5),g5	# post-increment src word addr
146	mov	LSW,g1		# extract 4 bytes of src
147	 bl.f	Lcase_13_cloop_setup	# branch if < four bytes left to move
148	scanbyte g4,g1		# branch if word has char in it
149	 bo.f	Lcase_13_cloop_setup
150	mov	MSW,LSW		# move msw to lsw
151	 ld	(g5),MSW	# pre-fetch msw of operand for double shift
152	subi	4,g3,g3		# decrease max_byte count by the 4 bytes moved
153	 st	g1,(g0)		# store 4 bytes to dest
154	addo	4,g0,g0		# post-increment dest ptr
155	 b	Lcase_3_wloop2
156#endif
157
158Lcase_3_wloop:
159	cmpi	g3,4		# less than four bytes to move?
160	 lda	4(g5),g5	# post-increment src word addr
161	eshro	g14,g6,g1	# extract 4 bytes of src
162	 bl.f	Lcase_13_cloop_setup	# branch if < four bytes left to move
163	scanbyte g4,g1		# branch if word has char in it
164	 bo.f	Lcase_13_cloop_setup
165	mov	MSW,LSW		# move msw to lsw
166	 ld	(g5),MSW	# pre-fetch msw of operand for double shift
167	subi	4,g3,g3		# decrease max_byte count by the 4 bytes moved
168	 st	g1,(g0)		# store 4 bytes to dest
169	addo	4,g0,g0		# post-increment dest ptr
170	 b	Lcase_3_wloop
171
172Lcase_1_setup:
173	subo	4,g0,g0		# store is pre-incrementing;  back up dest addr
174	shro	8,g2,g4
175	or	g4,g2,MSW
176	shro	16,MSW,g4
177	or	g4,MSW,g4
178	 b	Lcase_1
179Lcase_1_wloop:			# word copying loop
180	subi	4,g3,g3		# decrease max_byte count by the 4 bytes moved
181	 ld	(g5),LSW	# pre-fetch next word of src
182	addo	4,g5,g5		# post-increment src addr
183	 st	g1,(g0)		# store word in dest string
184Lcase_1:				# src and dest are word aligned
185	cmpi	g3,4		# check for fewer than four bytes to move
186	addo	4,g0,g0		# pre-increment dest addr
187	 lda	(LSW),g1	# keep a copy of the src word
188	 bl.f	Lcase_13_cloop_setup	# branch if less than four bytes to copy
189	scanbyte LSW,g4		# branch if char is not in foursome
190	bno.t	Lcase_1_wloop
191Lcase_13_cloop_setup:
192	cmpibe.f 0,g3,Lexit_char_not_found	# Lexit if max_bytes is exhausted
193
194Lcase_1_cloop:
195#if __i960_BIG_ENDIAN__
196	rotate	8,g1,g1		# move next byte into position for extraction
197#endif
198	shlo	24,g1,g4
199	 stob	g1,(g0)		# store the byte in dest
200	cmpo	g4,g2
201	 lda	1(g0),g0	# post-increment dest byte addr
202	subi	1,g3,g3
203	 be.f	Lexit_char_found	# Lexit if char reached
204	cmpi	0,g3
205#if ! __i960_BIG_ENDIAN__
206	shro	8,g1,g1		# move next byte into position for extraction
207#endif
208	 bne.t	Lcase_1_cloop	# continue if len not exhausted
209
210Lexit_char_not_found:
211	mov	0,g0
212Lexit_char_found:
213	lda	0,g14
214	bx	(g13)		# g0 = dest array address;  g14 = 0
215Lrett:
216	ret
217
218
219Lcase_245:
220	cmpo	g0,g4		# check alignment of dest
221	 ld	(g5),MSW	# pre-fetch second half
222	and	3,g1,g1		# compute shift count
223	shlo	3,g1,g14
224#if __i960_BIG_ENDIAN__
225	subo	g14,0,g14	# adjust shift count for big endian
226#endif
227	 be.t	Lcase_4		# branch if dest is word aligned
228	or	g4,g1,g1	# is src earlier in word, later, or sync w/ dst
229	cmpo    g0,g1		# < indicates first word of dest has more bytes
230				/* than first word of source. */
231	eshro	g14,g6,g4	# extract four bytes
232	 lda	4(g0),g1	# move dest word addr to first word boundary
233#if __i960_BIG_ENDIAN__
234	 bge.f	1f
235#else
236	 bg.f	1f
237#endif
238	mov	MSW,LSW
239	 lda	4(g5),g5	# move src word addr to second word boundary
2401:
241	notand	g1,3,g1
242	 mov	g4,MSW
243	 b	Lcase_25
244
245/* end of memccpy */
246