public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "sven.koehler at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/101950] New: __builtin_clrsb is never inlined
Date: Tue, 17 Aug 2021 16:13:21 +0000	[thread overview]
Message-ID: <bug-101950-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101950

            Bug ID: 101950
           Summary: __builtin_clrsb is never inlined
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: sven.koehler at gmail dot com
  Target Milestone: ---

With gcc 11.1 on ARM 32-bit and Intel, I don't see that __builtin_clrsb is
inlined. On AARCH64 it is inlined and the cls instruction is used, as expected.
I use the C-code below to compare the assembly generated. For ARM, I use -O3
-mcpu=cortex-a53 -marm and for Intel I just use -O3.


On ARM 32-bit, clrsb1 seems to be the fastest code (see below for the assembly
code) since clz handles zero correctly. On Intel, bsr does not handle zero,
hence the workaround of setting the lsb before calling __builtin_clzl (see
below for the assembly code). On Intel, clrsb1 is slighly longer and uses a
jump to handle the zero case. clang apparently uses variant clrsb1 on ARM and
Intel, and it's inlined on both architectures when using -O3.





#define SHIFT (sizeof(x)*8-1)

int clz(unsigned long x) {
    if (x == 0) {
        return sizeof(x)*8;
    }
    return __builtin_clzl(x);
}

int clsb(long x) {
    return clz(x ^ (x >> SHIFT));
}

int clrsb1(long x) {
    return clsb(x)-1;
}

int clrsb2(long x) {
    x = ((x << 1) ^ (x >> SHIFT)) | 1;
    return __builtin_clzl(x);
}

int clrsb3(long x) {
    return __builtin_clrsbl(x);
}



on ARM 32-bit:
clrsb1:
        eor     x0, x0, x0, asr 63
        clz     x0, x0
        sub     w0, w0, #1
        ret

on Intel:
clrsb2:
        lea     rax, [rdi+rdi]
        sar     rdi, 63
        xor     rax, rdi
        or      rax, 1
        bsr     rax, rax
        xor     eax, 63
        ret

             reply	other threads:[~2021-08-17 16:13 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-17 16:13 sven.koehler at gmail dot com [this message]
2021-08-17 18:01 ` [Bug c/101950] " jakub at gcc dot gnu.org
2021-08-18 12:04 ` [Bug middle-end/101950] " jakub at gcc dot gnu.org
2021-08-19  9:02 ` cvs-commit at gcc dot gnu.org
2021-08-19  9:03 ` jakub at gcc dot gnu.org
2021-08-25  2:45 ` pinskia at gcc dot gnu.org

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=bug-101950-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).