xref: /OK3568_Linux_fs/kernel/fs/xfs/libxfs/xfs_bit.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (c) 2000,2002,2005 Silicon Graphics, Inc.
4*4882a593Smuzhiyun  * All Rights Reserved.
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun #ifndef __XFS_BIT_H__
7*4882a593Smuzhiyun #define	__XFS_BIT_H__
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun /*
10*4882a593Smuzhiyun  * XFS bit manipulation routines.
11*4882a593Smuzhiyun  */
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun /*
14*4882a593Smuzhiyun  * masks with n high/low bits set, 64-bit values
15*4882a593Smuzhiyun  */
xfs_mask64hi(int n)16*4882a593Smuzhiyun static inline uint64_t xfs_mask64hi(int n)
17*4882a593Smuzhiyun {
18*4882a593Smuzhiyun 	return (uint64_t)-1 << (64 - (n));
19*4882a593Smuzhiyun }
xfs_mask32lo(int n)20*4882a593Smuzhiyun static inline uint32_t xfs_mask32lo(int n)
21*4882a593Smuzhiyun {
22*4882a593Smuzhiyun 	return ((uint32_t)1 << (n)) - 1;
23*4882a593Smuzhiyun }
xfs_mask64lo(int n)24*4882a593Smuzhiyun static inline uint64_t xfs_mask64lo(int n)
25*4882a593Smuzhiyun {
26*4882a593Smuzhiyun 	return ((uint64_t)1 << (n)) - 1;
27*4882a593Smuzhiyun }
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun /* Get high bit set out of 32-bit argument, -1 if none set */
xfs_highbit32(uint32_t v)30*4882a593Smuzhiyun static inline int xfs_highbit32(uint32_t v)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun 	return fls(v) - 1;
33*4882a593Smuzhiyun }
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /* Get high bit set out of 64-bit argument, -1 if none set */
xfs_highbit64(uint64_t v)36*4882a593Smuzhiyun static inline int xfs_highbit64(uint64_t v)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun 	return fls64(v) - 1;
39*4882a593Smuzhiyun }
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun /* Get low bit set out of 32-bit argument, -1 if none set */
xfs_lowbit32(uint32_t v)42*4882a593Smuzhiyun static inline int xfs_lowbit32(uint32_t v)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun 	return ffs(v) - 1;
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun /* Get low bit set out of 64-bit argument, -1 if none set */
xfs_lowbit64(uint64_t v)48*4882a593Smuzhiyun static inline int xfs_lowbit64(uint64_t v)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun 	uint32_t	w = (uint32_t)v;
51*4882a593Smuzhiyun 	int		n = 0;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	if (w) {	/* lower bits */
54*4882a593Smuzhiyun 		n = ffs(w);
55*4882a593Smuzhiyun 	} else {	/* upper bits */
56*4882a593Smuzhiyun 		w = (uint32_t)(v >> 32);
57*4882a593Smuzhiyun 		if (w) {
58*4882a593Smuzhiyun 			n = ffs(w);
59*4882a593Smuzhiyun 			if (n)
60*4882a593Smuzhiyun 				n += 32;
61*4882a593Smuzhiyun 		}
62*4882a593Smuzhiyun 	}
63*4882a593Smuzhiyun 	return n - 1;
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun /* Return whether bitmap is empty (1 == empty) */
67*4882a593Smuzhiyun extern int xfs_bitmap_empty(uint *map, uint size);
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun /* Count continuous one bits in map starting with start_bit */
70*4882a593Smuzhiyun extern int xfs_contig_bits(uint *map, uint size, uint start_bit);
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun /* Find next set bit in map */
73*4882a593Smuzhiyun extern int xfs_next_bit(uint *map, uint size, uint start_bit);
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun #endif	/* __XFS_BIT_H__ */
76