bitops.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * include/asm-xtensa/bitops.h
  3. *
  4. * Atomic operations that C can't guarantee us.Useful for resource counting etc.
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. *
  10. * Copyright (C) 2001 - 2005 Tensilica Inc.
  11. */
  12. #ifndef _XTENSA_BITOPS_H
  13. #define _XTENSA_BITOPS_H
  14. #ifdef __KERNEL__
  15. #include <asm/processor.h>
  16. #include <asm/byteorder.h>
  17. #include <asm/system.h>
  18. #ifdef CONFIG_SMP
  19. # error SMP not supported on this architecture
  20. #endif
  21. #define smp_mb__before_clear_bit() barrier()
  22. #define smp_mb__after_clear_bit() barrier()
  23. #include <asm-generic/bitops/atomic.h>
  24. #include <asm-generic/bitops/non-atomic.h>
  25. #if XCHAL_HAVE_NSA
  26. static __inline__ int __cntlz (unsigned long x)
  27. {
  28. int lz;
  29. asm ("nsau %0, %1" : "=r" (lz) : "r" (x));
  30. return 31 - lz;
  31. }
  32. #else
  33. static __inline__ int __cntlz (unsigned long x)
  34. {
  35. unsigned long sum, x1, x2, x4, x8, x16;
  36. x1 = x & 0xAAAAAAAA;
  37. x2 = x & 0xCCCCCCCC;
  38. x4 = x & 0xF0F0F0F0;
  39. x8 = x & 0xFF00FF00;
  40. x16 = x & 0xFFFF0000;
  41. sum = x2 ? 2 : 0;
  42. sum += (x16 != 0) * 16;
  43. sum += (x8 != 0) * 8;
  44. sum += (x4 != 0) * 4;
  45. sum += (x1 != 0);
  46. return sum;
  47. }
  48. #endif
  49. /*
  50. * ffz: Find first zero in word. Undefined if no zero exists.
  51. * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
  52. */
  53. static __inline__ int ffz(unsigned long x)
  54. {
  55. if ((x = ~x) == 0)
  56. return 32;
  57. return __cntlz(x & -x);
  58. }
  59. /*
  60. * __ffs: Find first bit set in word. Return 0 for bit 0
  61. */
  62. static __inline__ int __ffs(unsigned long x)
  63. {
  64. return __cntlz(x & -x);
  65. }
  66. /*
  67. * ffs: Find first bit set in word. This is defined the same way as
  68. * the libc and compiler builtin ffs routines, therefore
  69. * differs in spirit from the above ffz (man ffs).
  70. */
  71. static __inline__ int ffs(unsigned long x)
  72. {
  73. return __cntlz(x & -x) + 1;
  74. }
  75. /*
  76. * fls: Find last (most-significant) bit set in word.
  77. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
  78. */
  79. static __inline__ int fls (unsigned int x)
  80. {
  81. return __cntlz(x);
  82. }
  83. #include <asm-generic/bitops/fls64.h>
  84. #include <asm-generic/bitops/find.h>
  85. #include <asm-generic/bitops/ext2-non-atomic.h>
  86. #ifdef __XTENSA_EL__
  87. # define ext2_set_bit_atomic(lock,nr,addr) test_and_set_bit((nr),(addr))
  88. # define ext2_clear_bit_atomic(lock,nr,addr) test_and_clear_bit((nr),(addr))
  89. #elif defined(__XTENSA_EB__)
  90. # define ext2_set_bit_atomic(lock,nr,addr) test_and_set_bit((nr) ^ 0x18, (addr))
  91. # define ext2_clear_bit_atomic(lock,nr,addr) test_and_clear_bit((nr)^0x18,(addr))
  92. #else
  93. # error processor byte order undefined!
  94. #endif
  95. #include <asm-generic/bitops/hweight.h>
  96. #include <asm-generic/bitops/sched.h>
  97. #include <asm-generic/bitops/minix.h>
  98. #endif /* __KERNEL__ */
  99. #endif /* _XTENSA_BITOPS_H */