1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_WORD_AT_A_TIME_H
3 #define _ASM_WORD_AT_A_TIME_H
5 #include <linux/bitops.h>
6 #include <linux/wordpart.h>
7 #include <asm/byteorder.h>
11 struct word_at_a_time
{
12 const unsigned long high_bits
, low_bits
;
15 #define WORD_AT_A_TIME_CONSTANTS { REPEAT_BYTE(0xfe) + 1, REPEAT_BYTE(0x7f) }
17 /* Bit set in the bytes that have a zero */
18 static inline long prep_zero_mask(unsigned long val
, unsigned long rhs
, const struct word_at_a_time
*c
)
20 unsigned long mask
= (val
& c
->low_bits
) + c
->low_bits
;
24 #define create_zero_mask(mask) (mask)
26 static inline long find_zero(unsigned long mask
)
39 return (mask
>> 8) ? byte
: byte
+ 1;
42 static inline unsigned long has_zero(unsigned long val
, unsigned long *data
, const struct word_at_a_time
*c
)
44 unsigned long rhs
= val
| c
->low_bits
;
46 return (val
+ c
->high_bits
) & ~rhs
;
50 #define zero_bytemask(mask) (~1ul << __fls(mask))
56 * The optimal byte mask counting is probably going to be something
57 * that is architecture-specific. If you have a reliably fast
58 * bit count instruction, that might be better than the multiply
59 * and shift, for example.
61 struct word_at_a_time
{
62 const unsigned long one_bits
, high_bits
;
65 #define WORD_AT_A_TIME_CONSTANTS { REPEAT_BYTE(0x01), REPEAT_BYTE(0x80) }
70 * Jan Achrenius on G+: microoptimized version of
71 * the simpler "(mask & ONEBYTES) * ONEBYTES >> 56"
72 * that works for the bytemasks without having to
75 static inline long count_masked_bytes(unsigned long mask
)
77 return mask
*0x0001020304050608ul
>> 56;
80 #else /* 32-bit case */
82 /* Carl Chatfield / Jan Achrenius G+ version for 32-bit */
83 static inline long count_masked_bytes(long mask
)
85 /* (000000 0000ff 00ffff ffffff) -> ( 1 1 2 3 ) */
86 long a
= (0x0ff0001+mask
) >> 23;
87 /* Fix the 1 for 00 case */
93 /* Return nonzero if it has a zero */
94 static inline unsigned long has_zero(unsigned long a
, unsigned long *bits
, const struct word_at_a_time
*c
)
96 unsigned long mask
= ((a
- c
->one_bits
) & ~a
) & c
->high_bits
;
101 static inline unsigned long prep_zero_mask(unsigned long a
, unsigned long bits
, const struct word_at_a_time
*c
)
106 static inline unsigned long create_zero_mask(unsigned long bits
)
108 bits
= (bits
- 1) & ~bits
;
112 /* The mask we created is directly usable as a bytemask */
113 #define zero_bytemask(mask) (mask)
115 static inline unsigned long find_zero(unsigned long mask
)
117 return count_masked_bytes(mask
);
120 #endif /* __BIG_ENDIAN */
122 #endif /* _ASM_WORD_AT_A_TIME_H */