mirror of
				https://github.com/smaeul/u-boot.git
				synced 2025-11-04 14:00:19 +00:00 
			
		
		
		
	Import for_each_set_bit() and associated macros and functions from Linux. This is useful in parsing interrupt registers and take action on each bit that is set. Signed-off-by: Vignesh Raghavendra <vigneshr@ti.com>
		
			
				
	
	
		
			85 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			85 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
// SPDX-License-Identifier: GPL-2.0+
 | 
						|
#ifndef __LINUX_BITMAP_H
 | 
						|
#define __LINUX_BITMAP_H
 | 
						|
 | 
						|
#include <asm/types.h>
 | 
						|
#include <linux/types.h>
 | 
						|
#include <linux/bitops.h>
 | 
						|
 | 
						|
#define small_const_nbits(nbits) \
 | 
						|
	(__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG)
 | 
						|
 | 
						|
static inline void bitmap_zero(unsigned long *dst, int nbits)
 | 
						|
{
 | 
						|
	if (small_const_nbits(nbits)) {
 | 
						|
		*dst = 0UL;
 | 
						|
	} else {
 | 
						|
		int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
 | 
						|
 | 
						|
		memset(dst, 0, len);
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
static inline unsigned long
 | 
						|
find_next_bit(const unsigned long *addr, unsigned long size,
 | 
						|
	      unsigned long offset)
 | 
						|
{
 | 
						|
	const unsigned long *p = addr + BIT_WORD(offset);
 | 
						|
	unsigned long result = offset & ~(BITS_PER_LONG - 1);
 | 
						|
	unsigned long tmp;
 | 
						|
 | 
						|
	if (offset >= size)
 | 
						|
		return size;
 | 
						|
	size -= result;
 | 
						|
	offset %= BITS_PER_LONG;
 | 
						|
	if (offset) {
 | 
						|
		tmp = *(p++);
 | 
						|
		tmp &= (~0UL << offset);
 | 
						|
		if (size < BITS_PER_LONG)
 | 
						|
			goto found_first;
 | 
						|
		if (tmp)
 | 
						|
			goto found_middle;
 | 
						|
		size -= BITS_PER_LONG;
 | 
						|
		result += BITS_PER_LONG;
 | 
						|
	}
 | 
						|
	while (size & ~(BITS_PER_LONG - 1)) {
 | 
						|
		tmp = *(p++);
 | 
						|
		if ((tmp))
 | 
						|
			goto found_middle;
 | 
						|
		result += BITS_PER_LONG;
 | 
						|
		size -= BITS_PER_LONG;
 | 
						|
	}
 | 
						|
	if (!size)
 | 
						|
		return result;
 | 
						|
	tmp = *p;
 | 
						|
 | 
						|
found_first:
 | 
						|
	tmp &= (~0UL >> (BITS_PER_LONG - size));
 | 
						|
	if (tmp == 0UL)		/* Are any bits set? */
 | 
						|
		return result + size;	/* Nope. */
 | 
						|
found_middle:
 | 
						|
	return result + __ffs(tmp);
 | 
						|
}
 | 
						|
 | 
						|
/*
 | 
						|
 * Find the first set bit in a memory region.
 | 
						|
 */
 | 
						|
static inline unsigned long find_first_bit(const unsigned long *addr, unsigned long size)
 | 
						|
{
 | 
						|
	unsigned long idx;
 | 
						|
 | 
						|
	for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
 | 
						|
		if (addr[idx])
 | 
						|
			return min(idx * BITS_PER_LONG + __ffs(addr[idx]), size);
 | 
						|
	}
 | 
						|
 | 
						|
	return size;
 | 
						|
}
 | 
						|
 | 
						|
#define for_each_set_bit(bit, addr, size) \
 | 
						|
	for ((bit) = find_first_bit((addr), (size));		\
 | 
						|
	     (bit) < (size);					\
 | 
						|
	     (bit) = find_next_bit((addr), (size), (bit) + 1))
 | 
						|
 | 
						|
#endif /* __LINUX_BITMAP_H */
 |