This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]