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>
10 #include <asm/ppc_asm.h>
14 struct word_at_a_time
{
15 const unsigned long high_bits
, low_bits
;
18 #define WORD_AT_A_TIME_CONSTANTS { REPEAT_BYTE(0xfe) + 1, REPEAT_BYTE(0x7f) }
20 /* Bit set in the bytes that have a zero */
21 static inline long prep_zero_mask(unsigned long val
, unsigned long rhs
, const struct word_at_a_time
*c
)
23 unsigned long mask
= (val
& c
->low_bits
) + c
->low_bits
;
27 #define create_zero_mask(mask) (mask)
29 static inline long find_zero(unsigned long mask
)
31 long leading_zero_bits
;
33 asm (PPC_CNTLZL
"%0,%1" : "=r" (leading_zero_bits
) : "r" (mask
));
34 return leading_zero_bits
>> 3;
37 static inline bool has_zero(unsigned long val
, unsigned long *data
, const struct word_at_a_time
*c
)
39 unsigned long rhs
= val
| c
->low_bits
;
41 return (val
+ c
->high_bits
) & ~rhs
;
44 static inline unsigned long zero_bytemask(unsigned long mask
)
46 return ~1ul << __fls(mask
);
54 struct word_at_a_time
{
57 #define WORD_AT_A_TIME_CONSTANTS { }
59 /* This will give us 0xff for a NULL char and 0x00 elsewhere */
60 static inline unsigned long has_zero(unsigned long a
, unsigned long *bits
, const struct word_at_a_time
*c
)
63 unsigned long zero
= 0;
65 asm("cmpb %0,%1,%2" : "=r" (ret
) : "r" (a
), "r" (zero
));
71 static inline unsigned long prep_zero_mask(unsigned long a
, unsigned long bits
, const struct word_at_a_time
*c
)
76 /* Alan Modra's little-endian strlen tail for 64-bit */
77 static inline unsigned long create_zero_mask(unsigned long bits
)
79 unsigned long leading_zero_bits
;
80 long trailing_zero_bit_mask
;
82 asm("addi %1,%2,-1\n\t"
85 : "=r" (leading_zero_bits
), "=&r" (trailing_zero_bit_mask
)
88 return leading_zero_bits
;
91 static inline unsigned long find_zero(unsigned long mask
)
96 /* This assumes that we never ask for an all 1s bitmask */
97 static inline unsigned long zero_bytemask(unsigned long mask
)
99 return (1UL << mask
) - 1;
102 #else /* 32-bit case */
104 struct word_at_a_time
{
105 const unsigned long one_bits
, high_bits
;
108 #define WORD_AT_A_TIME_CONSTANTS { REPEAT_BYTE(0x01), REPEAT_BYTE(0x80) }
111 * This is largely generic for little-endian machines, but the
112 * optimal byte mask counting is probably going to be something
113 * that is architecture-specific. If you have a reliably fast
114 * bit count instruction, that might be better than the multiply
115 * and shift, for example.
118 /* Carl Chatfield / Jan Achrenius G+ version for 32-bit */
119 static inline long count_masked_bytes(long mask
)
121 /* (000000 0000ff 00ffff ffffff) -> ( 1 1 2 3 ) */
122 long a
= (0x0ff0001+mask
) >> 23;
123 /* Fix the 1 for 00 case */
127 static inline unsigned long create_zero_mask(unsigned long bits
)
129 bits
= (bits
- 1) & ~bits
;
133 static inline unsigned long find_zero(unsigned long mask
)
135 return count_masked_bytes(mask
);
138 /* Return nonzero if it has a zero */
139 static inline unsigned long has_zero(unsigned long a
, unsigned long *bits
, const struct word_at_a_time
*c
)
141 unsigned long mask
= ((a
- c
->one_bits
) & ~a
) & c
->high_bits
;
146 static inline unsigned long prep_zero_mask(unsigned long a
, unsigned long bits
, const struct word_at_a_time
*c
)
151 /* The mask we created is directly usable as a bytemask */
152 #define zero_bytemask(mask) (mask)
154 #endif /* CONFIG_64BIT */
156 #endif /* __BIG_ENDIAN__ */
159 * We use load_unaligned_zero() in a selftest, which builds a userspace
160 * program. Some linker scripts seem to discard the .fixup section, so allow
161 * the test code to use a different section name.
163 #ifndef FIXUP_SECTION
164 #define FIXUP_SECTION ".fixup"
167 static inline unsigned long load_unaligned_zeropad(const void *addr
)
169 unsigned long ret
, offset
, tmp
;
172 "1: " PPC_LL
"%[ret], 0(%[addr])\n"
174 ".section " FIXUP_SECTION
",\"ax\"\n"
177 "clrrdi %[tmp], %[addr], 3\n\t"
178 "clrlsldi %[offset], %[addr], 61, 3\n\t"
179 "ld %[ret], 0(%[tmp])\n\t"
180 #ifdef __BIG_ENDIAN__
181 "sld %[ret], %[ret], %[offset]\n\t"
183 "srd %[ret], %[ret], %[offset]\n\t"
186 "clrrwi %[tmp], %[addr], 2\n\t"
187 "clrlslwi %[offset], %[addr], 30, 3\n\t"
188 "lwz %[ret], 0(%[tmp])\n\t"
189 #ifdef __BIG_ENDIAN__
190 "slw %[ret], %[ret], %[offset]\n\t"
192 "srw %[ret], %[ret], %[offset]\n\t"
198 : [tmp
] "=&b" (tmp
), [offset
] "=&r" (offset
), [ret
] "=&r" (ret
)
199 : [addr
] "b" (addr
), "m" (*(unsigned long *)addr
));
206 #endif /* _ASM_WORD_AT_A_TIME_H */