473,626 Members | 3,216 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1494
"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
4739
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 where column_name = 'value' or 1 = 0 select count(*) from table_name where column_name = 'value' I do not want to go into the reason why the redundent condition exists, the example is representative of the real query where it
17
3282
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). the vc++ version is twice! as fast in release mode. in debug mode its as fast as cbuilder. it seems i cant get cbuilder to compile a real release version. when i check "Project options:compiler:release" it even gets slower than debug! i have played...
3
4428
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 do the following and verify what I am seeing (and offer any insight!), I would appreciate it Simply create a new "Win32 Console Project" in VC++ .NET 2003. On the "Application Settings" page, check the "Empty project" box. Click "Finish" Add a...
4
2297
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 emailed support, and the reply was: "We understand your concerns in this matter. We would like to inform you that at this moment we do not have any plan or information if there will be replacements for the discontinued exams." Does anybody...
2
1496
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/) which provides an optimizing compiler. Is it possible to replace the original "Standard" compiler with this one ? How could I do this ?
37
2134
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 develop with (i.e. no header files, objects are easier to create and use, C# language is native to .NET) I'm sure this question will stir some emotions :-) -- Greg McPherran www.McPherran.com
7
2130
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
1651
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 could help me out here, I'd really appreciate it. Iam a project trainee. I have to give an presentation over the topic, For engineering applications, whether VC++ 6.0 is more suitable
3
2372
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. ------------------------- Lets say I have a table FOO1 that has, among other columns, a column named A. There is a non-unique index on A that has medium selectivity. Lets also say I have a table FOO2 that has, among other columns, a column named B. ...
0
8268
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8707
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8641
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8366
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8510
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4202
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1812
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.