xfs_bit.h 1.5 KB

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