11629372cSPaul Mackerras #ifndef _ASM_WORD_AT_A_TIME_H
21629372cSPaul Mackerras #define _ASM_WORD_AT_A_TIME_H
31629372cSPaul Mackerras
41629372cSPaul Mackerras /*
51629372cSPaul Mackerras * Word-at-a-time interfaces for PowerPC.
61629372cSPaul Mackerras */
71629372cSPaul Mackerras
81629372cSPaul Mackerras #include <linux/kernel.h>
91629372cSPaul Mackerras #include <asm/asm-compat.h>
10e93dee18SChristophe Leroy #include <asm/extable.h>
111629372cSPaul Mackerras
124c74c330SAnton Blanchard #ifdef __BIG_ENDIAN__
134c74c330SAnton Blanchard
141629372cSPaul Mackerras struct word_at_a_time {
151629372cSPaul Mackerras const unsigned long high_bits, low_bits;
161629372cSPaul Mackerras };
171629372cSPaul Mackerras
181629372cSPaul Mackerras #define WORD_AT_A_TIME_CONSTANTS { REPEAT_BYTE(0xfe) + 1, REPEAT_BYTE(0x7f) }
191629372cSPaul Mackerras
201629372cSPaul Mackerras /* 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)211629372cSPaul Mackerras static inline long prep_zero_mask(unsigned long val, unsigned long rhs, const struct word_at_a_time *c)
221629372cSPaul Mackerras {
231629372cSPaul Mackerras unsigned long mask = (val & c->low_bits) + c->low_bits;
241629372cSPaul Mackerras return ~(mask | rhs);
251629372cSPaul Mackerras }
261629372cSPaul Mackerras
271629372cSPaul Mackerras #define create_zero_mask(mask) (mask)
281629372cSPaul Mackerras
find_zero(unsigned long mask)291629372cSPaul Mackerras static inline long find_zero(unsigned long mask)
301629372cSPaul Mackerras {
311629372cSPaul Mackerras long leading_zero_bits;
321629372cSPaul Mackerras
331629372cSPaul Mackerras asm (PPC_CNTLZL "%0,%1" : "=r" (leading_zero_bits) : "r" (mask));
341629372cSPaul Mackerras return leading_zero_bits >> 3;
351629372cSPaul Mackerras }
361629372cSPaul Mackerras
has_zero(unsigned long val,unsigned long * data,const struct word_at_a_time * c)37*79e8328eSndesaulniers@google.com static inline unsigned long has_zero(unsigned long val, unsigned long *data, const struct word_at_a_time *c)
381629372cSPaul Mackerras {
391629372cSPaul Mackerras unsigned long rhs = val | c->low_bits;
401629372cSPaul Mackerras *data = rhs;
411629372cSPaul Mackerras return (val + c->high_bits) & ~rhs;
421629372cSPaul Mackerras }
431629372cSPaul Mackerras
zero_bytemask(unsigned long mask)447a5692e6SChris Metcalf static inline unsigned long zero_bytemask(unsigned long mask)
457a5692e6SChris Metcalf {
467a5692e6SChris Metcalf return ~1ul << __fls(mask);
477a5692e6SChris Metcalf }
487a5692e6SChris Metcalf
494c74c330SAnton Blanchard #else
504c74c330SAnton Blanchard
514c74c330SAnton Blanchard #ifdef CONFIG_64BIT
524c74c330SAnton Blanchard
538989aa4aSAnton Blanchard /* unused */
548989aa4aSAnton Blanchard struct word_at_a_time {
558989aa4aSAnton Blanchard };
56d0cebfa6SPhilippe Bergheaud
578989aa4aSAnton Blanchard #define WORD_AT_A_TIME_CONSTANTS { }
588989aa4aSAnton Blanchard
598989aa4aSAnton Blanchard /* This will give us 0xff for a NULL char and 0x00 elsewhere */
has_zero(unsigned long a,unsigned long * bits,const struct word_at_a_time * c)608989aa4aSAnton Blanchard static inline unsigned long has_zero(unsigned long a, unsigned long *bits, const struct word_at_a_time *c)
618989aa4aSAnton Blanchard {
628989aa4aSAnton Blanchard unsigned long ret;
638989aa4aSAnton Blanchard unsigned long zero = 0;
648989aa4aSAnton Blanchard
658989aa4aSAnton Blanchard asm("cmpb %0,%1,%2" : "=r" (ret) : "r" (a), "r" (zero));
668989aa4aSAnton Blanchard *bits = ret;
678989aa4aSAnton Blanchard
688989aa4aSAnton Blanchard return ret;
698989aa4aSAnton Blanchard }
708989aa4aSAnton Blanchard
prep_zero_mask(unsigned long a,unsigned long bits,const struct word_at_a_time * c)718989aa4aSAnton Blanchard static inline unsigned long prep_zero_mask(unsigned long a, unsigned long bits, const struct word_at_a_time *c)
728989aa4aSAnton Blanchard {
738989aa4aSAnton Blanchard return bits;
748989aa4aSAnton Blanchard }
758989aa4aSAnton Blanchard
768989aa4aSAnton Blanchard /* Alan Modra's little-endian strlen tail for 64-bit */
create_zero_mask(unsigned long bits)778989aa4aSAnton Blanchard static inline unsigned long create_zero_mask(unsigned long bits)
784c74c330SAnton Blanchard {
79d0cebfa6SPhilippe Bergheaud unsigned long leading_zero_bits;
80d0cebfa6SPhilippe Bergheaud long trailing_zero_bit_mask;
81d0cebfa6SPhilippe Bergheaud
82d0cebfa6SPhilippe Bergheaud asm("addi %1,%2,-1\n\t"
83d0cebfa6SPhilippe Bergheaud "andc %1,%1,%2\n\t"
84d0cebfa6SPhilippe Bergheaud "popcntd %0,%1"
85d0cebfa6SPhilippe Bergheaud : "=r" (leading_zero_bits), "=&r" (trailing_zero_bit_mask)
86b4c11211SAnton Blanchard : "b" (bits));
878989aa4aSAnton Blanchard
888989aa4aSAnton Blanchard return leading_zero_bits;
898989aa4aSAnton Blanchard }
908989aa4aSAnton Blanchard
find_zero(unsigned long mask)918989aa4aSAnton Blanchard static inline unsigned long find_zero(unsigned long mask)
928989aa4aSAnton Blanchard {
938989aa4aSAnton Blanchard return mask >> 3;
948989aa4aSAnton Blanchard }
958989aa4aSAnton Blanchard
968989aa4aSAnton Blanchard /* This assumes that we never ask for an all 1s bitmask */
zero_bytemask(unsigned long mask)978989aa4aSAnton Blanchard static inline unsigned long zero_bytemask(unsigned long mask)
988989aa4aSAnton Blanchard {
998989aa4aSAnton Blanchard return (1UL << mask) - 1;
1004c74c330SAnton Blanchard }
1014c74c330SAnton Blanchard
1024c74c330SAnton Blanchard #else /* 32-bit case */
1034c74c330SAnton Blanchard
1048989aa4aSAnton Blanchard struct word_at_a_time {
1058989aa4aSAnton Blanchard const unsigned long one_bits, high_bits;
1068989aa4aSAnton Blanchard };
1078989aa4aSAnton Blanchard
1088989aa4aSAnton Blanchard #define WORD_AT_A_TIME_CONSTANTS { REPEAT_BYTE(0x01), REPEAT_BYTE(0x80) }
1098989aa4aSAnton Blanchard
110d0cebfa6SPhilippe Bergheaud /*
111d0cebfa6SPhilippe Bergheaud * This is largely generic for little-endian machines, but the
112d0cebfa6SPhilippe Bergheaud * optimal byte mask counting is probably going to be something
113d0cebfa6SPhilippe Bergheaud * that is architecture-specific. If you have a reliably fast
114d0cebfa6SPhilippe Bergheaud * bit count instruction, that might be better than the multiply
115d0cebfa6SPhilippe Bergheaud * and shift, for example.
116d0cebfa6SPhilippe Bergheaud */
117d0cebfa6SPhilippe Bergheaud
1184c74c330SAnton Blanchard /* Carl Chatfield / Jan Achrenius G+ version for 32-bit */
count_masked_bytes(long mask)1194c74c330SAnton Blanchard static inline long count_masked_bytes(long mask)
1204c74c330SAnton Blanchard {
1214c74c330SAnton Blanchard /* (000000 0000ff 00ffff ffffff) -> ( 1 1 2 3 ) */
1224c74c330SAnton Blanchard long a = (0x0ff0001+mask) >> 23;
1234c74c330SAnton Blanchard /* Fix the 1 for 00 case */
1244c74c330SAnton Blanchard return a & mask;
1254c74c330SAnton Blanchard }
1264c74c330SAnton Blanchard
create_zero_mask(unsigned long bits)127d0cebfa6SPhilippe Bergheaud static inline unsigned long create_zero_mask(unsigned long bits)
128d0cebfa6SPhilippe Bergheaud {
129d0cebfa6SPhilippe Bergheaud bits = (bits - 1) & ~bits;
130d0cebfa6SPhilippe Bergheaud return bits >> 7;
131d0cebfa6SPhilippe Bergheaud }
132d0cebfa6SPhilippe Bergheaud
find_zero(unsigned long mask)133d0cebfa6SPhilippe Bergheaud static inline unsigned long find_zero(unsigned long mask)
134d0cebfa6SPhilippe Bergheaud {
135d0cebfa6SPhilippe Bergheaud return count_masked_bytes(mask);
136d0cebfa6SPhilippe Bergheaud }
137d0cebfa6SPhilippe Bergheaud
1384c74c330SAnton Blanchard /* Return nonzero if it has a zero */
has_zero(unsigned long a,unsigned long * bits,const struct word_at_a_time * c)1394c74c330SAnton Blanchard static inline unsigned long has_zero(unsigned long a, unsigned long *bits, const struct word_at_a_time *c)
1404c74c330SAnton Blanchard {
1414c74c330SAnton Blanchard unsigned long mask = ((a - c->one_bits) & ~a) & c->high_bits;
1424c74c330SAnton Blanchard *bits = mask;
1434c74c330SAnton Blanchard return mask;
1444c74c330SAnton Blanchard }
1454c74c330SAnton Blanchard
prep_zero_mask(unsigned long a,unsigned long bits,const struct word_at_a_time * c)1464c74c330SAnton Blanchard static inline unsigned long prep_zero_mask(unsigned long a, unsigned long bits, const struct word_at_a_time *c)
1474c74c330SAnton Blanchard {
1484c74c330SAnton Blanchard return bits;
1494c74c330SAnton Blanchard }
1504c74c330SAnton Blanchard
1514c74c330SAnton Blanchard /* The mask we created is directly usable as a bytemask */
1524c74c330SAnton Blanchard #define zero_bytemask(mask) (mask)
1534c74c330SAnton Blanchard
1548989aa4aSAnton Blanchard #endif /* CONFIG_64BIT */
1558989aa4aSAnton Blanchard
1568989aa4aSAnton Blanchard #endif /* __BIG_ENDIAN__ */
1574c74c330SAnton Blanchard
158fe2a1bb1SMichael Ellerman /*
159fe2a1bb1SMichael Ellerman * We use load_unaligned_zero() in a selftest, which builds a userspace
160fe2a1bb1SMichael Ellerman * program. Some linker scripts seem to discard the .fixup section, so allow
161fe2a1bb1SMichael Ellerman * the test code to use a different section name.
162fe2a1bb1SMichael Ellerman */
163fe2a1bb1SMichael Ellerman #ifndef FIXUP_SECTION
164fe2a1bb1SMichael Ellerman #define FIXUP_SECTION ".fixup"
165fe2a1bb1SMichael Ellerman #endif
166fe2a1bb1SMichael Ellerman
load_unaligned_zeropad(const void * addr)167de5946c0SAnton Blanchard static inline unsigned long load_unaligned_zeropad(const void *addr)
168de5946c0SAnton Blanchard {
169de5946c0SAnton Blanchard unsigned long ret, offset, tmp;
170de5946c0SAnton Blanchard
171de5946c0SAnton Blanchard asm(
172de5946c0SAnton Blanchard "1: " PPC_LL "%[ret], 0(%[addr])\n"
173de5946c0SAnton Blanchard "2:\n"
174fe2a1bb1SMichael Ellerman ".section " FIXUP_SECTION ",\"ax\"\n"
175de5946c0SAnton Blanchard "3: "
176de5946c0SAnton Blanchard #ifdef __powerpc64__
177de5946c0SAnton Blanchard "clrrdi %[tmp], %[addr], 3\n\t"
178de5946c0SAnton Blanchard "clrlsldi %[offset], %[addr], 61, 3\n\t"
179de5946c0SAnton Blanchard "ld %[ret], 0(%[tmp])\n\t"
180de5946c0SAnton Blanchard #ifdef __BIG_ENDIAN__
181de5946c0SAnton Blanchard "sld %[ret], %[ret], %[offset]\n\t"
182de5946c0SAnton Blanchard #else
183de5946c0SAnton Blanchard "srd %[ret], %[ret], %[offset]\n\t"
184de5946c0SAnton Blanchard #endif
185de5946c0SAnton Blanchard #else
186de5946c0SAnton Blanchard "clrrwi %[tmp], %[addr], 2\n\t"
187de5946c0SAnton Blanchard "clrlslwi %[offset], %[addr], 30, 3\n\t"
188de5946c0SAnton Blanchard "lwz %[ret], 0(%[tmp])\n\t"
189de5946c0SAnton Blanchard #ifdef __BIG_ENDIAN__
190de5946c0SAnton Blanchard "slw %[ret], %[ret], %[offset]\n\t"
191de5946c0SAnton Blanchard #else
192de5946c0SAnton Blanchard "srw %[ret], %[ret], %[offset]\n\t"
193de5946c0SAnton Blanchard #endif
194de5946c0SAnton Blanchard #endif
195de5946c0SAnton Blanchard "b 2b\n"
196de5946c0SAnton Blanchard ".previous\n"
19724bfa6a9SNicholas Piggin EX_TABLE(1b, 3b)
198de5946c0SAnton Blanchard : [tmp] "=&b" (tmp), [offset] "=&r" (offset), [ret] "=&r" (ret)
199de5946c0SAnton Blanchard : [addr] "b" (addr), "m" (*(unsigned long *)addr));
200de5946c0SAnton Blanchard
201de5946c0SAnton Blanchard return ret;
202de5946c0SAnton Blanchard }
203de5946c0SAnton Blanchard
204fe2a1bb1SMichael Ellerman #undef FIXUP_SECTION
205fe2a1bb1SMichael Ellerman
2061629372cSPaul Mackerras #endif /* _ASM_WORD_AT_A_TIME_H */
207