__ffs.h 844 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _TOOLS_LINUX_ASM_GENERIC_BITOPS___FFS_H_
  3. #define _TOOLS_LINUX_ASM_GENERIC_BITOPS___FFS_H_
  4. #include <asm/types.h>
  5. #include <asm/bitsperlong.h>
  6. /**
  7. * __ffs - find first bit in word.
  8. * @word: The word to search
  9. *
  10. * Undefined if no bit exists, so code should check against 0 first.
  11. */
  12. static __always_inline unsigned long __ffs(unsigned long word)
  13. {
  14. int num = 0;
  15. #if __BITS_PER_LONG == 64
  16. if ((word & 0xffffffff) == 0) {
  17. num += 32;
  18. word >>= 32;
  19. }
  20. #endif
  21. if ((word & 0xffff) == 0) {
  22. num += 16;
  23. word >>= 16;
  24. }
  25. if ((word & 0xff) == 0) {
  26. num += 8;
  27. word >>= 8;
  28. }
  29. if ((word & 0xf) == 0) {
  30. num += 4;
  31. word >>= 4;
  32. }
  33. if ((word & 0x3) == 0) {
  34. num += 2;
  35. word >>= 2;
  36. }
  37. if ((word & 0x1) == 0)
  38. num += 1;
  39. return num;
  40. }
  41. #endif /* _TOOLS_LINUX_ASM_GENERIC_BITOPS___FFS_H_ */