find_bit.c 5.3 KB
Newer Older
1
/* bit search implementation
L
Linus Torvalds 已提交
2 3 4 5
 *
 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells (dhowells@redhat.com)
 *
6 7 8 9
 * Copyright (C) 2008 IBM Corporation
 * 'find_last_bit' is written by Rusty Russell <rusty@rustcorp.com.au>
 * (Inspired by David Howell's find_next_bit implementation)
 *
Y
Yury Norov 已提交
10 11 12
 * Rewritten by Yury Norov <yury.norov@gmail.com> to decrease
 * size and improve performance, 2015.
 *
L
Linus Torvalds 已提交
13 14 15 16 17 18 19
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

#include <linux/bitops.h>
20
#include <linux/bitmap.h>
21
#include <linux/export.h>
Y
Yury Norov 已提交
22
#include <linux/kernel.h>
L
Linus Torvalds 已提交
23

24 25
#if !defined(find_next_bit) || !defined(find_next_zero_bit) || \
		!defined(find_next_and_bit)
26

27
/*
28 29 30 31 32
 * This is a common helper function for find_next_bit, find_next_zero_bit, and
 * find_next_and_bit. The differences are:
 *  - The "invert" argument, which is XORed with each fetched word before
 *    searching it for one bits.
 *  - The optional "addr2", which is anded with "addr1" if present.
33
 */
34 35 36
static inline unsigned long _find_next_bit(const unsigned long *addr1,
		const unsigned long *addr2, unsigned long nbits,
		unsigned long start, unsigned long invert)
L
Linus Torvalds 已提交
37 38 39
{
	unsigned long tmp;

40
	if (unlikely(start >= nbits))
Y
Yury Norov 已提交
41 42
		return nbits;

43 44 45 46
	tmp = addr1[start / BITS_PER_LONG];
	if (addr2)
		tmp &= addr2[start / BITS_PER_LONG];
	tmp ^= invert;
Y
Yury Norov 已提交
47 48 49 50 51 52 53 54 55 56

	/* Handle 1st word. */
	tmp &= BITMAP_FIRST_WORD_MASK(start);
	start = round_down(start, BITS_PER_LONG);

	while (!tmp) {
		start += BITS_PER_LONG;
		if (start >= nbits)
			return nbits;

57 58 59 60
		tmp = addr1[start / BITS_PER_LONG];
		if (addr2)
			tmp &= addr2[start / BITS_PER_LONG];
		tmp ^= invert;
L
Linus Torvalds 已提交
61 62
	}

Y
Yury Norov 已提交
63
	return min(start + __ffs(tmp), nbits);
64
}
65
#endif
L
Linus Torvalds 已提交
66

Y
Yury Norov 已提交
67
#ifndef find_next_bit
68
/*
Y
Yury Norov 已提交
69
 * Find the next set bit in a memory region.
70
 */
Y
Yury Norov 已提交
71 72 73
unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
			    unsigned long offset)
{
74
	return _find_next_bit(addr, NULL, size, offset, 0UL);
Y
Yury Norov 已提交
75 76 77 78 79
}
EXPORT_SYMBOL(find_next_bit);
#endif

#ifndef find_next_zero_bit
T
Thomas Gleixner 已提交
80 81
unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
				 unsigned long offset)
82
{
83
	return _find_next_bit(addr, NULL, size, offset, ~0UL);
L
Linus Torvalds 已提交
84
}
T
Thomas Gleixner 已提交
85
EXPORT_SYMBOL(find_next_zero_bit);
86
#endif
87

88 89 90 91 92 93 94 95 96 97
#if !defined(find_next_and_bit)
unsigned long find_next_and_bit(const unsigned long *addr1,
		const unsigned long *addr2, unsigned long size,
		unsigned long offset)
{
	return _find_next_bit(addr1, addr2, size, offset, 0UL);
}
EXPORT_SYMBOL(find_next_and_bit);
#endif

98
#ifndef find_first_bit
99 100 101
/*
 * Find the first set bit in a memory region.
 */
T
Thomas Gleixner 已提交
102
unsigned long find_first_bit(const unsigned long *addr, unsigned long size)
103
{
Y
Yury Norov 已提交
104
	unsigned long idx;
105

Y
Yury Norov 已提交
106 107 108
	for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
		if (addr[idx])
			return min(idx * BITS_PER_LONG + __ffs(addr[idx]), size);
109 110
	}

Y
Yury Norov 已提交
111
	return size;
112
}
T
Thomas Gleixner 已提交
113
EXPORT_SYMBOL(find_first_bit);
114
#endif
115

116
#ifndef find_first_zero_bit
117 118 119
/*
 * Find the first cleared bit in a memory region.
 */
T
Thomas Gleixner 已提交
120
unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)
121
{
Y
Yury Norov 已提交
122
	unsigned long idx;
123

Y
Yury Norov 已提交
124 125 126
	for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
		if (addr[idx] != ~0UL)
			return min(idx * BITS_PER_LONG + ffz(addr[idx]), size);
127 128
	}

Y
Yury Norov 已提交
129
	return size;
130
}
T
Thomas Gleixner 已提交
131
EXPORT_SYMBOL(find_first_zero_bit);
132
#endif
133

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
#ifndef find_last_bit
unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
{
	if (size) {
		unsigned long val = BITMAP_LAST_WORD_MASK(size);
		unsigned long idx = (size-1) / BITS_PER_LONG;

		do {
			val &= addr[idx];
			if (val)
				return idx * BITS_PER_LONG + __fls(val);

			val = ~0ul;
		} while (idx--);
	}
	return size;
}
EXPORT_SYMBOL(find_last_bit);
#endif

154 155 156 157 158 159 160 161 162 163 164 165 166 167
#ifdef __BIG_ENDIAN

/* include/linux/byteorder does not support "unsigned long" type */
static inline unsigned long ext2_swab(const unsigned long y)
{
#if BITS_PER_LONG == 64
	return (unsigned long) __swab64((u64) y);
#elif BITS_PER_LONG == 32
	return (unsigned long) __swab32((u32) y);
#else
#error BITS_PER_LONG not defined
#endif
}

Y
Yury Norov 已提交
168
#if !defined(find_next_bit_le) || !defined(find_next_zero_bit_le)
169 170 171
static inline unsigned long _find_next_bit_le(const unsigned long *addr1,
		const unsigned long *addr2, unsigned long nbits,
		unsigned long start, unsigned long invert)
172 173 174
{
	unsigned long tmp;

175
	if (unlikely(start >= nbits))
Y
Yury Norov 已提交
176 177
		return nbits;

178 179 180 181
	tmp = addr1[start / BITS_PER_LONG];
	if (addr2)
		tmp &= addr2[start / BITS_PER_LONG];
	tmp ^= invert;
Y
Yury Norov 已提交
182 183 184 185

	/* Handle 1st word. */
	tmp &= ext2_swab(BITMAP_FIRST_WORD_MASK(start));
	start = round_down(start, BITS_PER_LONG);
186

Y
Yury Norov 已提交
187 188 189 190 191
	while (!tmp) {
		start += BITS_PER_LONG;
		if (start >= nbits)
			return nbits;

192 193 194 195
		tmp = addr1[start / BITS_PER_LONG];
		if (addr2)
			tmp &= addr2[start / BITS_PER_LONG];
		tmp ^= invert;
196 197
	}

Y
Yury Norov 已提交
198 199 200 201 202 203 204 205
	return min(start + __ffs(ext2_swab(tmp)), nbits);
}
#endif

#ifndef find_next_zero_bit_le
unsigned long find_next_zero_bit_le(const void *addr, unsigned
		long size, unsigned long offset)
{
206
	return _find_next_bit_le(addr, NULL, size, offset, ~0UL);
207
}
208
EXPORT_SYMBOL(find_next_zero_bit_le);
209
#endif
210

211
#ifndef find_next_bit_le
212
unsigned long find_next_bit_le(const void *addr, unsigned
213 214
		long size, unsigned long offset)
{
215
	return _find_next_bit_le(addr, NULL, size, offset, 0UL);
216
}
217
EXPORT_SYMBOL(find_next_bit_le);
218
#endif
219

220
#endif /* __BIG_ENDIAN */