blob: 49f875f1baf7ed13daad8ac23ec1ed3dde2e7edf [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Yury Norovb78c5712020-01-30 22:16:43 -080020#if !defined(find_next_bit) || !defined(find_next_zero_bit) || \
21 !defined(find_next_bit_le) || !defined(find_next_zero_bit_le) || \
22 !defined(find_next_and_bit)
Yury Norov2c57a0e2015-04-16 12:43:13 -070023/*
Clement Courbet0ade34c2018-02-06 15:38:34 -080024 * This is a common helper function for find_next_bit, find_next_zero_bit, and
25 * find_next_and_bit. The differences are:
26 * - The "invert" argument, which is XORed with each fetched word before
27 * searching it for one bits.
28 * - The optional "addr2", which is anded with "addr1" if present.
Yury Norov2c57a0e2015-04-16 12:43:13 -070029 */
Yury Norov7dfaa98f2020-01-30 22:16:47 -080030static unsigned long _find_next_bit(const unsigned long *addr1,
Clement Courbet0ade34c2018-02-06 15:38:34 -080031 const unsigned long *addr2, unsigned long nbits,
Yury Norovb78c5712020-01-30 22:16:43 -080032 unsigned long start, unsigned long invert, unsigned long le)
Yury Norov2c57a0e2015-04-16 12:43:13 -070033{
Yury Norovb78c5712020-01-30 22:16:43 -080034 unsigned long tmp, mask;
Yury Norov2c57a0e2015-04-16 12:43:13 -070035
Matthew Wilcoxe4afd2e2017-02-24 15:00:58 -080036 if (unlikely(start >= nbits))
Yury Norov2c57a0e2015-04-16 12:43:13 -070037 return nbits;
38
Clement Courbet0ade34c2018-02-06 15:38:34 -080039 tmp = addr1[start / BITS_PER_LONG];
40 if (addr2)
41 tmp &= addr2[start / BITS_PER_LONG];
42 tmp ^= invert;
Yury Norov2c57a0e2015-04-16 12:43:13 -070043
44 /* Handle 1st word. */
Yury Norovb78c5712020-01-30 22:16:43 -080045 mask = BITMAP_FIRST_WORD_MASK(start);
46 if (le)
47 mask = swab(mask);
48
49 tmp &= mask;
50
Yury Norov2c57a0e2015-04-16 12:43:13 -070051 start = round_down(start, BITS_PER_LONG);
52
53 while (!tmp) {
54 start += BITS_PER_LONG;
55 if (start >= nbits)
56 return nbits;
57
Clement Courbet0ade34c2018-02-06 15:38:34 -080058 tmp = addr1[start / BITS_PER_LONG];
59 if (addr2)
60 tmp &= addr2[start / BITS_PER_LONG];
61 tmp ^= invert;
Yury Norov2c57a0e2015-04-16 12:43:13 -070062 }
63
Yury Norovb78c5712020-01-30 22:16:43 -080064 if (le)
65 tmp = swab(tmp);
66
Yury Norov2c57a0e2015-04-16 12:43:13 -070067 return min(start + __ffs(tmp), nbits);
68}
69#endif
Akinobu Mitac7f612c2006-03-26 01:39:11 -080070
Akinobu Mita19de85e2011-05-26 16:26:09 -070071#ifndef find_next_bit
Alexander van Heukelum64970b62008-03-11 16:17:19 +010072/*
73 * Find the next set bit in a memory region.
Akinobu Mitac7f612c2006-03-26 01:39:11 -080074 */
Thomas Gleixnerfee4b192008-04-29 12:01:02 +020075unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
76 unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Yury Norovb78c5712020-01-30 22:16:43 -080078 return _find_next_bit(addr, NULL, size, offset, 0UL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
Thomas Gleixnerfee4b192008-04-29 12:01:02 +020080EXPORT_SYMBOL(find_next_bit);
Akinobu Mita19de85e2011-05-26 16:26:09 -070081#endif
Akinobu Mitac7f612c2006-03-26 01:39:11 -080082
Akinobu Mita19de85e2011-05-26 16:26:09 -070083#ifndef find_next_zero_bit
Thomas Gleixnerfee4b192008-04-29 12:01:02 +020084unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
85 unsigned long offset)
Akinobu Mitac7f612c2006-03-26 01:39:11 -080086{
Yury Norovb78c5712020-01-30 22:16:43 -080087 return _find_next_bit(addr, NULL, size, offset, ~0UL, 0);
Akinobu Mitac7f612c2006-03-26 01:39:11 -080088}
Thomas Gleixnerfee4b192008-04-29 12:01:02 +020089EXPORT_SYMBOL(find_next_zero_bit);
Akinobu Mita19de85e2011-05-26 16:26:09 -070090#endif
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +020091
Clement Courbet0ade34c2018-02-06 15:38:34 -080092#if !defined(find_next_and_bit)
93unsigned long find_next_and_bit(const unsigned long *addr1,
94 const unsigned long *addr2, unsigned long size,
95 unsigned long offset)
96{
Yury Norovb78c5712020-01-30 22:16:43 -080097 return _find_next_bit(addr1, addr2, size, offset, 0UL, 0);
Clement Courbet0ade34c2018-02-06 15:38:34 -080098}
99EXPORT_SYMBOL(find_next_and_bit);
100#endif
101
Akinobu Mita19de85e2011-05-26 16:26:09 -0700102#ifndef find_first_bit
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200103/*
104 * Find the first set bit in a memory region.
105 */
Thomas Gleixnerfee4b192008-04-29 12:01:02 +0200106unsigned long find_first_bit(const unsigned long *addr, unsigned long size)
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200107{
Yury Norov2c57a0e2015-04-16 12:43:13 -0700108 unsigned long idx;
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200109
Yury Norov2c57a0e2015-04-16 12:43:13 -0700110 for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
111 if (addr[idx])
112 return min(idx * BITS_PER_LONG + __ffs(addr[idx]), size);
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200113 }
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200114
Yury Norov2c57a0e2015-04-16 12:43:13 -0700115 return size;
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200116}
Thomas Gleixnerfee4b192008-04-29 12:01:02 +0200117EXPORT_SYMBOL(find_first_bit);
Akinobu Mita19de85e2011-05-26 16:26:09 -0700118#endif
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200119
Akinobu Mita19de85e2011-05-26 16:26:09 -0700120#ifndef find_first_zero_bit
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200121/*
122 * Find the first cleared bit in a memory region.
123 */
Thomas Gleixnerfee4b192008-04-29 12:01:02 +0200124unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200125{
Yury Norov2c57a0e2015-04-16 12:43:13 -0700126 unsigned long idx;
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200127
Yury Norov2c57a0e2015-04-16 12:43:13 -0700128 for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
129 if (addr[idx] != ~0UL)
130 return min(idx * BITS_PER_LONG + ffz(addr[idx]), size);
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200131 }
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200132
Yury Norov2c57a0e2015-04-16 12:43:13 -0700133 return size;
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200134}
Thomas Gleixnerfee4b192008-04-29 12:01:02 +0200135EXPORT_SYMBOL(find_first_zero_bit);
Akinobu Mita19de85e2011-05-26 16:26:09 -0700136#endif
Akinobu Mita930ae742006-03-26 01:39:15 -0800137
Yury Norov8f6f19d2015-04-16 12:43:16 -0700138#ifndef find_last_bit
139unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
140{
141 if (size) {
142 unsigned long val = BITMAP_LAST_WORD_MASK(size);
143 unsigned long idx = (size-1) / BITS_PER_LONG;
144
145 do {
146 val &= addr[idx];
147 if (val)
148 return idx * BITS_PER_LONG + __fls(val);
149
150 val = ~0ul;
151 } while (idx--);
152 }
153 return size;
154}
155EXPORT_SYMBOL(find_last_bit);
156#endif
157
Akinobu Mita930ae742006-03-26 01:39:15 -0800158#ifdef __BIG_ENDIAN
159
Akinobu Mita19de85e2011-05-26 16:26:09 -0700160#ifndef find_next_zero_bit_le
Akinobu Mitaa56560b2011-03-23 16:41:50 -0700161unsigned long find_next_zero_bit_le(const void *addr, unsigned
Akinobu Mita930ae742006-03-26 01:39:15 -0800162 long size, unsigned long offset)
163{
Yury Norovb78c5712020-01-30 22:16:43 -0800164 return _find_next_bit(addr, NULL, size, offset, ~0UL, 1);
Akinobu Mita930ae742006-03-26 01:39:15 -0800165}
Akinobu Mitac4945b92011-03-23 16:41:47 -0700166EXPORT_SYMBOL(find_next_zero_bit_le);
Akinobu Mita19de85e2011-05-26 16:26:09 -0700167#endif
Akinobu Mita930ae742006-03-26 01:39:15 -0800168
Akinobu Mita19de85e2011-05-26 16:26:09 -0700169#ifndef find_next_bit_le
Akinobu Mitaa56560b2011-03-23 16:41:50 -0700170unsigned long find_next_bit_le(const void *addr, unsigned
Aneesh Kumar K.Vaa02ad62008-01-28 23:58:27 -0500171 long size, unsigned long offset)
172{
Yury Norovb78c5712020-01-30 22:16:43 -0800173 return _find_next_bit(addr, NULL, size, offset, 0UL, 1);
Aneesh Kumar K.Vaa02ad62008-01-28 23:58:27 -0500174}
Akinobu Mitac4945b92011-03-23 16:41:47 -0700175EXPORT_SYMBOL(find_next_bit_le);
Akinobu Mita19de85e2011-05-26 16:26:09 -0700176#endif
Akinobu Mita06649962011-03-23 16:41:59 -0700177
Akinobu Mita930ae742006-03-26 01:39:15 -0800178#endif /* __BIG_ENDIAN */
William Breathitt Gray169c4742019-12-04 16:50:57 -0800179
180unsigned long find_next_clump8(unsigned long *clump, const unsigned long *addr,
181 unsigned long size, unsigned long offset)
182{
183 offset = find_next_bit(addr, size, offset);
184 if (offset == size)
185 return size;
186
187 offset = round_down(offset, 8);
188 *clump = bitmap_get_value8(addr, offset);
189
190 return offset;
191}
192EXPORT_SYMBOL(find_next_clump8);