__fls.h 920 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_GENERIC_BITOPS___FLS_H_
  3. #define _ASM_GENERIC_BITOPS___FLS_H_
  4. #include <asm/types.h>
  5. /**
  6. * __fls - find last (most-significant) set bit in a long word
  7. * @word: the word to search
  8. *
  9. * Undefined if no set bit exists, so code should check against 0 first.
  10. */
  11. static __always_inline unsigned long __fls(unsigned long word)
  12. {
  13. int num = BITS_PER_LONG - 1;
  14. #if BITS_PER_LONG == 64
  15. if (!(word & (~0ul << 32))) {
  16. num -= 32;
  17. word <<= 32;
  18. }
  19. #endif
  20. if (!(word & (~0ul << (BITS_PER_LONG-16)))) {
  21. num -= 16;
  22. word <<= 16;
  23. }
  24. if (!(word & (~0ul << (BITS_PER_LONG-8)))) {
  25. num -= 8;
  26. word <<= 8;
  27. }
  28. if (!(word & (~0ul << (BITS_PER_LONG-4)))) {
  29. num -= 4;
  30. word <<= 4;
  31. }
  32. if (!(word & (~0ul << (BITS_PER_LONG-2)))) {
  33. num -= 2;
  34. word <<= 2;
  35. }
  36. if (!(word & (~0ul << (BITS_PER_LONG-1))))
  37. num -= 1;
  38. return num;
  39. }
  40. #endif /* _ASM_GENERIC_BITOPS___FLS_H_ */