This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug middle-end/60540] Don't convert int to float when comparing int with float (double) constant
- From: "harald at gigawatt dot nl" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Sat, 15 Mar 2014 16:27:40 +0000
- Subject: [Bug middle-end/60540] Don't convert int to float when comparing int with float (double) constant
- Auto-submitted: auto-generated
- References: <bug-60540-4 at http dot gcc dot gnu dot org/bugzilla/>
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).