public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "tavianator at gmail dot com" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/64308] New: Missed optimization: 64-bit divide used when 32-bit divide would work Date: Sun, 14 Dec 2014 18:39:00 -0000 [thread overview] Message-ID: <bug-64308-4@http.gcc.gnu.org/bugzilla/> (raw) https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64308 Bug ID: 64308 Summary: Missed optimization: 64-bit divide used when 32-bit divide would work Product: gcc Version: 4.9.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: tavianator at gmail dot com Created attachment 34280 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=34280&action=edit Test case The following is a fairly typical implementation of exponentiation modulo m: $ cat ipowm.c // Computes (b**e) % m unsigned int ipowm(unsigned int b, unsigned int e, unsigned int m) { unsigned int ret; b %= m; for (ret = m > 1; e; e >>= 1) { if ((e & 1) == 1) { ret = (unsigned long long)ret * b % m; } b = (unsigned long long)b * b % m; } return ret; } Unfortunately, GCC emits a 64-bit multiply and divide for both "... * b % m" expressions on x86 and x86-64, where a 32-bit multiply and divide would be equivalent and faster. $ gcc -std=c11 -O3 -Wall -S -save-temps ipowm.c $ cat ipowm.s ... imulq %rdi, %rax divq %rcx ... imulq %rdi, %rax divq %rcx ... The pattern mull %edi divl %ecx would be much faster. They're equivalent because b is always reduced mod m, so b < m and therefore (for any unsigned int x), x * b / m <= x * m / m == x, thus the quotient will always fit in 32 bits.
next reply other threads:[~2014-12-14 18:39 UTC|newest] Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top 2014-12-14 18:39 tavianator at gmail dot com [this message] 2014-12-15 10:34 ` [Bug tree-optimization/64308] " glisse at gcc dot gnu.org 2014-12-20 1:29 ` olegendo at gcc dot gnu.org 2022-01-28 0:52 ` 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-64308-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).