From: Yury Norov <yury.norov@gmail.com>
To: Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
Guenter Roeck <linux@roeck-us.net>,
Dennis Zhou <dennis@kernel.org>,
Russell King <linux@armlinux.org.uk>,
Catalin Marinas <catalin.marinas@arm.com>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
Alexey Klimov <aklimov@redhat.com>,
Kees Cook <keescook@chromium.org>,
Andy Whitcroft <apw@canonical.com>
Subject: Re: [PATCH v2 3/3] lib/find_bit: optimize find_next_bit() functions
Date: Wed, 24 Aug 2022 14:27:26 -0700 [thread overview]
Message-ID: <YwaXvphVpy5A7fSs@yury-laptop> (raw)
In-Reply-To: <CAHp75Vcbkt09J1_reRJFeYAkjoTF1abfvHi1LWc4JyWPpLD=YQ@mail.gmail.com>
On Wed, Aug 24, 2022 at 08:56:02PM +0300, Andy Shevchenko wrote:
> On Wed, Aug 24, 2022 at 8:54 PM Andy Shevchenko
> <andy.shevchenko@gmail.com> wrote:
> > On Wed, Aug 24, 2022 at 4:53 PM Yury Norov <yury.norov@gmail.com> 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@gmail.com> 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?
No. For the first iteration, the tmp can't be calculated inside the loop
(my example above is wrong) because we need to clear first bits:
mask = BITMAP_FIRST_WORD_MASK(__start);
idx = __start / BITS_PER_LONG;
tmp = (EXPRESSION) & mask; // First fetch is here
while (1) {
if (tmp) { // Evaluate here
sz = min(idx * BITS_PER_LONG + __ffs(tmp), sz);
break;
}
if (++idx > sz) // Increment here
break;
tmp = (EXPRESSION); // Other fetches here
}
Trying to move iterator increment inside the for-loop, like you suggested
would break the sequence - common-case word fetch will happen before the
idx++.
next prev parent reply other threads:[~2022-08-24 21:27 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-24 1:26 [PATCH v2 0/4] lib: optimize find_bit() functions Yury Norov
2022-08-24 1:26 ` [PATCH v2 1/3] lib/find_bit: introduce FIND_FIRST_BIT() macro Yury Norov
2022-08-24 9:10 ` Andy Shevchenko
2022-08-24 13:19 ` Yury Norov
2022-08-24 14:18 ` David Laight
2022-08-24 17:45 ` Andy Shevchenko
2022-08-24 17:59 ` Linus Torvalds
2022-08-24 1:26 ` [PATCH v2 2/3] lib/find_bit: create find_first_zero_bit_le() Yury Norov
2022-08-24 9:22 ` Andy Shevchenko
2022-08-24 9:24 ` Andy Shevchenko
2022-08-24 13:37 ` Yury Norov
2022-08-24 17:50 ` Andy Shevchenko
2022-08-24 17:58 ` Russell King (Oracle)
2022-08-24 20:03 ` Yury Norov
2022-08-24 18:11 ` Linus Torvalds
2022-08-24 22:09 ` Yury Norov
2022-08-24 1:26 ` [PATCH v2 3/3] lib/find_bit: optimize find_next_bit() functions Yury Norov
2022-08-24 9:19 ` Andy Shevchenko
2022-08-24 13:53 ` Yury Norov
2022-08-24 17:54 ` Andy Shevchenko
2022-08-24 17:56 ` Andy Shevchenko
2022-08-24 21:27 ` Yury Norov [this message]
2022-08-24 9:00 ` [PATCH v2 0/4] lib: optimize find_bit() functions Andy Shevchenko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=YwaXvphVpy5A7fSs@yury-laptop \
--to=yury.norov@gmail.com \
--cc=aklimov@redhat.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=andy.shevchenko@gmail.com \
--cc=apw@canonical.com \
--cc=catalin.marinas@arm.com \
--cc=dennis@kernel.org \
--cc=keescook@chromium.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@armlinux.org.uk \
--cc=linux@rasmusvillemoes.dk \
--cc=linux@roeck-us.net \
--cc=torvalds@linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.