bitops.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright 1992, Linus Torvalds.
  3. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation, version 2.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  12. * NON INFRINGEMENT. See the GNU General Public License for
  13. * more details.
  14. */
  15. #ifndef _ASM_TILE_BITOPS_H
  16. #define _ASM_TILE_BITOPS_H
  17. #include <linux/types.h>
  18. #ifndef _LINUX_BITOPS_H
  19. #error only <linux/bitops.h> can be included directly
  20. #endif
  21. #ifdef __tilegx__
  22. #include <asm/bitops_64.h>
  23. #else
  24. #include <asm/bitops_32.h>
  25. #endif
  26. /**
  27. * __ffs - find first set bit in word
  28. * @word: The word to search
  29. *
  30. * Undefined if no set bit exists, so code should check against 0 first.
  31. */
  32. static inline unsigned long __ffs(unsigned long word)
  33. {
  34. return __builtin_ctzl(word);
  35. }
  36. /**
  37. * ffz - find first zero bit in word
  38. * @word: The word to search
  39. *
  40. * Undefined if no zero exists, so code should check against ~0UL first.
  41. */
  42. static inline unsigned long ffz(unsigned long word)
  43. {
  44. return __builtin_ctzl(~word);
  45. }
  46. /**
  47. * __fls - find last set bit in word
  48. * @word: The word to search
  49. *
  50. * Undefined if no set bit exists, so code should check against 0 first.
  51. */
  52. static inline unsigned long __fls(unsigned long word)
  53. {
  54. return (sizeof(word) * 8) - 1 - __builtin_clzl(word);
  55. }
  56. /**
  57. * ffs - find first set bit in word
  58. * @x: the word to search
  59. *
  60. * This is defined the same way as the libc and compiler builtin ffs
  61. * routines, therefore differs in spirit from the other bitops.
  62. *
  63. * ffs(value) returns 0 if value is 0 or the position of the first
  64. * set bit if value is nonzero. The first (least significant) bit
  65. * is at position 1.
  66. */
  67. static inline int ffs(int x)
  68. {
  69. return __builtin_ffs(x);
  70. }
  71. static inline int fls64(__u64 w)
  72. {
  73. return (sizeof(__u64) * 8) - __builtin_clzll(w);
  74. }
  75. /**
  76. * fls - find last set bit in word
  77. * @x: the word to search
  78. *
  79. * This is defined in a similar way as the libc and compiler builtin
  80. * ffs, but returns the position of the most significant set bit.
  81. *
  82. * fls(value) returns 0 if value is 0 or the position of the last
  83. * set bit if value is nonzero. The last (most significant) bit is
  84. * at position 32.
  85. */
  86. static inline int fls(int x)
  87. {
  88. return fls64((unsigned int) x);
  89. }
  90. static inline unsigned int __arch_hweight32(unsigned int w)
  91. {
  92. return __builtin_popcount(w);
  93. }
  94. static inline unsigned int __arch_hweight16(unsigned int w)
  95. {
  96. return __builtin_popcount(w & 0xffff);
  97. }
  98. static inline unsigned int __arch_hweight8(unsigned int w)
  99. {
  100. return __builtin_popcount(w & 0xff);
  101. }
  102. static inline unsigned long __arch_hweight64(__u64 w)
  103. {
  104. return __builtin_popcountll(w);
  105. }
  106. #include <asm-generic/bitops/const_hweight.h>
  107. #include <asm-generic/bitops/lock.h>
  108. #include <asm-generic/bitops/find.h>
  109. #include <asm-generic/bitops/sched.h>
  110. #include <asm-generic/bitops/non-atomic.h>
  111. #include <asm-generic/bitops/le.h>
  112. #endif /* _ASM_TILE_BITOPS_H */