Re: [PATCH 00/18] lib: bitmap: Various improvements

From: Andrew Morton
Date: Mon Jul 07 2014 - 19:20:17 EST


On Fri, 4 Jul 2014 00:42:46 +0200 Rasmus Villemoes <linux@xxxxxxxxxxxxxxxxxx> wrote:

> Many functions in lib/bitmap.c start with an expression such as lim =
> bits/BITS_PER_LONG. Since bits has type (signed) int, and since gcc
> cannot know that it is in fact non-negative, it generates worse code
> than it could. These patches, mostly consisting of changing various
> parameters to unsigned, gives a slight overall code reduction:

Yes, we have a bad habit of using signed types for things where
negative values are absurd.

The patches look OK to me.

> A few issues I thought about, but didn't know what to do with:
>
> * Many of the functions misbehave if nbits is compile-time 0; the
> out-of-line functions generally handle 0 correctly. bitmap_fill() is
> particularly bad, whether the 0 is known at compile time or not. It
> would probably be nice to add detection of at least compile-time 0
> and handle that appropriately.

The best option here would be a compile-time check. Presumably
BUILD_BUG_ON(). That will catch the errant use and will add no runtime
overhead.

> * I didn't change __bitmap_shift_{left,right} to use unsigned because
> I want to fully understand why the algorithm works before making
> that change. However, AFAICT, they behave correctly for all
> (positive) shift amounts. This is not the case for the
> small_const_nbits versions. If for example nbits = n =
> BITS_PER_LONG, the shift operators turn into no-ops (at least on
> x86), so one get *dst = *src, whereas one would expect to get
> *dst=0. That difference in behaviour is somewhat annoying.

yup.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/