word-at-a-time.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #include <asm/extable.h>
  9. #ifdef __BIG_ENDIAN__
  10. struct word_at_a_time {
  11. const unsigned long high_bits, low_bits;
  12. };
  13. #define WORD_AT_A_TIME_CONSTANTS { REPEAT_BYTE(0xfe) + 1, REPEAT_BYTE(0x7f) }
  14. /* Bit set in the bytes that have a zero */
  15. static inline long prep_zero_mask(unsigned long val, unsigned long rhs, const struct word_at_a_time *c)
  16. {
  17. unsigned long mask = (val & c->low_bits) + c->low_bits;
  18. return ~(mask | rhs);
  19. }
  20. #define create_zero_mask(mask) (mask)
  21. static inline long find_zero(unsigned long mask)
  22. {
  23. long leading_zero_bits;
  24. asm (PPC_CNTLZL "%0,%1" : "=r" (leading_zero_bits) : "r" (mask));
  25. return leading_zero_bits >> 3;
  26. }
  27. static inline unsigned long has_zero(unsigned long val, unsigned long *data, const struct word_at_a_time *c)
  28. {
  29. unsigned long rhs = val | c->low_bits;
  30. *data = rhs;
  31. return (val + c->high_bits) & ~rhs;
  32. }
  33. static inline unsigned long zero_bytemask(unsigned long mask)
  34. {
  35. return ~1ul << __fls(mask);
  36. }
  37. #else
  38. #ifdef CONFIG_64BIT
  39. /* unused */
  40. struct word_at_a_time {
  41. };
  42. #define WORD_AT_A_TIME_CONSTANTS { }
  43. /* This will give us 0xff for a NULL char and 0x00 elsewhere */
  44. static inline unsigned long has_zero(unsigned long a, unsigned long *bits, const struct word_at_a_time *c)
  45. {
  46. unsigned long ret;
  47. unsigned long zero = 0;
  48. asm("cmpb %0,%1,%2" : "=r" (ret) : "r" (a), "r" (zero));
  49. *bits = ret;
  50. return ret;
  51. }
  52. static inline unsigned long prep_zero_mask(unsigned long a, unsigned long bits, const struct word_at_a_time *c)
  53. {
  54. return bits;
  55. }
  56. /* Alan Modra's little-endian strlen tail for 64-bit */
  57. static inline unsigned long create_zero_mask(unsigned long bits)
  58. {
  59. unsigned long leading_zero_bits;
  60. long trailing_zero_bit_mask;
  61. asm("addi %1,%2,-1\n\t"
  62. "andc %1,%1,%2\n\t"
  63. "popcntd %0,%1"
  64. : "=r" (leading_zero_bits), "=&r" (trailing_zero_bit_mask)
  65. : "b" (bits));
  66. return leading_zero_bits;
  67. }
  68. static inline unsigned long find_zero(unsigned long mask)
  69. {
  70. return mask >> 3;
  71. }
  72. /* This assumes that we never ask for an all 1s bitmask */
  73. static inline unsigned long zero_bytemask(unsigned long mask)
  74. {
  75. return (1UL << mask) - 1;
  76. }
  77. #else /* 32-bit case */
  78. struct word_at_a_time {
  79. const unsigned long one_bits, high_bits;
  80. };
  81. #define WORD_AT_A_TIME_CONSTANTS { REPEAT_BYTE(0x01), REPEAT_BYTE(0x80) }
  82. /*
  83. * This is largely generic for little-endian machines, but the
  84. * optimal byte mask counting is probably going to be something
  85. * that is architecture-specific. If you have a reliably fast
  86. * bit count instruction, that might be better than the multiply
  87. * and shift, for example.
  88. */
  89. /* Carl Chatfield / Jan Achrenius G+ version for 32-bit */
  90. static inline long count_masked_bytes(long mask)
  91. {
  92. /* (000000 0000ff 00ffff ffffff) -> ( 1 1 2 3 ) */
  93. long a = (0x0ff0001+mask) >> 23;
  94. /* Fix the 1 for 00 case */
  95. return a & mask;
  96. }
  97. static inline unsigned long create_zero_mask(unsigned long bits)
  98. {
  99. bits = (bits - 1) & ~bits;
  100. return bits >> 7;
  101. }
  102. static inline unsigned long find_zero(unsigned long mask)
  103. {
  104. return count_masked_bytes(mask);
  105. }
  106. /* Return nonzero if it has a zero */
  107. static inline unsigned long has_zero(unsigned long a, unsigned long *bits, const struct word_at_a_time *c)
  108. {
  109. unsigned long mask = ((a - c->one_bits) & ~a) & c->high_bits;
  110. *bits = mask;
  111. return mask;
  112. }
  113. static inline unsigned long prep_zero_mask(unsigned long a, unsigned long bits, const struct word_at_a_time *c)
  114. {
  115. return bits;
  116. }
  117. /* The mask we created is directly usable as a bytemask */
  118. #define zero_bytemask(mask) (mask)
  119. #endif /* CONFIG_64BIT */
  120. #endif /* __BIG_ENDIAN__ */
  121. /*
  122. * We use load_unaligned_zero() in a selftest, which builds a userspace
  123. * program. Some linker scripts seem to discard the .fixup section, so allow
  124. * the test code to use a different section name.
  125. */
  126. #ifndef FIXUP_SECTION
  127. #define FIXUP_SECTION ".fixup"
  128. #endif
  129. static inline unsigned long load_unaligned_zeropad(const void *addr)
  130. {
  131. unsigned long ret, offset, tmp;
  132. asm(
  133. "1: " PPC_LL "%[ret], 0(%[addr])\n"
  134. "2:\n"
  135. ".section " FIXUP_SECTION ",\"ax\"\n"
  136. "3: "
  137. #ifdef __powerpc64__
  138. "clrrdi %[tmp], %[addr], 3\n\t"
  139. "clrlsldi %[offset], %[addr], 61, 3\n\t"
  140. "ld %[ret], 0(%[tmp])\n\t"
  141. #ifdef __BIG_ENDIAN__
  142. "sld %[ret], %[ret], %[offset]\n\t"
  143. #else
  144. "srd %[ret], %[ret], %[offset]\n\t"
  145. #endif
  146. #else
  147. "clrrwi %[tmp], %[addr], 2\n\t"
  148. "clrlslwi %[offset], %[addr], 30, 3\n\t"
  149. "lwz %[ret], 0(%[tmp])\n\t"
  150. #ifdef __BIG_ENDIAN__
  151. "slw %[ret], %[ret], %[offset]\n\t"
  152. #else
  153. "srw %[ret], %[ret], %[offset]\n\t"
  154. #endif
  155. #endif
  156. "b 2b\n"
  157. ".previous\n"
  158. EX_TABLE(1b, 3b)
  159. : [tmp] "=&b" (tmp), [offset] "=&r" (offset), [ret] "=&r" (ret)
  160. : [addr] "b" (addr), "m" (*(unsigned long *)addr));
  161. return ret;
  162. }
  163. #undef FIXUP_SECTION
  164. #endif /* _ASM_WORD_AT_A_TIME_H */