public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/55393] New: gcc/g++ multiplies two unsigned integers using the IMULQ instruction
@ 2012-11-19 11:28 cosmos at claycon dot org
  2012-11-19 14:03 ` [Bug rtl-optimization/55393] " jakub at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: cosmos at claycon dot org @ 2012-11-19 11:28 UTC (permalink / raw)
  To: gcc-bugs


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

             Bug #: 55393
           Summary: gcc/g++ multiplies two unsigned integers using the
                    IMULQ instruction
    Classification: Unclassified
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: cosmos@claycon.org


g++ -Wall -Wextra -O2 -o mult mult.cpp
g++ (GCC) 4.7.2 20120921 (Red Hat 4.7.2-2)
64bit

mult.cpp:

#include <iostream>

void
display(
        unsigned long int num,
        unsigned long int mult)
{
        unsigned long int tmp = num * mult;
        std::cout << "mult " << mult << "\n num " << num
                << "\n tmp " << tmp << std::endl;

        if (tmp < num)
                std::cout << "overflow" << std::endl;
}

int
main(
        int /* argc */,
        char ** /* argv */)
{
        unsigned long int num = 999999999999999999;
        unsigned long int mult = 1024;

        display(num, mult);
        return 0;
}

Problem:
"overflow" is not displayed as expected.

Analysis:
gcc generates an IMULQ instruction to calculate the value of tmp.
The value of num has bit 63 set.  Since IMULQ sees that argument
as signed, it results in an incorrect number that happens to be
greater than num.

IMULQ will generate the wrong result when the result just fits
into 64 bits too, even though the result would have been correct
(with no overflow) had the proper instruction been used.

Fix:
Whenever the multiplication operands are both unsigned, gcc should
generate an unsigned multiply instruction (MULQ in this case), unless
it can prove that the result would fit into 63 bits.


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

end of thread, other threads:[~2012-11-20  7:56 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-11-19 11:28 [Bug c++/55393] New: gcc/g++ multiplies two unsigned integers using the IMULQ instruction cosmos at claycon dot org
2012-11-19 14:03 ` [Bug rtl-optimization/55393] " jakub at gcc dot gnu.org
2012-11-20  7:46 ` cosmos at claycon dot org
2012-11-20  7:56 ` jakub 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).