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