public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/101649] New: -Wdouble-promotion warning emitted when floating point literals are not actually promoted to doubles
@ 2021-07-27 18:38 make_all@t-online.de
  2021-07-27 18:42 ` [Bug c++/101649] " mpolacek at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: make_all@t-online.de @ 2021-07-27 18:38 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101649

            Bug ID: 101649
           Summary: -Wdouble-promotion warning emitted when floating point
                    literals are not actually promoted to doubles
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: make_all@t-online.de
  Target Milestone: ---

GCC emits a double promotion warning when using the literal 0.5 with a float.
For example

float func(float num) {
    return num * 0.5;
}

warning: implicit conversion from 'float' to 'double' to match other operand of
binary expression [-Wdouble-promotion]
    3 |     return num * 0.5;

However, GCC does not actually use the double representation for this literal
(and others than can be represented equally in float as in double).
The assembler output for both "0.5" and "0.5f" is the same:

func(float):
        push    rbp
        mov     rbp, rsp
        movss   DWORD PTR [rbp-4], xmm0
        movss   xmm1, DWORD PTR [rbp-4]
        movss   xmm0, DWORD PTR .LC0[rip]
        mulss   xmm0, xmm1
        pop     rbp
        ret
.LC0:
        .long   1056964608

This warning is misleading and should not be emitted

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

* [Bug c++/101649] -Wdouble-promotion warning emitted when floating point literals are not actually promoted to doubles
  2021-07-27 18:38 [Bug c++/101649] New: -Wdouble-promotion warning emitted when floating point literals are not actually promoted to doubles make_all@t-online.de
@ 2021-07-27 18:42 ` mpolacek at gcc dot gnu.org
  2021-07-27 19:18 ` make_all@t-online.de
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2021-07-27 18:42 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101649

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mpolacek at gcc dot gnu.org

--- Comment #1 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Use 0.5f if you want to avoid promotion to double.

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

* [Bug c++/101649] -Wdouble-promotion warning emitted when floating point literals are not actually promoted to doubles
  2021-07-27 18:38 [Bug c++/101649] New: -Wdouble-promotion warning emitted when floating point literals are not actually promoted to doubles make_all@t-online.de
  2021-07-27 18:42 ` [Bug c++/101649] " mpolacek at gcc dot gnu.org
@ 2021-07-27 19:18 ` make_all@t-online.de
  2021-07-27 19:29 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: make_all@t-online.de @ 2021-07-27 19:18 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101649

--- Comment #2 from make_all@t-online.de ---
I don't want to avoid double promotion as double promotion is not actually
happening. I want to not be bothered by a false warning :)

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

* [Bug c++/101649] -Wdouble-promotion warning emitted when floating point literals are not actually promoted to doubles
  2021-07-27 18:38 [Bug c++/101649] New: -Wdouble-promotion warning emitted when floating point literals are not actually promoted to doubles make_all@t-online.de
  2021-07-27 18:42 ` [Bug c++/101649] " mpolacek at gcc dot gnu.org
  2021-07-27 19:18 ` make_all@t-online.de
@ 2021-07-27 19:29 ` pinskia at gcc dot gnu.org
  2021-07-27 19:54 ` make_all@t-online.de
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-07-27 19:29 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101649

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
It is a promotion to double, just happens GCC is able to undo the promotion.

Note -Wconversion also avoids warning in similar cases where there is implicit
conversion and then the conversion was removed.

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

* [Bug c++/101649] -Wdouble-promotion warning emitted when floating point literals are not actually promoted to doubles
  2021-07-27 18:38 [Bug c++/101649] New: -Wdouble-promotion warning emitted when floating point literals are not actually promoted to doubles make_all@t-online.de
                   ` (2 preceding siblings ...)
  2021-07-27 19:29 ` pinskia at gcc dot gnu.org
@ 2021-07-27 19:54 ` make_all@t-online.de
  2021-07-28  6:33 ` rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: make_all@t-online.de @ 2021-07-27 19:54 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101649

--- Comment #4 from South Window <make_all@t-online.de> ---
For the user of GCC it doesn't matter whether GCC at compile times converts the
literal to a double, and then notices that 32 bits of are zero, and uses a
float instead, or if GCC knows right away that 0.5 (and others) don't need to
be promoted.

Promoting first and then completely undoing the promotion is a purely internal
process, effectively a null operation that is of no interest of the user of the
compiler.

One of the most relevant scopes for -Wdouble-promotion is probably optimization
(in particular for SIMD), where the unintended use of a double would cause a
significant performance hit. 
But if the compiler is smart enough and is not using doubles and double
instructions, and no precision is changed anywhere in the process, it should be
smart enough to not through a this warning.

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

* [Bug c++/101649] -Wdouble-promotion warning emitted when floating point literals are not actually promoted to doubles
  2021-07-27 18:38 [Bug c++/101649] New: -Wdouble-promotion warning emitted when floating point literals are not actually promoted to doubles make_all@t-online.de
                   ` (3 preceding siblings ...)
  2021-07-27 19:54 ` make_all@t-online.de
@ 2021-07-28  6:33 ` rguenth at gcc dot gnu.org
  2021-08-02 17:03 ` make_all@t-online.de
  2021-08-02 17:21 ` make_all@t-online.de
  6 siblings, 0 replies; 8+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-07-28  6:33 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101649

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2021-07-28
     Ever confirmed|0                           |1

--- Comment #5 from Richard Biener <rguenth at gcc dot gnu.org> ---
I agree with your reasoning from a QOI perspective.  Now, the warning is
currently implemented where the language specified promotion happens and the
optimization back to float precision happens elsewhere.  I suppose we could
anticipate this - or at least not do this optimization at -O0.

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

* [Bug c++/101649] -Wdouble-promotion warning emitted when floating point literals are not actually promoted to doubles
  2021-07-27 18:38 [Bug c++/101649] New: -Wdouble-promotion warning emitted when floating point literals are not actually promoted to doubles make_all@t-online.de
                   ` (4 preceding siblings ...)
  2021-07-28  6:33 ` rguenth at gcc dot gnu.org
@ 2021-08-02 17:03 ` make_all@t-online.de
  2021-08-02 17:21 ` make_all@t-online.de
  6 siblings, 0 replies; 8+ messages in thread
From: make_all@t-online.de @ 2021-08-02 17:03 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101649

--- Comment #6 from South Window <make_all@t-online.de> ---
Not doing this optimization in -O0 would still leave this warning wrong when
optimization is turned on. In my view, the fundamental difference between a
generic linter and compiler warnings is that the compiler warning should
reflect what the compiler is really doing.

If it is not easy to not emit this warning, could it be possible to undo this
warning from "elsewhere", or at least to accompany this warning with a
withdrawal, e.g.


warning: implicit conversion from 'float' to 'double' to match other operand of
binary expression [-Wdouble-promotion]
warning withdrawn: literal '0.5' can be represented without loss of precision
as 'float'. Therefore, 'float' precision is used instead of 'double'.
[-Wdouble-promotion]

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

* [Bug c++/101649] -Wdouble-promotion warning emitted when floating point literals are not actually promoted to doubles
  2021-07-27 18:38 [Bug c++/101649] New: -Wdouble-promotion warning emitted when floating point literals are not actually promoted to doubles make_all@t-online.de
                   ` (5 preceding siblings ...)
  2021-08-02 17:03 ` make_all@t-online.de
@ 2021-08-02 17:21 ` make_all@t-online.de
  6 siblings, 0 replies; 8+ messages in thread
From: make_all@t-online.de @ 2021-08-02 17:21 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101649

--- Comment #7 from South Window <make_all@t-online.de> ---
Since the code generation seems to always reduce a double literal to a floating
point in the cases this discussed here, could the function that throws this
warning be extended to not throw the warning if all 32 least significant bits
of the double literal are zero?

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

end of thread, other threads:[~2021-08-02 17:21 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-27 18:38 [Bug c++/101649] New: -Wdouble-promotion warning emitted when floating point literals are not actually promoted to doubles make_all@t-online.de
2021-07-27 18:42 ` [Bug c++/101649] " mpolacek at gcc dot gnu.org
2021-07-27 19:18 ` make_all@t-online.de
2021-07-27 19:29 ` pinskia at gcc dot gnu.org
2021-07-27 19:54 ` make_all@t-online.de
2021-07-28  6:33 ` rguenth at gcc dot gnu.org
2021-08-02 17:03 ` make_all@t-online.de
2021-08-02 17:21 ` make_all@t-online.de

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