brintos

brintos / linux-shallow public Read only

0
0
Text · 4.7 KiB · 38c0a54 Raw
178 lines · c
1/* SPDX-License-Identifier: GPL-2.0 */2#ifndef _TOOLS_LINUX_FIND_H_3#define _TOOLS_LINUX_FIND_H_4 5#ifndef _TOOLS_LINUX_BITMAP_H6#error tools: only <linux/bitmap.h> can be included directly7#endif8 9#include <linux/bitops.h>10 11unsigned long _find_next_bit(const unsigned long *addr1, unsigned long nbits,12				unsigned long start);13unsigned long _find_next_and_bit(const unsigned long *addr1, const unsigned long *addr2,14					unsigned long nbits, unsigned long start);15unsigned long _find_next_zero_bit(const unsigned long *addr, unsigned long nbits,16					 unsigned long start);17extern unsigned long _find_first_bit(const unsigned long *addr, unsigned long size);18extern unsigned long _find_first_and_bit(const unsigned long *addr1,19					 const unsigned long *addr2, unsigned long size);20extern unsigned long _find_first_zero_bit(const unsigned long *addr, unsigned long size);21 22#ifndef find_next_bit23/**24 * find_next_bit - find the next set bit in a memory region25 * @addr: The address to base the search on26 * @size: The bitmap size in bits27 * @offset: The bitnumber to start searching at28 *29 * Returns the bit number for the next set bit30 * If no bits are set, returns @size.31 */32static inline33unsigned long find_next_bit(const unsigned long *addr, unsigned long size,34			    unsigned long offset)35{36	if (small_const_nbits(size)) {37		unsigned long val;38 39		if (unlikely(offset >= size))40			return size;41 42		val = *addr & GENMASK(size - 1, offset);43		return val ? __ffs(val) : size;44	}45 46	return _find_next_bit(addr, size, offset);47}48#endif49 50#ifndef find_next_and_bit51/**52 * find_next_and_bit - find the next set bit in both memory regions53 * @addr1: The first address to base the search on54 * @addr2: The second address to base the search on55 * @size: The bitmap size in bits56 * @offset: The bitnumber to start searching at57 *58 * Returns the bit number for the next set bit59 * If no bits are set, returns @size.60 */61static inline62unsigned long find_next_and_bit(const unsigned long *addr1,63		const unsigned long *addr2, unsigned long size,64		unsigned long offset)65{66	if (small_const_nbits(size)) {67		unsigned long val;68 69		if (unlikely(offset >= size))70			return size;71 72		val = *addr1 & *addr2 & GENMASK(size - 1, offset);73		return val ? __ffs(val) : size;74	}75 76	return _find_next_and_bit(addr1, addr2, size, offset);77}78#endif79 80#ifndef find_next_zero_bit81/**82 * find_next_zero_bit - find the next cleared bit in a memory region83 * @addr: The address to base the search on84 * @size: The bitmap size in bits85 * @offset: The bitnumber to start searching at86 *87 * Returns the bit number of the next zero bit88 * If no bits are zero, returns @size.89 */90static inline91unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,92				 unsigned long offset)93{94	if (small_const_nbits(size)) {95		unsigned long val;96 97		if (unlikely(offset >= size))98			return size;99 100		val = *addr | ~GENMASK(size - 1, offset);101		return val == ~0UL ? size : ffz(val);102	}103 104	return _find_next_zero_bit(addr, size, offset);105}106#endif107 108#ifndef find_first_bit109/**110 * find_first_bit - find the first set bit in a memory region111 * @addr: The address to start the search at112 * @size: The maximum number of bits to search113 *114 * Returns the bit number of the first set bit.115 * If no bits are set, returns @size.116 */117static inline118unsigned long find_first_bit(const unsigned long *addr, unsigned long size)119{120	if (small_const_nbits(size)) {121		unsigned long val = *addr & GENMASK(size - 1, 0);122 123		return val ? __ffs(val) : size;124	}125 126	return _find_first_bit(addr, size);127}128#endif129 130#ifndef find_first_and_bit131/**132 * find_first_and_bit - find the first set bit in both memory regions133 * @addr1: The first address to base the search on134 * @addr2: The second address to base the search on135 * @size: The bitmap size in bits136 *137 * Returns the bit number for the next set bit138 * If no bits are set, returns @size.139 */140static inline141unsigned long find_first_and_bit(const unsigned long *addr1,142				 const unsigned long *addr2,143				 unsigned long size)144{145	if (small_const_nbits(size)) {146		unsigned long val = *addr1 & *addr2 & GENMASK(size - 1, 0);147 148		return val ? __ffs(val) : size;149	}150 151	return _find_first_and_bit(addr1, addr2, size);152}153#endif154 155#ifndef find_first_zero_bit156/**157 * find_first_zero_bit - find the first cleared bit in a memory region158 * @addr: The address to start the search at159 * @size: The maximum number of bits to search160 *161 * Returns the bit number of the first cleared bit.162 * If no bits are zero, returns @size.163 */164static inline165unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)166{167	if (small_const_nbits(size)) {168		unsigned long val = *addr | ~GENMASK(size - 1, 0);169 170		return val == ~0UL ? size : ffz(val);171	}172 173	return _find_first_zero_bit(addr, size);174}175#endif176 177#endif /*__LINUX_FIND_H_ */178