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 "memchr_ca.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 memchr  (optimized assembler version for the CA)
46
47	src_addr = memchr (src_addr, char, max_bytes)
48
49	searching from src_addr for max_bytes bytes, return a pointer to the
50	first byte that contains the indicated byte in the source string.
51	Return null if the byte is not found.
52
53	Undefined behavior will occur if the end of the source string (i.e.
54	the terminating null byte) is in the last two words of the program's
55	allocated memory space.  This is so because, in several cases, memchr
56	will fetch ahead.  Disallowing the fetch ahead would impose a severe
57	performance penalty.
58
59	This program handles two cases:
60
61	1) the argument starts on a word boundary
62	2) the argument doesn't start on a word boundary
63
64	At the time of this writing, only g0 thru g7 and g13 are available
65	for use in this leafproc;  other registers would have to be saved and
66	restored.  These nine registers, plus tricky use of g14 are sufficient
67	to implement the routine.  The registers are used as follows:
68
69	g0  src ptr;  upon return it is a pointer to the matching byte, or null
70	g1  char to seek
71	g2  maximum number of bytes to check
72	g3  char to seek, broadcast to all four bytes
73	g4  word of the source string
74	g5  copy of the word
75	g6  mask to avoid unimportant bytes in first word
76	g7  byte extraction mask
77	g13 return address
78	g14
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	 lda	0,g14		# conform to register linkage standard
97	cmpibge.f 0,g2,Lnot_found	# do nothing if max_bytes <= 0
98	addo	g0,g2,g2	# compute ending address from start and len
99	and	g0,3,g6		# extract byte offset of src
100	notand	g0,3,g0		# extract word addr of start of src
101	shlo	8,g1,g3		# broadcast the char to four bytes
102	 ld	(g0),g4		# fetch word containing at least first byte
103	or	g1,g3,g3
104	shlo	16,g3,g5
105	cmpo	g1,g7		# is char being sought 0xff?
106	or	g5,g3,g3
107	shlo	3,g6,g6		# get shift count for making mask for first word
108	subi	1,0,g5		# mask initially all ones
109#if __i960_BIG_ENDIAN__
110	shro	g6,g5,g5	# get mask for bytes needed from first word
111#else
112	shlo	g6,g5,g5	# get mask for bytes needed from first word
113#endif
114	notor	g4,g5,g4	# set unneeded bytes to all ones
115	 be.f	Lsearch_for_0xff	# branch if seeking 0xff
116
117Lsearch_for_word_with_char:
118	scanbyte g3,g4		# check for byte with char
119	 lda	4(g0),g0	# pre-increment src word pointer
120	mov	g4,g5		# keep a copy of word
121	ld	(g0),g4		# fetch next word of src
122	 bo.f	Lsearch_for_char	# branch if null found
123	cmpoble.t g0,g2,Lsearch_for_word_with_char	# branch if not null
124
125Lnot_found:
126	mov	0,g0		# char not found.  Return null
127Lexit_code:
128	bx	(g13)		# g0 = addr of char in src (or null);  g14 = 0
129Lrett:
130	ret
131
132Lsearch_for_char:
133	subo	4,g0,g0		# back up the byte pointer
134Lsearch_for_char.a:
135	cmpobe.f g0,g2,Lnot_found	# quit if max_bytes exhausted
136#if __i960_BIG_ENDIAN__
137	rotate	8,g5,g5		# shift word to position next byte
138#endif
139	and	g5,g7,g6	# extract byte
140	cmpo	g1,g6		# is it char?
141	 lda	1(g0),g0	# bump src byte ptr
142#if ! __i960_BIG_ENDIAN__
143	shro	8,g5,g5		# shift word to position next byte
144#endif
145	 bne.t	Lsearch_for_char.a
146	subo	1,g0,g0		# back up the byte pointer
147	 b	Lexit_code
148
149Lsearch_for_0xff:
150	lda	0xf0f0f0f0,g6	# make first comparison mask for char=-1 case.
151	or	g6,g5,g6
152	and	g4,g6,g4	# make unimportant bytes of first word 0x0f
153	 b	Lsearch_for_word_with_char
154
155/* end of memchr */
156