xref: /linux/fs/xfs/libxfs/xfs_bit.h (revision 4b4193256c8d3bc3a5397b5cd9494c2ad386317d)
1*508578f2SNishad Kamdar /* SPDX-License-Identifier: GPL-2.0 */
284be0ffcSDave Chinner /*
384be0ffcSDave Chinner  * Copyright (c) 2000,2002,2005 Silicon Graphics, Inc.
484be0ffcSDave Chinner  * All Rights Reserved.
584be0ffcSDave Chinner  */
684be0ffcSDave Chinner #ifndef __XFS_BIT_H__
784be0ffcSDave Chinner #define	__XFS_BIT_H__
884be0ffcSDave Chinner 
984be0ffcSDave Chinner /*
1084be0ffcSDave Chinner  * XFS bit manipulation routines.
1184be0ffcSDave Chinner  */
1284be0ffcSDave Chinner 
1384be0ffcSDave Chinner /*
1484be0ffcSDave Chinner  * masks with n high/low bits set, 64-bit values
1584be0ffcSDave Chinner  */
xfs_mask64hi(int n)16c8ce540dSDarrick J. Wong static inline uint64_t xfs_mask64hi(int n)
1784be0ffcSDave Chinner {
18c8ce540dSDarrick J. Wong 	return (uint64_t)-1 << (64 - (n));
1984be0ffcSDave Chinner }
xfs_mask32lo(int n)20c8ce540dSDarrick J. Wong static inline uint32_t xfs_mask32lo(int n)
2184be0ffcSDave Chinner {
22c8ce540dSDarrick J. Wong 	return ((uint32_t)1 << (n)) - 1;
2384be0ffcSDave Chinner }
xfs_mask64lo(int n)24c8ce540dSDarrick J. Wong static inline uint64_t xfs_mask64lo(int n)
2584be0ffcSDave Chinner {
26c8ce540dSDarrick J. Wong 	return ((uint64_t)1 << (n)) - 1;
2784be0ffcSDave Chinner }
2884be0ffcSDave Chinner 
2984be0ffcSDave Chinner /* Get high bit set out of 32-bit argument, -1 if none set */
xfs_highbit32(uint32_t v)30c8ce540dSDarrick J. Wong static inline int xfs_highbit32(uint32_t v)
3184be0ffcSDave Chinner {
3284be0ffcSDave Chinner 	return fls(v) - 1;
3384be0ffcSDave Chinner }
3484be0ffcSDave Chinner 
3584be0ffcSDave Chinner /* Get high bit set out of 64-bit argument, -1 if none set */
xfs_highbit64(uint64_t v)36c8ce540dSDarrick J. Wong static inline int xfs_highbit64(uint64_t v)
3784be0ffcSDave Chinner {
3884be0ffcSDave Chinner 	return fls64(v) - 1;
3984be0ffcSDave Chinner }
4084be0ffcSDave Chinner 
4184be0ffcSDave Chinner /* Get low bit set out of 32-bit argument, -1 if none set */
xfs_lowbit32(uint32_t v)42c8ce540dSDarrick J. Wong static inline int xfs_lowbit32(uint32_t v)
4384be0ffcSDave Chinner {
4484be0ffcSDave Chinner 	return ffs(v) - 1;
4584be0ffcSDave Chinner }
4684be0ffcSDave Chinner 
4784be0ffcSDave Chinner /* Get low bit set out of 64-bit argument, -1 if none set */
xfs_lowbit64(uint64_t v)48c8ce540dSDarrick J. Wong static inline int xfs_lowbit64(uint64_t v)
4984be0ffcSDave Chinner {
50c8ce540dSDarrick J. Wong 	uint32_t	w = (uint32_t)v;
5184be0ffcSDave Chinner 	int		n = 0;
5284be0ffcSDave Chinner 
5384be0ffcSDave Chinner 	if (w) {	/* lower bits */
5484be0ffcSDave Chinner 		n = ffs(w);
5584be0ffcSDave Chinner 	} else {	/* upper bits */
56c8ce540dSDarrick J. Wong 		w = (uint32_t)(v >> 32);
5784be0ffcSDave Chinner 		if (w) {
5884be0ffcSDave Chinner 			n = ffs(w);
5984be0ffcSDave Chinner 			if (n)
6084be0ffcSDave Chinner 				n += 32;
6184be0ffcSDave Chinner 		}
6284be0ffcSDave Chinner 	}
6384be0ffcSDave Chinner 	return n - 1;
6484be0ffcSDave Chinner }
6584be0ffcSDave Chinner 
6684be0ffcSDave Chinner /* Return whether bitmap is empty (1 == empty) */
6784be0ffcSDave Chinner extern int xfs_bitmap_empty(uint *map, uint size);
6884be0ffcSDave Chinner 
6984be0ffcSDave Chinner /* Count continuous one bits in map starting with start_bit */
7084be0ffcSDave Chinner extern int xfs_contig_bits(uint *map, uint size, uint start_bit);
7184be0ffcSDave Chinner 
7284be0ffcSDave Chinner /* Find next set bit in map */
7384be0ffcSDave Chinner extern int xfs_next_bit(uint *map, uint size, uint start_bit);
7484be0ffcSDave Chinner 
7584be0ffcSDave Chinner #endif	/* __XFS_BIT_H__ */
76