public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/34417]  New: simplify '(x & A) % B' if 'B > A/2'
@ 2007-12-10 10:56 wouter dot vermaelen at scarlet dot be
  2007-12-10 13:40 ` [Bug tree-optimization/34417] " rguenth at gcc dot gnu dot org
  0 siblings, 1 reply; 3+ messages in thread
From: wouter dot vermaelen at scarlet dot be @ 2007-12-10 10:56 UTC (permalink / raw)
  To: gcc-bugs

Hi,

In one of my application I need the expression '(x & 0xf) % 9'. Because of the
restricted range of (x & 0xf) it's possible to replace the modulo operation
with an if (see examples below). It would be nice if gcc could do this
optimization automatically. 

unsigned fooA(unsigned x)
{
        return (x & 0xf) % 9;
}
unsigned fooB(unsigned x)
{
        x &= 0xf;
        if (x >= 9) x -= 9;
        return x;
}
unsigned fooC(unsigned x)
{
        x &= 0xf;
        return std::min(x, x - 9);
}


The generated code is pasted below (SVN revision r130738, linux_x86_64, -O3).
Version B and C are very close (maybe C is slightly better) but they are
clearly better than version A.

fooA:   andl    $15, %edi
        movl    $954437177, %edx
        movl    %edi, %eax
        mull    %edx
        shrl    %edx
        leal    0(,%rdx,8), %eax
        addl    %edx, %eax
        subl    %eax, %edi
        movl    %edi, %eax
        ret

fooB:   andl    $15, %edi
        leal    -9(%rdi), %eax
        cmpl    $9, %edi
        cmovae  %eax, %edi
        movl    %edi, %eax
        ret

fooC:   andl    $15, %edi
        leal    -9(%rdi), %eax
        cmpl    %edi, %eax
        cmova   %edi, %eax
        ret


-- 
           Summary: simplify '(x & A) % B' if 'B > A/2'
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: wouter dot vermaelen at scarlet dot be


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34417


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-07-26 20:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-34417-4@http.gcc.gnu.org/bugzilla/>
2012-02-27  4:27 ` [Bug tree-optimization/34417] simplify '(x & A) % B' if 'B > A/2' pinskia at gcc dot gnu.org
2021-07-26 20:45 ` pinskia at gcc dot gnu.org
2007-12-10 10:56 [Bug tree-optimization/34417] New: " wouter dot vermaelen at scarlet dot be
2007-12-10 13:40 ` [Bug tree-optimization/34417] " rguenth at gcc dot gnu dot org

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).