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 "memccpy.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	procedure memccpy  (optimized assembler version for the 80960K series)
43
44	dest_addr = memccpy (dest_addr, src_addr, char, len)
45
46	copy len bytes pointed to by src_addr to the space pointed to by
47        dest_addr, stopping if char is copied.  If char is copied,
48        return address of byte after char in dest string;  else null.
49
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	Strategy:
60
61	Fetch the source array by words and store them by words to the
62	destination array, until there are fewer than three bytes left
63	to copy.  Then, using the last word of the source (the one that
64	contains the remaining 0, 1, 2, or 3 bytes to be copied), store
65	a byte at a time until Ldone.
66
67	Tactics:
68
69	1) Do NOT try to fetch and store the words in a word aligned manner
70	because, in my judgement, the performance degradation experienced due
71	to non-aligned accesses does NOT outweigh the time and complexity added
72	by the preamble and convoluted body that would be necessary to assure
73	alignment.  This is supported by the intuition that most source and
74	destination arrays (even more true of most big source arrays) will
75	be word aligned to begin with.
76
77	2) Rather than decrementing len to zero,
78	I calculate the address of the byte after the last byte of the
79	destination array, and quit when the destination byte pointer passes
80	that.
81
82*/
83
84	.globl _memccpy
85	.leafproc _memccpy, __memccpy
86	.align    2
87_memccpy:
88#ifndef __PIC
89 	lda	Lrett,g14
90#else
91 	lda	Lrett-(.+8)(ip),g14
92#endif
93__memccpy:
94	mov	g14, g13	# preserve return address
95	cmpibge	0,g3,Lexit_char_not_found
96
97	addo	g3,g1,g3	# compute beyond end of src
98	ld	(g1), g7	# fetch first word of source
99	lda	0xff,g5		# mask for char
100	and	g5,g2,g2	# extract only char
101	shlo	8,g2,g6
102	or	g2,g6,g6
103	shlo	16,g6,g4
104	or	g6,g4,g6	# word of char
105	b	Lwloop_b
106
107Lwloop_a:
108	ld	(g1), g7	# fetch ahead next word of source
109	st	g4, (g0)	# store word to dest
110	addo	4, g0, g0	# post-increment dest pointer
111Lwloop_b:			# word copying loop
112	addo	4, g1, g1	# pre-increment src pointer
113	cmpo	g3, g1		# is len <= 3 ?
114	mov	g7, g4		# keep a copy of the current word
115	bl	Lcloop_setup	# quit word loop if less than 4 bytes
116	scanbyte g6, g7		# check for char
117	bno	Lwloop_a		# continue word loop if char not found.
118
119Lcloop_setup:
120	subo	4, g1, g1	# back down src pointer
121	cmpobe	g1, g3, Lexit_char_not_found
122
123Lcloop_a:			# character copying loop (len < 3)
124	and	g5,g4,g7	# check the byte against char
125	cmpo	g7,g2
126	stob	g7,(g0)		# store the byte
127	addo	1, g0, g0
128	be	Lexit_char_found
129	addo	1,g1,g1
130	cmpo	g1,g3
131	shro	8,g4,g4		# position next byte
132	bne	Lcloop_a
133
134Lexit_char_not_found:
135	mov	0, g0
136Lexit_char_found:
137	lda	0,g14
138	bx	(g13)		# g0 = dest array address; g14 = 0
139Lrett:
140	ret
141
142
143/* end of memccpy */
144