18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/* bit search implementation
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
58c2ecf20Sopenharmony_ci * Written by David Howells (dhowells@redhat.com)
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (C) 2008 IBM Corporation
88c2ecf20Sopenharmony_ci * 'find_last_bit' is written by Rusty Russell <rusty@rustcorp.com.au>
98c2ecf20Sopenharmony_ci * (Inspired by David Howell's find_next_bit implementation)
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Rewritten by Yury Norov <yury.norov@gmail.com> to decrease
128c2ecf20Sopenharmony_ci * size and improve performance, 2015.
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/bitops.h>
168c2ecf20Sopenharmony_ci#include <linux/bitmap.h>
178c2ecf20Sopenharmony_ci#include <linux/export.h>
188c2ecf20Sopenharmony_ci#include <linux/kernel.h>
198c2ecf20Sopenharmony_ci#include <linux/minmax.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#if !defined(find_next_bit) || !defined(find_next_zero_bit) ||			\
228c2ecf20Sopenharmony_ci	!defined(find_next_bit_le) || !defined(find_next_zero_bit_le) ||	\
238c2ecf20Sopenharmony_ci	!defined(find_next_and_bit)
248c2ecf20Sopenharmony_ci/*
258c2ecf20Sopenharmony_ci * This is a common helper function for find_next_bit, find_next_zero_bit, and
268c2ecf20Sopenharmony_ci * find_next_and_bit. The differences are:
278c2ecf20Sopenharmony_ci *  - The "invert" argument, which is XORed with each fetched word before
288c2ecf20Sopenharmony_ci *    searching it for one bits.
298c2ecf20Sopenharmony_ci *  - The optional "addr2", which is anded with "addr1" if present.
308c2ecf20Sopenharmony_ci */
318c2ecf20Sopenharmony_cistatic unsigned long _find_next_bit(const unsigned long *addr1,
328c2ecf20Sopenharmony_ci		const unsigned long *addr2, unsigned long nbits,
338c2ecf20Sopenharmony_ci		unsigned long start, unsigned long invert, unsigned long le)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	unsigned long tmp, mask;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	if (unlikely(start >= nbits))
388c2ecf20Sopenharmony_ci		return nbits;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	tmp = addr1[start / BITS_PER_LONG];
418c2ecf20Sopenharmony_ci	if (addr2)
428c2ecf20Sopenharmony_ci		tmp &= addr2[start / BITS_PER_LONG];
438c2ecf20Sopenharmony_ci	tmp ^= invert;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	/* Handle 1st word. */
468c2ecf20Sopenharmony_ci	mask = BITMAP_FIRST_WORD_MASK(start);
478c2ecf20Sopenharmony_ci	if (le)
488c2ecf20Sopenharmony_ci		mask = swab(mask);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	tmp &= mask;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	start = round_down(start, BITS_PER_LONG);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	while (!tmp) {
558c2ecf20Sopenharmony_ci		start += BITS_PER_LONG;
568c2ecf20Sopenharmony_ci		if (start >= nbits)
578c2ecf20Sopenharmony_ci			return nbits;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci		tmp = addr1[start / BITS_PER_LONG];
608c2ecf20Sopenharmony_ci		if (addr2)
618c2ecf20Sopenharmony_ci			tmp &= addr2[start / BITS_PER_LONG];
628c2ecf20Sopenharmony_ci		tmp ^= invert;
638c2ecf20Sopenharmony_ci	}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	if (le)
668c2ecf20Sopenharmony_ci		tmp = swab(tmp);
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	return min(start + __ffs(tmp), nbits);
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci#endif
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci#ifndef find_next_bit
738c2ecf20Sopenharmony_ci/*
748c2ecf20Sopenharmony_ci * Find the next set bit in a memory region.
758c2ecf20Sopenharmony_ci */
768c2ecf20Sopenharmony_ciunsigned long find_next_bit(const unsigned long *addr, unsigned long size,
778c2ecf20Sopenharmony_ci			    unsigned long offset)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	return _find_next_bit(addr, NULL, size, offset, 0UL, 0);
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ciEXPORT_SYMBOL(find_next_bit);
828c2ecf20Sopenharmony_ci#endif
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci#ifndef find_next_zero_bit
858c2ecf20Sopenharmony_ciunsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
868c2ecf20Sopenharmony_ci				 unsigned long offset)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	return _find_next_bit(addr, NULL, size, offset, ~0UL, 0);
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ciEXPORT_SYMBOL(find_next_zero_bit);
918c2ecf20Sopenharmony_ci#endif
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci#if !defined(find_next_and_bit)
948c2ecf20Sopenharmony_ciunsigned long find_next_and_bit(const unsigned long *addr1,
958c2ecf20Sopenharmony_ci		const unsigned long *addr2, unsigned long size,
968c2ecf20Sopenharmony_ci		unsigned long offset)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	return _find_next_bit(addr1, addr2, size, offset, 0UL, 0);
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ciEXPORT_SYMBOL(find_next_and_bit);
1018c2ecf20Sopenharmony_ci#endif
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci#ifndef find_first_bit
1048c2ecf20Sopenharmony_ci/*
1058c2ecf20Sopenharmony_ci * Find the first set bit in a memory region.
1068c2ecf20Sopenharmony_ci */
1078c2ecf20Sopenharmony_ciunsigned long find_first_bit(const unsigned long *addr, unsigned long size)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	unsigned long idx;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
1128c2ecf20Sopenharmony_ci		if (addr[idx])
1138c2ecf20Sopenharmony_ci			return min(idx * BITS_PER_LONG + __ffs(addr[idx]), size);
1148c2ecf20Sopenharmony_ci	}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	return size;
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ciEXPORT_SYMBOL(find_first_bit);
1198c2ecf20Sopenharmony_ci#endif
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci#ifndef find_first_zero_bit
1228c2ecf20Sopenharmony_ci/*
1238c2ecf20Sopenharmony_ci * Find the first cleared bit in a memory region.
1248c2ecf20Sopenharmony_ci */
1258c2ecf20Sopenharmony_ciunsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	unsigned long idx;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
1308c2ecf20Sopenharmony_ci		if (addr[idx] != ~0UL)
1318c2ecf20Sopenharmony_ci			return min(idx * BITS_PER_LONG + ffz(addr[idx]), size);
1328c2ecf20Sopenharmony_ci	}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	return size;
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ciEXPORT_SYMBOL(find_first_zero_bit);
1378c2ecf20Sopenharmony_ci#endif
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci#ifndef find_last_bit
1408c2ecf20Sopenharmony_ciunsigned long find_last_bit(const unsigned long *addr, unsigned long size)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	if (size) {
1438c2ecf20Sopenharmony_ci		unsigned long val = BITMAP_LAST_WORD_MASK(size);
1448c2ecf20Sopenharmony_ci		unsigned long idx = (size-1) / BITS_PER_LONG;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci		do {
1478c2ecf20Sopenharmony_ci			val &= addr[idx];
1488c2ecf20Sopenharmony_ci			if (val)
1498c2ecf20Sopenharmony_ci				return idx * BITS_PER_LONG + __fls(val);
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci			val = ~0ul;
1528c2ecf20Sopenharmony_ci		} while (idx--);
1538c2ecf20Sopenharmony_ci	}
1548c2ecf20Sopenharmony_ci	return size;
1558c2ecf20Sopenharmony_ci}
1568c2ecf20Sopenharmony_ciEXPORT_SYMBOL(find_last_bit);
1578c2ecf20Sopenharmony_ci#endif
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci#ifdef __BIG_ENDIAN
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci#ifndef find_next_zero_bit_le
1628c2ecf20Sopenharmony_ciunsigned long find_next_zero_bit_le(const void *addr, unsigned
1638c2ecf20Sopenharmony_ci		long size, unsigned long offset)
1648c2ecf20Sopenharmony_ci{
1658c2ecf20Sopenharmony_ci	return _find_next_bit(addr, NULL, size, offset, ~0UL, 1);
1668c2ecf20Sopenharmony_ci}
1678c2ecf20Sopenharmony_ciEXPORT_SYMBOL(find_next_zero_bit_le);
1688c2ecf20Sopenharmony_ci#endif
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci#ifndef find_next_bit_le
1718c2ecf20Sopenharmony_ciunsigned long find_next_bit_le(const void *addr, unsigned
1728c2ecf20Sopenharmony_ci		long size, unsigned long offset)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	return _find_next_bit(addr, NULL, size, offset, 0UL, 1);
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ciEXPORT_SYMBOL(find_next_bit_le);
1778c2ecf20Sopenharmony_ci#endif
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci#endif /* __BIG_ENDIAN */
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ciunsigned long find_next_clump8(unsigned long *clump, const unsigned long *addr,
1828c2ecf20Sopenharmony_ci			       unsigned long size, unsigned long offset)
1838c2ecf20Sopenharmony_ci{
1848c2ecf20Sopenharmony_ci	offset = find_next_bit(addr, size, offset);
1858c2ecf20Sopenharmony_ci	if (offset == size)
1868c2ecf20Sopenharmony_ci		return size;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	offset = round_down(offset, 8);
1898c2ecf20Sopenharmony_ci	*clump = bitmap_get_value8(addr, offset);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	return offset;
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ciEXPORT_SYMBOL(find_next_clump8);
194