473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

VC++ 7.1 optimizer bug

We've recently encountered a bug in the optimization of
floating point computations inside loops. To summarize, the
optimizer reorders floating point operations in a fashion that
is not permitted by the C++ Standard; even worse, when the
optimizer unrolls a loop, the operations are ordered
differently in different iterations. (This occurs in version
13.10.3077.)

Here's a sample program simplified from production code:

#pragma optimize("g",on)

int main(void) {
extern double hB[];
double x[4];
double f0[4];
double f1[4];
double f2[4];

int i;
for (int p = 0; p < 1; p++)
{
for (i = 0; i < 4; i++) {
x[i] = f0[i]*hB[0] + f1[i]*hB[1] + f2[i]*hB[2] ;
}
if ( (x[0] != x[2]) || (x[1] != x[3])) {
return 1;
}
}
return 0;
}

The problem appears in the computation of x[i]. Here's the assembly
code for that loop:

fld QWORD PTR ?hB@@3PANA
xor ecx, ecx
fmul QWORD PTR _f0$[ebp]
fld QWORD PTR ?hB@@3PANA+8
fmul QWORD PTR _f1$[ebp]
faddp ST(1), ST(0)
fld QWORD PTR ?hB@@3PANA+16
fmul QWORD PTR _f2$[ebp]
faddp ST(1), ST(0)
fld QWORD PTR ?hB@@3PANA
fmul QWORD PTR _f0$[ebp+8]
fld QWORD PTR ?hB@@3PANA+8
fmul QWORD PTR _f1$[ebp+8]
faddp ST(1), ST(0)
fld QWORD PTR ?hB@@3PANA+16
fmul QWORD PTR _f2$[ebp+8]
faddp ST(1), ST(0)
fld QWORD PTR ?hB@@3PANA
fmul QWORD PTR _f0$[ebp+16]
fld QWORD PTR ?hB@@3PANA+8
fmul QWORD PTR _f1$[ebp+16]
faddp ST(1), ST(0)
fld QWORD PTR ?hB@@3PANA+16
fmul QWORD PTR _f2$[ebp+16]
faddp ST(1), ST(0)
fld QWORD PTR _f2$[ebp+24]
fmul QWORD PTR ?hB@@3PANA+16
fld QWORD PTR _f1$[ebp+24]
fmul QWORD PTR ?hB@@3PANA+8
faddp ST(1), ST(0)
fld QWORD PTR _f0$[ebp+24]
fmul QWORD PTR ?hB@@3PANA
faddp ST(1), ST(0)

Note that for the first three iterations of the unrolled loop,
the sum "f0[i]*hB[0] + f1[i]*hB[1]" is first computed, then
the result of "f2[i]*hB[2]" is added. In the fourth iteration,
however, the sum "f2[i]*hB[2] + f1[i]*hB[1]" is computed and
then "f0[i]*hB[0]" is added.

Because of the effects of rounding, floating point calculations
are sensitive to the order in which operations are performed;
as noted in the C89 Standard, on which the C++ Standard was
based, floating point addition and multiplication are not
associative operations. The C++ Standard (1.9 para 15) says
that "operators can be regrouped according to the usual
mathematical rules only where the operators really are
associative or commutative."

Clause 5 para 4 says, "Except where noted, the order of
evaluation of operands of individual operators and
subexpressions of individual expressions and the order in
which side effects take place, is unspecified." However, that
doesn't apply to something like "a + b + c", as here. Because
additive operators group left-to-right (5.7 para 1), the meaning
of "a + b + c" is "(a + b) + c", so "b + c" is _not_ a
subexpression of "a + b + c" -- no reordering of the "+"
operators is permitted. The violation of this constraint is
especially pernicious when it's inconsistently applied across
unrolled loop iterations.

Interestingly, if the implicit grouping of the operators is made
explicit by adding parentheses, the optimizer does not reorder
the expression, even though there is no semantic difference
between "a + b + c" and "(a + b) + c".

-- William M. Miller
The MathWorks, Inc.
Nov 17 '05 #1
1 1486
"William M. Miller" <wm*@world.std.com> wrote in message
news:bu************@ID-166721.news.uni-berlin.de...
We've recently encountered a bug in the optimization of
floating point computations inside loops. To summarize, the
optimizer reorders floating point operations in a fashion that
is not permitted by the C++ Standard; even worse, when the
optimizer unrolls a loop, the operations are ordered
differently in different iterations. (This occurs in version
13.10.3077.)


So, is this a known bug? Is it still present in the Whidbey
alpha release? If so, could one of the MVPs report it to
Microsoft? Or should I do so myself?

Thanks.

-- William M. Miller
The MathWorks
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: hemal | last post by:
I came across a very strange situation at work. There is an order of magnitude difference in execution time for the following two queries (10 v/s ~130 msec): select count(*) from table_name...
17
by: PDQBach | last post by:
Hello, im a visual c++ und borland c++builder newbie. i have witten a simple mandelbrot algorithm and compiled it with both vc++ (mfc) and cbuilder (vcl) (same code besides the drawing part)....
3
by: Binary | last post by:
VC++ .NET 2003: Access violation with /O2 compiler switch; no AV with /O Hi I'm in the process of narrowing down a problem, and I have reduced the code involved to the following If someone could...
4
by: ultranet | last post by:
I have cruised around http://msdn.microsoft.com/visualc/ and the rest of the site, and i am not able to find a single C++ or VC++ certification exam that will be available after June 30, 2004. I...
2
by: zorro | last post by:
Hi, I use the Visual C++ .NET 2003 Standard Edition which doesn't include an optimizing compiler. But Microsoft released the VC++ 2003 Toolkit (http://msdn.microsoft.com/visualc/vctoolkit2003/)...
37
by: Greg | last post by:
Except for legacy or non-.NET applications, is there any reason to use VC++ anymore? It seems that for .NET applications, there would be no reason to choose C++ over C# since C# is faster to...
7
by: Frederico Pissarra | last post by:
Recently I tried to use -G5 option on CL compiler (from Visual Studio 2005)... To my surprise, there is no processor specific optimizations anymore! Is that correct? Is so, why? s Fred
2
by: bhag | last post by:
hi all, I'm writing because I haven't been able to find enough information at the book stores and at the MS web site on some of my questions. Perhaps I'm asking the wrong questions, but if you...
3
by: aj | last post by:
DB2 LUW v8.2 FP 14 RHAS 2.1 Sorry if these are newbie questions. Optimizer stuff is black magic to me. For both of these, assume stats are current and an even distribution of data....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.