| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 |  * Copyright (c) 2000,2002,2005 Silicon Graphics, Inc. | 
 | 3 |  * All Rights Reserved. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 |  * | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 |  * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 |  * published by the Free Software Foundation. | 
 | 8 |  * | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 |  * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 |  * | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 |  * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 |  */ | 
 | 18 | #ifndef __XFS_BIT_H__ | 
 | 19 | #define	__XFS_BIT_H__ | 
 | 20 |  | 
 | 21 | /* | 
 | 22 |  * XFS bit manipulation routines. | 
 | 23 |  */ | 
 | 24 |  | 
 | 25 | /* | 
| Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 26 |  * masks with n high/low bits set, 64-bit values | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 |  */ | 
| Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame^] | 28 | static inline uint64_t xfs_mask64hi(int n) | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 29 | { | 
| Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame^] | 30 | 	return (uint64_t)-1 << (64 - (n)); | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 31 | } | 
| Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame^] | 32 | static inline uint32_t xfs_mask32lo(int n) | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 33 | { | 
| Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame^] | 34 | 	return ((uint32_t)1 << (n)) - 1; | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 35 | } | 
| Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame^] | 36 | static inline uint64_t xfs_mask64lo(int n) | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 37 | { | 
| Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame^] | 38 | 	return ((uint64_t)1 << (n)) - 1; | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 39 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 |  | 
 | 41 | /* Get high bit set out of 32-bit argument, -1 if none set */ | 
| Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame^] | 42 | static inline int xfs_highbit32(uint32_t v) | 
| David Chinner | 79071eb | 2008-08-13 15:41:12 +1000 | [diff] [blame] | 43 | { | 
 | 44 | 	return fls(v) - 1; | 
 | 45 | } | 
| Lachlan McIlroy | ef8ece5 | 2008-02-26 17:00:22 +1100 | [diff] [blame] | 46 |  | 
 | 47 | /* Get high bit set out of 64-bit argument, -1 if none set */ | 
| Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame^] | 48 | static inline int xfs_highbit64(uint64_t v) | 
| David Chinner | 79071eb | 2008-08-13 15:41:12 +1000 | [diff] [blame] | 49 | { | 
 | 50 | 	return fls64(v) - 1; | 
 | 51 | } | 
 | 52 |  | 
 | 53 | /* Get low bit set out of 32-bit argument, -1 if none set */ | 
| Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame^] | 54 | static inline int xfs_lowbit32(uint32_t v) | 
| David Chinner | 79071eb | 2008-08-13 15:41:12 +1000 | [diff] [blame] | 55 | { | 
| Barry Naujok | 847fff5 | 2008-10-30 17:05:38 +1100 | [diff] [blame] | 56 | 	return ffs(v) - 1; | 
| David Chinner | 79071eb | 2008-08-13 15:41:12 +1000 | [diff] [blame] | 57 | } | 
 | 58 |  | 
 | 59 | /* Get low bit set out of 64-bit argument, -1 if none set */ | 
| Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame^] | 60 | static inline int xfs_lowbit64(uint64_t v) | 
| David Chinner | 79071eb | 2008-08-13 15:41:12 +1000 | [diff] [blame] | 61 | { | 
| Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame^] | 62 | 	uint32_t	w = (uint32_t)v; | 
| David Chinner | 79071eb | 2008-08-13 15:41:12 +1000 | [diff] [blame] | 63 | 	int		n = 0; | 
 | 64 |  | 
 | 65 | 	if (w) {	/* lower bits */ | 
 | 66 | 		n = ffs(w); | 
 | 67 | 	} else {	/* upper bits */ | 
| Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame^] | 68 | 		w = (uint32_t)(v >> 32); | 
| Dan Carpenter | 72208ee | 2014-06-06 16:04:42 +1000 | [diff] [blame] | 69 | 		if (w) { | 
 | 70 | 			n = ffs(w); | 
 | 71 | 			if (n) | 
 | 72 | 				n += 32; | 
 | 73 | 		} | 
| David Chinner | 79071eb | 2008-08-13 15:41:12 +1000 | [diff] [blame] | 74 | 	} | 
 | 75 | 	return n - 1; | 
 | 76 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 |  | 
| Eric Sandeen | 24ad33f | 2007-06-28 16:43:30 +1000 | [diff] [blame] | 78 | /* Return whether bitmap is empty (1 == empty) */ | 
 | 79 | extern int xfs_bitmap_empty(uint *map, uint size); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 |  | 
 | 81 | /* Count continuous one bits in map starting with start_bit */ | 
 | 82 | extern int xfs_contig_bits(uint *map, uint size, uint start_bit); | 
 | 83 |  | 
 | 84 | /* Find next set bit in map */ | 
 | 85 | extern int xfs_next_bit(uint *map, uint size, uint start_bit); | 
 | 86 |  | 
 | 87 | #endif	/* __XFS_BIT_H__ */ |