1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
236126f8fSLinus Torvalds #ifndef _ASM_WORD_AT_A_TIME_H
336126f8fSLinus Torvalds #define _ASM_WORD_AT_A_TIME_H
436126f8fSLinus Torvalds
536126f8fSLinus Torvalds #include <linux/kernel.h>
6a6e2f029SChris Metcalf #include <asm/byteorder.h>
7a6e2f029SChris Metcalf
8a6e2f029SChris Metcalf #ifdef __BIG_ENDIAN
936126f8fSLinus Torvalds
1036126f8fSLinus Torvalds struct word_at_a_time {
1136126f8fSLinus Torvalds const unsigned long high_bits, low_bits;
1236126f8fSLinus Torvalds };
1336126f8fSLinus Torvalds
1436126f8fSLinus Torvalds #define WORD_AT_A_TIME_CONSTANTS { REPEAT_BYTE(0xfe) + 1, REPEAT_BYTE(0x7f) }
1536126f8fSLinus Torvalds
1636126f8fSLinus Torvalds /* Bit set in the bytes that have a zero */
prep_zero_mask(unsigned long val,unsigned long rhs,const struct word_at_a_time * c)1736126f8fSLinus Torvalds static inline long prep_zero_mask(unsigned long val, unsigned long rhs, const struct word_at_a_time *c)
1836126f8fSLinus Torvalds {
1936126f8fSLinus Torvalds unsigned long mask = (val & c->low_bits) + c->low_bits;
2036126f8fSLinus Torvalds return ~(mask | rhs);
2136126f8fSLinus Torvalds }
2236126f8fSLinus Torvalds
2336126f8fSLinus Torvalds #define create_zero_mask(mask) (mask)
2436126f8fSLinus Torvalds
find_zero(unsigned long mask)2536126f8fSLinus Torvalds static inline long find_zero(unsigned long mask)
2636126f8fSLinus Torvalds {
2736126f8fSLinus Torvalds long byte = 0;
2836126f8fSLinus Torvalds #ifdef CONFIG_64BIT
2936126f8fSLinus Torvalds if (mask >> 32)
3036126f8fSLinus Torvalds mask >>= 32;
3136126f8fSLinus Torvalds else
3236126f8fSLinus Torvalds byte = 4;
3336126f8fSLinus Torvalds #endif
3436126f8fSLinus Torvalds if (mask >> 16)
3536126f8fSLinus Torvalds mask >>= 16;
3636126f8fSLinus Torvalds else
3736126f8fSLinus Torvalds byte += 2;
3836126f8fSLinus Torvalds return (mask >> 8) ? byte : byte + 1;
3936126f8fSLinus Torvalds }
4036126f8fSLinus Torvalds
has_zero(unsigned long val,unsigned long * data,const struct word_at_a_time * c)41*79e8328eSndesaulniers@google.com static inline unsigned long has_zero(unsigned long val, unsigned long *data, const struct word_at_a_time *c)
4236126f8fSLinus Torvalds {
4336126f8fSLinus Torvalds unsigned long rhs = val | c->low_bits;
4436126f8fSLinus Torvalds *data = rhs;
4536126f8fSLinus Torvalds return (val + c->high_bits) & ~rhs;
4636126f8fSLinus Torvalds }
4736126f8fSLinus Torvalds
4811ec50caSWill Deacon #ifndef zero_bytemask
49789ce9dcSH. Peter Anvin #define zero_bytemask(mask) (~1ul << __fls(mask))
50ec6931b2SWill Deacon #endif
5111ec50caSWill Deacon
52a6e2f029SChris Metcalf #else
53a6e2f029SChris Metcalf
54a6e2f029SChris Metcalf /*
55a6e2f029SChris Metcalf * The optimal byte mask counting is probably going to be something
56a6e2f029SChris Metcalf * that is architecture-specific. If you have a reliably fast
57a6e2f029SChris Metcalf * bit count instruction, that might be better than the multiply
58a6e2f029SChris Metcalf * and shift, for example.
59a6e2f029SChris Metcalf */
60a6e2f029SChris Metcalf struct word_at_a_time {
61a6e2f029SChris Metcalf const unsigned long one_bits, high_bits;
62a6e2f029SChris Metcalf };
63a6e2f029SChris Metcalf
64a6e2f029SChris Metcalf #define WORD_AT_A_TIME_CONSTANTS { REPEAT_BYTE(0x01), REPEAT_BYTE(0x80) }
65a6e2f029SChris Metcalf
66a6e2f029SChris Metcalf #ifdef CONFIG_64BIT
67a6e2f029SChris Metcalf
68a6e2f029SChris Metcalf /*
69a6e2f029SChris Metcalf * Jan Achrenius on G+: microoptimized version of
70a6e2f029SChris Metcalf * the simpler "(mask & ONEBYTES) * ONEBYTES >> 56"
71a6e2f029SChris Metcalf * that works for the bytemasks without having to
72a6e2f029SChris Metcalf * mask them first.
73a6e2f029SChris Metcalf */
count_masked_bytes(unsigned long mask)74a6e2f029SChris Metcalf static inline long count_masked_bytes(unsigned long mask)
75a6e2f029SChris Metcalf {
76a6e2f029SChris Metcalf return mask*0x0001020304050608ul >> 56;
77a6e2f029SChris Metcalf }
78a6e2f029SChris Metcalf
79a6e2f029SChris Metcalf #else /* 32-bit case */
80a6e2f029SChris Metcalf
81a6e2f029SChris Metcalf /* Carl Chatfield / Jan Achrenius G+ version for 32-bit */
count_masked_bytes(long mask)82a6e2f029SChris Metcalf static inline long count_masked_bytes(long mask)
83a6e2f029SChris Metcalf {
84a6e2f029SChris Metcalf /* (000000 0000ff 00ffff ffffff) -> ( 1 1 2 3 ) */
85a6e2f029SChris Metcalf long a = (0x0ff0001+mask) >> 23;
86a6e2f029SChris Metcalf /* Fix the 1 for 00 case */
87a6e2f029SChris Metcalf return a & mask;
88a6e2f029SChris Metcalf }
89a6e2f029SChris Metcalf
90a6e2f029SChris Metcalf #endif
91a6e2f029SChris Metcalf
92a6e2f029SChris Metcalf /* Return nonzero if it has a zero */
has_zero(unsigned long a,unsigned long * bits,const struct word_at_a_time * c)93a6e2f029SChris Metcalf static inline unsigned long has_zero(unsigned long a, unsigned long *bits, const struct word_at_a_time *c)
94a6e2f029SChris Metcalf {
95a6e2f029SChris Metcalf unsigned long mask = ((a - c->one_bits) & ~a) & c->high_bits;
96a6e2f029SChris Metcalf *bits = mask;
97a6e2f029SChris Metcalf return mask;
98a6e2f029SChris Metcalf }
99a6e2f029SChris Metcalf
prep_zero_mask(unsigned long a,unsigned long bits,const struct word_at_a_time * c)100a6e2f029SChris Metcalf static inline unsigned long prep_zero_mask(unsigned long a, unsigned long bits, const struct word_at_a_time *c)
101a6e2f029SChris Metcalf {
102a6e2f029SChris Metcalf return bits;
103a6e2f029SChris Metcalf }
104a6e2f029SChris Metcalf
create_zero_mask(unsigned long bits)105a6e2f029SChris Metcalf static inline unsigned long create_zero_mask(unsigned long bits)
106a6e2f029SChris Metcalf {
107a6e2f029SChris Metcalf bits = (bits - 1) & ~bits;
108a6e2f029SChris Metcalf return bits >> 7;
109a6e2f029SChris Metcalf }
110a6e2f029SChris Metcalf
111a6e2f029SChris Metcalf /* The mask we created is directly usable as a bytemask */
112a6e2f029SChris Metcalf #define zero_bytemask(mask) (mask)
113a6e2f029SChris Metcalf
find_zero(unsigned long mask)114a6e2f029SChris Metcalf static inline unsigned long find_zero(unsigned long mask)
115a6e2f029SChris Metcalf {
116a6e2f029SChris Metcalf return count_masked_bytes(mask);
117a6e2f029SChris Metcalf }
118a6e2f029SChris Metcalf
119a6e2f029SChris Metcalf #endif /* __BIG_ENDIAN */
120a6e2f029SChris Metcalf
12136126f8fSLinus Torvalds #endif /* _ASM_WORD_AT_A_TIME_H */
122