public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
From: Richard Addison-Wood <richard@wetafx.co.nz>
To: nobody@gcc.gnu.org
Cc: gcc-prs@gcc.gnu.org,
Subject: Re: optimization/9736: same fp comparison can lead to different results
Date: Thu, 27 Feb 2003 07:46:00 -0000	[thread overview]
Message-ID: <20030227074600.886.qmail@sources.redhat.com> (raw)

The following reply was made to PR optimization/9736; it has been noted by GNATS.

From: Richard Addison-Wood <richard@wetafx.co.nz>
To: richard@wetafx.co.nz, gcc-gnats@gcc.gnu.org, gcc-bugs@gcc.gnu.org,
   nobody@gcc.gnu.org, gcc-prs@gcc.gnu.org
Cc:  
Subject: Re: optimization/9736: same fp comparison can lead to different results
Date: Thu, 27 Feb 2003 20:37:25 +1300

 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=9736
 
 From various parts of the ISO/IEC C++ standard:
 
   5 Expressions
 
   ...
 
   Many binary operators that expect operands of
   arithmetic or enumeration type cause conversions
   and yield result types in a similar way. The
   purpose is to yield a common type, which is also
   the type of the result. This pattern is called
   the usual arithmetic conversions, which are
   defined as follows:
 
     If either operand is of type long double, the
     other shall be converted to long double.
 
     Otherwise, if either operand is double, the
     other shall be converted to double.
 
     Otherwise, if either operand is float, the
     other shall be converted to float.
 
     ...
 
   The values of the floating operands and the
   results of floating expressions may be
   represented in greater precision and range than
   that required by the type; the types are not
   changed thereby.  55)
 
   ...
 
   55) The cast and assignment operators must still
   perform their specific conversions as described
   in 5.4, 5.2.9 and 5.17.
 
 ...
 
   5.17 Assignment operators
 
   There are several assignment operators, all of
   which group right-to-left. All require a
   modifiable lvalue as their left operand, and the
   type of an assignment expression is that of its
   left operand. The result of the assignment
   operation is the value stored in the left
   operand after the assignment has taken place;
   the result is an lvalue.
 
   ...
 
   In simple assignment (=), the value of the
   expression replaces that of the object referred
   to by the left operand.
 
 ...
 
   4 Standard conversions
 
   ... A standard conversion sequence will be
   applied to an expression if necessary to convert
   it to a required destination type.
 
   [Note: expressions with a given type will be
   implicitly converted to other types in several
   contexts:
 
   -- When used as operands of operators. The
   operator's requirements for its operands dictate
   the destination type (clause 5).
 
   ...
 
   -- When used as the source expression for an
   initialization (which includes use as an
   argument in a function call and use as the
   expression in a return statement). The type of
   the entity being initialized is (generally) the
   destination type. See 8.5, 8.5.3.
 
   -- end note]
 
 ...
 
   4.8 Floating point conversions
 
   An rvalue of floating point type can be
   converted to an rvalue of another floating point
   type. If the source value can be exactly
   represented in the destination type, the result
   of the conversion is that exact
   representation. If the source value is between
   two adjacent destination values, the result of
   the conversion is an implementation-defined
   choice of either of those values. Otherwise, the
   behavior is undefined.
 
 
 
 
 Consider this small test program:
 
 // original.cc:
 int f(float x, float y, float z, float w)
 {
   float v = x*x+y*y+z*z+w;
   int test1 = v < 1.0f ? 1 : 2;
   return test1;
 }
 int main(void)
 {
   return f(0.5f, 0.5f, 0.5f, 0.25f-1.0f/67108864.0f);
 }
 
 The standard allows the intermediate calculations
 for v to be at greater precision than float,
 although the types are still considered to be
 float.  However, when v itself is initialized,
 there is an implicit standard conversion to float.
 Thus, we are allowed rewrite the small test
 program as this (where the types represent the
 actual precision):
 
 // transformed.cc:
 int f(float x, float y, float z, float w)
 {
   const long double temp_x = x;
   const long double temp_y = y;
   const long double temp_z = z;
   const long double temp_w = w;
   const long double temp_xx = temp_x*temp_x;
   const long double temp_yy = temp_y*temp_y;
   const long double temp_xxpyy = temp_xx+temp_yy;
   const long double temp_zz = temp_z*temp_z;
   const long double temp_xxpyypzz = temp_xxpyy+temp_zz;
   const long double temp_xxpyypzzpw = temp_xxpyypzz+temp_w;
   float v = temp_xxpyypzzpw;
   int test1 = v < 1.0f ? 1 : 2;
   return test1;
 }
 int main(void)
 {
   return f(0.5f, 0.5f, 0.5f, 0.25f-1.0f/67108864.0f);
 }
 
 I expect both programs to initialize v to 1.0f
 (and return 2 from f() and main()).  However, with
 -O1, original.cc is not compiled according to the
 standard.
 
 [Paradoxically, transformed.cc is compiled
 correctly with -O1.  The generated code is
 essentially the same except for transformed.cc
 that the proper conversion to float is generated
 for initializing v.]


             reply	other threads:[~2003-02-27  7:46 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-02-27  7:46 Richard Addison-Wood [this message]
  -- strict thread matches above, loose matches on Subject: below --
2003-03-05  7:46 ebotcazou
2003-03-05  3:26 Richard Addison-Wood
2003-02-19 14:06 Tim Prince
2003-02-19  9:06 Eric Botcazou
2003-02-18 22:16 Richard Addison-Wood
2003-02-18  8:04 ebotcazou
2003-02-17 23:16 richard

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=20030227074600.886.qmail@sources.redhat.com \
    --to=richard@wetafx.co.nz \
    --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).