1/* Copyright (c) 2002, 2007 Marek Michalkiewicz
2   Copyright (c) 2007  Dmitry Xmelkov
3   All rights reserved.
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions are met:
7
8   * Redistributions of source code must retain the above copyright
9     notice, this list of conditions and the following disclaimer.
10   * Redistributions in binary form must reproduce the above copyright
11     notice, this list of conditions and the following disclaimer in
12     the documentation and/or other materials provided with the
13     distribution.
14   * Neither the name of the copyright holders nor the names of
15     contributors may be used to endorse or promote products derived
16     from this software without specific prior written permission.
17
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  POSSIBILITY OF SUCH DAMAGE. */
29
30/* $Id: memchr_P.S 2503 2016-02-07 22:59:47Z joerg_wunsch $ */
31
32#if !defined(__AVR_TINY__)
33
34#if !defined(__DOXYGEN__)
35
36#include "macros.inc"
37
38#define src_hi	r25
39#define src_lo	r24
40#define val	r22
41#define len_hi	r21
42#define len_lo	r20
43
44#define ret_hi	r25
45#define ret_lo	r24
46
47	ASSEMBLY_CLIB_SECTION
48	.global	_U(memchr_P)
49	.type	_U(memchr_P), @function
50_U(memchr_P):
51	X_movw	ZL, src_lo
52
53#if  __AVR_HAVE_LPMX__
541:	subi	len_lo, lo8(1)
55	sbci	len_hi, hi8(1)
56	brlo	2f
57	lpm	r0, Z+
58	cp	r0, val
59	brne	1b
60  ; OK, val is founded
61	sbiw	ZL, 1
62	X_movw	ret_lo, ZL
63	ret
64  ; fault, val is't founded
652:	clr	ret_lo
66	clr	ret_hi
67	ret
68#else
69	rjmp	2f
70
711:	lpm
72	cp	r0, val
73	breq	3f
74	adiw	ZL, 1
752:	subi	len_lo, lo8(1)
76	sbci	len_hi, hi8(1)
77	brsh	1b
78
79	clr	ZL
80	clr	ZH
813:	X_movw	ret_lo, ZL	; practicaly, this will be 2 movs
82	ret
83#endif
84
85	.size	_U(memchr_P), . - _U(memchr_P)
86#endif /* not __DOXYGEN__ */
87
88#endif /* !defined(__AVR_TINY__) */
89