1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * linux/arch/arm/lib/findbit.S
5 * Copyright (C) 1995-2000 Russell King
7 * 16th March 2001 - John Ripley <jripley@sonicblue.com>
8 * Fixed so that "size" is an exclusive not an inclusive quantity.
9 * All users of these functions expect exclusive sizes, and may
10 * also call with zero size.
13 #include <linux/linkage.h>
14 #include <asm/assembler.h>
15 #include <asm/unwind.h>
19 #define SWAB_ENDIAN le
21 #define SWAB_ENDIAN be
24 .macro find_first, endian, set, name
25 ENTRY(_find_first_\name\()bit_\endian)
32 mvns r3, r3 @ invert/test bits
34 movs r3, r3 @ test bits
36 .ifc \endian, SWAB_ENDIAN
39 bne .L_found @ found the bit?
41 add r2, r2, #32 @ next index
42 2: cmp r2, r1 @ any more?
44 3: mov r0, r1 @ no more bits
47 ENDPROC(_find_first_\name\()bit_\endian)
50 .macro find_next, endian, set, name
51 ENTRY(_find_next_\name\()bit_\endian)
55 mov ip, r2, lsr #5 @ word index
56 add r0, r0, ip, lsl #2
57 ands ip, r2, #31 @ bit position
61 mvn r3, r3 @ invert bits
63 .ifc \endian, SWAB_ENDIAN
66 @ we need to recompute ip because rev_l will have overwritten
68 and ip, r2, #31 @ bit position
71 movs r3, r3, lsr ip @ shift off unused bits
73 orr r2, r2, #31 @ no zero bits
74 add r2, r2, #1 @ align bit pointer
75 b 2b @ loop for next bit
77 ENDPROC(_find_next_\name\()bit_\endian)
80 .macro find_bit, endian, set, name
81 find_first \endian, \set, \name
82 find_next \endian, \set, \name
85 /* _find_first_zero_bit_le and _find_next_zero_bit_le */
88 /* _find_first_bit_le and _find_next_bit_le */
93 /* _find_first_zero_bit_be and _find_next_zero_bit_be */
96 /* _find_first_bit_be and _find_next_bit_be */
102 * One or more bits in the LSB of r3 are assumed to be set.
108 #if __LINUX_ARM_ARCH__ >= 7
109 rbit r3, r3 @ reverse bits
110 clz r3, r3 @ count high zero bits
111 add r0, r2, r3 @ add offset of first set bit
112 #elif __LINUX_ARM_ARCH__ >= 5
114 and r3, r3, r0 @ mask out lowest bit set
115 clz r3, r3 @ count high zero bits
116 rsb r3, r3, #31 @ offset of first set bit
117 add r0, r2, r3 @ add offset of first set bit
120 tst r3, ip, lsr #16 @ test bits 0-15
122 moveq r3, r3, lsr #16
136 cmp r1, r0 @ Clamp to maxbit