Skip to content

Commit 2fbad29

Browse files
Villemoestorvalds
authored andcommitted
lib: bitmap: change bitmap_shift_right to take unsigned parameters
I've previously changed the nbits parameter of most bitmap_* functions to unsigned; now it is bitmap_shift_{left,right}'s turn. This alone saves some .text, but while at it I found that there were a few other things one could do. The end result of these seven patches is $ scripts/bloat-o-meter /tmp/bitmap.o.{old,new} add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-328 (-328) function old new delta __bitmap_shift_right 384 226 -158 __bitmap_shift_left 306 136 -170 and less importantly also a smaller stack footprint $ stack-o-meter.pl master bitmap file function old new delta lib/bitmap.o __bitmap_shift_right 24 8 -16 lib/bitmap.o __bitmap_shift_left 24 0 -24 For each pair of 0 <= shift <= nbits <= 256 I've tested the end result with a few randomly filled src buffers (including garbage beyond nbits), in each case verifying that the shift {left,right}-most bits of dst are zero and the remaining nbits-shift bits correspond to src, so I'm fairly confident I didn't screw up. That hasn't stopped me from being wrong before, though. This patch (of 7): gcc can generate slightly better code for stuff like "nbits % BITS_PER_LONG" when it knows nbits is not negative. Since negative size bitmaps or shift amounts don't make sense, change these parameters of bitmap_shift_right to unsigned. The expressions involving "lim - 1" are still ok, since if lim is 0 the loop is never executed. Also use "shift" and "nbits" consistently for the parameter names. Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent e8f2427 commit 2fbad29

2 files changed

Lines changed: 11 additions & 11 deletions

File tree

‎include/linux/bitmap.h‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ extern int __bitmap_equal(const unsigned long *bitmap1,
9696
const unsigned long *bitmap2, unsigned int nbits);
9797
extern void __bitmap_complement(unsigned long *dst, const unsigned long *src,
9898
unsigned int nbits);
99-
extern void __bitmap_shift_right(unsigned long *dst,
100-
const unsigned long *src, int shift, int bits);
99+
extern void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
100+
unsigned int shift, unsigned int nbits);
101101
extern void __bitmap_shift_left(unsigned long *dst,
102102
const unsigned long *src, int shift, int bits);
103103
extern int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
@@ -313,13 +313,13 @@ static inline int bitmap_weight(const unsigned long *src, unsigned int nbits)
313313
return __bitmap_weight(src, nbits);
314314
}
315315

316-
static inline void bitmap_shift_right(unsigned long *dst,
317-
const unsigned long *src, int n, int nbits)
316+
static inline void bitmap_shift_right(unsigned long *dst, const unsigned long *src,
317+
unsigned int shift, int nbits)
318318
{
319319
if (small_const_nbits(nbits))
320-
*dst = (*src & BITMAP_LAST_WORD_MASK(nbits)) >> n;
320+
*dst = (*src & BITMAP_LAST_WORD_MASK(nbits)) >> shift;
321321
else
322-
__bitmap_shift_right(dst, src, n, nbits);
322+
__bitmap_shift_right(dst, src, shift, nbits);
323323
}
324324

325325
static inline void bitmap_shift_left(unsigned long *dst,

‎lib/bitmap.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,17 @@ EXPORT_SYMBOL(__bitmap_complement);
104104
* @dst : destination bitmap
105105
* @src : source bitmap
106106
* @shift : shift by this many bits
107-
* @bits : bitmap size, in bits
107+
* @nbits : bitmap size, in bits
108108
*
109109
* Shifting right (dividing) means moving bits in the MS -> LS bit
110110
* direction. Zeros are fed into the vacated MS positions and the
111111
* LS bits shifted off the bottom are lost.
112112
*/
113-
void __bitmap_shift_right(unsigned long *dst,
114-
const unsigned long *src, int shift, int bits)
113+
void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
114+
unsigned shift, unsigned nbits)
115115
{
116-
int k, lim = BITS_TO_LONGS(bits), left = bits % BITS_PER_LONG;
117-
int off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
116+
unsigned k, lim = BITS_TO_LONGS(nbits), left = nbits % BITS_PER_LONG;
117+
unsigned off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG;
118118
unsigned long mask = (1UL << left) - 1;
119119
for (k = 0; off + k < lim; ++k) {
120120
unsigned long upper, lower;

0 commit comments

Comments
 (0)