word-at-a-time.h 4.7 KB

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