public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/60540] New: Don't convert int to float when comparing int with float constant
@ 2014-03-15 14:59 olegendo at gcc dot gnu.org
  2014-03-15 15:13 ` [Bug middle-end/60540] Don't convert int to float when comparing int with float (double) constant pinskia at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: olegendo at gcc dot gnu.org @ 2014-03-15 14:59 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 60540
           Summary: Don't convert int to float when comparing int with
                    float constant
           Product: gcc
           Version: 4.9.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: olegendo at gcc dot gnu.org
            Target: sh*-*-*

This is probably not something that one would write on purpose, but I've seen
it somewhere:

bool test (int a)
{
  return a > 1.0;
}

On SH with -O2 -m4 this compiles to:

        lds     r4,fpul
        mova    .L3,r0
        fmov.s  @r0+,fr5    // load double constant 1.0
        fmov.s  @r0+,fr4

        float   fpul,dr2    // convert 'a' to double

        fcmp/gt dr4,dr2     // double > double
        rts
        movt    r0
.L4:
        .align 2
.L3:
        .long   0
        .long   1072693248

In this case an integer comparison could be done instead, which does not
require converting the integer variable to float/double.  This seems like a
target independent issue.


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

* [Bug middle-end/60540] Don't convert int to float when comparing int with float (double) constant
  2014-03-15 14:59 [Bug middle-end/60540] New: Don't convert int to float when comparing int with float constant olegendo at gcc dot gnu.org
@ 2014-03-15 15:13 ` pinskia at gcc dot gnu.org
  2014-03-15 15:18 ` olegendo at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2014-03-15 15:13 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Don't convert int to float  |Don't convert int to float
                   |when comparing int with     |when comparing int with
                   |float constant              |float (double) constant

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
>In this case an integer comparison could be done instead, which does not require converting the integer variable to float/double.

Well the C standard requires it, though we could optimize it away since 32bit
integers can be exactly represented in a 64bit IEEE double.


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

* [Bug middle-end/60540] Don't convert int to float when comparing int with float (double) constant
  2014-03-15 14:59 [Bug middle-end/60540] New: Don't convert int to float when comparing int with float constant olegendo at gcc dot gnu.org
  2014-03-15 15:13 ` [Bug middle-end/60540] Don't convert int to float when comparing int with float (double) constant pinskia at gcc dot gnu.org
@ 2014-03-15 15:18 ` olegendo at gcc dot gnu.org
  2014-03-15 16:27 ` harald at gigawatt dot nl
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: olegendo at gcc dot gnu.org @ 2014-03-15 15:18 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Oleg Endo <olegendo at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #1)

> Well the C standard requires it, though we could optimize it away since
> 32bit integers can be exactly represented in a 64bit IEEE double.

Yes, for doubles, absolutely.

If converting a 32 bit int value to a 32 bit float, the resulting value is
undefined behavior if it can't be represented by a 32 bit float, at least as
far as I know.  If this is the case, it could also be OK to do it for 32 bit
floats, at least when doing unsafe math optimizations.


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

* [Bug middle-end/60540] Don't convert int to float when comparing int with float (double) constant
  2014-03-15 14:59 [Bug middle-end/60540] New: Don't convert int to float when comparing int with float constant olegendo at gcc dot gnu.org
  2014-03-15 15:13 ` [Bug middle-end/60540] Don't convert int to float when comparing int with float (double) constant pinskia at gcc dot gnu.org
  2014-03-15 15:18 ` olegendo at gcc dot gnu.org
@ 2014-03-15 16:27 ` harald at gigawatt dot nl
  2014-03-17 15:20 ` joseph at codesourcery dot com
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: harald at gigawatt dot nl @ 2014-03-15 16:27 UTC (permalink / raw)
  To: gcc-bugs

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

Harald van Dijk <harald at gigawatt dot nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |harald at gigawatt dot nl

--- Comment #3 from Harald van Dijk <harald at gigawatt dot nl> ---
(In reply to Oleg Endo from comment #2)
> If converting a 32 bit int value to a 32 bit float, the resulting value is
> undefined behavior if it can't be represented by a 32 bit float, at least as
> far as I know.

Only if the int is out of float's range. If the int is in float's range, but
merely cannot be represented exactly, the value is rounded. Whether it is
rounded up or down is implementation-defined, but the result must be one of the
two nearest representable values. (C99 6.3.1.5p2)

Testcase:

#include <stdlib.h>

int f(int i) {
  return i <= 16777216.f;
}

int main(void) {
  if (!f(16777217))
    abort();
}

16777217 is rounded down, so this does not abort, but would abort if (i <=
16777216.f) is optimised to (i <= 16777216).

However, it would still be possible to optimise this to (i <= 16777217).


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

* [Bug middle-end/60540] Don't convert int to float when comparing int with float (double) constant
  2014-03-15 14:59 [Bug middle-end/60540] New: Don't convert int to float when comparing int with float constant olegendo at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2014-03-15 16:27 ` harald at gigawatt dot nl
@ 2014-03-17 15:20 ` joseph at codesourcery dot com
  2021-08-07 19:48 ` [Bug tree-optimization/60540] " pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: joseph at codesourcery dot com @ 2014-03-17 15:20 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from joseph at codesourcery dot com <joseph at codesourcery dot com> ---
On Sat, 15 Mar 2014, harald at gigawatt dot nl wrote:

> Only if the int is out of float's range. If the int is in float's range, but
> merely cannot be represented exactly, the value is rounded. Whether it is

With -ftrapping-math (default) you also need to allow for "inexact" 
exceptions.  See bug 57371 for more discussion.


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

* [Bug tree-optimization/60540] Don't convert int to float when comparing int with float (double) constant
  2014-03-15 14:59 [Bug middle-end/60540] New: Don't convert int to float when comparing int with float constant olegendo at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2014-03-17 15:20 ` joseph at codesourcery dot com
@ 2021-08-07 19:48 ` pinskia at gcc dot gnu.org
  2021-08-07 19:54 ` pinskia at gcc dot gnu.org
  2021-08-07 19:57 ` pinskia at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-07 19:48 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |gabravier at gmail dot com

--- Comment #13 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 95697 has been marked as a duplicate of this bug. ***

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

* [Bug tree-optimization/60540] Don't convert int to float when comparing int with float (double) constant
  2014-03-15 14:59 [Bug middle-end/60540] New: Don't convert int to float when comparing int with float constant olegendo at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2021-08-07 19:48 ` [Bug tree-optimization/60540] " pinskia at gcc dot gnu.org
@ 2021-08-07 19:54 ` pinskia at gcc dot gnu.org
  2021-08-07 19:57 ` pinskia at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-07 19:54 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #14 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
So the original case we do handle since r9-3447.

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

* [Bug tree-optimization/60540] Don't convert int to float when comparing int with float (double) constant
  2014-03-15 14:59 [Bug middle-end/60540] New: Don't convert int to float when comparing int with float constant olegendo at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2021-08-07 19:54 ` pinskia at gcc dot gnu.org
@ 2021-08-07 19:57 ` pinskia at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-07 19:57 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=81376

--- Comment #15 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #14)
> So the original case we do handle since r9-3447.

I should say the cases where integer type does not fit into float/double is not
handled and I cannot find it this was discussed when the patch was submitted or
not.  See PR 81376 also.

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

end of thread, other threads:[~2021-08-07 19:57 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-03-15 14:59 [Bug middle-end/60540] New: Don't convert int to float when comparing int with float constant olegendo at gcc dot gnu.org
2014-03-15 15:13 ` [Bug middle-end/60540] Don't convert int to float when comparing int with float (double) constant pinskia at gcc dot gnu.org
2014-03-15 15:18 ` olegendo at gcc dot gnu.org
2014-03-15 16:27 ` harald at gigawatt dot nl
2014-03-17 15:20 ` joseph at codesourcery dot com
2021-08-07 19:48 ` [Bug tree-optimization/60540] " pinskia at gcc dot gnu.org
2021-08-07 19:54 ` pinskia at gcc dot gnu.org
2021-08-07 19:57 ` 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).