find_next_bit.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* find_next_bit.c: fallback find next bit implementation
  2. *
  3. * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/bitops.h>
  12. int find_next_bit(const unsigned long *addr, int size, int offset)
  13. {
  14. const unsigned long *base;
  15. const int NBITS = sizeof(*addr) * 8;
  16. unsigned long tmp;
  17. base = addr;
  18. if (offset) {
  19. int suboffset;
  20. addr += offset / NBITS;
  21. suboffset = offset % NBITS;
  22. if (suboffset) {
  23. tmp = *addr;
  24. tmp >>= suboffset;
  25. if (tmp)
  26. goto finish;
  27. }
  28. addr++;
  29. }
  30. while ((tmp = *addr) == 0)
  31. addr++;
  32. offset = (addr - base) * NBITS;
  33. finish:
  34. /* count the remaining bits without using __ffs() since that takes a 32-bit arg */
  35. while (!(tmp & 0xff)) {
  36. offset += 8;
  37. tmp >>= 8;
  38. }
  39. while (!(tmp & 1)) {
  40. offset++;
  41. tmp >>= 1;
  42. }
  43. return offset;
  44. }