public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* Re: optimization/9325: wrong conversion of constants: (int)(float)(int) (INT_MAX)
@ 2003-02-18 10:56 Eric Botcazou
0 siblings, 0 replies; 3+ messages in thread
From: Eric Botcazou @ 2003-02-18 10:56 UTC (permalink / raw)
To: nobody; +Cc: gcc-prs
The following reply was made to PR optimization/9325; it has been noted by GNATS.
From: Eric Botcazou <ebotcazou@libertysurf.fr>
To: kcc@mcst.ru
Cc: gcc-gnats@gcc.gnu.org
Subject: Re: optimization/9325: wrong conversion of constants: (int)(float)(int) (INT_MAX)
Date: Tue, 18 Feb 2003 11:54:06 +0100
> (int)(float)(int)(INT_MAX)
> is transformed to INT_MAX, which is incorrect.
> Should leave the conversions.
>
> f1() and f2() are optimized wrong,
> while f3() and f4() are ok
>
> ///// file 2_31.c
> extern int printf(const char *, ...);
> int f1(){
> return (int)2147483648.0f;
> }
> int f2(){
> return (int)(float)( 2147483647 );
> }
> int f3(){
> float a = 2147483648.0f;
> return (int)a;
> }
> int f4(){
> int a = 2147483647;
> float b = (float)a;
> return (int)b;
> }
> int main()
> {
> printf("%d\n%d\n%d\n%d\n", f1(), f2(), f3(), f4());
> return 0;
> }
I think the return values of the 4 functions are undefined according to the C
standard:
- it's very clear for f1() and f3(): the value of the integral part
(INT_MAX+1) cannot be represented by the integer type so the behavior is
undefined.
- it's less clear for f2() and f4(): INT_MAX is in the range of values that
can be represented by float but cannot be represented exactly (not enough
bits in the mantissa) so it is converted to the nearest higher representable
value, which happens to be "(INT_MAX+1)f". Then the argument for f1() and
f3() applies again.
The only nit is that, in the second case, the standard says that the value
must be converted to the nearest higher or nearest lower representable value
in an implementation-defined manner. For f2(), the conversion is done in
software while for f4() it is done in hardware ('fitos') and I'm not sure
whether they match.
--
Eric Botcazou
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: optimization/9325: wrong conversion of constants: (int)(float)(int) (INT_MAX)
@ 2003-02-19 10:51 ebotcazou
0 siblings, 0 replies; 3+ messages in thread
From: ebotcazou @ 2003-02-19 10:51 UTC (permalink / raw)
To: gcc-bugs, gcc-prs, kcc, nobody
Synopsis: wrong conversion of constants: (int)(float)(int) (INT_MAX)
State-Changed-From-To: open->analyzed
State-Changed-By: ebotcazou
State-Changed-When: Wed Feb 19 10:51:14 2003
State-Changed-Why:
Already analyzed.
http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=9325
^ permalink raw reply [flat|nested] 3+ messages in thread
* optimization/9325: wrong conversion of constants: (int)(float)(int) (INT_MAX)
@ 2003-01-15 12:16 kcc
0 siblings, 0 replies; 3+ messages in thread
From: kcc @ 2003-01-15 12:16 UTC (permalink / raw)
To: gcc-gnats
>Number: 9325
>Category: optimization
>Synopsis: wrong conversion of constants: (int)(float)(int) (INT_MAX)
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: unassigned
>State: open
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Wed Jan 15 04:16:01 PST 2003
>Closed-Date:
>Last-Modified:
>Originator: Konstantine Serebriany
>Release: 3.2
>Organization:
>Environment:
Sparc Solaris 2.8
>Description:
(int)(float)(int)(INT_MAX)
is transformed to INT_MAX, which is incorrect.
Should leave the conversions.
f1() and f2() are optimized wrong,
while f3() and f4() are ok
///// file 2_31.c
extern int printf(const char *, ...);
int f1(){
return (int)2147483648.0f;
}
int f2(){
return (int)(float)( 2147483647 );
}
int f3(){
float a = 2147483648.0f;
return (int)a;
}
int f4(){
int a = 2147483647;
float b = (float)a;
return (int)b;
}
int main()
{
printf("%d\n%d\n%d\n%d\n", f1(), f2(), f3(), f4());
return 0;
}
>How-To-Repeat:
gcc 2_31.c ; a.out
>Fix:
>Release-Note:
>Audit-Trail:
>Unformatted:
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2003-02-19 10:51 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-18 10:56 optimization/9325: wrong conversion of constants: (int)(float)(int) (INT_MAX) Eric Botcazou
-- strict thread matches above, loose matches on Subject: below --
2003-02-19 10:51 ebotcazou
2003-01-15 12:16 kcc
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).