[Bug middle-end/60540] Don't convert int to float when comparing int with float (double) constant

harald at gigawatt dot nl gcc-bugzilla@gcc.gnu.org
Sat Mar 15 16:27:00 GMT 2014


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



More information about the Gcc-bugs mailing list