1/* Copyright (c) 2005, 2007 Dmitry Xmelkov
2   All rights reserved.
3
4   Redistribution and use in source and binary forms, with or without
5   modification, are permitted provided that the following conditions are met:
6
7   * Redistributions of source code must retain the above copyright
8     notice, this list of conditions and the following disclaimer.
9   * Redistributions in binary form must reproduce the above copyright
10     notice, this list of conditions and the following disclaimer in
11     the documentation and/or other materials provided with the
12     distribution.
13   * Neither the name of the copyright holders nor the names of
14     contributors may be used to endorse or promote products derived
15     from this software without specific prior written permission.
16
17  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  POSSIBILITY OF SUCH DAMAGE. */
28
29/* $Id: ffsll.S 2191 2010-11-05 13:45:57Z arcanum $ */
30
31#if !defined(__AVR_TINY__)
32
33/** \file */
34
35/** \ingroup avr_string
36    \fn int ffsll (long long val);
37
38    \brief Same as ffs(), for an argument of type long long.	*/
39
40#ifndef	__DOXYGEN__
41
42#include "macros.inc"
43
44#define	val_0	r18
45#define	val_8	r19
46#define	val_16	r20
47#define	val_24	r21
48#define	val_32	r22
49#define	val_40	r23
50#define	val_48	r24
51#define	val_56	r25
52
53#define	wrd_lo	r30
54#define	wrd_hi	r31
55#define	cntr	r18
56
57    ASSEMBLY_CLIB_SECTION
58    .global	_U(ffsll)
59    .type	_U(ffsll), @function
60_U(ffsll):
61
62	X_movw	wrd_lo, val_0
63	ldi	cntr, 1
64	sbiw	wrd_lo, 0
65	brne	1f
66	X_movw	wrd_lo, val_16
67	subi	cntr, -16
68	sbiw	wrd_lo, 0
69	brne	1f
70	X_movw	wrd_lo, val_32
71	subi	cntr, -16
72	sbiw	wrd_lo, 0
73	brne	1f
74	X_movw	wrd_lo, val_48
75	subi	cntr, -16
76	sbiw	wrd_lo, 0
77	breq	9f
78
791:	tst	wrd_lo
80	brne	3f
81	subi	cntr, -7
82	mov	wrd_lo, wrd_hi
832:	inc	cntr
843:	ror	wrd_lo
85	brcc	2b
86
87	mov	r24, cntr
88	clr	r25
899:	ret
90
91    .size  _U(ffsll), . - _U(ffsll)
92    .end
93
94#endif	/* not __DOXYGEN__ */
95
96#endif /* !defined(__AVR_TINY__) */
97