162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/* bit search implementation
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Copied from lib/find_bit.c to tools/lib/find_bit.c
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
762306a36Sopenharmony_ci * Written by David Howells (dhowells@redhat.com)
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * Copyright (C) 2008 IBM Corporation
1062306a36Sopenharmony_ci * 'find_last_bit' is written by Rusty Russell <rusty@rustcorp.com.au>
1162306a36Sopenharmony_ci * (Inspired by David Howell's find_next_bit implementation)
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * Rewritten by Yury Norov <yury.norov@gmail.com> to decrease
1462306a36Sopenharmony_ci * size and improve performance, 2015.
1562306a36Sopenharmony_ci */
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#include <linux/bitops.h>
1862306a36Sopenharmony_ci#include <linux/bitmap.h>
1962306a36Sopenharmony_ci#include <linux/kernel.h>
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci/*
2262306a36Sopenharmony_ci * Common helper for find_bit() function family
2362306a36Sopenharmony_ci * @FETCH: The expression that fetches and pre-processes each word of bitmap(s)
2462306a36Sopenharmony_ci * @MUNGE: The expression that post-processes a word containing found bit (may be empty)
2562306a36Sopenharmony_ci * @size: The bitmap size in bits
2662306a36Sopenharmony_ci */
2762306a36Sopenharmony_ci#define FIND_FIRST_BIT(FETCH, MUNGE, size)					\
2862306a36Sopenharmony_ci({										\
2962306a36Sopenharmony_ci	unsigned long idx, val, sz = (size);					\
3062306a36Sopenharmony_ci										\
3162306a36Sopenharmony_ci	for (idx = 0; idx * BITS_PER_LONG < sz; idx++) {			\
3262306a36Sopenharmony_ci		val = (FETCH);							\
3362306a36Sopenharmony_ci		if (val) {							\
3462306a36Sopenharmony_ci			sz = min(idx * BITS_PER_LONG + __ffs(MUNGE(val)), sz);	\
3562306a36Sopenharmony_ci			break;							\
3662306a36Sopenharmony_ci		}								\
3762306a36Sopenharmony_ci	}									\
3862306a36Sopenharmony_ci										\
3962306a36Sopenharmony_ci	sz;									\
4062306a36Sopenharmony_ci})
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci/*
4362306a36Sopenharmony_ci * Common helper for find_next_bit() function family
4462306a36Sopenharmony_ci * @FETCH: The expression that fetches and pre-processes each word of bitmap(s)
4562306a36Sopenharmony_ci * @MUNGE: The expression that post-processes a word containing found bit (may be empty)
4662306a36Sopenharmony_ci * @size: The bitmap size in bits
4762306a36Sopenharmony_ci * @start: The bitnumber to start searching at
4862306a36Sopenharmony_ci */
4962306a36Sopenharmony_ci#define FIND_NEXT_BIT(FETCH, MUNGE, size, start)				\
5062306a36Sopenharmony_ci({										\
5162306a36Sopenharmony_ci	unsigned long mask, idx, tmp, sz = (size), __start = (start);		\
5262306a36Sopenharmony_ci										\
5362306a36Sopenharmony_ci	if (unlikely(__start >= sz))						\
5462306a36Sopenharmony_ci		goto out;							\
5562306a36Sopenharmony_ci										\
5662306a36Sopenharmony_ci	mask = MUNGE(BITMAP_FIRST_WORD_MASK(__start));				\
5762306a36Sopenharmony_ci	idx = __start / BITS_PER_LONG;						\
5862306a36Sopenharmony_ci										\
5962306a36Sopenharmony_ci	for (tmp = (FETCH) & mask; !tmp; tmp = (FETCH)) {			\
6062306a36Sopenharmony_ci		if ((idx + 1) * BITS_PER_LONG >= sz)				\
6162306a36Sopenharmony_ci			goto out;						\
6262306a36Sopenharmony_ci		idx++;								\
6362306a36Sopenharmony_ci	}									\
6462306a36Sopenharmony_ci										\
6562306a36Sopenharmony_ci	sz = min(idx * BITS_PER_LONG + __ffs(MUNGE(tmp)), sz);			\
6662306a36Sopenharmony_ciout:										\
6762306a36Sopenharmony_ci	sz;									\
6862306a36Sopenharmony_ci})
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci#ifndef find_first_bit
7162306a36Sopenharmony_ci/*
7262306a36Sopenharmony_ci * Find the first set bit in a memory region.
7362306a36Sopenharmony_ci */
7462306a36Sopenharmony_ciunsigned long _find_first_bit(const unsigned long *addr, unsigned long size)
7562306a36Sopenharmony_ci{
7662306a36Sopenharmony_ci	return FIND_FIRST_BIT(addr[idx], /* nop */, size);
7762306a36Sopenharmony_ci}
7862306a36Sopenharmony_ci#endif
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci#ifndef find_first_and_bit
8162306a36Sopenharmony_ci/*
8262306a36Sopenharmony_ci * Find the first set bit in two memory regions.
8362306a36Sopenharmony_ci */
8462306a36Sopenharmony_ciunsigned long _find_first_and_bit(const unsigned long *addr1,
8562306a36Sopenharmony_ci				  const unsigned long *addr2,
8662306a36Sopenharmony_ci				  unsigned long size)
8762306a36Sopenharmony_ci{
8862306a36Sopenharmony_ci	return FIND_FIRST_BIT(addr1[idx] & addr2[idx], /* nop */, size);
8962306a36Sopenharmony_ci}
9062306a36Sopenharmony_ci#endif
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci#ifndef find_first_zero_bit
9362306a36Sopenharmony_ci/*
9462306a36Sopenharmony_ci * Find the first cleared bit in a memory region.
9562306a36Sopenharmony_ci */
9662306a36Sopenharmony_ciunsigned long _find_first_zero_bit(const unsigned long *addr, unsigned long size)
9762306a36Sopenharmony_ci{
9862306a36Sopenharmony_ci	return FIND_FIRST_BIT(~addr[idx], /* nop */, size);
9962306a36Sopenharmony_ci}
10062306a36Sopenharmony_ci#endif
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci#ifndef find_next_bit
10362306a36Sopenharmony_ciunsigned long _find_next_bit(const unsigned long *addr, unsigned long nbits, unsigned long start)
10462306a36Sopenharmony_ci{
10562306a36Sopenharmony_ci	return FIND_NEXT_BIT(addr[idx], /* nop */, nbits, start);
10662306a36Sopenharmony_ci}
10762306a36Sopenharmony_ci#endif
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci#ifndef find_next_and_bit
11062306a36Sopenharmony_ciunsigned long _find_next_and_bit(const unsigned long *addr1, const unsigned long *addr2,
11162306a36Sopenharmony_ci					unsigned long nbits, unsigned long start)
11262306a36Sopenharmony_ci{
11362306a36Sopenharmony_ci	return FIND_NEXT_BIT(addr1[idx] & addr2[idx], /* nop */, nbits, start);
11462306a36Sopenharmony_ci}
11562306a36Sopenharmony_ci#endif
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci#ifndef find_next_zero_bit
11862306a36Sopenharmony_ciunsigned long _find_next_zero_bit(const unsigned long *addr, unsigned long nbits,
11962306a36Sopenharmony_ci					 unsigned long start)
12062306a36Sopenharmony_ci{
12162306a36Sopenharmony_ci	return FIND_NEXT_BIT(~addr[idx], /* nop */, nbits, start);
12262306a36Sopenharmony_ci}
12362306a36Sopenharmony_ci#endif
124