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#include <picolibc.h>
32
33	.file "memccpy.s"
34#ifdef	__PIC
35	.pic
36#endif
37#ifdef	__PID
38	.pid
39#endif
40/*
41 * (c) copyright 1989,1993 Intel Corp., all rights reserved
42 */
43/*
44	procedure memccpy  (optimized assembler version for the 80960K series)
45
46	dest_addr = memccpy (dest_addr, src_addr, char, len)
47
48	copy len bytes pointed to by src_addr to the space pointed to by
49        dest_addr, stopping if char is copied.  If char is copied,
50        return address of byte after char in dest string;  else null.
51
52
53	Undefined behavior will occur if the end of the source array is in
54	the last two words of the program's allocated memory space.  This
55	is so because the routine fetches ahead.  Disallowing the fetch
56	ahead would impose a severe performance penalty.
57
58        Undefined behavior will also occur if the source and destination
59        strings overlap.
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) 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*/
85
86	.globl _memccpy
87	.leafproc _memccpy, __memccpy
88	.align    2
89_memccpy:
90#ifndef __PIC
91 	lda	Lrett,g14
92#else
93 	lda	Lrett-(.+8)(ip),g14
94#endif
95__memccpy:
96	mov	g14, g13	# preserve return address
97	cmpibge	0,g3,Lexit_char_not_found
98
99	addo	g3,g1,g3	# compute beyond end of src
100	ld	(g1), g7	# fetch first word of source
101	lda	0xff,g5		# mask for char
102	and	g5,g2,g2	# extract only char
103	shlo	8,g2,g6
104	or	g2,g6,g6
105	shlo	16,g6,g4
106	or	g6,g4,g6	# word of char
107	b	Lwloop_b
108
109Lwloop_a:
110	ld	(g1), g7	# fetch ahead next word of source
111	st	g4, (g0)	# store word to dest
112	addo	4, g0, g0	# post-increment dest pointer
113Lwloop_b:			# word copying loop
114	addo	4, g1, g1	# pre-increment src pointer
115	cmpo	g3, g1		# is len <= 3 ?
116	mov	g7, g4		# keep a copy of the current word
117	bl	Lcloop_setup	# quit word loop if less than 4 bytes
118	scanbyte g6, g7		# check for char
119	bno	Lwloop_a		# continue word loop if char not found.
120
121Lcloop_setup:
122	subo	4, g1, g1	# back down src pointer
123	cmpobe	g1, g3, Lexit_char_not_found
124
125Lcloop_a:			# character copying loop (len < 3)
126	and	g5,g4,g7	# check the byte against char
127	cmpo	g7,g2
128	stob	g7,(g0)		# store the byte
129	addo	1, g0, g0
130	be	Lexit_char_found
131	addo	1,g1,g1
132	cmpo	g1,g3
133	shro	8,g4,g4		# position next byte
134	bne	Lcloop_a
135
136Lexit_char_not_found:
137	mov	0, g0
138Lexit_char_found:
139	lda	0,g14
140	bx	(g13)		# g0 = dest array address; g14 = 0
141Lrett:
142	ret
143
144
145/* end of memccpy */
146