473,513 Members | 4,753 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Observable behavior and instruction reordering

<CODE>

#include <iostream>
using namespace std;

int Foo(int x,int y)
{
int result = x;
result*=y;
result+=y;
return result;
}

int main()
{
cout<<Foo(2,4)<<endl; // I expect this to output 12 --LINE 1

cout<<Foo(4,2)<<endl; //I expect this to output 10 --LINE 2

return 0;
}

</CODE>

Are my expectations concerning the output of this program reasonable?

My concern stems from a lack of understanding of instruction reordering as it
relates to observable behavior.

Specifically I wonder if the expressions result*=y and result+=y in function
Foo could be swapped. That of course would change the meaning of Foo.
However, the following quote from the Draft Ansi C++ Standard makes me think
that the meaning of Foo is not observable behavior...

"The observable behavior of the abstract machine is its sequence of reads and
writes to volatile data and calls to library I/O functions."

Based on this quote the only behavior I would expect is that LINE 1 will
execute before LINE 2.

When a compiler reorders instructions that don't involve R/W to volatile data
and calls to library I/O functions how does it know that it is not changing the
meaning of a program?

Any insights as to what I am not grokking would be appreciated.
Jul 22 '05 #1
4 2639

"DaKoadMunky" <da*********@aol.com> wrote in message
news:20***************************@mb-m22.aol.com...
<CODE>

#include <iostream>
using namespace std;

int Foo(int x,int y)
{
int result = x;
result*=y;
result+=y;
return result;
}

int main()
{
cout<<Foo(2,4)<<endl; // I expect this to output 12 --LINE 1

cout<<Foo(4,2)<<endl; //I expect this to output 10 --LINE 2

return 0;
}

</CODE>

Are my expectations concerning the output of this program reasonable?

My concern stems from a lack of understanding of instruction reordering as it relates to observable behavior.

Specifically I wonder if the expressions result*=y and result+=y in function Foo could be swapped. That of course would change the meaning of Foo.
However, the following quote from the Draft Ansi C++ Standard makes me think that the meaning of Foo is not observable behavior...

"The observable behavior of the abstract machine is its sequence of reads and writes to volatile data and calls to library I/O functions."

Based on this quote the only behavior I would expect is that LINE 1 will
execute before LINE 2.

When a compiler reorders instructions that don't involve R/W to volatile data and calls to library I/O functions how does it know that it is not changing the meaning of a program?

Any insights as to what I am not grokking would be appreciated.


If the instructions were reordered in the way you suggest then you would not
see cout << 12 followed by cout << 10 (you would see cout << 24 followed by
cout << 12), therefore the observable behaviour would have changed because
the values of the arguments used in a standard library call would have
changed, and therefore the compiler would not be allowed to do that
reordering.

John
Jul 22 '05 #2
da*********@aol.com (DaKoadMunky) wrote in
news:20***************************@mb-m22.aol.com:
<CODE>

#include <iostream>
using namespace std;

int Foo(int x,int y)
{
int result = x;
result*=y;
result+=y;
return result;
}

int main()
{
cout<<Foo(2,4)<<endl; // I expect this to output 12 --LINE 1

cout<<Foo(4,2)<<endl; //I expect this to output 10 --LINE 2

return 0;
}

</CODE>

Are my expectations concerning the output of this program reasonable?

My concern stems from a lack of understanding of instruction
reordering as it relates to observable behavior.

Specifically I wonder if the expressions result*=y and result+=y in
function Foo could be swapped. That of course would change the
meaning of Foo. However, the following quote from the Draft Ansi C++
Standard makes me think that the meaning of Foo is not observable
behavior...

"The observable behavior of the abstract machine is its sequence of
reads and writes to volatile data and calls to library I/O functions."

Based on this quote the only behavior I would expect is that LINE 1
will execute before LINE 2.

When a compiler reorders instructions that don't involve R/W to
volatile data and calls to library I/O functions how does it know that
it is not changing the meaning of a program?


Because the people who created it, told it to only reorder those things
that won't break the meaning of the program. Observable behaviour is not
the only criteria when dealing with instruction reordering, obviously.

Cheers.
--
:: bartekd / o2 pl
:: "out of confusion comes chaos -- out of chaos comes confusion and fear
:: -- then comes lunch."

Jul 22 '05 #3
On 09 Aug 2004 11:17:04 GMT, da*********@aol.com (DaKoadMunky) wrote:
<CODE>

#include <iostream>
using namespace std;

int Foo(int x,int y)
{
int result = x;
result*=y;
result+=y;
return result;
}

int main()
{
cout<<Foo(2,4)<<endl; // I expect this to output 12 --LINE 1

cout<<Foo(4,2)<<endl; //I expect this to output 10 --LINE 2

return 0;
}

</CODE>

Are my expectations concerning the output of this program reasonable?
Yes, any standards conforming compiler should produce a program that
outputs:
12
10
My concern stems from a lack of understanding of instruction reordering as it
relates to observable behavior.

Specifically I wonder if the expressions result*=y and result+=y in function
Foo could be swapped. That of course would change the meaning of Foo.
However, the following quote from the Draft Ansi C++ Standard makes me think
that the meaning of Foo is not observable behavior...

"The observable behavior of the abstract machine is its sequence of reads and
writes to volatile data and calls to library I/O functions."

Based on this quote the only behavior I would expect is that LINE 1 will
execute before LINE 2.
"calls to library I/O functions" includes what is passed to those
functions! i.e. any reorderings that the compiler does must not change
the appearance of the output of a conforming program in any way.
When a compiler reorders instructions that don't involve R/W to volatile data
and calls to library I/O functions how does it know that it is not changing the
meaning of a program?


If it can't determine that it won't make a difference to observable
behaviour, it won't reorder (since it is forbidden by the standard).

Tom
Jul 22 '05 #4
DaKoadMunky wrote:
<CODE>

#include <iostream>
using namespace std;

int Foo(int x,int y)
{
int result = x;
result*=y;
result+=y;
return result;
}

int main()
{
cout<<Foo(2,4)<<endl; // I expect this to output 12 --LINE 1

cout<<Foo(4,2)<<endl; //I expect this to output 10 --LINE 2

return 0;
}

</CODE>

Are my expectations concerning the output of this program reasonable?

My concern stems from a lack of understanding of instruction reordering as it
relates to observable behavior.

Specifically I wonder if the expressions result*=y and result+=y in function
Foo could be swapped. That of course would change the meaning of Foo.
However, the following quote from the Draft Ansi C++ Standard makes me think
that the meaning of Foo is not observable behavior...

"The observable behavior of the abstract machine is its sequence of reads and
writes to volatile data and calls to library I/O functions."

Based on this quote the only behavior I would expect is that LINE 1 will
execute before LINE 2.

When a compiler reorders instructions that don't involve R/W to volatile data
and calls to library I/O functions how does it know that it is not changing the
meaning of a program?

Any insights as to what I am not grokking would be appreciated.


A translator is allowed to set the ordering of the instructions as long
as the functionality remains the same.

In your example, changing the order of the mathematical assignments
will change the meaning. However, there is nothing stopping a
translator changing the order of the assignment operation.

Your function evaluates the expression: f(x,y) = x * y + y;
The actually assignment of the value to result could be postponed
or not even occur at all (since the value is returned and not
stored in any static memory location). For example, your function
could be translated as:
int Foo(int x, int y)
{
return x * y + y;
}
The above function provides the same functionality, but has
no assignment operations.

There are no libraries calls, no I/O and no volatile or static
variables.

However, changing the order of the operations will change the
functionality.

The rules allow for compilers to optimize code. The qualifiers
"static", "volatile" and "mutable" are used to control the
optimization. During debugging, I may declare a variable
in a function as "static" so that the variable isn't "optimized
away". I use "volatile" when accessing hardware values so that
the compiler doesn't "optimize away" a variable that is read
only once.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #5

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

Similar topics

10
4247
by: tgh003 | last post by:
So lets assume I have a list of tasks in db table (table looks like: ID, task, sort) And I want to reorder the task list without updating all the records in the table. I dont want to run a sql...
3
2299
by: ThunderMusic | last post by:
Hi, I'm trying to have a MSN Messenger like form/app closing behavior. When I click on the X button, I only want the form to disappear and when I double-click on the notify icon or right-click...
5
6863
by: John Dann | last post by:
Can anyone point me to a tutorial on reordering items within a single listbox using the mouse, ie drag and drop, specifically with vb.net. Google shows me various half-references eg to related...
5
2022
by: Steven T. Hatton | last post by:
If find the following excerpt from the Standard a bit confusing: <quote> 3.3.6 - Class scope -1- The following rules describe the scope of names declared in classes. 1) The potential scope...
15
1820
by: loic-dev | last post by:
Hello everybody! Assume that I have the following function: void call_once(int* done, void(*routine)(void)) { if (!(*done)) { /* L1 */ routine(); /* L2 */ *done = 1; /* L3 */
7
1749
by: =?Utf-8?B?QnJpYW4gS3Vueg==?= | last post by:
Hello, I'm new to the boards, and I've been struggling with a problem porting my company's code from Visual C++ 6.0 to Visual C++ 2005. We've found some crashes that I've traced to the...
12
3036
by: Rajesh S R | last post by:
Can anyone tell me what is the difference between undefined behavior and unspecified behavior? Though I've read what is given about them, in ISO standards, I'm still not able to get the...
36
2039
by: dspfun | last post by:
Hi! Is there any way in C that one can guarantee that instructions are executed in the same order as they are written in the program? For example, say I have the following: instruction1;...
20
1349
by: Tommy Vercetti | last post by:
Hi - Great group! I have 2 queries about undefined behavior: 1) Is the following code undefined? float myfunction(float f) {
0
7153
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
7373
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,...
0
7519
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...
0
5677
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,...
1
5079
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1585
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.