public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Andrew Cooper <andrew.cooper3@citrix.com>
To: Andreas Schwab <schwab@suse.de>, Andrew Cooper via Gcc <gcc@gcc.gnu.org>
Cc: Alexander Monakov <amonakov@ispras.ru>
Subject: Re: Builtin for consulting value analysis (better ffs() code gen)
Date: Thu, 14 Mar 2024 15:33:52 +0000	[thread overview]
Message-ID: <9704774f-00f9-48d4-ad27-6cd07a816359@citrix.com> (raw)
In-Reply-To: <mvm8r2lm34u.fsf@suse.de>

On 14/03/2024 12:03 pm, Andreas Schwab wrote:
> On Mär 14 2024, Andrew Cooper via Gcc wrote:
>
>> so any known-constant value can be folded.  What I'm dealing with is the
>> remainder of the cases.
> Which cases remain?

None, thanks to the answers on this thread.

The overall structure I've got now is:

unsigned int ffs(unsigned int x)
{
    if ( __builtin_constant_p(x) )
        return __builtin_ffs(x);  // Allows constant folding

#ifndef arch_ffs
#define arch_ffs __builtin_ffs
#endif

    return arch_ffs(x);
}

And for x86's arch_ffs(),

unsigned int arch_ffs(unsigned int x)
{
    unsigned int res;

    if ( __builtin_constant_p(x > 0) && x > 0 )
    {
        // Well defined when x is known non-zero
        asm("bsf %1, %0" : "=r"(res) : "rm"(x));
    }
    else
    {
        // The architects say this is safe even for 0.
        res = -1;
        asm("bsf %1, %0" : "+r"(res) : "rm"(x));
    }

    return res + 1;
}


This gives the same code gen as before (give or take some register
shuffling), without having to rely on the programmer to remember to get
their ffs()'s separated from their __ffs()'s as it pertains to undefined
input.

The other architectures which have better-defined instructions don't
need any of these games to retain the same good code-gen that is
currently expressed with a maze of subtly-different functions.

~Andrew

  reply	other threads:[~2024-03-14 15:33 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
2024-03-14 12:03     ` Andreas Schwab
2024-03-14 15:33       ` Andrew Cooper [this message]
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=9704774f-00f9-48d4-ad27-6cd07a816359@citrix.com \
    --to=andrew.cooper3@citrix.com \
    --cc=amonakov@ispras.ru \
    --cc=gcc@gcc.gnu.org \
    --cc=schwab@suse.de \
    /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).