ffs.h 615 B

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