fls.h 674 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_GENERIC_BITOPS_FLS_H_
  3. #define _ASM_GENERIC_BITOPS_FLS_H_
  4. /**
  5. * fls - find last (most-significant) bit set
  6. * @x: the word to search
  7. *
  8. * This is defined the same way as ffs.
  9. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
  10. */
  11. static __always_inline int fls(int x)
  12. {
  13. int r = 32;
  14. if (!x)
  15. return 0;
  16. if (!(x & 0xffff0000u)) {
  17. x <<= 16;
  18. r -= 16;
  19. }
  20. if (!(x & 0xff000000u)) {
  21. x <<= 8;
  22. r -= 8;
  23. }
  24. if (!(x & 0xf0000000u)) {
  25. x <<= 4;
  26. r -= 4;
  27. }
  28. if (!(x & 0xc0000000u)) {
  29. x <<= 2;
  30. r -= 2;
  31. }
  32. if (!(x & 0x80000000u)) {
  33. x <<= 1;
  34. r -= 1;
  35. }
  36. return r;
  37. }
  38. #endif /* _ASM_GENERIC_BITOPS_FLS_H_ */