From: Tim Deegan Subject: Re: [PATCH] bitmap_*() should cope with zero size bitmaps Date: Mon, 3 Jun 2013 16:40:43 +0100 Message-ID: <20130603154043.GC16240@ocelot.phlegethon.org> References: <51ACB74402000078000DA8F0@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: Content-Disposition: inline In-Reply-To: <51ACB74402000078000DA8F0@nat28.tlf.novell.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Jan Beulich Cc: Keir Fraser , xen-devel List-Id: xen-devel@lists.xenproject.org At 14:33 +0100 on 03 Jun (1370270004), Jan Beulich wrote: > ... to match expectations set by memset()/memcpy(). > > Similarly for find_{first,next}_{,zero_}_bit() on x86. > > __bitmap_shift_{left,right}() would also need fixing (they more > generally can't cope with the shift count being larger than the bitmap > size, and they perform undefined operations by possibly shifting an > unsigned long value by BITS_PER_LONG bits), but since these functions > aren't really used anywhere I wonder if we wouldn't better simply get > rid of them. > > Signed-off-by: Jan Beulich > > --- a/xen/include/asm-x86/bitops.h > +++ b/xen/include/asm-x86/bitops.h > @@ -327,10 +327,7 @@ static inline unsigned int __scanbit(uns > * Returns the bit-number of the first set bit, not the number of the byte > * containing a bit. > */ > -#define find_first_bit(addr,size) \ > -((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \ > - (__scanbit(*(const unsigned long *)addr, size)) : \ > - __find_first_bit(addr,size))) > +#define find_first_bit(addr, size) find_next_bit(addr, size, 0) > > /** > * find_next_bit - find the first set bit in a memory region > @@ -338,10 +335,24 @@ static inline unsigned int __scanbit(uns > * @offset: The bitnumber to start searching at > * @size: The maximum size to search > */ > -#define find_next_bit(addr,size,off) \ > -((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \ > - ((off) + (__scanbit((*(const unsigned long *)addr) >> (off), size))) : \ > - __find_next_bit(addr,size,off))) > +#define find_next_bit(addr, size, off) ({ \ > + unsigned int r__ = (size); \ > + unsigned int o__ = (off); \ > + switch ( -!__builtin_constant_p(size) | r__ ) \ Mmmm, binary trickery. :) But doesn't leave us with the old behaviour in cases where 'size' is a non-compile-time-constant zero? Tim. > + { \ > + case 0: (void)(addr); break; \ > + case 1 ... BITS_PER_LONG: \ > + r__ = o__ + __scanbit(*(const unsigned long *)(addr) >> o__, r__); \ > + break; \ > + default: \ > + if ( __builtin_constant_p(off) && !o__ ) \ > + r__ = __find_first_bit(addr, r__); \ > + else \ > + r__ = __find_next_bit(addr, r__, o__); \ > + break; \ > + } \ > + r__; \ > +})