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. #include <asm/system.h>
  14. /*
  15. * try flipping a bit using BSET and BCLR
  16. */
  17. void change_bit(int nr, volatile void *addr)
  18. {
  19. if (test_bit(nr, addr))
  20. goto try_clear_bit;
  21. try_set_bit:
  22. if (!test_and_set_bit(nr, addr))
  23. return;
  24. try_clear_bit:
  25. if (test_and_clear_bit(nr, addr))
  26. return;
  27. goto try_set_bit;
  28. }
  29. /*
  30. * try flipping a bit using BSET and BCLR and returning the old value
  31. */
  32. int test_and_change_bit(int nr, volatile void *addr)
  33. {
  34. if (test_bit(nr, addr))
  35. goto try_clear_bit;
  36. try_set_bit:
  37. if (!test_and_set_bit(nr, addr))
  38. return 0;
  39. try_clear_bit:
  40. if (test_and_clear_bit(nr, addr))
  41. return 1;
  42. goto try_set_bit;
  43. }