From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 13815 invoked by alias); 18 Feb 2003 10:56:01 -0000 Mailing-List: contact gcc-prs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-prs-owner@gcc.gnu.org Received: (qmail 13736 invoked by uid 71); 18 Feb 2003 10:56:01 -0000 Date: Tue, 18 Feb 2003 10:56:00 -0000 Message-ID: <20030218105601.13735.qmail@sources.redhat.com> To: nobody@gcc.gnu.org Cc: gcc-prs@gcc.gnu.org, From: Eric Botcazou Subject: Re: optimization/9325: wrong conversion of constants: (int)(float)(int) (INT_MAX) Reply-To: Eric Botcazou X-SW-Source: 2003-02/txt/msg00795.txt.bz2 List-Id: The following reply was made to PR optimization/9325; it has been noted by GNATS. From: Eric Botcazou 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