word-at-a-time.h 4.8 KB

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