From: David Laight <david.laight.linux@gmail.com>
To: I Hsin Cheng <richard120310@gmail.com>
Cc: yury.norov@gmail.com, anshuman.khandual@arm.com, arnd@arndb.de,
linux-kernel@vger.kernel.org, jserv@ccns.ncku.edu.tw,
skhan@linuxfoundation.org
Subject: Re: [PATCH 2/2] uapi: Refactor __GENMASK_ULL() for speed-up
Date: Wed, 12 Feb 2025 13:52:00 +0000 [thread overview]
Message-ID: <20250212135200.5a1f2dca@pumpkin> (raw)
In-Reply-To: <Z6yWbROu5rREhw85@vaxr-BM6660-BM6360>
On Wed, 12 Feb 2025 20:39:09 +0800
I Hsin Cheng <richard120310@gmail.com> wrote:
> On Tue, Feb 11, 2025 at 10:30:45PM +0000, David Laight wrote:
> > On Wed, 12 Feb 2025 00:24:12 +0800
> > I Hsin Cheng <richard120310@gmail.com> wrote:
> >
> > > The calculation of "((~_ULL(0)) - (_ULL(1) << (l)) + 1)" is to generate
> > > a bitmask with "l" trailing zeroes, which is equivalent to
> > > "(~_ULL(0) << (l))".
> >
> > Yes, and if you look through the commit history you'll see it was changed
> > to avoid a compiler warning about the shift losing significant bits.
> > So you are just reverting that change and the compiler warnings will
> > reappear.
> >
> > For non-constants I suspect that (2ul << hi) - (1ul << lo) is the
> > best answer.
> > But the compiler (clang with some options?) will still complain
> > for constants when trying to set the high bit.
> >
> > That version also doesn't need BITS_PER_[U]LONG.
> > While that is valid for C, the _ULL() are there for the assembler
> > (when they are no-ops) so there is no way asm copies can be right
> > for both GENMASK() ans GENMASK_ULL().
> >
> > David
>
> Hi David,
>
> Thanks for the review!
>
> > Yes, and if you look through the commit history you'll see it was changed
> > to avoid a compiler warning about the shift losing significant bits.
>
> I've browse through the commits of include/linux/bits.h , where
> GENMASK() was placed under. Though I didn't find specific commit of it,
> would you be so kind to paste the link of the commit?
>
> I assume you're talking about warnings like the following?
> warning: left shift count >= width of type [-Wshift-count-overflow]
No, there is one about clang 'objecting' to ~0u << count because
significant bits get discarded.
The 'strange' expression was used to avoid that - even though it is
mathematically equivalent.
>
> If this is the case then it happens for the current version as well, I
> mean from the perspective of operations, "(~_ULL(0) << (l))" and
> "(_ULL(1) << (1))" are basically the same, they all shift a constant
> left with an amount of "l". When "l" is large enough the compiler will
> complain about losing msb.
>
> > While that is valid for C, the _ULL() are there for the assembler
> > (when they are no-ops) so there is no way asm copies can be right
> > for both GENMASK() ans GENMASK_ULL().
>
> I don't understand this part sorry, if asm copies can't be right for
> "~_ULL(0) << (l)" , how can it be right for "(_ULL(1) << (l))" ?
> At least from my pespective these 2 ops only differs at the value of
> constant. Please let me know about it, I'm not familiar with assembler
> and would love to know more about it. Thanks.
It is the >> that go wrong, since they rely on the number of bits in
the integer type being used.
For asm both ~_UL(0) and ~_ULL(0) are just ~0, but the >> uses a
different constant.
I'm not even sure that the _ULL() should even be in a uapi header.
I proposed using:
((1u << (hi)) - 1) * 2) + 1 - (((1u << (lo)) - 1)
not that long ago.
That avoid any bits being shifted off the top and I'm sure it
that gcc optimised add the +/- away.
It also has the advantage that the type of the result only depends
of the type of the 1u (so can be 1ul, 1ull etc).
David
>
> Best regards,
> I Hsin Cheng
>
next prev parent reply other threads:[~2025-02-12 13:52 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-11 16:24 [PATCH 0/2] uapi: Refactor __GENMASK() and __GENMASK_ULL() for I Hsin Cheng
2025-02-11 16:24 ` [PATCH 1/2] uapi: Refactor __GENMASK() for speed-up I Hsin Cheng
2025-02-11 18:39 ` Yury Norov
2025-02-11 18:44 ` Yury Norov
2025-02-12 14:01 ` I Hsin Cheng
2025-02-13 19:54 ` Yury Norov
2025-02-14 14:51 ` I Hsin Cheng
2025-02-14 15:55 ` Yury Norov
2025-02-12 13:50 ` I Hsin Cheng
2025-02-11 16:24 ` [PATCH 2/2] uapi: Refactor __GENMASK_ULL() " I Hsin Cheng
2025-02-11 22:30 ` David Laight
2025-02-12 12:39 ` I Hsin Cheng
2025-02-12 13:52 ` David Laight [this message]
2025-02-12 14:12 ` Yury Norov
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=20250212135200.5a1f2dca@pumpkin \
--to=david.laight.linux@gmail.com \
--cc=anshuman.khandual@arm.com \
--cc=arnd@arndb.de \
--cc=jserv@ccns.ncku.edu.tw \
--cc=linux-kernel@vger.kernel.org \
--cc=richard120310@gmail.com \
--cc=skhan@linuxfoundation.org \
--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