__fls.h 881 B

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