word-at-a-time.h 2.8 KB

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