blob: 4a8751010d59ffb08f7bf8f25c82096ceaf12398 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Yury Norov8f6f19d2015-04-16 12:43:16 -07002/* bit search implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 *
Yury Norov8f6f19d2015-04-16 12:43:16 -07007 * Copyright (C) 2008 IBM Corporation
8 * 'find_last_bit' is written by Rusty Russell <rusty@rustcorp.com.au>
9 * (Inspired by David Howell's find_next_bit implementation)
10 *
Yury Norov2c57a0e2015-04-16 12:43:13 -070011 * Rewritten by Yury Norov <yury.norov@gmail.com> to decrease
12 * size and improve performance, 2015.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14
15#include <linux/bitops.h>
Yury Norov8f6f19d2015-04-16 12:43:16 -070016#include <linux/bitmap.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050017#include <linux/export.h>
Yury Norov2c57a0e2015-04-16 12:43:13 -070018#include <linux/kernel.h>
Andy Shevchenkob296a6d2020-10-15 20:10:21 -070019#include <linux/minmax.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Yury Norovb78c5712020-01-30 22:16:43 -080021#if !defined(find_next_bit) || !defined(find_next_zero_bit) || \
22 !defined(find_next_bit_le) || !defined(find_next_zero_bit_le) || \
23 !defined(find_next_and_bit)
Yury Norov2c57a0e2015-04-16 12:43:13 -070024/*
Clement Courbet0ade34c2018-02-06 15:38:34 -080025 * This is a common helper function for find_next_bit, find_next_zero_bit, and
26 * find_next_and_bit. The differences are:
27 * - The "invert" argument, which is XORed with each fetched word before
28 * searching it for one bits.
29 * - The optional "addr2", which is anded with "addr1" if present.
Yury Norov2c57a0e2015-04-16 12:43:13 -070030 */
Yury Norov7dfaa98f2020-01-30 22:16:47 -080031static unsigned long _find_next_bit(const unsigned long *addr1,
Clement Courbet0ade34c2018-02-06 15:38:34 -080032 const unsigned long *addr2, unsigned long nbits,
Yury Norovb78c5712020-01-30 22:16:43 -080033 unsigned long start, unsigned long invert, unsigned long le)
Yury Norov2c57a0e2015-04-16 12:43:13 -070034{
Yury Norovb78c5712020-01-30 22:16:43 -080035 unsigned long tmp, mask;
Yury Norov2c57a0e2015-04-16 12:43:13 -070036
Matthew Wilcoxe4afd2e2017-02-24 15:00:58 -080037 if (unlikely(start >= nbits))
Yury Norov2c57a0e2015-04-16 12:43:13 -070038 return nbits;
39
Clement Courbet0ade34c2018-02-06 15:38:34 -080040 tmp = addr1[start / BITS_PER_LONG];
41 if (addr2)
42 tmp &= addr2[start / BITS_PER_LONG];
43 tmp ^= invert;
Yury Norov2c57a0e2015-04-16 12:43:13 -070044
45 /* Handle 1st word. */
Yury Norovb78c5712020-01-30 22:16:43 -080046 mask = BITMAP_FIRST_WORD_MASK(start);
47 if (le)
48 mask = swab(mask);
49
50 tmp &= mask;
51
Yury Norov2c57a0e2015-04-16 12:43:13 -070052 start = round_down(start, BITS_PER_LONG);
53
54 while (!tmp) {
55 start += BITS_PER_LONG;
56 if (start >= nbits)
57 return nbits;
58
Clement Courbet0ade34c2018-02-06 15:38:34 -080059 tmp = addr1[start / BITS_PER_LONG];
60 if (addr2)
61 tmp &= addr2[start / BITS_PER_LONG];
62 tmp ^= invert;
Yury Norov2c57a0e2015-04-16 12:43:13 -070063 }
64
Yury Norovb78c5712020-01-30 22:16:43 -080065 if (le)
66 tmp = swab(tmp);
67
Yury Norov2c57a0e2015-04-16 12:43:13 -070068 return min(start + __ffs(tmp), nbits);
69}
70#endif
Akinobu Mitac7f612c2006-03-26 01:39:11 -080071
Akinobu Mita19de85e2011-05-26 16:26:09 -070072#ifndef find_next_bit
Alexander van Heukelum64970b62008-03-11 16:17:19 +010073/*
74 * Find the next set bit in a memory region.
Akinobu Mitac7f612c2006-03-26 01:39:11 -080075 */
Thomas Gleixnerfee4b192008-04-29 12:01:02 +020076unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
77 unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Yury Norovb78c5712020-01-30 22:16:43 -080079 return _find_next_bit(addr, NULL, size, offset, 0UL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
Thomas Gleixnerfee4b192008-04-29 12:01:02 +020081EXPORT_SYMBOL(find_next_bit);
Akinobu Mita19de85e2011-05-26 16:26:09 -070082#endif
Akinobu Mitac7f612c2006-03-26 01:39:11 -080083
Akinobu Mita19de85e2011-05-26 16:26:09 -070084#ifndef find_next_zero_bit
Thomas Gleixnerfee4b192008-04-29 12:01:02 +020085unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
86 unsigned long offset)
Akinobu Mitac7f612c2006-03-26 01:39:11 -080087{
Yury Norovb78c5712020-01-30 22:16:43 -080088 return _find_next_bit(addr, NULL, size, offset, ~0UL, 0);
Akinobu Mitac7f612c2006-03-26 01:39:11 -080089}
Thomas Gleixnerfee4b192008-04-29 12:01:02 +020090EXPORT_SYMBOL(find_next_zero_bit);
Akinobu Mita19de85e2011-05-26 16:26:09 -070091#endif
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +020092
Clement Courbet0ade34c2018-02-06 15:38:34 -080093#if !defined(find_next_and_bit)
94unsigned long find_next_and_bit(const unsigned long *addr1,
95 const unsigned long *addr2, unsigned long size,
96 unsigned long offset)
97{
Yury Norovb78c5712020-01-30 22:16:43 -080098 return _find_next_bit(addr1, addr2, size, offset, 0UL, 0);
Clement Courbet0ade34c2018-02-06 15:38:34 -080099}
100EXPORT_SYMBOL(find_next_and_bit);
101#endif
102
Akinobu Mita19de85e2011-05-26 16:26:09 -0700103#ifndef find_first_bit
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200104/*
105 * Find the first set bit in a memory region.
106 */
Thomas Gleixnerfee4b192008-04-29 12:01:02 +0200107unsigned long find_first_bit(const unsigned long *addr, unsigned long size)
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200108{
Yury Norov2c57a0e2015-04-16 12:43:13 -0700109 unsigned long idx;
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200110
Yury Norov2c57a0e2015-04-16 12:43:13 -0700111 for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
112 if (addr[idx])
113 return min(idx * BITS_PER_LONG + __ffs(addr[idx]), size);
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200114 }
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200115
Yury Norov2c57a0e2015-04-16 12:43:13 -0700116 return size;
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200117}
Thomas Gleixnerfee4b192008-04-29 12:01:02 +0200118EXPORT_SYMBOL(find_first_bit);
Akinobu Mita19de85e2011-05-26 16:26:09 -0700119#endif
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200120
Akinobu Mita19de85e2011-05-26 16:26:09 -0700121#ifndef find_first_zero_bit
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200122/*
123 * Find the first cleared bit in a memory region.
124 */
Thomas Gleixnerfee4b192008-04-29 12:01:02 +0200125unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200126{
Yury Norov2c57a0e2015-04-16 12:43:13 -0700127 unsigned long idx;
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200128
Yury Norov2c57a0e2015-04-16 12:43:13 -0700129 for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
130 if (addr[idx] != ~0UL)
131 return min(idx * BITS_PER_LONG + ffz(addr[idx]), size);
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200132 }
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200133
Yury Norov2c57a0e2015-04-16 12:43:13 -0700134 return size;
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200135}
Thomas Gleixnerfee4b192008-04-29 12:01:02 +0200136EXPORT_SYMBOL(find_first_zero_bit);
Akinobu Mita19de85e2011-05-26 16:26:09 -0700137#endif
Akinobu Mita930ae742006-03-26 01:39:15 -0800138
Yury Norov8f6f19d2015-04-16 12:43:16 -0700139#ifndef find_last_bit
140unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
141{
142 if (size) {
143 unsigned long val = BITMAP_LAST_WORD_MASK(size);
144 unsigned long idx = (size-1) / BITS_PER_LONG;
145
146 do {
147 val &= addr[idx];
148 if (val)
149 return idx * BITS_PER_LONG + __fls(val);
150
151 val = ~0ul;
152 } while (idx--);
153 }
154 return size;
155}
156EXPORT_SYMBOL(find_last_bit);
157#endif
158
Akinobu Mita930ae742006-03-26 01:39:15 -0800159#ifdef __BIG_ENDIAN
160
Akinobu Mita19de85e2011-05-26 16:26:09 -0700161#ifndef find_next_zero_bit_le
Akinobu Mitaa56560b2011-03-23 16:41:50 -0700162unsigned long find_next_zero_bit_le(const void *addr, unsigned
Akinobu Mita930ae742006-03-26 01:39:15 -0800163 long size, unsigned long offset)
164{
Yury Norovb78c5712020-01-30 22:16:43 -0800165 return _find_next_bit(addr, NULL, size, offset, ~0UL, 1);
Akinobu Mita930ae742006-03-26 01:39:15 -0800166}
Akinobu Mitac4945b92011-03-23 16:41:47 -0700167EXPORT_SYMBOL(find_next_zero_bit_le);
Akinobu Mita19de85e2011-05-26 16:26:09 -0700168#endif
Akinobu Mita930ae742006-03-26 01:39:15 -0800169
Akinobu Mita19de85e2011-05-26 16:26:09 -0700170#ifndef find_next_bit_le
Akinobu Mitaa56560b2011-03-23 16:41:50 -0700171unsigned long find_next_bit_le(const void *addr, unsigned
Aneesh Kumar K.Vaa02ad62008-01-28 23:58:27 -0500172 long size, unsigned long offset)
173{
Yury Norovb78c5712020-01-30 22:16:43 -0800174 return _find_next_bit(addr, NULL, size, offset, 0UL, 1);
Aneesh Kumar K.Vaa02ad62008-01-28 23:58:27 -0500175}
Akinobu Mitac4945b92011-03-23 16:41:47 -0700176EXPORT_SYMBOL(find_next_bit_le);
Akinobu Mita19de85e2011-05-26 16:26:09 -0700177#endif
Akinobu Mita06649962011-03-23 16:41:59 -0700178
Akinobu Mita930ae742006-03-26 01:39:15 -0800179#endif /* __BIG_ENDIAN */
William Breathitt Gray169c4742019-12-04 16:50:57 -0800180
181unsigned long find_next_clump8(unsigned long *clump, const unsigned long *addr,
182 unsigned long size, unsigned long offset)
183{
184 offset = find_next_bit(addr, size, offset);
185 if (offset == size)
186 return size;
187
188 offset = round_down(offset, 8);
189 *clump = bitmap_get_value8(addr, offset);
190
191 return offset;
192}
193EXPORT_SYMBOL(find_next_clump8);