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