xfs_bit.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2000,2002,2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef __XFS_BIT_H__
  19. #define __XFS_BIT_H__
  20. /*
  21. * XFS bit manipulation routines.
  22. */
  23. /*
  24. * masks with n high/low bits set, 32-bit values & 64-bit values
  25. */
  26. #define XFS_MASK32HI(n) xfs_mask32hi(n)
  27. static inline __uint32_t xfs_mask32hi(int n)
  28. {
  29. return (__uint32_t)-1 << (32 - (n));
  30. }
  31. #define XFS_MASK64HI(n) xfs_mask64hi(n)
  32. static inline __uint64_t xfs_mask64hi(int n)
  33. {
  34. return (__uint64_t)-1 << (64 - (n));
  35. }
  36. #define XFS_MASK32LO(n) xfs_mask32lo(n)
  37. static inline __uint32_t xfs_mask32lo(int n)
  38. {
  39. return ((__uint32_t)1 << (n)) - 1;
  40. }
  41. #define XFS_MASK64LO(n) xfs_mask64lo(n)
  42. static inline __uint64_t xfs_mask64lo(int n)
  43. {
  44. return ((__uint64_t)1 << (n)) - 1;
  45. }
  46. /* Get high bit set out of 32-bit argument, -1 if none set */
  47. static inline int xfs_highbit32(__uint32_t v)
  48. {
  49. return fls(v) - 1;
  50. }
  51. /* Get high bit set out of 64-bit argument, -1 if none set */
  52. static inline int xfs_highbit64(__uint64_t v)
  53. {
  54. return fls64(v) - 1;
  55. }
  56. /* Get low bit set out of 32-bit argument, -1 if none set */
  57. static inline int xfs_lowbit32(__uint32_t v)
  58. {
  59. unsigned long t = v;
  60. return (v) ? find_first_bit(&t, 32) : -1;
  61. }
  62. /* Get low bit set out of 64-bit argument, -1 if none set */
  63. static inline int xfs_lowbit64(__uint64_t v)
  64. {
  65. __uint32_t w = (__uint32_t)v;
  66. int n = 0;
  67. if (w) { /* lower bits */
  68. n = ffs(w);
  69. } else { /* upper bits */
  70. w = (__uint32_t)(v >> 32);
  71. if (w && (n = ffs(w)))
  72. n += 32;
  73. }
  74. return n - 1;
  75. }
  76. /* Return whether bitmap is empty (1 == empty) */
  77. extern int xfs_bitmap_empty(uint *map, uint size);
  78. /* Count continuous one bits in map starting with start_bit */
  79. extern int xfs_contig_bits(uint *map, uint size, uint start_bit);
  80. /* Find next set bit in map */
  81. extern int xfs_next_bit(uint *map, uint size, uint start_bit);
  82. #endif /* __XFS_BIT_H__ */