1 #ifndef _ASM_WORD_AT_A_TIME_H
2 #define _ASM_WORD_AT_A_TIME_H
5 * Word-at-a-time interfaces for PowerPC.
8 #include <linux/kernel.h>
9 #include <asm/asm-compat.h>
13 struct word_at_a_time
{
14 const unsigned long high_bits
, low_bits
;
17 #define WORD_AT_A_TIME_CONSTANTS { REPEAT_BYTE(0xfe) + 1, REPEAT_BYTE(0x7f) }
19 /* Bit set in the bytes that have a zero */
20 static inline long prep_zero_mask(unsigned long val
, unsigned long rhs
, const struct word_at_a_time
*c
)
22 unsigned long mask
= (val
& c
->low_bits
) + c
->low_bits
;
26 #define create_zero_mask(mask) (mask)
28 static inline long find_zero(unsigned long mask
)
30 long leading_zero_bits
;
32 asm (PPC_CNTLZL
"%0,%1" : "=r" (leading_zero_bits
) : "r" (mask
));
33 return leading_zero_bits
>> 3;
36 static inline bool has_zero(unsigned long val
, unsigned long *data
, const struct word_at_a_time
*c
)
38 unsigned long rhs
= val
| c
->low_bits
;
40 return (val
+ c
->high_bits
) & ~rhs
;
48 struct word_at_a_time
{
51 #define WORD_AT_A_TIME_CONSTANTS { }
53 /* This will give us 0xff for a NULL char and 0x00 elsewhere */
54 static inline unsigned long has_zero(unsigned long a
, unsigned long *bits
, const struct word_at_a_time
*c
)
57 unsigned long zero
= 0;
59 asm("cmpb %0,%1,%2" : "=r" (ret
) : "r" (a
), "r" (zero
));
65 static inline unsigned long prep_zero_mask(unsigned long a
, unsigned long bits
, const struct word_at_a_time
*c
)
70 /* Alan Modra's little-endian strlen tail for 64-bit */
71 static inline unsigned long create_zero_mask(unsigned long bits
)
73 unsigned long leading_zero_bits
;
74 long trailing_zero_bit_mask
;
76 asm("addi %1,%2,-1\n\t"
79 : "=r" (leading_zero_bits
), "=&r" (trailing_zero_bit_mask
)
82 return leading_zero_bits
;
85 static inline unsigned long find_zero(unsigned long mask
)
90 /* This assumes that we never ask for an all 1s bitmask */
91 static inline unsigned long zero_bytemask(unsigned long mask
)
93 return (1UL << mask
) - 1;
96 #else /* 32-bit case */
98 struct word_at_a_time
{
99 const unsigned long one_bits
, high_bits
;
102 #define WORD_AT_A_TIME_CONSTANTS { REPEAT_BYTE(0x01), REPEAT_BYTE(0x80) }
105 * This is largely generic for little-endian machines, but the
106 * optimal byte mask counting is probably going to be something
107 * that is architecture-specific. If you have a reliably fast
108 * bit count instruction, that might be better than the multiply
109 * and shift, for example.
112 /* Carl Chatfield / Jan Achrenius G+ version for 32-bit */
113 static inline long count_masked_bytes(long mask
)
115 /* (000000 0000ff 00ffff ffffff) -> ( 1 1 2 3 ) */
116 long a
= (0x0ff0001+mask
) >> 23;
117 /* Fix the 1 for 00 case */
121 static inline unsigned long create_zero_mask(unsigned long bits
)
123 bits
= (bits
- 1) & ~bits
;
127 static inline unsigned long find_zero(unsigned long mask
)
129 return count_masked_bytes(mask
);
132 /* Return nonzero if it has a zero */
133 static inline unsigned long has_zero(unsigned long a
, unsigned long *bits
, const struct word_at_a_time
*c
)
135 unsigned long mask
= ((a
- c
->one_bits
) & ~a
) & c
->high_bits
;
140 static inline unsigned long prep_zero_mask(unsigned long a
, unsigned long bits
, const struct word_at_a_time
*c
)
145 /* The mask we created is directly usable as a bytemask */
146 #define zero_bytemask(mask) (mask)
148 #endif /* CONFIG_64BIT */
150 #endif /* __BIG_ENDIAN__ */
153 * We use load_unaligned_zero() in a selftest, which builds a userspace
154 * program. Some linker scripts seem to discard the .fixup section, so allow
155 * the test code to use a different section name.
157 #ifndef FIXUP_SECTION
158 #define FIXUP_SECTION ".fixup"
161 static inline unsigned long load_unaligned_zeropad(const void *addr
)
163 unsigned long ret
, offset
, tmp
;
166 "1: " PPC_LL
"%[ret], 0(%[addr])\n"
168 ".section " FIXUP_SECTION
",\"ax\"\n"
171 "clrrdi %[tmp], %[addr], 3\n\t"
172 "clrlsldi %[offset], %[addr], 61, 3\n\t"
173 "ld %[ret], 0(%[tmp])\n\t"
174 #ifdef __BIG_ENDIAN__
175 "sld %[ret], %[ret], %[offset]\n\t"
177 "srd %[ret], %[ret], %[offset]\n\t"
180 "clrrwi %[tmp], %[addr], 2\n\t"
181 "clrlslwi %[offset], %[addr], 30, 3\n\t"
182 "lwz %[ret], 0(%[tmp])\n\t"
183 #ifdef __BIG_ENDIAN__
184 "slw %[ret], %[ret], %[offset]\n\t"
186 "srw %[ret], %[ret], %[offset]\n\t"
191 ".section __ex_table,\"a\"\n\t"
192 PPC_LONG_ALIGN
"\n\t"
195 : [tmp
] "=&b" (tmp
), [offset
] "=&r" (offset
), [ret
] "=&r" (ret
)
196 : [addr
] "b" (addr
), "m" (*(unsigned long *)addr
));
203 #endif /* _ASM_WORD_AT_A_TIME_H */