]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/powerpc/include/asm/word-at-a-time.h
Merge remote-tracking branch 'powerpc/next'
[karo-tx-linux.git] / arch / powerpc / include / asm / word-at-a-time.h
1 #ifndef _ASM_WORD_AT_A_TIME_H
2 #define _ASM_WORD_AT_A_TIME_H
3
4 /*
5  * Word-at-a-time interfaces for PowerPC.
6  */
7
8 #include <linux/kernel.h>
9 #include <asm/asm-compat.h>
10
11 #ifdef __BIG_ENDIAN__
12
13 struct word_at_a_time {
14         const unsigned long high_bits, low_bits;
15 };
16
17 #define WORD_AT_A_TIME_CONSTANTS { REPEAT_BYTE(0xfe) + 1, REPEAT_BYTE(0x7f) }
18
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)
21 {
22         unsigned long mask = (val & c->low_bits) + c->low_bits;
23         return ~(mask | rhs);
24 }
25
26 #define create_zero_mask(mask) (mask)
27
28 static inline long find_zero(unsigned long mask)
29 {
30         long leading_zero_bits;
31
32         asm (PPC_CNTLZL "%0,%1" : "=r" (leading_zero_bits) : "r" (mask));
33         return leading_zero_bits >> 3;
34 }
35
36 static inline bool has_zero(unsigned long val, unsigned long *data, const struct word_at_a_time *c)
37 {
38         unsigned long rhs = val | c->low_bits;
39         *data = rhs;
40         return (val + c->high_bits) & ~rhs;
41 }
42
43 #else
44
45 /*
46  * This is largely generic for little-endian machines, but the
47  * optimal byte mask counting is probably going to be something
48  * that is architecture-specific. If you have a reliably fast
49  * bit count instruction, that might be better than the multiply
50  * and shift, for example.
51  */
52 struct word_at_a_time {
53         const unsigned long one_bits, high_bits;
54 };
55
56 #define WORD_AT_A_TIME_CONSTANTS { REPEAT_BYTE(0x01), REPEAT_BYTE(0x80) }
57
58 #ifdef CONFIG_64BIT
59
60 /*
61  * Jan Achrenius on G+: microoptimized version of
62  * the simpler "(mask & ONEBYTES) * ONEBYTES >> 56"
63  * that works for the bytemasks without having to
64  * mask them first.
65  */
66 static inline long count_masked_bytes(unsigned long mask)
67 {
68         return mask*0x0001020304050608ul >> 56;
69 }
70
71 #else   /* 32-bit case */
72
73 /* Carl Chatfield / Jan Achrenius G+ version for 32-bit */
74 static inline long count_masked_bytes(long mask)
75 {
76         /* (000000 0000ff 00ffff ffffff) -> ( 1 1 2 3 ) */
77         long a = (0x0ff0001+mask) >> 23;
78         /* Fix the 1 for 00 case */
79         return a & mask;
80 }
81
82 #endif
83
84 /* Return nonzero if it has a zero */
85 static inline unsigned long has_zero(unsigned long a, unsigned long *bits, const struct word_at_a_time *c)
86 {
87         unsigned long mask = ((a - c->one_bits) & ~a) & c->high_bits;
88         *bits = mask;
89         return mask;
90 }
91
92 static inline unsigned long prep_zero_mask(unsigned long a, unsigned long bits, const struct word_at_a_time *c)
93 {
94         return bits;
95 }
96
97 static inline unsigned long create_zero_mask(unsigned long bits)
98 {
99         bits = (bits - 1) & ~bits;
100         return bits >> 7;
101 }
102
103 /* The mask we created is directly usable as a bytemask */
104 #define zero_bytemask(mask) (mask)
105
106 static inline unsigned long find_zero(unsigned long mask)
107 {
108         return count_masked_bytes(mask);
109 }
110 #endif
111
112 #endif /* _ASM_WORD_AT_A_TIME_H */