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