473,399 Members | 3,888 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,399 software developers and data experts.

loops - Case where goto's cannot be removed (optimization)

Hi!

The following code is a snippet where the goto's cannot be removed
"without increasing code-size, or decreasing performance".

/*********A**************/
int var_other_thread;
// value changed by another thread

int var_this_thread;

int array[100];

#define EQUAL \
var_this_thread == var_other_thread

#define NOT_EQUAL \
var_this_thread != var_other_thread

void myfuncA(void)
{
if (EQUAL) goto label2;

label1:
var_this_thread = var_other_thread;

label2:
array[var_this_thread] = 1;
if (NOT_EQUAL) goto label1;
}
/***********************/

Actually one goto can be removed without worsening code-size or
performance:

/*********B**************/
#define EQUAL \
var_this_thread == var_other_thread

void myfuncB(void)
{
if (EQUAL)
goto here;
while(1) {
var_this_thread = var_other_thread;
here:
array[var_this_thread] = 1;
if (EQUAL)
break;
}
}
/***********************/

In terms of machine code, myfuncB(void) will be the same as
myfuncA(void), right???
hmm... the above example is not so good.
I just realized that an optimization would be to leave the first
conditional and always force the assignment, right? Example below:
->
/*********C**************/
#define NOT_EQUAL \
var_this_thread != var_other_thread

void myfuncC(void)
{
do {
var_this_thread = var_other_thread;
array[var_this_thread] = 1;
} while (NOT_EQUAL);
}
/***********************/

-anon.asdf

Aug 14 '07 #1
9 1443
an*******@gmail.com wrote On 08/14/07 15:33,:
Hi!

The following code is a snippet where the goto's cannot be removed
"without increasing code-size, or decreasing performance".

/*********A**************/
int var_other_thread;
// value changed by another thread

int var_this_thread;

int array[100];

#define EQUAL \
var_this_thread == var_other_thread

#define NOT_EQUAL \
var_this_thread != var_other_thread

void myfuncA(void)
{
if (EQUAL) goto label2;

label1:
var_this_thread = var_other_thread;

label2:
array[var_this_thread] = 1;
if (NOT_EQUAL) goto label1;
}
/***********************/
void myfuncA(void)
{
do {
array[var_this_thread = var_other_thread] = 1;
} while (NOT_EQUAL);
}

Argument: The test for EQUAL is wasted, because if
the two variables are equal it does no harm to copy one
to the other. Yes, it takes time -- but eliminating the
test and the conditional jump saves time, so it is by no
means obvious that there's a net loss.

<off-topic>

Of course, neither the original nor my rewrite is
any use at all in an actual multi-threaded program. And
before anybody mentions it, no: `volatile' doesn't help.

</off-topic>
Actually one goto can be removed without worsening code-size or
performance:

/*********B**************/
#define EQUAL \
var_this_thread == var_other_thread

void myfuncB(void)
{
if (EQUAL)
goto here;
while(1) {
var_this_thread = var_other_thread;
here:
array[var_this_thread] = 1;
if (EQUAL)
break;
}
}
/***********************/
This doesn't remove a goto; it just changes the
way it's spelled.
In terms of machine code, myfuncB(void) will be the same as
myfuncA(void), right???
You'll need to ask your compiler; the C language
itself cannot answer the question.
hmm... the above example is not so good.
"Herein he was right."
I just realized that an optimization would be to leave the first
conditional and always force the assignment, right? Example below:
->
/*********C**************/
#define NOT_EQUAL \
var_this_thread != var_other_thread

void myfuncC(void)
{
do {
var_this_thread = var_other_thread;
array[var_this_thread] = 1;
} while (NOT_EQUAL);
}
/***********************/
How about that? You arrived at the same answer I
did, and disproved your own assertion that the gotos
could not be removed. Inquiring minds want to know:
Having refuted your own thesis, why did you press Send?

--
Er*********@sun.com

Aug 14 '07 #2
On Aug 14, 10:00 pm, Eric Sosman <Eric.Sos...@sun.comwrote:
>
How about that? You arrived at the same answer I
did, and disproved your own assertion that the gotos
could not be removed. Inquiring minds want to know:
Having refuted your own thesis, why did you press Send?
Hi Eric!

Thank's for your kind reply. I hit send, since I wanted to know if the
code structure (in terms of "if" and "goto") could be reformed, to an
equivalent that does not use goto.
(It is not possible.)

I wanted an analysis of code structure (in terms of "if" and "goto")
and not the contents...
which were badly chosen (and I did not take the time to change it -
sorry.): still the contents did reveal that one optimization - which
is interesting nonetheless

- anon.asdf

Aug 14 '07 #3
On Aug 14, 10:47 pm, anon.a...@gmail.com wrote:
I wanted an analysis of code structure (in terms of "if" and "goto")
and not the contents...
which were badly chosen (and I did not take the time to change it -
sorry.): still the contents did reveal that one optimization - which
is interesting nonetheless
Here is an example with better "content":

/*******************/

/* think of var_other_thread
as increasing steadily from 0 to 100
in another thread.

var_this_thread is handled in this code!
Think of it as 0 initially.
*/

#define NEARBY_BELOW \
(var_this_thread >= var_other_thread - 2) \
&& (var_this_thread <= var_other_thread) \

#define NOT_NEARBY_BELOW \
(var_this_thread < var_other_thread - 2) \
|| (var_this_thread var_other_thread)

/* only limited range is used -
we're not going to fall of (wrap around)
the (overflow) ends :)
*/

void myfuncA(void)
{
if (NEARBY_BELOW) goto label2;

label1:
var_this_thread = var_other_thread - 2;

label2:
array[var_this_thread] = 1;
if (NOT_NEARBY_BELOW) goto label1;

var_this_thread++;
}
/*******************/

(one goto could be removed - as was shown in the original post /
**B**/)

- Albert

Aug 14 '07 #4
On Tue, 14 Aug 2007 16:00:38 -0400, Eric Sosman wrote:
an*******@gmail.com wrote On 08/14/07 15:33,:
>Hi!

The following code is a snippet where the goto's cannot be removed
"without increasing code-size, or decreasing performance".

/*********A**************/
int var_other_thread;
// value changed by another thread

int var_this_thread;

int array[100];

#define EQUAL \
var_this_thread == var_other_thread

#define NOT_EQUAL \
var_this_thread != var_other_thread

void myfuncA(void)
{
if (EQUAL) goto label2;

label1:
var_this_thread = var_other_thread;

label2:
array[var_this_thread] = 1;
if (NOT_EQUAL) goto label1;
}
/***********************/

void myfuncA(void)
{
do {
array[var_this_thread = var_other_thread] = 1;
} while (NOT_EQUAL);
}

Argument: The test for EQUAL is wasted, because if
the two variables are equal it does no harm to copy one
to the other. Yes, it takes time -- but eliminating the
test and the conditional jump saves time, so it is by no
means obvious that there's a net loss.

<off-topic>

Of course, neither the original nor my rewrite is
any use at all in an actual multi-threaded program. And
before anybody mentions it, no: `volatile' doesn't help.

</off-topic>
Why doesn't it?
--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower

Aug 14 '07 #5
On Aug 14, 9:47 pm, anon.a...@gmail.com wrote:
Thank's for your kind reply. I hit send, since I wanted to know if the
code structure (in terms of "if" and "goto") could be reformed, to an
equivalent that does not use goto.
(It is not possible.)
Of course the code can be written without goto's. It has been proven
more than 30 years ago that any bit of code using goto's can be
rewritten without them (however, it is not necessarily an
improvement :-)

Aug 14 '07 #6
an*******@gmail.com wrote, On 14/08/07 22:01:
On Aug 14, 10:47 pm, anon.a...@gmail.com wrote:
>I wanted an analysis of code structure (in terms of "if" and "goto")
and not the contents...
which were badly chosen (and I did not take the time to change it -
sorry.): still the contents did reveal that one optimization - which
is interesting nonetheless

Here is an example with better "content":
<snip>
void myfuncA(void)
{
if (NEARBY_BELOW) goto label2;

label1:
var_this_thread = var_other_thread - 2;

label2:
array[var_this_thread] = 1;
if (NOT_NEARBY_BELOW) goto label1;

var_this_thread++;
}
/*******************/

(one goto could be removed - as was shown in the original post /
**B**/)

- Albert
Well, if I've not made any mistakes...
void myfuncA(void)
{
switch (NEARBY_BELOW) {
do {
case 0: var_this_thread = var_other_thread - 2;
default: array[var_this_thread] = 1;
} while (NOT_NEARBY_BELOW);
}
var_this_thread++;
}

Not even one goto statement. Of course, it is late at night so I probabl
have made a mistake. No repeated conditions. A loop construct to
implement the loop.
--
Flash Gordon
Aug 14 '07 #7
On Tue, 14 Aug 2007 23:11:59 +0200, Army1987 <ar******@NOSPAM.it>
wrote:
>On Tue, 14 Aug 2007 16:00:38 -0400, Eric Sosman wrote:
> void myfuncA(void)
{
do {
array[var_this_thread = var_other_thread] = 1;
} while (NOT_EQUAL);
}

Argument: The test for EQUAL is wasted, because if
the two variables are equal it does no harm to copy one
to the other. Yes, it takes time -- but eliminating the
test and the conditional jump saves time, so it is by no
means obvious that there's a net loss.

<off-topic>

Of course, neither the original nor my rewrite is
any use at all in an actual multi-threaded program. And
before anybody mentions it, no: `volatile' doesn't help.

</off-topic>
Why doesn't it?
volatile doesn't guarantee atomicity of operations.
If I write

volatile int x = 0;
x++;

That might likely come out as

mov reg,[x]
inc reg
mov [x],reg

then there's no reason that another thread can't come along in between
those operations and change [x].

Jim
Aug 15 '07 #8
On Aug 15, 12:25 am, Flash Gordon <s...@flash-gordon.me.ukwrote:
Well, if I've not made any mistakes...
void myfuncA(void)
{
switch (NEARBY_BELOW) {
do {
case 0: var_this_thread = var_other_thread - 2;
default: array[var_this_thread] = 1;
} while (NOT_NEARBY_BELOW);
}
var_this_thread++;

}

Not even one goto statement. Of course, it is late at night so I probabl
have made a mistake. No repeated conditions. A loop construct to
implement the loop.
Hi Flash! That's really good! -Albert

Aug 15 '07 #9
an*******@gmail.com wrote, On 15/08/07 08:18:
On Aug 15, 12:25 am, Flash Gordon <s...@flash-gordon.me.ukwrote:
>Well, if I've not made any mistakes...
void myfuncA(void)
{
switch (NEARBY_BELOW) {
do {
case 0: var_this_thread = var_other_thread - 2;
default: array[var_this_thread] = 1;
} while (NOT_NEARBY_BELOW);
}
var_this_thread++;

}

Not even one goto statement. Of course, it is late at night so I probabl
have made a mistake. No repeated conditions. A loop construct to
implement the loop.

Hi Flash! That's really good! -Albert
No it isn't, it's absolutely horrible :-)
It actually breaks one thing I would expect to be in any serious coding
standard, namely it jumps in to a loop.
If you like it I suggest you search for Duffs Device which was my
inspiration.
--
Flash Gordon
Aug 15 '07 #10

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

Similar topics

15
by: Steven T. Hatton | last post by:
It blows up on a 9 dimensional space, but I'm pretty sure that's due to the size of size_t. --- begin: DataGeneration_Test --- Tensor<T 3, 3>...
109
by: Neo | last post by:
Hi Folks,http://www.abarnett.demon.co.uk/tutorial.html#FASTFOR Page states:for( i=0; i<10; i++){ ... }i loops through the values 0,1,2,3,4,5,6,7,8,9 If you don't care about the order of the loop...
9
by: cody | last post by:
for (int serie=1; serie < TurnierGruppe.AktuelleSerie.Serie; serie++) { //outer: here is works foreach (Tischbesetzung tb in TurnierGruppe.Serien.Tischebesetzungen) { for (int i=0;...
24
by: Kunal | last post by:
Hello, I need help in removing if ..else conditions inside for loops. I have used the following method but I am not sure whether it has actually helped. Below is an example to illustrate what I...
6
by: Scott Brady Drummonds | last post by:
Hi, everyone, I was in a code review a couple of days ago and noticed one of my coworkers never used for() loops. Instead, he would use while() loops such as the following: i = 0; while (i...
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
29
by: Steve R. Hastings | last post by:
When you compile the expression for i in range(1000): pass does Python make an iterator for range(), and then generate the values on the fly? Or does Python actually allocate the list and...
11
by: O.B. | last post by:
Does C# support anything like PHP's break command that optionally accepts a parameter specifying how many loops to break out of?
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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
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,...
0
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...

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.