18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* bit search implementation 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Copied from lib/find_bit.c to tools/lib/find_bit.c 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved. 78c2ecf20Sopenharmony_ci * Written by David Howells (dhowells@redhat.com) 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * Copyright (C) 2008 IBM Corporation 108c2ecf20Sopenharmony_ci * 'find_last_bit' is written by Rusty Russell <rusty@rustcorp.com.au> 118c2ecf20Sopenharmony_ci * (Inspired by David Howell's find_next_bit implementation) 128c2ecf20Sopenharmony_ci * 138c2ecf20Sopenharmony_ci * Rewritten by Yury Norov <yury.norov@gmail.com> to decrease 148c2ecf20Sopenharmony_ci * size and improve performance, 2015. 158c2ecf20Sopenharmony_ci */ 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#include <linux/bitops.h> 188c2ecf20Sopenharmony_ci#include <linux/bitmap.h> 198c2ecf20Sopenharmony_ci#include <linux/kernel.h> 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci#if !defined(find_next_bit) || !defined(find_next_zero_bit) || \ 228c2ecf20Sopenharmony_ci !defined(find_next_and_bit) 238c2ecf20Sopenharmony_ci 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 inline 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) 348c2ecf20Sopenharmony_ci{ 358c2ecf20Sopenharmony_ci unsigned long tmp; 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 tmp &= BITMAP_FIRST_WORD_MASK(start); 478c2ecf20Sopenharmony_ci start = round_down(start, BITS_PER_LONG); 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci while (!tmp) { 508c2ecf20Sopenharmony_ci start += BITS_PER_LONG; 518c2ecf20Sopenharmony_ci if (start >= nbits) 528c2ecf20Sopenharmony_ci return nbits; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci tmp = addr1[start / BITS_PER_LONG]; 558c2ecf20Sopenharmony_ci if (addr2) 568c2ecf20Sopenharmony_ci tmp &= addr2[start / BITS_PER_LONG]; 578c2ecf20Sopenharmony_ci tmp ^= invert; 588c2ecf20Sopenharmony_ci } 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci return min(start + __ffs(tmp), nbits); 618c2ecf20Sopenharmony_ci} 628c2ecf20Sopenharmony_ci#endif 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci#ifndef find_next_bit 658c2ecf20Sopenharmony_ci/* 668c2ecf20Sopenharmony_ci * Find the next set bit in a memory region. 678c2ecf20Sopenharmony_ci */ 688c2ecf20Sopenharmony_ciunsigned long find_next_bit(const unsigned long *addr, unsigned long size, 698c2ecf20Sopenharmony_ci unsigned long offset) 708c2ecf20Sopenharmony_ci{ 718c2ecf20Sopenharmony_ci return _find_next_bit(addr, NULL, size, offset, 0UL); 728c2ecf20Sopenharmony_ci} 738c2ecf20Sopenharmony_ci#endif 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci#ifndef find_first_bit 768c2ecf20Sopenharmony_ci/* 778c2ecf20Sopenharmony_ci * Find the first set bit in a memory region. 788c2ecf20Sopenharmony_ci */ 798c2ecf20Sopenharmony_ciunsigned long find_first_bit(const unsigned long *addr, unsigned long size) 808c2ecf20Sopenharmony_ci{ 818c2ecf20Sopenharmony_ci unsigned long idx; 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci for (idx = 0; idx * BITS_PER_LONG < size; idx++) { 848c2ecf20Sopenharmony_ci if (addr[idx]) 858c2ecf20Sopenharmony_ci return min(idx * BITS_PER_LONG + __ffs(addr[idx]), size); 868c2ecf20Sopenharmony_ci } 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci return size; 898c2ecf20Sopenharmony_ci} 908c2ecf20Sopenharmony_ci#endif 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci#ifndef find_first_zero_bit 938c2ecf20Sopenharmony_ci/* 948c2ecf20Sopenharmony_ci * Find the first cleared bit in a memory region. 958c2ecf20Sopenharmony_ci */ 968c2ecf20Sopenharmony_ciunsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size) 978c2ecf20Sopenharmony_ci{ 988c2ecf20Sopenharmony_ci unsigned long idx; 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci for (idx = 0; idx * BITS_PER_LONG < size; idx++) { 1018c2ecf20Sopenharmony_ci if (addr[idx] != ~0UL) 1028c2ecf20Sopenharmony_ci return min(idx * BITS_PER_LONG + ffz(addr[idx]), size); 1038c2ecf20Sopenharmony_ci } 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci return size; 1068c2ecf20Sopenharmony_ci} 1078c2ecf20Sopenharmony_ci#endif 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci#ifndef find_next_zero_bit 1108c2ecf20Sopenharmony_ciunsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size, 1118c2ecf20Sopenharmony_ci unsigned long offset) 1128c2ecf20Sopenharmony_ci{ 1138c2ecf20Sopenharmony_ci return _find_next_bit(addr, NULL, size, offset, ~0UL); 1148c2ecf20Sopenharmony_ci} 1158c2ecf20Sopenharmony_ci#endif 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci#ifndef find_next_and_bit 1188c2ecf20Sopenharmony_ciunsigned long find_next_and_bit(const unsigned long *addr1, 1198c2ecf20Sopenharmony_ci const unsigned long *addr2, unsigned long size, 1208c2ecf20Sopenharmony_ci unsigned long offset) 1218c2ecf20Sopenharmony_ci{ 1228c2ecf20Sopenharmony_ci return _find_next_bit(addr1, addr2, size, offset, 0UL); 1238c2ecf20Sopenharmony_ci} 1248c2ecf20Sopenharmony_ci#endif 125