Whamcloud - gitweb
use LASSERT_SPIN_LOCKED() in LASSERT(spin_is_locked()) stead as the latter does not...
[fs/lustre-release.git] / libcfs / include / libcfs / user-bitops.h
index d2eea0e..ae7d569 100644 (file)
@@ -1,35 +1,50 @@
 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
  * vim:expandtab:shiftwidth=8:tabstop=8:
  *
- * Copyright (C) 2004 Cluster File Systems, Inc.
- * Author: Nikita Danilov <nikita@clusterfs.com>
+ * GPL HEADER START
  *
- * This file is part of Lustre, http://www.lustre.org.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
- * Lustre is free software; you can redistribute it and/or modify it under the
- * terms of version 2 of the GNU General Public License as published by the
- * Free Software Foundation.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 only,
+ * as published by the Free Software Foundation.
  *
- * Lustre is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
- * details.
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License version 2 for more details (a copy is included
+ * in the LICENSE file that accompanied this code).
  *
- * You should have received a copy of the GNU General Public License along
- * with Lustre; if not, write to the Free Software Foundation, Inc., 675 Mass
- * Ave, Cambridge, MA 02139, USA.
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; If not, see
+ * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
  *
- * Implementation of portable time API for user-level.
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
  *
+ * GPL HEADER END
+ */
+/*
+ * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Use is subject to license terms.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
+ *
+ * libcfs/include/libcfs/user-bitops.h
+ *
+ * Author: Nikita Danilov <nikita@clusterfs.com>
  */
 
 #ifndef __LIBCFS_USER_BITOPS_H__
 #define __LIBCFS_USER_BITOPS_H__
 
 /* test if bit nr is set in bitmap addr; returns previous value of bit nr */
-static __inline__ int set_bit(int nr, unsigned long * addr)
+static __inline__ int set_bit(int nr, unsigned long *addr)
 {
-        long    mask;
+        unsigned long mask;
 
         addr += nr / BITS_PER_LONG;
         mask = 1UL << (nr & (BITS_PER_LONG - 1));
@@ -39,9 +54,9 @@ static __inline__ int set_bit(int nr, unsigned long * addr)
 }
 
 /* clear bit nr in bitmap addr; returns previous value of bit nr*/
-static __inline__ int clear_bit(int nr, unsigned long * addr)
+static __inline__ int clear_bit(int nr, unsigned long *addr)
 {
-        long    mask;
+        unsigned long mask;
 
         addr += nr / BITS_PER_LONG;
         mask = 1UL << (nr & (BITS_PER_LONG - 1));
@@ -50,12 +65,52 @@ static __inline__ int clear_bit(int nr, unsigned long * addr)
         return nr;
 }
 
-static __inline__ int test_bit(int nr, const unsigned long * addr)
+static __inline__ int test_bit(int nr, const unsigned long *addr)
 {
-        return ((1UL << (nr & (BITS_PER_LONG - 1))) & ((addr)[nr / BITS_PER_LONG])) != 0;
+        return ((1UL << (nr & (BITS_PER_LONG - 1))) &
+                ((addr)[nr / BITS_PER_LONG])) != 0;
 }
 
 /* using binary seach */
+static __inline__ unsigned long __fls(long data)
+{
+       int pos = 32;
+
+       if (!data)
+               return 0;
+
+#if BITS_PER_LONG == 64
+        pos += 32;
+
+        if ((data & 0xFFFFFFFF) == 0) {
+                data <<= 32;
+                pos -= 32;
+        }
+#endif
+
+       if (!(data & 0xFFFF0000u)) {
+               data <<= 16;
+               pos -= 16;
+       }
+       if (!(data & 0xFF000000u)) {
+               data <<= 8;
+               pos -= 8;
+       }
+       if (!(data & 0xF0000000u)) {
+               data <<= 4;
+               pos -= 4;
+       }
+       if (!(data & 0xC0000000u)) {
+               data <<= 2;
+               pos -= 2;
+       }
+       if (!(data & 0x80000000u)) {
+               data <<= 1;
+               pos -= 1;
+       }
+       return pos;
+}
+
 static __inline__ unsigned long __ffs(long data)
 {
         int pos = 0;
@@ -89,6 +144,7 @@ static __inline__ unsigned long __ffs(long data)
 }
 
 #define __ffz(x)       __ffs(~(x))
+#define __flz(x)       __fls(~(x))
 
 unsigned long find_next_bit(unsigned long *addr,
                             unsigned long size, unsigned long offset);