public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/41779]  New: Spurious integral promotion
@ 2009-10-21  9:16 zweije at xs4all dot nl
  2009-12-29 18:02 ` [Bug c++/41779] " foo at mailinator dot com
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: zweije at xs4all dot nl @ 2009-10-21  9:16 UTC (permalink / raw)
  To: gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1476 bytes --]

The following code produces a warning about precision loss in an int-to-float
conversion:

    $cat warn.cpp
    float f(float x, unsigned short y)
    {
        return x * y;
    }
    $g++ -Wconversion -c -o warn.o warn.cpp
    warn.cpp: In function ‘float f(float, short unsigned int)’:
    warn.cpp:3: warning: conversion to ‘float’ from ‘int’ may alter its value
    $

The code snippet does not have an int-to-float conversion.

It does have an unsigned short-to-float conversion according to the C++
standard, chapter 5 [expr] para 9: "[inapplicable note on long double and
double...] Otherwise, if either operand is float, the other shall be converted
to float [...]". Only after this rule, integral promotions are performed, which
could promote the unsigned short to an int.

Moreover, he conversion does not lose information since the 16 bits of the
unsigned short fit amply in the float's mantissa.

Not tested with other shorter-than-int types. This problem might apply there as
well...


-- 
           Summary: Spurious integral promotion
           Product: gcc
           Version: 4.4.1
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: zweije at xs4all dot nl
 GCC build triplet: x86_64-linux-gnu
  GCC host triplet: x86_64-linux-gnu
GCC target triplet: x86_64-linux-gnu


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


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

* [Bug c++/41779] Spurious integral promotion
  2009-10-21  9:16 [Bug c++/41779] New: Spurious integral promotion zweije at xs4all dot nl
@ 2009-12-29 18:02 ` foo at mailinator dot com
  2009-12-31 11:02 ` zweije at xs4all dot nl
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: foo at mailinator dot com @ 2009-12-29 18:02 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from foo at mailinator dot com  2009-12-29 18:02 -------
5/9 (usual arithmetic conversions) isn't relevant until after 4.5/1 (integral
promotions) has happened. Unsigned short *always* promotes to int in rvalue
contexts; then *that* int is implicitly converted to double in the context of
multiplication by a double (which is what's happening here).


-- 


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


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

* [Bug c++/41779] Spurious integral promotion
  2009-10-21  9:16 [Bug c++/41779] New: Spurious integral promotion zweije at xs4all dot nl
  2009-12-29 18:02 ` [Bug c++/41779] " foo at mailinator dot com
@ 2009-12-31 11:02 ` zweije at xs4all dot nl
  2010-02-14 20:28 ` [Bug c++/41779] Wconversion cannot see throught real*integer promotions manu at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: zweije at xs4all dot nl @ 2009-12-31 11:02 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from zweije at xs4all dot nl  2009-12-31 11:01 -------
I beg to differ. I cannot find where the standard says that 
unsigned short *always* promotes to int in rvalue contexts.

My reading in more detail is:

 1. The value of y is an lvalue of type unsigned short (5.1/7).

 2. 13.3.1.2/7 mentions (effectively) promoting the 
    operands (because built-in candidates (13.6/12) have 
    promoted arguments), but it applies only when the
    built-in multiplication has been selected by overload 
    resolution. This is not the case, because there are no
    class or enum arguments.

 3. Therefore, clause 5 applies directly (13.3.1.2/1).

 4. Clause 5 in itself does not require promoted operands
    (5.6/2), and does not distinguish lvalues from
    rvalues. Therefore the value of y is promoted directly
    to an lvalue of type float, through the usual arithmetic 
    conversion (5/9).


-- 


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


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

* [Bug c++/41779] Wconversion cannot see throught real*integer promotions
  2009-10-21  9:16 [Bug c++/41779] New: Spurious integral promotion zweije at xs4all dot nl
  2009-12-29 18:02 ` [Bug c++/41779] " foo at mailinator dot com
  2009-12-31 11:02 ` zweije at xs4all dot nl
@ 2010-02-14 20:28 ` manu at gcc dot gnu dot org
  2010-02-19 21:12 ` 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-14 20:28 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from manu at gcc dot gnu dot org  2010-02-14 20:28 -------
I think this is a bug in Wconversion. It should be able to see through
promotions that the conversion cannot lead to a change of value.


-- 

manu at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |manu at gcc dot gnu dot org
           Severity|minor                       |enhancement
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
           Keywords|                            |diagnostic
   Last reconfirmed|0000-00-00 00:00:00         |2010-02-14 20:28:45
               date|                            |
            Summary|Spurious integral promotion |Wconversion cannot see
                   |                            |throught real*integer
                   |                            |promotions


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


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

* [Bug c++/41779] Wconversion cannot see throught real*integer promotions
  2009-10-21  9:16 [Bug c++/41779] New: Spurious integral promotion zweije at xs4all dot nl
                   ` (2 preceding siblings ...)
  2010-02-14 20:28 ` [Bug c++/41779] Wconversion cannot see throught real*integer promotions manu at gcc dot gnu dot org
@ 2010-02-19 21:12 ` manu at gcc dot gnu dot org
  2010-02-19 21:14 ` manu at gcc dot gnu dot org
  2010-02-23 13:59 ` hjl dot tools at gmail dot com
  5 siblings, 0 replies; 7+ messages in thread
From: manu at gcc dot gnu dot org @ 2010-02-19 21:12 UTC (permalink / raw)
  To: gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 700 bytes --]



------- Comment #4 from manu at gcc dot gnu dot org  2010-02-19 21:12 -------
Subject: Bug 41779

Author: manu
Date: Fri Feb 19 21:12:09 2010
New Revision: 156911

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=156911
Log:
2010-02-19  Manuel López-Ibáñez  <manu@gcc.gnu.org>

        PR 41779
        * c-common.c (conversion_warning): Remove widening conversions
        before checking the conversion of integers to reals.
testsuite/
        * c-c++-common/pr41779.c: New.

Added:
    trunk/gcc/testsuite/c-c++-common/pr41779.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/c-common.c
    trunk/gcc/testsuite/ChangeLog


-- 


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


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

* [Bug c++/41779] Wconversion cannot see throught real*integer promotions
  2009-10-21  9:16 [Bug c++/41779] New: Spurious integral promotion zweije at xs4all dot nl
                   ` (3 preceding siblings ...)
  2010-02-19 21:12 ` manu at gcc dot gnu dot org
@ 2010-02-19 21:14 ` manu at gcc dot gnu dot org
  2010-02-23 13:59 ` hjl dot tools at gmail dot com
  5 siblings, 0 replies; 7+ messages in thread
From: manu at gcc dot gnu dot org @ 2010-02-19 21:14 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from manu at gcc dot gnu dot org  2010-02-19 21:14 -------
FIXED in GCC 4.5


-- 

manu at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


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


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

* [Bug c++/41779] Wconversion cannot see throught real*integer promotions
  2009-10-21  9:16 [Bug c++/41779] New: Spurious integral promotion zweije at xs4all dot nl
                   ` (4 preceding siblings ...)
  2010-02-19 21:14 ` manu at gcc dot gnu dot org
@ 2010-02-23 13:59 ` hjl dot tools at gmail dot com
  5 siblings, 0 replies; 7+ messages in thread
From: hjl dot tools at gmail dot com @ 2010-02-23 13:59 UTC (permalink / raw)
  To: gcc-bugs



-- 

hjl dot tools at gmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |4.5.0


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


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

end of thread, other threads:[~2010-02-23 13:59 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-10-21  9:16 [Bug c++/41779] New: Spurious integral promotion zweije at xs4all dot nl
2009-12-29 18:02 ` [Bug c++/41779] " foo at mailinator dot com
2009-12-31 11:02 ` zweije at xs4all dot nl
2010-02-14 20:28 ` [Bug c++/41779] Wconversion cannot see throught real*integer promotions manu at gcc dot gnu dot org
2010-02-19 21:12 ` manu at gcc dot gnu dot org
2010-02-19 21:14 ` manu at gcc dot gnu dot org
2010-02-23 13:59 ` hjl dot tools at gmail dot com

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