ffs.h 654 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_GENERIC_BITOPS_FFS_H_
  3. #define _ASM_GENERIC_BITOPS_FFS_H_
  4. /**
  5. * ffs - find first bit set
  6. * @x: the word to search
  7. *
  8. * This is defined the same way as
  9. * the libc and compiler builtin ffs routines, therefore
  10. * differs in spirit from the above ffz (man ffs).
  11. */
  12. static inline int ffs(int x)
  13. {
  14. int r = 1;
  15. if (!x)
  16. return 0;
  17. if (!(x & 0xffff)) {
  18. x >>= 16;
  19. r += 16;
  20. }
  21. if (!(x & 0xff)) {
  22. x >>= 8;
  23. r += 8;
  24. }
  25. if (!(x & 0xf)) {
  26. x >>= 4;
  27. r += 4;
  28. }
  29. if (!(x & 3)) {
  30. x >>= 2;
  31. r += 2;
  32. }
  33. if (!(x & 1)) {
  34. x >>= 1;
  35. r += 1;
  36. }
  37. return r;
  38. }
  39. #endif /* _ASM_GENERIC_BITOPS_FFS_H_ */