word-at-a-time.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #ifndef _ASM_WORD_AT_A_TIME_H
  2. #define _ASM_WORD_AT_A_TIME_H
  3. #include <linux/kernel.h>
  4. /*
  5. * This is largely generic for little-endian machines, but the
  6. * optimal byte mask counting is probably going to be something
  7. * that is architecture-specific. If you have a reliably fast
  8. * bit count instruction, that might be better than the multiply
  9. * and shift, for example.
  10. */
  11. struct word_at_a_time {
  12. const unsigned long one_bits, high_bits;
  13. };
  14. #define WORD_AT_A_TIME_CONSTANTS { REPEAT_BYTE(0x01), REPEAT_BYTE(0x80) }
  15. #ifdef CONFIG_64BIT
  16. /*
  17. * Jan Achrenius on G+: microoptimized version of
  18. * the simpler "(mask & ONEBYTES) * ONEBYTES >> 56"
  19. * that works for the bytemasks without having to
  20. * mask them first.
  21. */
  22. static inline long count_masked_bytes(unsigned long mask)
  23. {
  24. return mask*0x0001020304050608ul >> 56;
  25. }
  26. #else /* 32-bit case */
  27. /* Carl Chatfield / Jan Achrenius G+ version for 32-bit */
  28. static inline long count_masked_bytes(long mask)
  29. {
  30. /* (000000 0000ff 00ffff ffffff) -> ( 1 1 2 3 ) */
  31. long a = (0x0ff0001+mask) >> 23;
  32. /* Fix the 1 for 00 case */
  33. return a & mask;
  34. }
  35. #endif
  36. /* Return nonzero if it has a zero */
  37. static inline unsigned long has_zero(unsigned long a, unsigned long *bits, const struct word_at_a_time *c)
  38. {
  39. unsigned long mask = ((a - c->one_bits) & ~a) & c->high_bits;
  40. *bits = mask;
  41. return mask;
  42. }
  43. static inline unsigned long prep_zero_mask(unsigned long a, unsigned long bits, const struct word_at_a_time *c)
  44. {
  45. return bits;
  46. }
  47. static inline unsigned long create_zero_mask(unsigned long bits)
  48. {
  49. bits = (bits - 1) & ~bits;
  50. return bits >> 7;
  51. }
  52. /* The mask we created is directly usable as a bytemask */
  53. #define zero_bytemask(mask) (mask)
  54. static inline unsigned long find_zero(unsigned long mask)
  55. {
  56. return count_masked_bytes(mask);
  57. }
  58. /*
  59. * Load an unaligned word from kernel space.
  60. *
  61. * In the (very unlikely) case of the word being a page-crosser
  62. * and the next page not being mapped, take the exception and
  63. * return zeroes in the non-existing part.
  64. */
  65. static inline unsigned long load_unaligned_zeropad(const void *addr)
  66. {
  67. unsigned long ret, dummy;
  68. asm(
  69. "1:\tmov %2,%0\n"
  70. "2:\n"
  71. ".section .fixup,\"ax\"\n"
  72. "3:\t"
  73. "lea %2,%1\n\t"
  74. "and %3,%1\n\t"
  75. "mov (%1),%0\n\t"
  76. "leal %2,%%ecx\n\t"
  77. "andl %4,%%ecx\n\t"
  78. "shll $3,%%ecx\n\t"
  79. "shr %%cl,%0\n\t"
  80. "jmp 2b\n"
  81. ".previous\n"
  82. _ASM_EXTABLE(1b, 3b)
  83. :"=&r" (ret),"=&c" (dummy)
  84. :"m" (*(unsigned long *)addr),
  85. "i" (-sizeof(unsigned long)),
  86. "i" (sizeof(unsigned long)-1));
  87. return ret;
  88. }
  89. #endif /* _ASM_WORD_AT_A_TIME_H */