xfs_bit.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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, 64-bit values
  25. */
  26. static inline __uint64_t xfs_mask64hi(int n)
  27. {
  28. return (__uint64_t)-1 << (64 - (n));
  29. }
  30. static inline __uint32_t xfs_mask32lo(int n)
  31. {
  32. return ((__uint32_t)1 << (n)) - 1;
  33. }
  34. static inline __uint64_t xfs_mask64lo(int n)
  35. {
  36. return ((__uint64_t)1 << (n)) - 1;
  37. }
  38. /* Get high bit set out of 32-bit argument, -1 if none set */
  39. static inline int xfs_highbit32(__uint32_t v)
  40. {
  41. return fls(v) - 1;
  42. }
  43. /* Get high bit set out of 64-bit argument, -1 if none set */
  44. static inline int xfs_highbit64(__uint64_t v)
  45. {
  46. return fls64(v) - 1;
  47. }
  48. /* Get low bit set out of 32-bit argument, -1 if none set */
  49. static inline int xfs_lowbit32(__uint32_t v)
  50. {
  51. return ffs(v) - 1;
  52. }
  53. /* Get low bit set out of 64-bit argument, -1 if none set */
  54. static inline int xfs_lowbit64(__uint64_t v)
  55. {
  56. __uint32_t w = (__uint32_t)v;
  57. int n = 0;
  58. if (w) { /* lower bits */
  59. n = ffs(w);
  60. } else { /* upper bits */
  61. w = (__uint32_t)(v >> 32);
  62. if (w && (n = ffs(w)))
  63. n += 32;
  64. }
  65. return n - 1;
  66. }
  67. /* Return whether bitmap is empty (1 == empty) */
  68. extern int xfs_bitmap_empty(uint *map, uint size);
  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. /* Find next set bit in map */
  72. extern int xfs_next_bit(uint *map, uint size, uint start_bit);
  73. #endif /* __XFS_BIT_H__ */