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/kp30.h>
26 #include <libcfs/user-bitops.h>
27
28 #define OFF_BY_START(start)     ((start)/BITS_PER_LONG)
29
30 unsigned long find_next_bit(unsigned long *addr,
31                             unsigned long size, unsigned long offset)
32 {
33         unsigned long *word, *last;
34         unsigned long first_bit, bit, base;
35
36         word = addr + OFF_BY_START(offset);
37         last = addr + OFF_BY_START(size-1);
38         first_bit = offset % BITS_PER_LONG;
39         base = offset - first_bit;
40
41         if (offset >= size)
42                 return size;
43         if (first_bit != 0) {
44                 int tmp = (*word++) & (~0UL << first_bit);
45                 bit = __ffs(tmp);
46                 if (bit < BITS_PER_LONG)
47                         goto found;
48                 word++;
49                 base += BITS_PER_LONG;
50         }
51         while (word <= last) {
52                 if (*word != 0UL) {
53                         bit = __ffs(*word);
54                         goto found;
55                 }
56                 word++;
57                 base += BITS_PER_LONG;
58         }
59         return size;
60 found:
61         return base + bit;
62 }
63
64 unsigned long find_next_zero_bit(unsigned long *addr,
65                                  unsigned long size, unsigned long offset)
66 {
67         unsigned long *word, *last;
68         unsigned long first_bit, bit, base;
69
70         word = addr + OFF_BY_START(offset);
71         last = addr + OFF_BY_START(size-1);
72         first_bit = offset % BITS_PER_LONG;
73         base = offset - first_bit;
74
75         if (offset >= size)
76                 return size;
77         if (first_bit != 0) {
78                 int tmp = (*word++) & (~0UL << first_bit);
79                 bit = __ffz(tmp);
80                 if (bit < BITS_PER_LONG)
81                         goto found;
82                 word++;
83                 base += BITS_PER_LONG;
84         }
85         while (word <= last) {
86                 if (*word != ~0UL) {
87                         bit = __ffz(*word);
88                         goto found;
89                 }
90                 word++;
91                 base += BITS_PER_LONG;
92         }
93         return size;
94 found:
95         return base + bit;
96 }
97
98 #endif