public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/42935]  New: Warning "u64 = u32 * u32;" - i.e. not casting one u32 to u64
@ 2010-02-02 10:10 etienne_lorrain at yahoo dot fr
  2010-02-24  0:17 ` [Bug c/42935] " manu at gcc dot gnu dot org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: etienne_lorrain at yahoo dot fr @ 2010-02-02 10:10 UTC (permalink / raw)
  To: gcc-bugs

It is a beginer bug, but it would be nice to get a warning in:

#include <stdio.h>
unsigned val1 = 0x10000000, val2 = 0x100;
int main(int argc, char **argv)
{
    unsigned long long val3 = val1 * val2;
    printf ("val1 = 0x%X, val2 = 0x%X, val3 = 0x%llX, val1*val2 = 0x%llX\n",
        val1, val2, val3, (unsigned long long)(val1*val2));
}

Obviously the line printed is:
val1 = 0x10000000, val2 = 0x100, val3 = 0x0, val1*val2 = 0x0
(compiled with "gcc -w -Wall -O2 tmp.c" you get no warning)
Got bitten yesterday by gcc-4.4.3, reproduced today on gcc version 4.1.2.


-- 
           Summary: Warning "u64 = u32 * u32;" - i.e. not casting one u32 to
                    u64
           Product: gcc
           Version: 4.4.3
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: etienne_lorrain at yahoo dot fr
 GCC build triplet: i686-pc-cygwin
  GCC host triplet: i686-pc-cygwin
GCC target triplet: i686-pc-cygwin


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


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

* [Bug c/42935] Warning "u64 = u32 * u32;" - i.e. not casting one u32 to u64
  2010-02-02 10:10 [Bug c/42935] New: Warning "u64 = u32 * u32;" - i.e. not casting one u32 to u64 etienne_lorrain at yahoo dot fr
@ 2010-02-24  0:17 ` manu at gcc dot gnu dot org
  2010-02-24  9:45 ` etienne_lorrain at yahoo dot fr
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: manu at gcc dot gnu dot org @ 2010-02-24  0:17 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from manu at gcc dot gnu dot org  2010-02-24 00:16 -------
Confirmed. I am not sure if this fits Wconversion but it is something subtle
that would be nice to detect.

The testcase should be:

#include <stdio.h>
unsigned val1 = 0x10000000, val2 = 0x100;
int main(int argc, char **argv)
{
    unsigned long long val3 = val1 * val2;
    printf ("val1 = 0x%X, val2 = 0x%X, val3 = 0x%llX, val1*val2 = 0x%llX\n",
        val1, val2, val3, ((unsigned long long)val1)*val2);
}


-- 

manu at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |manu at gcc dot gnu dot org
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
   Last reconfirmed|0000-00-00 00:00:00         |2010-02-24 00:16:51
               date|                            |


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


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

* [Bug c/42935] Warning "u64 = u32 * u32;" - i.e. not casting one u32 to u64
  2010-02-02 10:10 [Bug c/42935] New: Warning "u64 = u32 * u32;" - i.e. not casting one u32 to u64 etienne_lorrain at yahoo dot fr
  2010-02-24  0:17 ` [Bug c/42935] " manu at gcc dot gnu dot org
@ 2010-02-24  9:45 ` etienne_lorrain at yahoo dot fr
  2010-02-24 10:23 ` manu at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: etienne_lorrain at yahoo dot fr @ 2010-02-24  9:45 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from etienne_lorrain at yahoo dot fr  2010-02-24 09:45 -------
It would be nice to have the warning in the second case too, i.e. for
(unsigned long long)(val1*val2).
Another solution, probably a lot more complex to implement, is to have
a compilation switch to expand all calculus to 64 bits even when "int"
is 32 bits, and let the optimiser convert back to simpler assembly code
when the result used is only 32 bits.


-- 


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


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

* [Bug c/42935] Warning "u64 = u32 * u32;" - i.e. not casting one u32 to u64
  2010-02-02 10:10 [Bug c/42935] New: Warning "u64 = u32 * u32;" - i.e. not casting one u32 to u64 etienne_lorrain at yahoo dot fr
  2010-02-24  0:17 ` [Bug c/42935] " manu at gcc dot gnu dot org
  2010-02-24  9:45 ` etienne_lorrain at yahoo dot fr
@ 2010-02-24 10:23 ` manu at gcc dot gnu dot org
  2010-06-07 17:52 ` [Bug c/42935] warn if a binary operation is performed on a type but the result is then casted to a larger type manu at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: manu at gcc dot gnu dot org @ 2010-02-24 10:23 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from manu at gcc dot gnu dot org  2010-02-24 10:23 -------
(In reply to comment #2)
> It would be nice to have the warning in the second case too, i.e. for
> (unsigned long long)(val1*val2).

Actually, it is a bit weird that the operands are not promoted to unsigned long
long. Normally, I would say that if you do an explicit cast, then we have to
assume that you know what you are doing. However, in this case, the result is
totally unexpected, so I think we could warn.

Also for (double) (int * int) versus ((double) int * int). This is another
subtle case. I think this is an interesting warning.

> Another solution, probably a lot more complex to implement, is to have
> a compilation switch to expand all calculus to 64 bits even when "int"
> is 32 bits, and let the optimiser convert back to simpler assembly code
> when the result used is only 32 bits.

That is independent of this warning. If you want that, open a new PR and see
what others say.


-- 


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


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

* [Bug c/42935] warn if a binary operation is performed on a type but the result is then casted to a larger type
  2010-02-02 10:10 [Bug c/42935] New: Warning "u64 = u32 * u32;" - i.e. not casting one u32 to u64 etienne_lorrain at yahoo dot fr
                   ` (2 preceding siblings ...)
  2010-02-24 10:23 ` manu at gcc dot gnu dot org
@ 2010-06-07 17:52 ` manu at gcc dot gnu dot org
  2010-06-07 17:53 ` manu at gcc dot gnu dot org
  2010-06-07 18:05 ` manu at gcc dot gnu dot org
  5 siblings, 0 replies; 7+ messages in thread
From: manu at gcc dot gnu dot org @ 2010-06-07 17:52 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from manu at gcc dot gnu dot org  2010-06-07 17:52 -------
  uint32_t bar;
  uint64_t foo;
  ...
  foo = bar << 20;

Is another testcase from PR 44420.


-- 

manu at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|2010-02-24 00:16:51         |2010-06-07 17:52:46
               date|                            |
            Summary|Warning "u64 = u32 * u32;" -|warn if a binary operation
                   |i.e. not casting one u32 to |is performed on a type but
                   |u64                         |the result is then casted to
                   |                            |a larger type


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


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

* [Bug c/42935] warn if a binary operation is performed on a type but the result is then casted to a larger type
  2010-02-02 10:10 [Bug c/42935] New: Warning "u64 = u32 * u32;" - i.e. not casting one u32 to u64 etienne_lorrain at yahoo dot fr
                   ` (3 preceding siblings ...)
  2010-06-07 17:52 ` [Bug c/42935] warn if a binary operation is performed on a type but the result is then casted to a larger type manu at gcc dot gnu dot org
@ 2010-06-07 17:53 ` manu at gcc dot gnu dot org
  2010-06-07 18:05 ` manu at gcc dot gnu dot org
  5 siblings, 0 replies; 7+ messages in thread
From: manu at gcc dot gnu dot org @ 2010-06-07 17:53 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from manu at gcc dot gnu dot org  2010-06-07 17:53 -------
*** Bug 44420 has been marked as a duplicate of this bug. ***


-- 

manu at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |fm3 at os dot inf dot tu-
                   |                            |dresden dot de


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


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

* [Bug c/42935] warn if a binary operation is performed on a type but the result is then casted to a larger type
  2010-02-02 10:10 [Bug c/42935] New: Warning "u64 = u32 * u32;" - i.e. not casting one u32 to u64 etienne_lorrain at yahoo dot fr
                   ` (4 preceding siblings ...)
  2010-06-07 17:53 ` manu at gcc dot gnu dot org
@ 2010-06-07 18:05 ` manu at gcc dot gnu dot org
  5 siblings, 0 replies; 7+ messages in thread
From: manu at gcc dot gnu dot org @ 2010-06-07 18:05 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from manu at gcc dot gnu dot org  2010-06-07 18:04 -------



I think the condition of this warning should be:

warn if a binary operation is performed on a type but the result is then
implicitly converted to a larger type. The workaround for a valid case should
be casting to the same type as the result beforehand. For example:

unsigned val1 = 0x10000000, val2 = 0x100;
unsigned long long val3 = val1 * val2; // "warning: binary operation '*' is
performed on type 'unsigned int' but the result is converted to type 'double'"
unsigned long long val4 = (unsigned) (val1 * val2); // silence

This probably can be implemented in convert_and_check in c-common.c or in
build_binary_op in c-typeck.c. I am not working on this.


-- 

manu at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
  GCC build triplet|i686-pc-cygwin              |
   GCC host triplet|i686-pc-cygwin              |
 GCC target triplet|i686-pc-cygwin              |


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


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

end of thread, other threads:[~2010-06-07 18:05 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-02-02 10:10 [Bug c/42935] New: Warning "u64 = u32 * u32;" - i.e. not casting one u32 to u64 etienne_lorrain at yahoo dot fr
2010-02-24  0:17 ` [Bug c/42935] " manu at gcc dot gnu dot org
2010-02-24  9:45 ` etienne_lorrain at yahoo dot fr
2010-02-24 10:23 ` manu at gcc dot gnu dot org
2010-06-07 17:52 ` [Bug c/42935] warn if a binary operation is performed on a type but the result is then casted to a larger type manu at gcc dot gnu dot org
2010-06-07 17:53 ` manu at gcc dot gnu dot org
2010-06-07 18:05 ` manu 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).