ffs.c 527 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /* $OpenBSD: ffs.c,v 1.9 2014/06/10 04:16:57 deraadt Exp $ */
  2. /*
  3. * Public domain.
  4. * Written by Dale Rahn.
  5. */
  6. #include <lib/libkern/libkern.h>
  7. /*
  8. * ffs -- vax ffs instruction
  9. */
  10. int
  11. ffs(int mask)
  12. {
  13. int bit;
  14. unsigned int r = mask;
  15. static const signed char t[16] = {
  16. -28, 1, 2, 1,
  17. 3, 1, 2, 1,
  18. 4, 1, 2, 1,
  19. 3, 1, 2, 1
  20. };
  21. bit = 0;
  22. if (!(r & 0xffff)) {
  23. bit += 16;
  24. r >>= 16;
  25. }
  26. if (!(r & 0xff)) {
  27. bit += 8;
  28. r >>= 8;
  29. }
  30. if (!(r & 0xf)) {
  31. bit += 4;
  32. r >>= 4;
  33. }
  34. return (bit + t[ r & 0xf ]);
  35. }