Files
laptops-kernel/lib/find_bit.c
T

142 lines
3.3 KiB
C
Raw Normal View History

// SPDX-License-Identifier: GPL-2.0-or-later
/* bit search implementation
2005-04-16 15:20:36 -07:00
*
* Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
* Written by David Howells (dhowells@redhat.com)
*
* Copyright (C) 2008 IBM Corporation
* 'find_last_bit' is written by Rusty Russell <rusty@rustcorp.com.au>
* (Inspired by David Howell's find_next_bit implementation)
*
2015-04-16 12:43:13 -07:00
* Rewritten by Yury Norov <yury.norov@gmail.com> to decrease
* size and improve performance, 2015.
2005-04-16 15:20:36 -07:00
*/
#include <linux/bitops.h>
#include <linux/bitmap.h>
#include <linux/export.h>
2020-12-15 20:42:48 -08:00
#include <linux/math.h>
#include <linux/minmax.h>
2020-12-15 20:42:48 -08:00
#include <linux/swab.h>
2005-04-16 15:20:36 -07:00
2020-01-30 22:16:43 -08:00
#if !defined(find_next_bit) || !defined(find_next_zero_bit) || \
!defined(find_next_bit_le) || !defined(find_next_zero_bit_le) || \
!defined(find_next_and_bit)
2015-04-16 12:43:13 -07:00
/*
2018-02-06 15:38:34 -08:00
* This is a common helper function for find_next_bit, find_next_zero_bit, and
* find_next_and_bit. The differences are:
* - The "invert" argument, which is XORed with each fetched word before
* searching it for one bits.
* - The optional "addr2", which is anded with "addr1" if present.
2015-04-16 12:43:13 -07:00
*/
2021-05-06 18:03:03 -07:00
unsigned long _find_next_bit(const unsigned long *addr1,
2018-02-06 15:38:34 -08:00
const unsigned long *addr2, unsigned long nbits,
2020-01-30 22:16:43 -08:00
unsigned long start, unsigned long invert, unsigned long le)
2015-04-16 12:43:13 -07:00
{
2020-01-30 22:16:43 -08:00
unsigned long tmp, mask;
2015-04-16 12:43:13 -07:00
if (unlikely(start >= nbits))
2015-04-16 12:43:13 -07:00
return nbits;
2018-02-06 15:38:34 -08:00
tmp = addr1[start / BITS_PER_LONG];
if (addr2)
tmp &= addr2[start / BITS_PER_LONG];
tmp ^= invert;
2015-04-16 12:43:13 -07:00
/* Handle 1st word. */
2020-01-30 22:16:43 -08:00
mask = BITMAP_FIRST_WORD_MASK(start);
if (le)
mask = swab(mask);
tmp &= mask;
2015-04-16 12:43:13 -07:00
start = round_down(start, BITS_PER_LONG);
while (!tmp) {
start += BITS_PER_LONG;
if (start >= nbits)
return nbits;
2018-02-06 15:38:34 -08:00
tmp = addr1[start / BITS_PER_LONG];
if (addr2)
tmp &= addr2[start / BITS_PER_LONG];
tmp ^= invert;
2015-04-16 12:43:13 -07:00
}
2020-01-30 22:16:43 -08:00
if (le)
tmp = swab(tmp);
2015-04-16 12:43:13 -07:00
return min(start + __ffs(tmp), nbits);
}
2021-05-06 18:03:03 -07:00
EXPORT_SYMBOL(_find_next_bit);
2018-02-06 15:38:34 -08:00
#endif
2011-05-26 16:26:09 -07:00
#ifndef find_first_bit
/*
* Find the first set bit in a memory region.
*/
unsigned long _find_first_bit(const unsigned long *addr, unsigned long size)
{
2015-04-16 12:43:13 -07:00
unsigned long idx;
2015-04-16 12:43:13 -07:00
for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
if (addr[idx])
return min(idx * BITS_PER_LONG + __ffs(addr[idx]), size);
}
2015-04-16 12:43:13 -07:00
return size;
}
EXPORT_SYMBOL(_find_first_bit);
2011-05-26 16:26:09 -07:00
#endif
2011-05-26 16:26:09 -07:00
#ifndef find_first_zero_bit
/*
* Find the first cleared bit in a memory region.
*/
unsigned long _find_first_zero_bit(const unsigned long *addr, unsigned long size)
{
2015-04-16 12:43:13 -07:00
unsigned long idx;
2015-04-16 12:43:13 -07:00
for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
if (addr[idx] != ~0UL)
return min(idx * BITS_PER_LONG + ffz(addr[idx]), size);
}
2015-04-16 12:43:13 -07:00
return size;
}
EXPORT_SYMBOL(_find_first_zero_bit);
2011-05-26 16:26:09 -07:00
#endif
#ifndef find_last_bit
unsigned long _find_last_bit(const unsigned long *addr, unsigned long size)
{
if (size) {
unsigned long val = BITMAP_LAST_WORD_MASK(size);
unsigned long idx = (size-1) / BITS_PER_LONG;
do {
val &= addr[idx];
if (val)
return idx * BITS_PER_LONG + __fls(val);
val = ~0ul;
} while (idx--);
}
return size;
}
EXPORT_SYMBOL(_find_last_bit);
#endif
unsigned long find_next_clump8(unsigned long *clump, const unsigned long *addr,
unsigned long size, unsigned long offset)
{
offset = find_next_bit(addr, size, offset);
if (offset == size)
return size;
offset = round_down(offset, 8);
*clump = bitmap_get_value8(addr, offset);
return offset;
}
EXPORT_SYMBOL(find_next_clump8);