LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Yury Norov <yury.norov@gmail.com>
To: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: "Vaittinen, Matti" <Matti.Vaittinen@fi.rohmeurope.com>,
	Matti Vaittinen <mazziesaccount@gmail.com>,
	Liam Girdwood <lgirdwood@gmail.com>,
	Mark Brown <broonie@kernel.org>, Jiri Kosina <trivial@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	Geert Uytterhoeven <geert+renesas@glider.be>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 1/4] bitops: Add single_bit_set()
Date: Mon, 22 Nov 2021 23:51:40 -0800	[thread overview]
Message-ID: <20211123075140.GB1628@lapt> (raw)
In-Reply-To: <YZv1/vKe46jmRMJa@smile.fi.intel.com>

On Mon, Nov 22, 2021 at 09:56:46PM +0200, Andy Shevchenko wrote:
> On Mon, Nov 22, 2021 at 09:54:14AM -0800, Yury Norov wrote:
> > On Mon, Nov 22, 2021 at 02:57:27PM +0200, Andy Shevchenko wrote:
> > > On Mon, Nov 22, 2021 at 12:42:21PM +0000, Vaittinen, Matti wrote:
> > > > On 11/22/21 13:28, Andy Shevchenko wrote:
> > > > > On Mon, Nov 22, 2021 at 01:03:25PM +0200, Matti Vaittinen wrote:
> > > > >> There are cases when it is useful to check a bit-mask has only one bit
> > > > >> set. Add a generic helper for it instead of baking own one for each
> > > > >> user.
> > > 
> > > > > So, you decided to reinvent hamming weight...
> > > > > Please, drop this patch and use corresponding hweight() call.
> > > 
> > > > Thanks Andy.
> > > > 
> > > > There are few differences to hamming weight here. We scan only given 
> > > > amount of bits - and we will end scanning immediately when we hit second 
> > > > set bit. Oh, and obviously we only return information whether there is 
> > > > exactly one bit set. So no, this is not hamming weight().
> > > 
> > > What do you mean by this?
> > > 
> > > hweight() will return you the number of the non-zero elements in the set.
> > > In application to boolean based arrays it means the number of bits that
> > > are set. Obviously, the condition `hweight() == 1` is what you are looking
> > > for.
> > 
> > Hi Andy,
> > 
> > I think, Matti means earlier return when part of bitmap counts set
> > bits to a greater nubmer, and we can skip the rest. Right, Matti?
> > 
> > I agree that for Matti's usecase it's useless because 32-bit int is small,
> > and hweight() would count set bits with a single machine instruction. (And
> > it should be hweight32(), not bitmap_weight() in this case.)
> > 
> > But in general, it might be useful for long bitmaps.
> > 
> > The more complete way of doing this would be adding a new set of
> > functions: bitmap_weight_{eq,neq,gt,le}
> > 
> > I'm looking at how bitmap_weight is used in the kernel and see
> > quite a lot of places where this optimization may take place. For
> > example otx2_remove_flow() in drivers/net/ethernet/marvell/octeontx2/nic/otx2_flows.c:
> > 
> >         if (bitmap_weight(&flow_cfg->dmacflt_bmap,
> >                           flow_cfg->dmacflt_max_flows) == 1)
> >                 otx2_update_rem_pfmac(pfvf, DMAC_ADDR_DEL);
> > 
> > may be replaced with:
> > 
> >         if (bitmap_weight_eq(&flow_cfg->dmacflt_bmap, flow_cfg->dmacflt_max_flows, 1)
> >                 otx2_update_rem_pfmac(pfvf, DMAC_ADDR_DEL);
> > 
> > Most of that places are in drivers however, and the length of bitmaps
> > there is typically small, so that there's no chance to get any
> > measurable performance improvement.
> > 
> > There is always a chance that we have opencoded bitmap_weight_eq()
> > et all. If we add these API, it might help people wright better code.
> > 
> > What do you think?
> 
> Before answering this I would like to see how hweight() is currently being used
> in the kernel against bitmaps. Like histogram collection

hweight() is not used against bitmap. We have bitmap_weight() for it.
 
> 	comparison number	number of usages
> 	variadic			X
> 	1				Y
> 	2				Z
> 	...				...

I don't think it would be helpful to build this histogram. For the
proposed optimization it's (almost) not important what number is
compared against bitmap_weight().

The important thing is that some callers in core code can save 
measurable amount of time if we switch them from
        bitmap_weight() == XXX
to
        bitmap_weight_eq(..., XXX)

Consider cpumask_weight and nodes_weight.

  reply	other threads:[~2021-11-23  7:51 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-22 11:03 [PATCH 0/4] Provide event map helper for regulator drivers Matti Vaittinen
2021-11-22 11:03 ` [PATCH 1/4] bitops: Add single_bit_set() Matti Vaittinen
2021-11-22 11:28   ` Andy Shevchenko
2021-11-22 12:42     ` Vaittinen, Matti
2021-11-22 12:57       ` Andy Shevchenko
2021-11-22 13:00         ` Andy Shevchenko
2021-11-22 13:18         ` Vaittinen, Matti
2021-11-23 10:42           ` David Laight
2021-11-23 10:47             ` Andy Shevchenko
2021-11-23 10:58               ` David Laight
2021-11-23 13:43                 ` 'Andy Shevchenko'
2021-11-23 14:36                   ` David Laight
2021-11-23 11:42             ` Vaittinen, Matti
2021-11-22 17:54         ` Yury Norov
2021-11-22 19:56           ` Andy Shevchenko
2021-11-23  7:51             ` Yury Norov [this message]
2021-11-23  5:26           ` Vaittinen, Matti
2021-11-23  7:33             ` Yury Norov
2021-11-23  9:03               ` Andy Shevchenko
2021-11-23  9:10                 ` Geert Uytterhoeven
2021-11-22 11:03 ` [PATCH 2/4] regulators: Add regulator_err2notif() helper Matti Vaittinen
2021-11-22 11:04 ` [PATCH 3/4] regulators: irq_helper: Provide helper for trivial IRQ notifications Matti Vaittinen
2021-11-22 11:48   ` Andy Shevchenko
2021-11-22 12:44     ` Vaittinen, Matti
2021-11-22 11:04 ` [PATCH 4/4] regulator: Drop unnecessary struct member Matti Vaittinen

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=20211123075140.GB1628@lapt \
    --to=yury.norov@gmail.com \
    --cc=Matti.Vaittinen@fi.rohmeurope.com \
    --cc=akpm@linux-foundation.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=broonie@kernel.org \
    --cc=geert+renesas@glider.be \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=mazziesaccount@gmail.com \
    --cc=memxor@gmail.com \
    --cc=trivial@kernel.org \
    --subject='Re: [PATCH 1/4] bitops: Add single_bit_set()' \
    /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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).