Re: [PATCH 1/5] bitops: Introduce find_next_andnot_bit()

From: Valentin Schneider
Date: Wed Aug 17 2022 - 06:09:47 EST


On 16/08/22 15:13, Yury Norov wrote:
> On Tue, Aug 16, 2022 at 07:07:23PM +0100, Valentin Schneider wrote:
>> @@ -59,7 +63,9 @@ unsigned long _find_next_bit(const unsigned long *addr1,
>>
>> tmp = addr1[start / BITS_PER_LONG];
>> if (addr2)
>> - tmp &= addr2[start / BITS_PER_LONG];
>> + tmp &= negate ?
>> + ~addr2[start / BITS_PER_LONG] :
>> + addr2[start / BITS_PER_LONG];
>> tmp ^= invert;
>> }
>
> So it flips addr2 bits twice - first with new 'negate', and second
> with the existing 'invert'. There is no such combination in the
> existing code, but the pattern looks ugly, particularly because we use
> different inverting approaches. Because of that, and because XOR trick
> generates better code, I'd suggest something like this:
>
> tmp = addr1[start / BITS_PER_LONG] ^ invert1;
> if (addr2)
> tmp &= addr2[start / BITS_PER_LONG] ^ invert2;

That does look much better, and also gets rid of the ternary. Thanks!