blob: 589fd2f26f940cee6412af0e35359686e79dfa94 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Arnaldo Carvalho de Melo64af4e02016-01-08 11:26:43 -03002/* bit search implementation
Arnaldo Carvalho de Melo23e1a352014-12-15 19:50:12 -03003 *
Arnaldo Carvalho de Melo64af4e02016-01-08 11:26:43 -03004 * Copied from lib/find_bit.c to tools/lib/find_bit.c
Arnaldo Carvalho de Melo23e1a352014-12-15 19:50:12 -03005 *
6 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
7 * Written by David Howells (dhowells@redhat.com)
8 *
Arnaldo Carvalho de Melo64af4e02016-01-08 11:26:43 -03009 * Copyright (C) 2008 IBM Corporation
10 * 'find_last_bit' is written by Rusty Russell <rusty@rustcorp.com.au>
11 * (Inspired by David Howell's find_next_bit implementation)
12 *
13 * Rewritten by Yury Norov <yury.norov@gmail.com> to decrease
14 * size and improve performance, 2015.
Arnaldo Carvalho de Melo23e1a352014-12-15 19:50:12 -030015 */
16
17#include <linux/bitops.h>
Arnaldo Carvalho de Melo64af4e02016-01-08 11:26:43 -030018#include <linux/bitmap.h>
19#include <linux/kernel.h>
Arnaldo Carvalho de Melo23e1a352014-12-15 19:50:12 -030020
Clement Courbet0ade34c2018-02-06 15:38:34 -080021#if !defined(find_next_bit) || !defined(find_next_zero_bit) || \
22 !defined(find_next_and_bit)
Arnaldo Carvalho de Melo64af4e02016-01-08 11:26:43 -030023
24/*
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.
Arnaldo Carvalho de Melo64af4e02016-01-08 11:26:43 -030030 */
Yury Norovea81c1e2021-05-06 18:03:07 -070031unsigned long _find_next_bit(const unsigned long *addr1,
Clement Courbet0ade34c2018-02-06 15:38:34 -080032 const unsigned long *addr2, unsigned long nbits,
Yury Norovea81c1e2021-05-06 18:03:07 -070033 unsigned long start, unsigned long invert, unsigned long le)
Arnaldo Carvalho de Melo64af4e02016-01-08 11:26:43 -030034{
Yury Norovea81c1e2021-05-06 18:03:07 -070035 unsigned long tmp, mask;
36 (void) le;
Arnaldo Carvalho de Melo64af4e02016-01-08 11:26:43 -030037
Matthew Wilcoxe4afd2e2017-02-24 15:00:58 -080038 if (unlikely(start >= nbits))
Arnaldo Carvalho de Melo64af4e02016-01-08 11:26:43 -030039 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;
Arnaldo Carvalho de Melo64af4e02016-01-08 11:26:43 -030045
46 /* Handle 1st word. */
Yury Norovea81c1e2021-05-06 18:03:07 -070047 mask = BITMAP_FIRST_WORD_MASK(start);
48
49 /*
50 * Due to the lack of swab() in tools, and the fact that it doesn't
51 * need little-endian support, just comment it out
52 */
53#if (0)
54 if (le)
55 mask = swab(mask);
56#endif
57
58 tmp &= mask;
59
Arnaldo Carvalho de Melo64af4e02016-01-08 11:26:43 -030060 start = round_down(start, BITS_PER_LONG);
61
62 while (!tmp) {
63 start += BITS_PER_LONG;
64 if (start >= nbits)
65 return nbits;
66
Clement Courbet0ade34c2018-02-06 15:38:34 -080067 tmp = addr1[start / BITS_PER_LONG];
68 if (addr2)
69 tmp &= addr2[start / BITS_PER_LONG];
70 tmp ^= invert;
Arnaldo Carvalho de Melo64af4e02016-01-08 11:26:43 -030071 }
72
Yury Norovea81c1e2021-05-06 18:03:07 -070073#if (0)
74 if (le)
75 tmp = swab(tmp);
Arnaldo Carvalho de Melo64af4e02016-01-08 11:26:43 -030076#endif
Arnaldo Carvalho de Melo23e1a352014-12-15 19:50:12 -030077
Yury Norovea81c1e2021-05-06 18:03:07 -070078 return min(start + __ffs(tmp), nbits);
Arnaldo Carvalho de Melo23e1a352014-12-15 19:50:12 -030079}
80#endif
81
82#ifndef find_first_bit
83/*
84 * Find the first set bit in a memory region.
85 */
86unsigned long find_first_bit(const unsigned long *addr, unsigned long size)
87{
Arnaldo Carvalho de Melo64af4e02016-01-08 11:26:43 -030088 unsigned long idx;
Arnaldo Carvalho de Melo23e1a352014-12-15 19:50:12 -030089
Arnaldo Carvalho de Melo64af4e02016-01-08 11:26:43 -030090 for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
91 if (addr[idx])
92 return min(idx * BITS_PER_LONG + __ffs(addr[idx]), size);
Arnaldo Carvalho de Melo23e1a352014-12-15 19:50:12 -030093 }
Arnaldo Carvalho de Melo23e1a352014-12-15 19:50:12 -030094
Arnaldo Carvalho de Melo64af4e02016-01-08 11:26:43 -030095 return size;
Arnaldo Carvalho de Melo23e1a352014-12-15 19:50:12 -030096}
97#endif
Jiri Olsa02bc11d2016-10-10 09:26:33 +020098
99#ifndef find_first_zero_bit
100/*
101 * Find the first cleared bit in a memory region.
102 */
103unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)
104{
105 unsigned long idx;
106
107 for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
108 if (addr[idx] != ~0UL)
109 return min(idx * BITS_PER_LONG + ffz(addr[idx]), size);
110 }
111
112 return size;
113}
114#endif