word-at-a-time.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_WORD_AT_A_TIME_H
  3. #define _ASM_WORD_AT_A_TIME_H
  4. #include <linux/bitops.h>
  5. #include <linux/wordpart.h>
  6. #include <asm/byteorder.h>
  7. #ifdef __BIG_ENDIAN
  8. struct word_at_a_time {
  9. const unsigned long high_bits, low_bits;
  10. };
  11. #define WORD_AT_A_TIME_CONSTANTS { REPEAT_BYTE(0xfe) + 1, REPEAT_BYTE(0x7f) }
  12. /* Bit set in the bytes that have a zero */
  13. static inline long prep_zero_mask(unsigned long val, unsigned long rhs, const struct word_at_a_time *c)
  14. {
  15. unsigned long mask = (val & c->low_bits) + c->low_bits;
  16. return ~(mask | rhs);
  17. }
  18. #define create_zero_mask(mask) (mask)
  19. static inline long find_zero(unsigned long mask)
  20. {
  21. long byte = 0;
  22. #ifdef CONFIG_64BIT
  23. if (mask >> 32)
  24. mask >>= 32;
  25. else
  26. byte = 4;
  27. #endif
  28. if (mask >> 16)
  29. mask >>= 16;
  30. else
  31. byte += 2;
  32. return (mask >> 8) ? byte : byte + 1;
  33. }
  34. static inline unsigned long has_zero(unsigned long val, unsigned long *data, const struct word_at_a_time *c)
  35. {
  36. unsigned long rhs = val | c->low_bits;
  37. *data = rhs;
  38. return (val + c->high_bits) & ~rhs;
  39. }
  40. #ifndef zero_bytemask
  41. #define zero_bytemask(mask) (~1ul << __fls(mask))
  42. #endif
  43. #else
  44. /*
  45. * The optimal byte mask counting is probably going to be something
  46. * that is architecture-specific. If you have a reliably fast
  47. * bit count instruction, that might be better than the multiply
  48. * and shift, for example.
  49. */
  50. struct word_at_a_time {
  51. const unsigned long one_bits, high_bits;
  52. };
  53. #define WORD_AT_A_TIME_CONSTANTS { REPEAT_BYTE(0x01), REPEAT_BYTE(0x80) }
  54. #ifdef CONFIG_64BIT
  55. /*
  56. * Jan Achrenius on G+: microoptimized version of
  57. * the simpler "(mask & ONEBYTES) * ONEBYTES >> 56"
  58. * that works for the bytemasks without having to
  59. * mask them first.
  60. */
  61. static inline long count_masked_bytes(unsigned long mask)
  62. {
  63. return mask*0x0001020304050608ul >> 56;
  64. }
  65. #else /* 32-bit case */
  66. /* Carl Chatfield / Jan Achrenius G+ version for 32-bit */
  67. static inline long count_masked_bytes(long mask)
  68. {
  69. /* (000000 0000ff 00ffff ffffff) -> ( 1 1 2 3 ) */
  70. long a = (0x0ff0001+mask) >> 23;
  71. /* Fix the 1 for 00 case */
  72. return a & mask;
  73. }
  74. #endif
  75. /* Return nonzero if it has a zero */
  76. static inline unsigned long has_zero(unsigned long a, unsigned long *bits, const struct word_at_a_time *c)
  77. {
  78. unsigned long mask = ((a - c->one_bits) & ~a) & c->high_bits;
  79. *bits = mask;
  80. return mask;
  81. }
  82. static inline unsigned long prep_zero_mask(unsigned long a, unsigned long bits, const struct word_at_a_time *c)
  83. {
  84. return bits;
  85. }
  86. static inline unsigned long create_zero_mask(unsigned long bits)
  87. {
  88. bits = (bits - 1) & ~bits;
  89. return bits >> 7;
  90. }
  91. /* The mask we created is directly usable as a bytemask */
  92. #define zero_bytemask(mask) (mask)
  93. static inline unsigned long find_zero(unsigned long mask)
  94. {
  95. return count_masked_bytes(mask);
  96. }
  97. #endif /* __BIG_ENDIAN */
  98. #endif /* _ASM_WORD_AT_A_TIME_H */