Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / libcfs / libcfs / user-bitops.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2007 Cluster File Systems, Inc.
5  *
6  * This file is part of Lustre, http://www.lustre.org.
7  *
8  * Lustre is free software; you can redistribute it and/or modify it under the
9  * terms of version 2 of the GNU General Public License as published by the
10  * Free Software Foundation.
11  *
12  * Lustre is distributed in the hope that it will be useful, but WITHOUT ANY
13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with Lustre; if not, write to the Free Software Foundation, Inc., 675 Mass
19  * Ave, Cambridge, MA 02139, USA.
20  *
21  */
22 #ifndef __KERNEL__
23
24 #include <libcfs/libcfs.h>
25 #include <libcfs/user-bitops.h>
26
27 #define OFF_BY_START(start)     ((start)/BITS_PER_LONG)
28
29 unsigned long find_next_bit(unsigned long *addr,
30                             unsigned long size, unsigned long offset)
31 {
32         unsigned long *word, *last;
33         unsigned long first_bit, bit, base;
34
35         word = addr + OFF_BY_START(offset);
36         last = addr + OFF_BY_START(size-1);
37         first_bit = offset % BITS_PER_LONG;
38         base = offset - first_bit;
39
40         if (offset >= size)
41                 return size;
42         if (first_bit != 0) {
43                 int tmp = (*word++) & (~0UL << first_bit);
44                 bit = __ffs(tmp);
45                 if (bit < BITS_PER_LONG)
46                         goto found;
47                 word++;
48                 base += BITS_PER_LONG;
49         }
50         while (word <= last) {
51                 if (*word != 0UL) {
52                         bit = __ffs(*word);
53                         goto found;
54                 }
55                 word++;
56                 base += BITS_PER_LONG;
57         }
58         return size;
59 found:
60         return base + bit;
61 }
62
63 unsigned long find_next_zero_bit(unsigned long *addr,
64                                  unsigned long size, unsigned long offset)
65 {
66         unsigned long *word, *last;
67         unsigned long first_bit, bit, base;
68
69         word = addr + OFF_BY_START(offset);
70         last = addr + OFF_BY_START(size-1);
71         first_bit = offset % BITS_PER_LONG;
72         base = offset - first_bit;
73
74         if (offset >= size)
75                 return size;
76         if (first_bit != 0) {
77                 int tmp = (*word++) & (~0UL << first_bit);
78                 bit = __ffz(tmp);
79                 if (bit < BITS_PER_LONG)
80                         goto found;
81                 word++;
82                 base += BITS_PER_LONG;
83         }
84         while (word <= last) {
85                 if (*word != ~0UL) {
86                         bit = __ffz(*word);
87                         goto found;
88                 }
89                 word++;
90                 base += BITS_PER_LONG;
91         }
92         return size;
93 found:
94         return base + bit;
95 }
96
97 #endif