public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug rtl-optimization/48580] New: missed optimization: integer overflow checks
@ 2011-04-12 18:36 zackw at panix dot com
  2011-04-12 20:18 ` [Bug rtl-optimization/48580] " joseph at codesourcery dot com
                   ` (24 more replies)
  0 siblings, 25 replies; 26+ messages in thread
From: zackw at panix dot com @ 2011-04-12 18:36 UTC (permalink / raw)
  To: gcc-bugs

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

           Summary: missed optimization: integer overflow checks
           Product: gcc
           Version: 4.6.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: rtl-optimization
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: zackw@panix.com


To the best of my knowledge, this is the only safe way (without -fwrapv) to
check whether the product of two signed integers overflowed:

bool product_does_not_overflow(signed x, signed y)
{
  unsigned tmp = x * unsigned(y);

  return signed(tmp) > 0 && tmp / x == unsigned(y);
}

(I believe C and C++ are the same in this regard but I could be wrong.  If
there is a better way to write this test I would love to know about it.)

g++ 4.6 produces this assembly dump on x86-64:

_Z25product_does_not_overflowii:
    movl    %esi, %edx
    xorl    %eax, %eax
    imull    %edi, %edx
    testl    %edx, %edx
    jle    .L2
    movl    %edx, %eax
    xorl    %edx, %edx
    divl    %edi
    cmpl    %eax, %esi
    sete    %al
.L2:
    rep
    ret

but, if I understand the semantics of IMUL correctly, it could do this instead:

_Z25product_does_not_overflowii:
    xorl    %eax, %eax
    imull    %edi, %esi
    setno    %al
    ret

which is a pretty substantial micro-win, particularly in getting rid of a
divide.


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

end of thread, other threads:[~2023-08-09 22:29 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-04-12 18:36 [Bug rtl-optimization/48580] New: missed optimization: integer overflow checks zackw at panix dot com
2011-04-12 20:18 ` [Bug rtl-optimization/48580] " joseph at codesourcery dot com
2011-04-12 20:40 ` zackw at panix dot com
2011-04-12 20:52 ` joseph at codesourcery dot com
2011-04-12 21:03 ` zackw at panix dot com
2011-04-12 21:04 ` zackw at panix dot com
2011-04-12 21:10 ` joseph at codesourcery dot com
2011-04-12 21:16 ` joseph at codesourcery dot com
2011-04-13 12:11 ` [Bug middle-end/48580] " rguenth at gcc dot gnu.org
2011-04-13 12:46 ` joseph at codesourcery dot com
2011-04-13 17:44 ` svfuerst at gmail dot com
2011-06-20  9:47 ` jsm28 at gcc dot gnu.org
2011-10-05 12:44 ` jules at gcc dot gnu.org
2011-10-05 13:08 ` jules at gcc dot gnu.org
2011-10-05 15:20 ` joseph at codesourcery dot com
2013-02-02 14:03 ` Martin.vGagern at gmx dot net
2013-02-02 17:02 ` noloader at gmail dot com
2013-02-02 18:54 ` Martin.vGagern at gmx dot net
2013-02-02 21:59 ` zackw at panix dot com
2013-02-02 22:08 ` Martin.vGagern at gmx dot net
2013-05-19 13:04 ` glisse at gcc dot gnu.org
2014-08-24  7:06 ` Martin.vGagern at gmx dot net
2021-08-15 11:45 ` pinskia at gcc dot gnu.org
2021-08-15 11:49 ` pinskia at gcc dot gnu.org
2021-10-22 22:18 ` pinskia at gcc dot gnu.org
2023-08-09 22:29 ` pinskia at gcc dot gnu.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).