fls.h 635 B

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