bitops.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* asm/arch/bitops.h for Linux/CRISv10 */
  2. #ifndef _CRIS_ARCH_BITOPS_H
  3. #define _CRIS_ARCH_BITOPS_H
  4. /*
  5. * Helper functions for the core of the ff[sz] functions, wrapping the
  6. * syntactically awkward asms. The asms compute the number of leading
  7. * zeroes of a bits-in-byte and byte-in-word and word-in-dword-swapped
  8. * number. They differ in that the first function also inverts all bits
  9. * in the input.
  10. */
  11. extern inline unsigned long cris_swapnwbrlz(unsigned long w)
  12. {
  13. /* Let's just say we return the result in the same register as the
  14. input. Saying we clobber the input but can return the result
  15. in another register:
  16. ! __asm__ ("swapnwbr %2\n\tlz %2,%0"
  17. ! : "=r,r" (res), "=r,X" (dummy) : "1,0" (w));
  18. confuses gcc (sched.c, gcc from cris-dist-1.14). */
  19. unsigned long res;
  20. __asm__ ("swapnwbr %0 \n\t"
  21. "lz %0,%0"
  22. : "=r" (res) : "0" (w));
  23. return res;
  24. }
  25. extern inline unsigned long cris_swapwbrlz(unsigned long w)
  26. {
  27. unsigned res;
  28. __asm__ ("swapwbr %0 \n\t"
  29. "lz %0,%0"
  30. : "=r" (res)
  31. : "0" (w));
  32. return res;
  33. }
  34. /*
  35. * ffz = Find First Zero in word. Undefined if no zero exists,
  36. * so code should check against ~0UL first..
  37. */
  38. extern inline unsigned long ffz(unsigned long w)
  39. {
  40. return cris_swapnwbrlz(w);
  41. }
  42. /**
  43. * __ffs - find first bit in word.
  44. * @word: The word to search
  45. *
  46. * Undefined if no bit exists, so code should check against 0 first.
  47. */
  48. extern inline unsigned long __ffs(unsigned long word)
  49. {
  50. return cris_swapnwbrlz(~word);
  51. }
  52. /**
  53. * ffs - find first bit set
  54. * @x: the word to search
  55. *
  56. * This is defined the same way as
  57. * the libc and compiler builtin ffs routines, therefore
  58. * differs in spirit from the above ffz (man ffs).
  59. */
  60. extern inline unsigned long kernel_ffs(unsigned long w)
  61. {
  62. return w ? cris_swapwbrlz (w) + 1 : 0;
  63. }
  64. #endif