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