word-at-a-time.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #ifndef _ASM_WORD_AT_A_TIME_H
  2. #define _ASM_WORD_AT_A_TIME_H
  3. /*
  4. * Word-at-a-time interfaces for PowerPC.
  5. */
  6. #include <linux/kernel.h>
  7. #include <asm/asm-compat.h>
  8. #ifdef __BIG_ENDIAN__
  9. struct word_at_a_time {
  10. const unsigned long high_bits, low_bits;
  11. };
  12. #define WORD_AT_A_TIME_CONSTANTS { REPEAT_BYTE(0xfe) + 1, REPEAT_BYTE(0x7f) }
  13. /* Bit set in the bytes that have a zero */
  14. static inline long prep_zero_mask(unsigned long val, unsigned long rhs, const struct word_at_a_time *c)
  15. {
  16. unsigned long mask = (val & c->low_bits) + c->low_bits;
  17. return ~(mask | rhs);
  18. }
  19. #define create_zero_mask(mask) (mask)
  20. static inline long find_zero(unsigned long mask)
  21. {
  22. long leading_zero_bits;
  23. asm (PPC_CNTLZL "%0,%1" : "=r" (leading_zero_bits) : "r" (mask));
  24. return leading_zero_bits >> 3;
  25. }
  26. static inline bool has_zero(unsigned long val, unsigned long *data, const struct word_at_a_time *c)
  27. {
  28. unsigned long rhs = val | c->low_bits;
  29. *data = rhs;
  30. return (val + c->high_bits) & ~rhs;
  31. }
  32. #else
  33. struct word_at_a_time {
  34. const unsigned long one_bits, high_bits;
  35. };
  36. #define WORD_AT_A_TIME_CONSTANTS { REPEAT_BYTE(0x01), REPEAT_BYTE(0x80) }
  37. #ifdef CONFIG_64BIT
  38. /* Alan Modra's little-endian strlen tail for 64-bit */
  39. #define create_zero_mask(mask) (mask)
  40. static inline unsigned long find_zero(unsigned long mask)
  41. {
  42. unsigned long leading_zero_bits;
  43. long trailing_zero_bit_mask;
  44. asm ("addi %1,%2,-1\n\t"
  45. "andc %1,%1,%2\n\t"
  46. "popcntd %0,%1"
  47. : "=r" (leading_zero_bits), "=&r" (trailing_zero_bit_mask)
  48. : "r" (mask));
  49. return leading_zero_bits >> 3;
  50. }
  51. #else /* 32-bit case */
  52. /*
  53. * This is largely generic for little-endian machines, but the
  54. * optimal byte mask counting is probably going to be something
  55. * that is architecture-specific. If you have a reliably fast
  56. * bit count instruction, that might be better than the multiply
  57. * and shift, for example.
  58. */
  59. /* Carl Chatfield / Jan Achrenius G+ version for 32-bit */
  60. static inline long count_masked_bytes(long mask)
  61. {
  62. /* (000000 0000ff 00ffff ffffff) -> ( 1 1 2 3 ) */
  63. long a = (0x0ff0001+mask) >> 23;
  64. /* Fix the 1 for 00 case */
  65. return a & mask;
  66. }
  67. static inline unsigned long create_zero_mask(unsigned long bits)
  68. {
  69. bits = (bits - 1) & ~bits;
  70. return bits >> 7;
  71. }
  72. static inline unsigned long find_zero(unsigned long mask)
  73. {
  74. return count_masked_bytes(mask);
  75. }
  76. #endif
  77. /* Return nonzero if it has a zero */
  78. static inline unsigned long has_zero(unsigned long a, unsigned long *bits, const struct word_at_a_time *c)
  79. {
  80. unsigned long mask = ((a - c->one_bits) & ~a) & c->high_bits;
  81. *bits = mask;
  82. return mask;
  83. }
  84. static inline unsigned long prep_zero_mask(unsigned long a, unsigned long bits, const struct word_at_a_time *c)
  85. {
  86. return bits;
  87. }
  88. /* The mask we created is directly usable as a bytemask */
  89. #define zero_bytemask(mask) (mask)
  90. #endif
  91. #endif /* _ASM_WORD_AT_A_TIME_H */