Re: [PATCH v2 3/3] lib/find_bit: optimize find_next_bit() functions

From: Andy Shevchenko
Date: Wed Aug 24 2022 - 13:57:01 EST


On Wed, Aug 24, 2022 at 8:54 PM Andy Shevchenko
<andy.shevchenko@xxxxxxxxx> wrote:
> On Wed, Aug 24, 2022 at 4:53 PM Yury Norov <yury.norov@xxxxxxxxx> wrote:
> > On Wed, Aug 24, 2022 at 12:19:05PM +0300, Andy Shevchenko wrote:
> > > On Wed, Aug 24, 2022 at 4:56 AM Yury Norov <yury.norov@xxxxxxxxx> wrote:

...

> > > > +#define FIND_NEXT_BIT(EXPRESSION, size, start) \
> > > > +({ \
> > > > + unsigned long mask, idx, tmp, sz = (size), __start = (start); \
> > > > + \
> > > > + if (unlikely(__start >= sz)) \
> > > > + goto out; \
> > > > + \
> > > > + mask = word_op(BITMAP_FIRST_WORD_MASK(__start)); \
> > > > + idx = __start / BITS_PER_LONG; \
> > > > + \
> > > > + for (tmp = (EXPRESSION) & mask; !tmp; tmp = (EXPRESSION)) { \
> > >
> > > for (unsigned long tmp ...;
> > > But hey, why not loop over idx (which probably should be named as
> > > offset)
> >
> > Offset in structure, index in array, isn't?
> >
> > > as I proposed in the first patch? You will drop a lot of
> > > divisions / multiplications, no?
> >
> > Those divisions and multiplications are optimized away, and
> > what you suggested blows up the EXPRESSION.
> >
> > I tried like this:
> > mask = word_op(BITMAP_FIRST_WORD_MASK(__start));
> > idx = __start / BITS_PER_LONG;
> > tmp = (EXPRESSION);
> >
> > while (1) {
> > if (tmp) {
> > sz = min(idx * BITS_PER_LONG + __ffs(word_op(tmp)), sz);
> > break;
> > }
> >
> > if (++idx > sz)
> > break;
> >
> > tmp = (EXPRESSION);
> > }
> >
> > And it generated the same code, but looks less expressive to me.
> > If you have some elegant approach in mind - can you please share
> > it, and how the generated code looks?
>
> for (unsigned long idx = 0; idx < sz; idx++) {

Of source 0 should be changed to whatever start you have there.

> unsigned long tmp;
>
> tmp = (EXPRESSION);
> if (tmp) {
> ...
> }
> }
>
> No?

--
With Best Regards,
Andy Shevchenko