1/* Copyright (c) 2009 Nick Clifton <nickc@redhat.com> */
2#include <picolibc.h>
3
4	.file	"memchr.S"
5
6	.section .text
7
8	.global  _memchr
9	.type	 _memchr,@function
10_memchr:
11	;; R1: string pointer
12	;; R2: byte sought
13	;; R3: max number to scan
14#ifdef __RX_DISALLOW_STRING_INSNS__
15	mov.b	r2, r2		; The mov.b below sign extends as it loads, so make sure that r2 is sign-extended as well.
162:	cmp	#0, r3
17	beq	1f
18	sub	#1, r3
19	mov.b	[r1+], r5
20	cmp	r5, r2
21	bne	2b
22
23	sub	#1, r1		; We have found a match, bit now R1 points to the byte after the match.
241:	rts
25#else
26	cmp	#0, r3		; If r3 is 0 suntil.b will do nothing and not set any flags...
27	stz     #1, r1		; ...so store 1 into r1.  It will be decremented by the SUB later.
28	suntil.b    		; Search until *r1 == r2 or r3 bytes have been examined.
29	stnz	#1, r1		; If no match was found return NULL.
30	sub	#1, r1		; suntil.b leaves r1 pointing at the address *after* the match.
31	rts
32#endif
33
34	.size _memchr, . - _memchr
35
36