blob: 9c90853b4472b85eb9b7d17e56ae69182f718467 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* find_next_bit.c: fallback find next bit implementation
2 *
3 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/bitops.h>
David Howells40234402006-01-08 01:01:19 -080013#include <linux/module.h>
Akinobu Mitac7f612c2006-03-26 01:39:11 -080014#include <asm/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Akinobu Mitac7f612c2006-03-26 01:39:11 -080016#define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
17
18/**
19 * find_next_bit - find the next set bit in a memory region
20 * @addr: The address to base the search on
21 * @offset: The bitnumber to start searching at
22 * @size: The maximum size to search
23 */
24unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
25 unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
Akinobu Mitac7f612c2006-03-26 01:39:11 -080027 const unsigned long *p = addr + BITOP_WORD(offset);
28 unsigned long result = offset & ~(BITS_PER_LONG-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 unsigned long tmp;
30
Akinobu Mitac7f612c2006-03-26 01:39:11 -080031 if (offset >= size)
32 return size;
33 size -= result;
34 offset %= BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 if (offset) {
Akinobu Mitac7f612c2006-03-26 01:39:11 -080036 tmp = *(p++);
37 tmp &= (~0UL << offset);
38 if (size < BITS_PER_LONG)
39 goto found_first;
40 if (tmp)
41 goto found_middle;
42 size -= BITS_PER_LONG;
43 result += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 }
Akinobu Mitac7f612c2006-03-26 01:39:11 -080045 while (size & ~(BITS_PER_LONG-1)) {
46 if ((tmp = *(p++)))
47 goto found_middle;
48 result += BITS_PER_LONG;
49 size -= BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 }
Akinobu Mitac7f612c2006-03-26 01:39:11 -080051 if (!size)
52 return result;
53 tmp = *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Akinobu Mitac7f612c2006-03-26 01:39:11 -080055found_first:
56 tmp &= (~0UL >> (BITS_PER_LONG - size));
57 if (tmp == 0UL) /* Are any bits set? */
58 return result + size; /* Nope. */
59found_middle:
60 return result + __ffs(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
David Howells40234402006-01-08 01:01:19 -080062
63EXPORT_SYMBOL(find_next_bit);
Akinobu Mitac7f612c2006-03-26 01:39:11 -080064
65/*
66 * This implementation of find_{first,next}_zero_bit was stolen from
67 * Linus' asm-alpha/bitops.h.
68 */
69unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
70 unsigned long offset)
71{
72 const unsigned long *p = addr + BITOP_WORD(offset);
73 unsigned long result = offset & ~(BITS_PER_LONG-1);
74 unsigned long tmp;
75
76 if (offset >= size)
77 return size;
78 size -= result;
79 offset %= BITS_PER_LONG;
80 if (offset) {
81 tmp = *(p++);
82 tmp |= ~0UL >> (BITS_PER_LONG - offset);
83 if (size < BITS_PER_LONG)
84 goto found_first;
85 if (~tmp)
86 goto found_middle;
87 size -= BITS_PER_LONG;
88 result += BITS_PER_LONG;
89 }
90 while (size & ~(BITS_PER_LONG-1)) {
91 if (~(tmp = *(p++)))
92 goto found_middle;
93 result += BITS_PER_LONG;
94 size -= BITS_PER_LONG;
95 }
96 if (!size)
97 return result;
98 tmp = *p;
99
100found_first:
101 tmp |= ~0UL << size;
102 if (tmp == ~0UL) /* Are any bits zero? */
103 return result + size; /* Nope. */
104found_middle:
105 return result + ffz(tmp);
106}
107
108EXPORT_SYMBOL(find_next_zero_bit);