public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "ppalka at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug rtl-optimization/105119] New: the division in x / (1 << y) is optimized away when x has unsigned type, but not when it's signed Date: Thu, 31 Mar 2022 18:03:44 +0000 [thread overview] Message-ID: <bug-105119-4@http.gcc.gnu.org/bugzilla/> (raw) https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105119 Bug ID: 105119 Summary: the division in x / (1 << y) is optimized away when x has unsigned type, but not when it's signed Product: gcc Version: 12.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: rtl-optimization Assignee: unassigned at gcc dot gnu.org Reporter: ppalka at gcc dot gnu.org Target Milestone: --- For the following on x86_64 with -O2 int f(int x, int y) { return x / (1 << y); } GCC generates an idiv instruction: movl %esi, %ecx movl $1, %edx movl %edi, %eax sall %cl, %edx movl %edx, %ecx cltd idivl %ecx ret But I believe this is equivalent to the division-less int g(int x, int y) { return (x + (x < 0) * ((1 << y) - 1)) >> y; } (which basically generalizes the existing x / (1 << y) -> x >> y transformation that we perform for unsigned x). For this latter function, we generate movl %esi, %ecx movl $1, %eax xorl %edx, %edx sall %cl, %eax subl $1, %eax testl %edi, %edi cmovns %edx, %eax addl %edi, %eax sarl %cl, %eax ret which seems to be significantly faster according to some rough benchmarks.
next reply other threads:[~2022-03-31 18:03 UTC|newest] Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top 2022-03-31 18:03 ppalka at gcc dot gnu.org [this message] 2022-04-01 6:22 ` [Bug rtl-optimization/105119] " rguenth at gcc dot gnu.org 2022-11-28 20:04 ` [Bug middle-end/105119] " pinskia at gcc dot gnu.org 2022-11-28 20:09 ` 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-105119-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: linkBe 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).