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 "strchr.s"
34#ifdef	__PIC
35	.pic
36#endif
37#ifdef	__PID
38	.pid
39#endif
40/*
41 * (c) copyright 1988,1993 Intel Corp., all rights reserved
42 */
43
44/*
45	procedure strchr  (optimized assembler version for the 80960K series)
46
47	src_addr = strchr (src_addr, char)
48
49	return a pointer to the first byte that contains the indicated
50	byte in the source string.  Return null if the byte is not found.
51
52	Undefined behavior will occur if the end of the source string (i.e.
53	the terminating null byte) is in the last two words of the program's
54	allocated memory space.  This is so because strchr fetches ahead.
55	Disallowing the fetch ahead would impose a severe performance penalty.
56
57	Strategy:
58
59	Fetch the source string by words and scanbyte the words for the
60	char until either a word with the byte is found or the null byte is
61	encountered.  In the former case, move through the word to find the
62	matching byte and return its memory address.  In the latter case,
63	return zero (null).
64
65	Tactics:
66
67	1) Do NOT try to fetch the words in a word aligned manner because,
68	in my judgement, the performance degradation experienced due to
69	non-aligned accesses does NOT outweigh the time and complexity added
70	by the preamble that would be necessary to assure alignment.  This
71	is supported by the intuition that most source arrays (even more
72	true of most big source arrays) will be word aligned to begin with.
73*/
74
75	.globl	_strchr
76	.globl	__strchr
77	.leafproc	_strchr, __strchr
78	.align	2
79_strchr:
80#ifndef __PIC
81	lda 	Lrett,g14
82#else
83	lda 	Lrett-(.+8)(ip),g14
84#endif
85__strchr:
86
87	ld	(g0),g4		# fetch first word
88	lda	0xff,g7		# byte extraction mask
89	and	g1,g7,g1	# make char an 8-bit ordinal
90	shlo	8,g1,g2		# broadcast the char to four bytes
91	or	g1,g2,g2
92	shlo	16,g2,g5
93	or	g2,g5,g3
94	mov	g14,g13		# preserve return address
95	addo	4,g0,g0		# post-increment src pointer
96	mov	0,g14		# conform to register linkage standard
97
98Lsearch_for_word_with_char_or_null:
99	mov	g4,g5		# copy word
100	scanbyte g3,g5		# check for byte with char
101	ld	(g0),g4		# fetch next word of src
102	bo	Lsearch_for_char	# branch if char found
103	scanbyte 0,g5		# check for null byte
104	addo	4,g0,g0		# post-increment src pointer
105	bno	Lsearch_for_word_with_char_or_null	# branch if not null
106
107Lnot_found:
108	mov	0,g0		# char not found.  Return null
109Lexit_code:
110	bx	(g13)		# g0 = addr of char in src (or null);  g14 = 0
111Lrett:
112	ret
113
114Lsearch_for_char:
115	subo	5,g0,g0		# back up the byte pointer
116Lsearch_for_char.a:
117	and	g5,g7,g6	# extract byte
118	cmpo	g1,g6		# is it char?
119	addo	1,g0,g0		# bump src byte ptr
120	shro	8,g5,g5		# shift word to position next byte
121	be	Lexit_code
122	cmpobne 0,g6,Lsearch_for_char.a	# quit if null comes before char
123	b	Lnot_found
124
125/* end of strchr */
126