isl_ffs.c 665 B

123456789101112131415161718192021222324
  1. #include <isl_config.h>
  2. #if !HAVE_DECL_FFS && !HAVE_DECL___BUILTIN_FFS && HAVE_DECL__BITSCANFORWARD
  3. #include <intrin.h>
  4. /* Implementation of ffs in terms of _BitScanForward.
  5. *
  6. * ffs returns the position of the least significant bit set in i,
  7. * with the least significant bit is position 1, or 0 if not bits are set.
  8. *
  9. * _BitScanForward returns 1 if mask is non-zero and sets index
  10. * to the position of the least significant bit set in i,
  11. * with the least significant bit is position 0.
  12. */
  13. int isl_ffs(int i)
  14. {
  15. unsigned char non_zero;
  16. unsigned long index, mask = i;
  17. non_zero = _BitScanForward(&index, mask);
  18. return non_zero ? 1 + index : 0;
  19. }
  20. #endif