This is the mail archive of the gcc-prs@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]

Re: optimization/9325: wrong conversion of constants: (int)(float)(int) (INT_MAX)


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


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