find_last_bit.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. #ifndef find_last_bit
  17. unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
  18. {
  19. unsigned long words;
  20. unsigned long tmp;
  21. /* Start at final word. */
  22. words = size / BITS_PER_LONG;
  23. /* Partial final word? */
  24. if (size & (BITS_PER_LONG-1)) {
  25. tmp = (addr[words] & (~0UL >> (BITS_PER_LONG
  26. - (size & (BITS_PER_LONG-1)))));
  27. if (tmp)
  28. goto found;
  29. }
  30. while (words) {
  31. tmp = addr[--words];
  32. if (tmp) {
  33. found:
  34. return words * BITS_PER_LONG + __fls(tmp);
  35. }
  36. }
  37. /* Not found */
  38. return size;
  39. }
  40. EXPORT_SYMBOL(find_last_bit);
  41. #endif