1/* Copyright (c) 2002, 2005, 2006, 2007 Reiner Patommel
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: strncasecmp_P.S 2503 2016-02-07 22:59:47Z joerg_wunsch $ */
31
32/*
33   strncasecmp_P.S
34   Case insensitive compare
35
36   Contributors:
37     Created by Reiner Patommel
38*/
39
40#if !defined(__AVR_TINY__)
41
42#if !defined(__DOXYGEN__)
43
44#include "macros.inc"
45
46#define s1_hi r25
47#define s1_lo r24
48#define s2_hi r23
49#define s2_lo r22
50#define len_hi r21
51#define len_lo r20
52
53#define tmp    r22
54#define ret_hi r25
55#define ret_lo r24
56
57	ASSEMBLY_CLIB_SECTION
58	.global	_U(strncasecmp_P)
59	.type	_U(strncasecmp_P), @function
60_U(strncasecmp_P):
61	X_movw	ZL, s2_lo
62	X_movw	XL, s1_lo
63
641:	subi	len_lo, lo8(1)		; if (--len == -1) return 0
65	sbci	len_hi, hi8(1)
66	brlo	5f
67
68	ld	ret_lo, X+		; *s1++
69	cpi	ret_lo, 'A'		; if in [A-Z] then tolower()
70	brlt	2f
71	cpi	ret_lo, 'Z'+1
72	brge	2f
73	subi	ret_lo, 'A'-'a'
74
752:	X_lpm	tmp, Z+			; *s2++
76	cpi	tmp, 'A'		; if in [A-Z] then tolower()
77	brlt	3f
78	cpi	tmp, 'Z'+1
79	brge	3f
80	subi	tmp, 'A'-'a'
81
823:	sub	ret_lo, tmp		; compare
83	cpse	tmp, __zero_reg__	; break, if end of string
84	breq	1b
854:	sbc	ret_hi, ret_hi		; sign extension
86	ret
87
885:	sub	ret_lo, ret_lo		; length limit, return 0
89	rjmp	4b
90
91	.size	_U(strncasecmp_P), . - _U(strncasecmp_P)
92
93#endif /* not __DOXYGEN__ */
94
95#endif /* !defined(__AVR_TINY__) */
96