public inbox for llvm@lists.linux.dev
 help / color / mirror / Atom feed
From: "Arnd Bergmann" <arnd@arndb.de>
To: "Ian Rogers" <irogers@google.com>,
	"Yury Norov" <yury.norov@gmail.com>,
	"Rasmus Villemoes" <linux@rasmusvillemoes.dk>,
	"Nathan Chancellor" <nathan@kernel.org>,
	"Nick Desaulniers" <nick.desaulniers+lkml@gmail.com>,
	"Bill Wendling" <morbo@google.com>,
	"Justin Stitt" <justinstitt@google.com>,
	"Adrian Hunter" <adrian.hunter@intel.com>,
	"Thomas Gleixner" <tglx@linutronix.de>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Jacob Keller" <jacob.e.keller@intel.com>,
	Linux-Arch <linux-arch@vger.kernel.org>,
	linux-kernel@vger.kernel.org, llvm@lists.linux.dev
Subject: Re: [PATCH v1 2/5] bitmap: Silence a clang -Wshorten-64-to-32 warning
Date: Fri, 04 Apr 2025 07:49:33 +0200	[thread overview]
Message-ID: <dbba94f1-27ee-4344-b4b2-d8dffe6b7d33@app.fastmail.com> (raw)
In-Reply-To: <20250403165702.396388-3-irogers@google.com>

On Thu, Apr 3, 2025, at 18:56, Ian Rogers wrote:
> The clang warning -Wshorten-64-to-32 can be useful to catch
> inadvertent truncation. In some instances this truncation can lead to
> changing the sign of a result, for example, truncation to return an
> int to fit a sort routine. Silence the warning by making the implicit
> truncation explicit.


>  unsigned int bitmap_weight(const unsigned long *src, unsigned int nbits)
>  {
>  	if (small_const_nbits(nbits))
> -		return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
> +		return (int)hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
>  	return __bitmap_weight(src, nbits);
>  }

I don't understand this one. hweight_long() and bitmap_weight()
both return unsigned value, so why do you need to cast this to
a signed value to avoid a signedness problem?

hweight_long() should never return anything larger than 64ul
anyway, which is way outside of the range where it would get
sign-extended.

A more logical change to me would be to make hweight_long()
and bitmap_weight() have the same return type, either
'unsigned long' or 'unsigned int'.

      Arnd

  reply	other threads:[~2025-04-04  5:49 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-03 16:56 [PATCH v1 0/5] Silence some clang -Wshorten-64-to-32 warnings Ian Rogers
2025-04-03 16:56 ` [PATCH v1 1/5] bitfield: Silence a clang -Wshorten-64-to-32 warning Ian Rogers
2025-04-03 16:56 ` [PATCH v1 2/5] bitmap: " Ian Rogers
2025-04-04  5:49   ` Arnd Bergmann [this message]
2025-04-04 11:31     ` Ian Rogers
2025-04-03 16:57 ` [PATCH v1 3/5] bitops: " Ian Rogers
2025-04-04  5:43   ` Arnd Bergmann
2025-04-04 11:35     ` Ian Rogers
2025-04-03 16:57 ` [PATCH v1 4/5] math64: " Ian Rogers
2025-04-03 16:57 ` [PATCH v1 5/5] hash.h: " Ian Rogers

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=dbba94f1-27ee-4344-b4b2-d8dffe6b7d33@app.fastmail.com \
    --to=arnd@arndb.de \
    --cc=adrian.hunter@intel.com \
    --cc=irogers@google.com \
    --cc=jacob.e.keller@intel.com \
    --cc=justinstitt@google.com \
    --cc=kuba@kernel.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=llvm@lists.linux.dev \
    --cc=morbo@google.com \
    --cc=nathan@kernel.org \
    --cc=nick.desaulniers+lkml@gmail.com \
    --cc=tglx@linutronix.de \
    --cc=yury.norov@gmail.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox