All of lore.kernel.org
 help / color / mirror / Atom feed
From: Paul Cercueil <paul@crapouillou.net>
To: Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: Linus Walleij <linus.walleij@linaro.org>,
	od@opendingux.net,
	BROADCOM NVRAM DRIVER <linux-mips@vger.kernel.org>
Subject: Re: [PATCH] pinctrl: ingenic: Garbage-collect code paths for SoCs disabled by config
Date: Wed, 16 Mar 2022 16:45:20 +0000	[thread overview]
Message-ID: <KVJU8R.0NUG0BM0DRAL@crapouillou.net> (raw)
In-Reply-To: <CAHp75Vez_9CGa6RY5iPrdzTQ79c8OBT7=UOY5w69reM1TLcHkA@mail.gmail.com>



Le mer., mars 16 2022 at 18:20:27 +0200, Andy Shevchenko 
<andy.shevchenko@gmail.com> a écrit :
> On Wed, Mar 16, 2022 at 6:03 PM Paul Cercueil <paul@crapouillou.net> 
> wrote:
>>  Le mer., mars 16 2022 at 17:37:37 +0200, Andy Shevchenko
>>  <andy.shevchenko@gmail.com> a écrit :
>>  > On Wed, Mar 16, 2022 at 5:05 PM Paul Cercueil 
>> <paul@crapouillou.net>
>>  > wrote:
>>  >>  Le mer., mars 16 2022 at 16:47:24 +0200, Andy Shevchenko
>>  >>  <andy.shevchenko@gmail.com> a écrit :
>>  >>  > On Wed, Mar 16, 2022 at 4:11 AM Paul Cercueil
>>  >> <paul@crapouillou.net>
>>  >>  > wrote:
> 
> ...
> 
>>  > It was given below, i.e. using test_bit().
>> 
>>  Again, I am not checking a bit, but a mask.
> 
> Yes, that's why "was".
> 
> ...
> 
>>  >>  >>  +       return (enabled_socs >> version) &&
>>  >>  >>  +               (!(enabled_socs & GENMASK((unsigned 
>> int)version
>>  >> -
>>  >>  >> 1, 0))
>>  >>  >
>>  >>  > Why casting? Why not use BIT()?
>>  >>
>>  >>  Casting just to be explicit about what we're doing here - I 
>> don't
>>  >> like
>>  >>  doing arithmetic on enums. But I can certainly remove it.
>>  >>
>>  >>  > But these two lines seem like a very interesting way to 
>> reinvent
>>  >> the
>>  >>  > test_bit().
>>  >>
>>  >>  I don't use BIT() or test_bit() because I am not checking a bit,
>>  >> but a
>>  >>  mask:
>>  >>  - (enabled_socs >> version) checks that the config supports 
>> either
>>  >>  (version) or anything above;
>>  >>  - !(enabled_socs & GENMASK(version - 1, 0)) checks that the 
>> config
>>  >> does
>>  >>  not support anything below. If true, the actual version check 
>> can be
>>  >>  skipped and the operation is a compile-time constant, and GCC 
>> will
>>  >> trim
>>  >>  the dead branches accordingly.
>>  >
>>  > You can still simplify the code, no? Calling ffs() (or similar, I
>>  > don't remember by heart all of the details) will give you the 
>> result
>>  > in one op. And it may also be optimized away by the compiler.
>> 
>>  ffs() gives me the first bit set, this is absolutely not what I 
>> want.
>> 
>>  Let's say that my kernel supports the JZ4750, JZ4755, JZ4760 SoCs.
>>  That means that I have enabled_socs == 0x38.
>> 
>>  Calling is_soc_or_above(jzpc, ID_JZ4740) would resolve to:
>>  (0x38 >> 1) && (!(0x38 & GENMASK(0, 0)) || jzpc->info->version >=
>>  version);
>>  == 1 && (1 || jzpc->info->version >= version)
>> 
>>  Which is a compile-time constant equal to 1.
>> 
>>  Calling is_soc_or_above(jzpc, ID_JZ4755) would resolve to:
>>  (0x38 >> 4) && (!(0x38 & GENMASK(3, 0)) || jzpc->info->version >=
>>  version);
>>  == 1 && (0 || jzpc->info->version >= version)
>> 
>>  which is not a compile-time constant, so the jzpc->info->version 
>> must
>>  be checked.
>> 
>>  Calling is_soc_or_above(jzpc, ID_JZ4770) would resolve to:
>>  (0x38 >> 6) && (!(0x38 & GENMASK(5, 0)) || jzpc->info->version >=
>>  version);
>>  == 0 && ...
>> 
>>  which is a compile-time constant equal to 0.
> 
> And what's wrong with
> 
>   ffs(enabled_socs) >= BIT(version)
> 
> ?

Well, for starters you're comparing a bit position [0:31] with a bit 
mask (1 << [0:31])
;)

I believe you meant:
ffs(enabled_socs) >= version

With my former example, the first case (ID_JZ4740) would give you a 
compile-time constant of 1, so it works.

The second case (ID_JZ4755) would cause the jzpc->info->version to be 
checked, which works too.

The third case (ID_JZ4770) however, would also cause the 
jzpc->info->version to be checked, which is not the wanted behaviour. 
Since in my example we only support up to JZ4760, this should instead 
resolve to a compile-time constant 0.

Cheers,
-Paul



      reply	other threads:[~2022-03-16 16:45 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-15 17:39 [PATCH] pinctrl: ingenic: Garbage-collect code paths for SoCs disabled by config Paul Cercueil
2022-03-16 14:47 ` Andy Shevchenko
2022-03-16 15:05   ` Paul Cercueil
2022-03-16 15:37     ` Andy Shevchenko
2022-03-16 16:03       ` Paul Cercueil
2022-03-16 16:20         ` Andy Shevchenko
2022-03-16 16:45           ` Paul Cercueil [this message]

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=KVJU8R.0NUG0BM0DRAL@crapouillou.net \
    --to=paul@crapouillou.net \
    --cc=andy.shevchenko@gmail.com \
    --cc=linus.walleij@linaro.org \
    --cc=linux-mips@vger.kernel.org \
    --cc=od@opendingux.net \
    /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.