blob: 0f8e2e369b1d2ab87c571cf163fd77eded2af4a5 [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>
Andy Shevchenkoaa6159a2020-12-15 20:42:48 -080018#include <linux/math.h>
Andy Shevchenkob296a6d2020-10-15 20:10:21 -070019#include <linux/minmax.h>
Andy Shevchenkoaa6159a2020-12-15 20:42:48 -080020#include <linux/swab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Yury Norovb78c5712020-01-30 22:16:43 -080022#if !defined(find_next_bit) || !defined(find_next_zero_bit) || \
23 !defined(find_next_bit_le) || !defined(find_next_zero_bit_le) || \
24 !defined(find_next_and_bit)
Yury Norov2c57a0e2015-04-16 12:43:13 -070025/*
Clement Courbet0ade34c2018-02-06 15:38:34 -080026 * This is a common helper function for find_next_bit, find_next_zero_bit, and
27 * find_next_and_bit. The differences are:
28 * - The "invert" argument, which is XORed with each fetched word before
29 * searching it for one bits.
30 * - The optional "addr2", which is anded with "addr1" if present.
Yury Norov2c57a0e2015-04-16 12:43:13 -070031 */
Yury Norov5c88af52021-05-06 18:03:03 -070032unsigned long _find_next_bit(const unsigned long *addr1,
Clement Courbet0ade34c2018-02-06 15:38:34 -080033 const unsigned long *addr2, unsigned long nbits,
Yury Norovb78c5712020-01-30 22:16:43 -080034 unsigned long start, unsigned long invert, unsigned long le)
Yury Norov2c57a0e2015-04-16 12:43:13 -070035{
Yury Norovb78c5712020-01-30 22:16:43 -080036 unsigned long tmp, mask;
Yury Norov2c57a0e2015-04-16 12:43:13 -070037
Matthew Wilcoxe4afd2e2017-02-24 15:00:58 -080038 if (unlikely(start >= nbits))
Yury Norov2c57a0e2015-04-16 12:43:13 -070039 return nbits;
40
Clement Courbet0ade34c2018-02-06 15:38:34 -080041 tmp = addr1[start / BITS_PER_LONG];
42 if (addr2)
43 tmp &= addr2[start / BITS_PER_LONG];
44 tmp ^= invert;
Yury Norov2c57a0e2015-04-16 12:43:13 -070045
46 /* Handle 1st word. */
Yury Norovb78c5712020-01-30 22:16:43 -080047 mask = BITMAP_FIRST_WORD_MASK(start);
48 if (le)
49 mask = swab(mask);
50
51 tmp &= mask;
52
Yury Norov2c57a0e2015-04-16 12:43:13 -070053 start = round_down(start, BITS_PER_LONG);
54
55 while (!tmp) {
56 start += BITS_PER_LONG;
57 if (start >= nbits)
58 return nbits;
59
Clement Courbet0ade34c2018-02-06 15:38:34 -080060 tmp = addr1[start / BITS_PER_LONG];
61 if (addr2)
62 tmp &= addr2[start / BITS_PER_LONG];
63 tmp ^= invert;
Yury Norov2c57a0e2015-04-16 12:43:13 -070064 }
65
Yury Norovb78c5712020-01-30 22:16:43 -080066 if (le)
67 tmp = swab(tmp);
68
Yury Norov2c57a0e2015-04-16 12:43:13 -070069 return min(start + __ffs(tmp), nbits);
70}
Yury Norov5c88af52021-05-06 18:03:03 -070071EXPORT_SYMBOL(_find_next_bit);
Clement Courbet0ade34c2018-02-06 15:38:34 -080072#endif
73
Akinobu Mita19de85e2011-05-26 16:26:09 -070074#ifndef find_first_bit
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +020075/*
76 * Find the first set bit in a memory region.
77 */
Yury Norov2cc7b6a2021-05-06 18:03:14 -070078unsigned long _find_first_bit(const unsigned long *addr, unsigned long size)
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +020079{
Yury Norov2c57a0e2015-04-16 12:43:13 -070080 unsigned long idx;
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +020081
Yury Norov2c57a0e2015-04-16 12:43:13 -070082 for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
83 if (addr[idx])
84 return min(idx * BITS_PER_LONG + __ffs(addr[idx]), size);
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +020085 }
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +020086
Yury Norov2c57a0e2015-04-16 12:43:13 -070087 return size;
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +020088}
Yury Norov2cc7b6a2021-05-06 18:03:14 -070089EXPORT_SYMBOL(_find_first_bit);
Akinobu Mita19de85e2011-05-26 16:26:09 -070090#endif
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +020091
Akinobu Mita19de85e2011-05-26 16:26:09 -070092#ifndef find_first_zero_bit
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +020093/*
94 * Find the first cleared bit in a memory region.
95 */
Yury Norov2cc7b6a2021-05-06 18:03:14 -070096unsigned long _find_first_zero_bit(const unsigned long *addr, unsigned long size)
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +020097{
Yury Norov2c57a0e2015-04-16 12:43:13 -070098 unsigned long idx;
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +020099
Yury Norov2c57a0e2015-04-16 12:43:13 -0700100 for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
101 if (addr[idx] != ~0UL)
102 return min(idx * BITS_PER_LONG + ffz(addr[idx]), size);
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200103 }
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200104
Yury Norov2c57a0e2015-04-16 12:43:13 -0700105 return size;
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200106}
Yury Norov2cc7b6a2021-05-06 18:03:14 -0700107EXPORT_SYMBOL(_find_first_zero_bit);
Akinobu Mita19de85e2011-05-26 16:26:09 -0700108#endif
Akinobu Mita930ae742006-03-26 01:39:15 -0800109
Yury Norov8f6f19d2015-04-16 12:43:16 -0700110#ifndef find_last_bit
Yury Norov2cc7b6a2021-05-06 18:03:14 -0700111unsigned long _find_last_bit(const unsigned long *addr, unsigned long size)
Yury Norov8f6f19d2015-04-16 12:43:16 -0700112{
113 if (size) {
114 unsigned long val = BITMAP_LAST_WORD_MASK(size);
115 unsigned long idx = (size-1) / BITS_PER_LONG;
116
117 do {
118 val &= addr[idx];
119 if (val)
120 return idx * BITS_PER_LONG + __fls(val);
121
122 val = ~0ul;
123 } while (idx--);
124 }
125 return size;
126}
Yury Norov2cc7b6a2021-05-06 18:03:14 -0700127EXPORT_SYMBOL(_find_last_bit);
Yury Norov8f6f19d2015-04-16 12:43:16 -0700128#endif
129
William Breathitt Gray169c4742019-12-04 16:50:57 -0800130unsigned long find_next_clump8(unsigned long *clump, const unsigned long *addr,
131 unsigned long size, unsigned long offset)
132{
133 offset = find_next_bit(addr, size, offset);
134 if (offset == size)
135 return size;
136
137 offset = round_down(offset, 8);
138 *clump = bitmap_get_value8(addr, offset);
139
140 return offset;
141}
142EXPORT_SYMBOL(find_next_clump8);