Add lib/find_bit.c and include/linux/find.h from Linux v6.19, trimmed to include only the functions needed for ext4l: find_first_bit(), find_first_zero_bit(), find_next_bit(), find_next_zero_bit() and find_last_bit() The following items are removed from the Linux originals: - find.h: _and_bit, _andnot_bit, _or_bit, _nth_bit variants, wrap functions, clump8 functions, big-endian support, most for_each_... macros - find_bit.c: Corresponding implementations, random.h include Add wrapper functions matching sandbox's asm/bitops.h declarations (int return type, void* addr) that call the _find_* implementations. Build find_bit.o only for sandbox for now. Co-developed-by: Claude Opus 4.5 <noreply@anthropic.com> Signed-off-by: Simon Glass <simon.glass@canonical.com>
143 lines
3.8 KiB
C
143 lines
3.8 KiB
C
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
/* bit search implementation
|
|
*
|
|
* 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)
|
|
*
|
|
* Rewritten by Yury Norov <yury.norov@gmail.com> to decrease
|
|
* size and improve performance, 2015.
|
|
*/
|
|
|
|
#include <asm/byteorder.h>
|
|
#include <linux/bitops.h>
|
|
#include <linux/bitmap.h>
|
|
#include <linux/export.h>
|
|
|
|
/*
|
|
* Common helper for find_bit() function family
|
|
* @FETCH: The expression that fetches and pre-processes each word of bitmap(s)
|
|
* @MUNGE: The expression that post-processes a word containing found bit (may be empty)
|
|
* @size: The bitmap size in bits
|
|
*/
|
|
#define FIND_FIRST_BIT(FETCH, MUNGE, size) \
|
|
({ \
|
|
unsigned long idx, val, sz = (size); \
|
|
\
|
|
for (idx = 0; idx * BITS_PER_LONG < sz; idx++) { \
|
|
val = (FETCH); \
|
|
if (val) { \
|
|
sz = min(idx * BITS_PER_LONG + __ffs(MUNGE(val)), sz); \
|
|
break; \
|
|
} \
|
|
} \
|
|
\
|
|
sz; \
|
|
})
|
|
|
|
/*
|
|
* Common helper for find_next_bit() function family
|
|
* @FETCH: The expression that fetches and pre-processes each word of bitmap(s)
|
|
* @MUNGE: The expression that post-processes a word containing found bit (may be empty)
|
|
* @size: The bitmap size in bits
|
|
* @start: The bitnumber to start searching at
|
|
*/
|
|
#define FIND_NEXT_BIT(FETCH, MUNGE, size, start) \
|
|
({ \
|
|
unsigned long mask, idx, tmp, sz = (size), __start = (start); \
|
|
\
|
|
if (unlikely(__start >= sz)) \
|
|
goto out; \
|
|
\
|
|
mask = MUNGE(BITMAP_FIRST_WORD_MASK(__start)); \
|
|
idx = __start / BITS_PER_LONG; \
|
|
\
|
|
for (tmp = (FETCH) & mask; !tmp; tmp = (FETCH)) { \
|
|
if ((idx + 1) * BITS_PER_LONG >= sz) \
|
|
goto out; \
|
|
idx++; \
|
|
} \
|
|
\
|
|
sz = min(idx * BITS_PER_LONG + __ffs(MUNGE(tmp)), sz); \
|
|
out: \
|
|
sz; \
|
|
})
|
|
|
|
#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)
|
|
{
|
|
return FIND_FIRST_BIT(addr[idx], /* nop */, size);
|
|
}
|
|
EXPORT_SYMBOL(_find_first_bit);
|
|
#endif
|
|
|
|
#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)
|
|
{
|
|
return FIND_FIRST_BIT(~addr[idx], /* nop */, size);
|
|
}
|
|
EXPORT_SYMBOL(_find_first_zero_bit);
|
|
#endif
|
|
|
|
#ifndef find_next_bit
|
|
unsigned long _find_next_bit(const unsigned long *addr, unsigned long nbits, unsigned long start)
|
|
{
|
|
return FIND_NEXT_BIT(addr[idx], /* nop */, nbits, start);
|
|
}
|
|
EXPORT_SYMBOL(_find_next_bit);
|
|
#endif
|
|
|
|
#ifndef find_next_zero_bit
|
|
unsigned long _find_next_zero_bit(const unsigned long *addr, unsigned long nbits,
|
|
unsigned long start)
|
|
{
|
|
return FIND_NEXT_BIT(~addr[idx], /* nop */, nbits, start);
|
|
}
|
|
EXPORT_SYMBOL(_find_next_zero_bit);
|
|
#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
|
|
|
|
/*
|
|
* Wrapper functions matching sandbox's asm/bitops.h declarations.
|
|
* These provide the expected function signatures for sandbox platform.
|
|
*/
|
|
int find_first_zero_bit(void *addr, unsigned int size)
|
|
{
|
|
return _find_first_zero_bit(addr, size);
|
|
}
|
|
EXPORT_SYMBOL(find_first_zero_bit);
|
|
|
|
int find_next_zero_bit(void *addr, int size, int offset)
|
|
{
|
|
return _find_next_zero_bit(addr, size, offset);
|
|
}
|
|
EXPORT_SYMBOL(find_next_zero_bit);
|