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