bitops.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* MN10300 Non-trivial bit operations
  2. *
  3. * Copyright (C) 2007 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 Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <asm/bitops.h>
  13. /*
  14. * try flipping a bit using BSET and BCLR
  15. */
  16. void change_bit(unsigned long nr, volatile void *addr)
  17. {
  18. if (test_bit(nr, addr))
  19. goto try_clear_bit;
  20. try_set_bit:
  21. if (!test_and_set_bit(nr, addr))
  22. return;
  23. try_clear_bit:
  24. if (test_and_clear_bit(nr, addr))
  25. return;
  26. goto try_set_bit;
  27. }
  28. /*
  29. * try flipping a bit using BSET and BCLR and returning the old value
  30. */
  31. int test_and_change_bit(unsigned long nr, volatile void *addr)
  32. {
  33. if (test_bit(nr, addr))
  34. goto try_clear_bit;
  35. try_set_bit:
  36. if (!test_and_set_bit(nr, addr))
  37. return 0;
  38. try_clear_bit:
  39. if (test_and_clear_bit(nr, addr))
  40. return 1;
  41. goto try_set_bit;
  42. }