find_last_bit.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /* find_last_bit.c: fallback find next bit implementation
  2. *
  3. * Copyright (C) 2008 IBM Corporation
  4. * Written by Rusty Russell <rusty@rustcorp.com.au>
  5. * (Inspired by David Howell's find_next_bit implementation)
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/bitops.h>
  13. #include <linux/module.h>
  14. #include <asm/types.h>
  15. #include <asm/byteorder.h>
  16. unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
  17. {
  18. unsigned long words;
  19. unsigned long tmp;
  20. /* Start at final word. */
  21. words = size / BITS_PER_LONG;
  22. /* Partial final word? */
  23. if (size & (BITS_PER_LONG-1)) {
  24. tmp = (addr[words] & (~0UL >> (BITS_PER_LONG
  25. - (size & (BITS_PER_LONG-1)))));
  26. if (tmp)
  27. goto found;
  28. }
  29. while (words) {
  30. tmp = addr[--words];
  31. if (tmp) {
  32. found:
  33. return words * BITS_PER_LONG + __fls(tmp);
  34. }
  35. }
  36. /* Not found */
  37. return size;
  38. }
  39. EXPORT_SYMBOL(find_last_bit);