find_next_bit.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. #include <linux/module.h>
  13. int find_next_bit(const unsigned long *addr, int size, int offset)
  14. {
  15. const unsigned long *base;
  16. const int NBITS = sizeof(*addr) * 8;
  17. unsigned long tmp;
  18. base = addr;
  19. if (offset) {
  20. int suboffset;
  21. addr += offset / NBITS;
  22. suboffset = offset % NBITS;
  23. if (suboffset) {
  24. tmp = *addr;
  25. tmp >>= suboffset;
  26. if (tmp)
  27. goto finish;
  28. }
  29. addr++;
  30. }
  31. while ((tmp = *addr) == 0)
  32. addr++;
  33. offset = (addr - base) * NBITS;
  34. finish:
  35. /* count the remaining bits without using __ffs() since that takes a 32-bit arg */
  36. while (!(tmp & 0xff)) {
  37. offset += 8;
  38. tmp >>= 8;
  39. }
  40. while (!(tmp & 1)) {
  41. offset++;
  42. tmp >>= 1;
  43. }
  44. return offset;
  45. }
  46. EXPORT_SYMBOL(find_next_bit);