Skip to content

Commit 61c1936

Browse files
Laurent Dufourxemul
Laurent Dufour
authored andcommitted
ppc64: Optimizing bit operation
Introduce optimized bit operation for PowerPc Signed-off-by: Laurent Dufour <[email protected]> Signed-off-by: Pavel Emelyanov <[email protected]>
1 parent d28984e commit 61c1936

File tree

1 file changed

+166
-3
lines changed

1 file changed

+166
-3
lines changed

arch/ppc64/include/asm/bitops.h

+166-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,174 @@
11
#ifndef __CR_BITOPS_H__
22
#define __CR_BITOPS_H__
3+
/*
4+
* PowerPC atomic bit operations.
5+
*
6+
* Merged version by David Gibson <[email protected]>.
7+
* Based on ppc64 versions by: Dave Engebretsen, Todd Inglett, Don
8+
* Reed, Pat McCarthy, Peter Bergner, Anton Blanchard. They
9+
* originally took it from the ppc32 code.
10+
*
11+
* Within a word, bits are numbered LSB first. Lot's of places make
12+
* this assumption by directly testing bits with (val & (1<<nr)).
13+
* This can cause confusion for large (> 1 word) bitmaps on a
14+
* big-endian system because, unlike little endian, the number of each
15+
* bit depends on the word size.
16+
*
17+
* The bitop functions are defined to work on unsigned longs, so for a
18+
* ppc64 system the bits end up numbered:
19+
* |63..............0|127............64|191...........128|255...........192|
20+
* and on ppc32:
21+
* |31.....0|63....32|95....64|127...96|159..128|191..160|223..192|255..224|
22+
*
23+
* There are a few little-endian macros used mostly for filesystem
24+
* bitmaps, these work on similar bit arrays layouts, but
25+
* byte-oriented:
26+
* |7...0|15...8|23...16|31...24|39...32|47...40|55...48|63...56|
27+
*
28+
* The main difference is that bit 3-5 (64b) or 3-4 (32b) in the bit
29+
* number field needs to be reversed compared to the big-endian bit
30+
* fields. This can be achieved by XOR with 0x38 (64b) or 0x18 (32b).
31+
*
32+
* This program is free software; you can redistribute it and/or
33+
* modify it under the terms of the GNU General Public License
34+
* as published by the Free Software Foundation; either version
35+
* 2 of the License, or (at your option) any later version.
36+
*
37+
* --
38+
* Copied from the kernel file arch/powerpc/include/asm/bitops.h
39+
*/
340

441
#include "compiler.h"
42+
43+
#include "asm/bitsperlong.h"
44+
45+
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
46+
#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_LONG)
47+
48+
#define DECLARE_BITMAP(name,bits) \
49+
unsigned long name[BITS_TO_LONGS(bits)]
50+
51+
#define __stringify_in_c(...) #__VA_ARGS__
52+
#define stringify_in_c(...) __stringify_in_c(__VA_ARGS__) " "
53+
54+
#define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
55+
#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
56+
57+
/* PPC bit number conversion */
58+
#define PPC_BITLSHIFT(be) (BITS_PER_LONG - 1 - (be))
59+
#define PPC_BIT(bit) (1UL << PPC_BITLSHIFT(bit))
60+
#define PPC_BITMASK(bs, be) ((PPC_BIT(bs) - PPC_BIT(be)) | PPC_BIT(bs))
61+
62+
63+
/* Macro for generating the ***_bits() functions */
64+
#define DEFINE_BITOP(fn, op) \
65+
static __inline__ void fn(unsigned long mask, \
66+
volatile unsigned long *_p) \
67+
{ \
68+
unsigned long old; \
69+
unsigned long *p = (unsigned long *)_p; \
70+
__asm__ __volatile__ ( \
71+
"1: ldarx %0,0,%3,0\n" \
72+
stringify_in_c(op) "%0,%0,%2\n" \
73+
"stdcx. %0,0,%3\n" \
74+
"bne- 1b\n" \
75+
: "=&r" (old), "+m" (*p) \
76+
: "r" (mask), "r" (p) \
77+
: "cc", "memory"); \
78+
}
79+
80+
DEFINE_BITOP(set_bits, or)
81+
DEFINE_BITOP(clear_bits, andc)
82+
DEFINE_BITOP(change_bits, xor)
83+
84+
static __inline__ void set_bit(int nr, volatile unsigned long *addr)
85+
{
86+
set_bits(BIT_MASK(nr), addr + BIT_WORD(nr));
87+
}
88+
89+
static __inline__ void clear_bit(int nr, volatile unsigned long *addr)
90+
{
91+
clear_bits(BIT_MASK(nr), addr + BIT_WORD(nr));
92+
}
93+
94+
static __inline__ void change_bit(int nr, volatile unsigned long *addr)
95+
{
96+
change_bits(BIT_MASK(nr), addr + BIT_WORD(nr));
97+
}
98+
99+
static inline int test_bit(int nr, const volatile unsigned long *addr)
100+
{
101+
return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
102+
}
103+
5104
/*
6-
* TODO: create some optimized version instead of falling down with the
7-
* generic ones.
105+
* Return the zero-based bit position (LE, not IBM bit numbering) of
106+
* the most significant 1-bit in a double word.
8107
*/
9-
#include "asm-generic/bitops.h"
108+
static __inline__ __attribute__((const))
109+
int __ilog2(unsigned long x)
110+
{
111+
int lz;
112+
113+
asm ("cntlzd %0,%1" : "=r" (lz) : "r" (x));
114+
return BITS_PER_LONG - 1 - lz;
115+
}
116+
117+
118+
static __inline__ unsigned long __ffs(unsigned long x)
119+
{
120+
return __ilog2(x & -x);
121+
}
122+
123+
124+
#define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
125+
/*
126+
* Find the next set bit in a memory region.
127+
*/
128+
static inline
129+
unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
130+
unsigned long offset)
131+
{
132+
const unsigned long *p = addr + BITOP_WORD(offset);
133+
unsigned long result = offset & ~(BITS_PER_LONG-1);
134+
unsigned long tmp;
135+
136+
if (offset >= size)
137+
return size;
138+
size -= result;
139+
offset %= BITS_PER_LONG;
140+
if (offset) {
141+
tmp = *(p++);
142+
tmp &= (~0UL << offset);
143+
if (size < BITS_PER_LONG)
144+
goto found_first;
145+
if (tmp)
146+
goto found_middle;
147+
size -= BITS_PER_LONG;
148+
result += BITS_PER_LONG;
149+
}
150+
while (size & ~(BITS_PER_LONG-1)) {
151+
if ((tmp = *(p++)))
152+
goto found_middle;
153+
result += BITS_PER_LONG;
154+
size -= BITS_PER_LONG;
155+
}
156+
if (!size)
157+
return result;
158+
tmp = *p;
159+
160+
found_first:
161+
tmp &= (~0UL >> (BITS_PER_LONG - size));
162+
if (tmp == 0UL) /* Are any bits set? */
163+
return result + size; /* Nope. */
164+
found_middle:
165+
return result + __ffs(tmp);
166+
}
167+
168+
#define for_each_bit(i, bitmask) \
169+
for (i = find_next_bit(bitmask, sizeof(bitmask), 0); \
170+
i < sizeof(bitmask); \
171+
i = find_next_bit(bitmask, sizeof(bitmask), i + 1))
172+
10173

11174
#endif /* __CR_BITOPS_H__ */

0 commit comments

Comments
 (0)