blob: e1649c0d3e02f0f4da1ce1f8476008d53c7a8a3f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000,2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18#ifndef __XFS_BIT_H__
19#define __XFS_BIT_H__
20
21/*
22 * XFS bit manipulation routines.
23 */
24
25/*
Eric Sandeenfb825572009-01-09 15:53:54 +110026 * masks with n high/low bits set, 64-bit values
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 */
Nathan Scotta844f452005-11-02 14:38:42 +110028static inline __uint64_t xfs_mask64hi(int n)
29{
30 return (__uint64_t)-1 << (64 - (n));
31}
Nathan Scotta844f452005-11-02 14:38:42 +110032static inline __uint32_t xfs_mask32lo(int n)
33{
34 return ((__uint32_t)1 << (n)) - 1;
35}
Nathan Scotta844f452005-11-02 14:38:42 +110036static inline __uint64_t xfs_mask64lo(int n)
37{
38 return ((__uint64_t)1 << (n)) - 1;
39}
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/* Get high bit set out of 32-bit argument, -1 if none set */
David Chinner79071eb2008-08-13 15:41:12 +100042static inline int xfs_highbit32(__uint32_t v)
43{
44 return fls(v) - 1;
45}
Lachlan McIlroyef8ece52008-02-26 17:00:22 +110046
47/* Get high bit set out of 64-bit argument, -1 if none set */
David Chinner79071eb2008-08-13 15:41:12 +100048static inline int xfs_highbit64(__uint64_t v)
49{
50 return fls64(v) - 1;
51}
52
53/* Get low bit set out of 32-bit argument, -1 if none set */
54static inline int xfs_lowbit32(__uint32_t v)
55{
Barry Naujok847fff52008-10-30 17:05:38 +110056 return ffs(v) - 1;
David Chinner79071eb2008-08-13 15:41:12 +100057}
58
59/* Get low bit set out of 64-bit argument, -1 if none set */
60static inline int xfs_lowbit64(__uint64_t v)
61{
62 __uint32_t w = (__uint32_t)v;
63 int n = 0;
64
65 if (w) { /* lower bits */
66 n = ffs(w);
67 } else { /* upper bits */
68 w = (__uint32_t)(v >> 32);
Dan Carpenter72208ee2014-06-06 16:04:42 +100069 if (w) {
70 n = ffs(w);
71 if (n)
72 n += 32;
73 }
David Chinner79071eb2008-08-13 15:41:12 +100074 }
75 return n - 1;
76}
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Eric Sandeen24ad33f2007-06-28 16:43:30 +100078/* Return whether bitmap is empty (1 == empty) */
79extern int xfs_bitmap_empty(uint *map, uint size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/* Count continuous one bits in map starting with start_bit */
82extern int xfs_contig_bits(uint *map, uint size, uint start_bit);
83
84/* Find next set bit in map */
85extern int xfs_next_bit(uint *map, uint size, uint start_bit);
86
87#endif /* __XFS_BIT_H__ */