public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
From: Eric Botcazou <ebotcazou@libertysurf.fr>
To: nobody@gcc.gnu.org
Cc: gcc-prs@gcc.gnu.org,
Subject: Re: optimization/9325: wrong conversion of constants: (int)(float)(int) (INT_MAX)
Date: Tue, 18 Feb 2003 10:56:00 -0000	[thread overview]
Message-ID: <20030218105601.13735.qmail@sources.redhat.com> (raw)

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


             reply	other threads:[~2003-02-18 10:56 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-02-18 10:56 Eric Botcazou [this message]
  -- strict thread matches above, loose matches on Subject: below --
2003-02-19 10:51 ebotcazou
2003-01-15 12:16 kcc

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20030218105601.13735.qmail@sources.redhat.com \
    --to=ebotcazou@libertysurf.fr \
    --cc=gcc-prs@gcc.gnu.org \
    --cc=nobody@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).