public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Andrew Cooper <andrew.cooper3@citrix.com>
To: Alexander Monakov <amonakov@ispras.ru>
Cc: gcc@gcc.gnu.org
Subject: Re: Builtin for consulting value analysis (better ffs() code gen)
Date: Thu, 14 Mar 2024 11:30:12 +0000	[thread overview]
Message-ID: <a49dd682-c5bb-4674-a044-4720f4c0a059@citrix.com> (raw)
In-Reply-To: <12b2c99d-993d-ba8d-75ff-b107de2eba67@ispras.ru>

On 14/03/2024 6:04 am, Alexander Monakov wrote:
> On Thu, 14 Mar 2024, Andrew Cooper via Gcc wrote:
>
>> I suppose that what I'm looking for is something a little like
>> __builtin_constant_p() which can either be used in a straight if(), or
>> in a __builtin_choose_expr().
>>
>> Anyway - is there a way of doing this that I've managed to overlook?
> I am missing what is lacking for you with __builtin_constant_p, I would
> do it like this:
>
> unsigned ffs(unsigned x)
> {
>     unsigned res;
>     unsigned nonzero = x != 0;
>     if (__builtin_constant_p(nonzero) && nonzero)
>         asm("bsf %1, %0" : "=r"(res) : "rm"(x));
>     else {
>         res = -1;
>         asm("bsf %1, %0" : "+r"(res) : "rm"(x));
>     }
>     return res;
> }

Oh - so it does.  I'd not considered that expressing it like that would
still work.

>
> or with handling known-zero-input case like this:
>
> unsigned ffs(unsigned x)
> {
>     unsigned res;
>     unsigned nonzero = x != 0;
>     if (!__builtin_constant_p(nonzero)) {
>         res = -1;
>         asm("bsf %1, %0" : "+r"(res) : "rm"(x));
>     } else if (nonzero) {
>         asm("bsf %1, %0" : "=r"(res) : "rm"(x));
>     } else {
>         res = -1;
>     }
>     return res;
> }
>
>
> Does it work for you?

I simplified things when asking the question.  The real implementation
has a general

    if (__builtin_constant_p(x))
        return __builtin_ffs(x);

so any known-constant value can be folded.  What I'm dealing with is the
remainder of the cases.

Anyway - thankyou for your help.

~Andrew

  reply	other threads:[~2024-03-14 11:30 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-14  0:03 Andrew Cooper
2024-03-14  6:04 ` Alexander Monakov
2024-03-14 11:30   ` Andrew Cooper [this message]
2024-03-14 12:03     ` Andreas Schwab
2024-03-14 15:33       ` Andrew Cooper
2024-03-21  8:15         ` LIU Hao
2024-03-14 11:02 ` Florian Weimer
2024-03-14 11:52   ` Andrew Cooper

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=a49dd682-c5bb-4674-a044-4720f4c0a059@citrix.com \
    --to=andrew.cooper3@citrix.com \
    --cc=amonakov@ispras.ru \
    --cc=gcc@gcc.gnu.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 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 read-only IMAP folder(s) and NNTP newsgroup(s).