fls64.h 860 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_GENERIC_BITOPS_FLS64_H_
  3. #define _ASM_GENERIC_BITOPS_FLS64_H_
  4. #include <asm/types.h>
  5. /**
  6. * fls64 - find last set bit in a 64-bit word
  7. * @x: the word to search
  8. *
  9. * This is defined in a similar way as the libc and compiler builtin
  10. * ffsll, but returns the position of the most significant set bit.
  11. *
  12. * fls64(value) returns 0 if value is 0 or the position of the last
  13. * set bit if value is nonzero. The last (most significant) bit is
  14. * at position 64.
  15. */
  16. #if BITS_PER_LONG == 32
  17. static __always_inline int fls64(__u64 x)
  18. {
  19. __u32 h = x >> 32;
  20. if (h)
  21. return fls(h) + 32;
  22. return fls(x);
  23. }
  24. #elif BITS_PER_LONG == 64
  25. static __always_inline int fls64(__u64 x)
  26. {
  27. if (x == 0)
  28. return 0;
  29. return __fls(x) + 1;
  30. }
  31. #else
  32. #error BITS_PER_LONG not 32 or 64
  33. #endif
  34. #endif /* _ASM_GENERIC_BITOPS_FLS64_H_ */