xref: /linux/fs/xfs/libxfs/xfs_bit.h (revision 84be0ffc9043f7c56044294eb775a2200452c76d)
1*84be0ffcSDave Chinner /*
2*84be0ffcSDave Chinner  * Copyright (c) 2000,2002,2005 Silicon Graphics, Inc.
3*84be0ffcSDave Chinner  * All Rights Reserved.
4*84be0ffcSDave Chinner  *
5*84be0ffcSDave Chinner  * This program is free software; you can redistribute it and/or
6*84be0ffcSDave Chinner  * modify it under the terms of the GNU General Public License as
7*84be0ffcSDave Chinner  * published by the Free Software Foundation.
8*84be0ffcSDave Chinner  *
9*84be0ffcSDave Chinner  * This program is distributed in the hope that it would be useful,
10*84be0ffcSDave Chinner  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*84be0ffcSDave Chinner  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*84be0ffcSDave Chinner  * GNU General Public License for more details.
13*84be0ffcSDave Chinner  *
14*84be0ffcSDave Chinner  * You should have received a copy of the GNU General Public License
15*84be0ffcSDave Chinner  * along with this program; if not, write the Free Software Foundation,
16*84be0ffcSDave Chinner  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17*84be0ffcSDave Chinner  */
18*84be0ffcSDave Chinner #ifndef __XFS_BIT_H__
19*84be0ffcSDave Chinner #define	__XFS_BIT_H__
20*84be0ffcSDave Chinner 
21*84be0ffcSDave Chinner /*
22*84be0ffcSDave Chinner  * XFS bit manipulation routines.
23*84be0ffcSDave Chinner  */
24*84be0ffcSDave Chinner 
25*84be0ffcSDave Chinner /*
26*84be0ffcSDave Chinner  * masks with n high/low bits set, 64-bit values
27*84be0ffcSDave Chinner  */
28*84be0ffcSDave Chinner static inline __uint64_t xfs_mask64hi(int n)
29*84be0ffcSDave Chinner {
30*84be0ffcSDave Chinner 	return (__uint64_t)-1 << (64 - (n));
31*84be0ffcSDave Chinner }
32*84be0ffcSDave Chinner static inline __uint32_t xfs_mask32lo(int n)
33*84be0ffcSDave Chinner {
34*84be0ffcSDave Chinner 	return ((__uint32_t)1 << (n)) - 1;
35*84be0ffcSDave Chinner }
36*84be0ffcSDave Chinner static inline __uint64_t xfs_mask64lo(int n)
37*84be0ffcSDave Chinner {
38*84be0ffcSDave Chinner 	return ((__uint64_t)1 << (n)) - 1;
39*84be0ffcSDave Chinner }
40*84be0ffcSDave Chinner 
41*84be0ffcSDave Chinner /* Get high bit set out of 32-bit argument, -1 if none set */
42*84be0ffcSDave Chinner static inline int xfs_highbit32(__uint32_t v)
43*84be0ffcSDave Chinner {
44*84be0ffcSDave Chinner 	return fls(v) - 1;
45*84be0ffcSDave Chinner }
46*84be0ffcSDave Chinner 
47*84be0ffcSDave Chinner /* Get high bit set out of 64-bit argument, -1 if none set */
48*84be0ffcSDave Chinner static inline int xfs_highbit64(__uint64_t v)
49*84be0ffcSDave Chinner {
50*84be0ffcSDave Chinner 	return fls64(v) - 1;
51*84be0ffcSDave Chinner }
52*84be0ffcSDave Chinner 
53*84be0ffcSDave Chinner /* Get low bit set out of 32-bit argument, -1 if none set */
54*84be0ffcSDave Chinner static inline int xfs_lowbit32(__uint32_t v)
55*84be0ffcSDave Chinner {
56*84be0ffcSDave Chinner 	return ffs(v) - 1;
57*84be0ffcSDave Chinner }
58*84be0ffcSDave Chinner 
59*84be0ffcSDave Chinner /* Get low bit set out of 64-bit argument, -1 if none set */
60*84be0ffcSDave Chinner static inline int xfs_lowbit64(__uint64_t v)
61*84be0ffcSDave Chinner {
62*84be0ffcSDave Chinner 	__uint32_t	w = (__uint32_t)v;
63*84be0ffcSDave Chinner 	int		n = 0;
64*84be0ffcSDave Chinner 
65*84be0ffcSDave Chinner 	if (w) {	/* lower bits */
66*84be0ffcSDave Chinner 		n = ffs(w);
67*84be0ffcSDave Chinner 	} else {	/* upper bits */
68*84be0ffcSDave Chinner 		w = (__uint32_t)(v >> 32);
69*84be0ffcSDave Chinner 		if (w) {
70*84be0ffcSDave Chinner 			n = ffs(w);
71*84be0ffcSDave Chinner 			if (n)
72*84be0ffcSDave Chinner 				n += 32;
73*84be0ffcSDave Chinner 		}
74*84be0ffcSDave Chinner 	}
75*84be0ffcSDave Chinner 	return n - 1;
76*84be0ffcSDave Chinner }
77*84be0ffcSDave Chinner 
78*84be0ffcSDave Chinner /* Return whether bitmap is empty (1 == empty) */
79*84be0ffcSDave Chinner extern int xfs_bitmap_empty(uint *map, uint size);
80*84be0ffcSDave Chinner 
81*84be0ffcSDave Chinner /* Count continuous one bits in map starting with start_bit */
82*84be0ffcSDave Chinner extern int xfs_contig_bits(uint *map, uint size, uint start_bit);
83*84be0ffcSDave Chinner 
84*84be0ffcSDave Chinner /* Find next set bit in map */
85*84be0ffcSDave Chinner extern int xfs_next_bit(uint *map, uint size, uint start_bit);
86*84be0ffcSDave Chinner 
87*84be0ffcSDave Chinner #endif	/* __XFS_BIT_H__ */
88