473,395 Members | 1,701 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,395 software developers and data experts.

"smart" indexing

Hi NG,

I am developing a piece of software, which contains basicly one loop,
which consinsts of 2 parts. Thos two parts are again basicly the same,
but they deiffer in some variables. So I changed the variables from foo0
and foo1 to foo[2], so I can index them. This way I can run through the
loop two times more, but switch the index between 0 and 1.
This is pretty easy. I did it this way:

int idx;
for(idx=0;;idx=!idx)
{
do_something_with(foo[idx]);
if(somestatement) break;
}

my question is: Does the standard guarantee that idx=!idx always result
in a 1 or a 0? And if I would want the compiler to loop unroll this,
would that work? Or does that only work for a for loop which has a clear
end statement in stead of a break?

Thanks in advance,
Mark

Nov 13 '05 #1
14 1803
"Capstar" <sp***@eg.homeip.net> wrote in message
news:3F**************@eg.homeip.net...

int idx;
for(idx=0;;idx=!idx)
{
do_something_with(foo[idx]);
if(somestatement) break;
}

my question is: Does the standard guarantee that idx=!idx always result
in a 1 or a 0?
Yes, it does (section 6.5.3.3, paragraph 5).
And if I would want the compiler to loop unroll this,
would that work? Or does that only work for a for loop which has a clear
end statement in stead of a break?


That I don't know. :-\

--
Russell Hanneken
rh*******@pobox.com
Nov 13 '05 #2
Hi,
"Capstar" <sp***@eg.homeip.net> wrote in message
news:3F**************@eg.homeip.net...
....
| This is pretty easy. I did it this way:
|
| int idx;
| for(idx=0;;idx=!idx)
| {
| do_something_with(foo[idx]);
| if(somestatement) break;
| }
|
| my question is:
| Does the standard guarantee that idx=!idx always result in a 1 or a 0?

Yes.
Alternatively, you could write:
idx = 1-idx; // not really better
or:
idx = (idx+1)%2; // could apply to any range ( %3, %4, etc... )

| And if I would want the compiler to loop unroll this,
| would that work? Or does that only work for a for loop which has a clear
| end statement in stead of a break?

As all optimizations, loop unrolling is very implementation-specific.
You'll have to try and check the generated code to be sure.
Or not worry about it, until you need to optimize the performance
of your code.

radix omnia malorum prematurae optimisatia est
-- Donald Knuth
Cheers,
Ivan
--
http://ivan.vecerina.com


Nov 13 '05 #3
Capstar wrote:
.... snip ...
int idx;
for(idx=0;;idx=!idx)
{
do_something_with(foo[idx]);
if(somestatement) break;
}

my question is: Does the standard guarantee that idx=!idx always
result in a 1 or a 0?


Yes. However you can have a much simpler and clearer loop with:

for (idx = 0; idx < 2; idx++) {
do_something_with(foo[idx]);
}

(Note that the blank shortage ended several years ago)

--
Replies should be to the newsgroup
Chuck Falconer, on vacation.
Nov 13 '05 #4
Capstar wrote:

Hi NG,

I am developing a piece of software, which contains basicly one loop,
which consinsts of 2 parts. Thos two parts are again basicly the same,
but they deiffer in some variables.
So I changed the variables from foo0
and foo1 to foo[2], so I can index them.
This way I can run through the
loop two times more, but switch the index between 0 and 1.
This is pretty easy. I did it this way:

int idx;
for(idx=0;;idx=!idx)
{
do_something_with(foo[idx]);
if(somestatement) break;
}

my question is: Does the standard guarantee
that idx=!idx always result in a 1 or a 0?
Yes.
And if I would want the compiler to loop unroll this,
would that work?
Or does that only work for a for loop which has a clear
end statement in stead of a break?


I don't know.
I'd write that loop, this way:

idx = 1;
do {
idx = !idx;
do_something_with(foo[idx]);
} while (!somestatement);
Nov 13 '05 #5
LibraryUser wrote:
Capstar wrote:

... snip ...
int idx;
for(idx=0;;idx=!idx)
{
do_something_with(foo[idx]);
if(somestatement) break;
}

my question is: Does the standard guarantee that idx=!idx always
result in a 1 or a 0?

Yes. However you can have a much simpler and clearer loop with:

for (idx = 0; idx < 2; idx++) {
do_something_with(foo[idx]);
}


I agree that this is a much simpler and clearer loop, but it doesn't
meet my requerements, I don't want the loop to end after 2 iterations.
It should continue until a certain condition is met. So the values for
idx shoudl be 0,1,0,1,0,1,0,1,....

(Note that the blank shortage ended several years ago)


What is "the blank shortage"?

Mark

Nov 13 '05 #6
pete wrote:
Capstar wrote:
Hi NG,

I am developing a piece of software, which contains basicly one loop,
which consinsts of 2 parts. Thos two parts are again basicly the same,
but they deiffer in some variables.
So I changed the variables from foo0
and foo1 to foo[2], so I can index them.
This way I can run through the
loop two times more, but switch the index between 0 and 1.
This is pretty easy. I did it this way:

int idx;
for(idx=0;;idx=!idx)
{
do_something_with(foo[idx]);
if(somestatement) break;
}

my question is: Does the standard guarantee
that idx=!idx always result in a 1 or a 0?

Yes.

And if I would want the compiler to loop unroll this,
would that work?
Or does that only work for a for loop which has a clear
end statement in stead of a break?

I don't know.
I'd write that loop, this way:

idx = 1;
do {
idx = !idx;
do_something_with(foo[idx]);
} while (!somestatement);


Yes, I appologise for my example being to much simplified perhaps. The
if(somestatement) break; is somewhat longer. It is actually if
(somestatement) { wait_event_interruptible_timeout(...); break;);

I could probably also do the wait_blabla after the while-loop, but again
that would give me some trouble.

Thanks for your input anyway,
Mark

Nov 13 '05 #7
Capstar wrote:
int idx;
for(idx=0;;idx=!idx)
{
do_something_with(foo[idx]);
if(somestatement) break;
} And if I would want the compiler to loop unroll this,
would that work?


According to my understanding of what "unrolling" is,
I think that you have to know the minimum number of times
that the loop will execute, in order to unroll it just that much.
I don't see any indication that that number is higher than one.
Nov 13 '05 #8

"Capstar" <sp***@eg.homeip.net> schrieb im Newsbeitrag
news:3F**************@eg.homeip.net...
LibraryUser wrote:
Capstar wrote:

... snip ...
[....]
What is "the blank shortage"?


Once upon a time the suppliers of blanks were unable to deliver enough
blanks, so every C programmer had to write things like

for(i=0,j=5;i<234&&somecondition||someotherconditi on;i+=somevalue)
{
yabbadabba...
}
(note there was an underliner - shortage as well)
But that is over, Now we can write:

for(i = 0, j = 5; i < 234 && some_condition || some_other_condition; i +=
some_value)
{
yabbadabba...
}
which is slightly better readable :)
no attack intended :)
Robert
Nov 13 '05 #9

"Capstar" <sp***@eg.homeip.net> wrote in message
news:3F**************@eg.homeip.net...
LibraryUser wrote:
Capstar wrote:

... snip ...
int idx;
for(idx=0;;idx=!idx)
{
do_something_with(foo[idx]);
if(somestatement) break;
} my question is: Does the standard guarantee that idx=!idx always
result in a 1 or a 0?
Yes. However you can have a much simpler and clearer loop with:

for (idx = 0; idx < 2; idx++) {
do_something_with(foo[idx]);
}


I agree that this is a much simpler and clearer loop, but it doesn't
meet my requerements, I don't want the loop to end after 2 iterations.
It should continue until a certain condition is met. So the values for
idx shoudl be 0,1,0,1,0,1,0,1,....


In that case, I would make two loops. One to do the 0,1 part, and another
loop outside that one, so that the compiler would easily see the loop to
unroll.

As in a post I answered yesterday, too bad the preprocessor can't do this.
In PL/I you can do:

%DO idx=0 TO 1;
do_something_with(foo[idx]);
%END;

and the preprocessor will expand it for you.

You could write your own preprocessor. It isn't all that uncommon a
solution to these problems.

-- glen
Nov 13 '05 #10
pete wrote:

Capstar wrote:
int idx;
for(idx=0;;idx=!idx)
{
do_something_with(foo[idx]);
if(somestatement) break;
}

And if I would want the compiler to loop unroll this,
would that work?


According to my understanding of what "unrolling" is,
I think that you have to know the minimum number of times
that the loop will execute, in order to unroll it just that much.
I don't see any indication that that number is higher than one.


I see two. Rather I see any number of iterations with idx == 0, idx ==
1, idx == 0, ad nauseum until (somestatement) is 'true'. Note 'idx=!idx'
is an assignment 'idx = !idx' which is a toggle, 0 to 1 to 0, etc.
--
Joe Wright mailto:jo********@earthlink.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #11

On Mon, 15 Sep 2003, Glen Herrmannsfeldt wrote:

"Capstar" <sp***@eg.homeip.net> wrote...

It should continue until a certain condition is met. So the values for
idx should be 0,1,0,1,0,1,0,1,....


In that case, I would make two loops. One to do the 0,1 part, and
another loop outside that one, so that the compiler would easily see
the loop to unroll.

As in a post I answered yesterday, too bad the preprocessor can't do
this. In PL/I you can do:

%DO idx=0 TO 1;
do_something_with(foo[idx]);
%END;

and the preprocessor will expand it for you.

You could write your own preprocessor. It isn't all that uncommon a
solution to these problems.


Well, in this case the C preprocessor will be fine. I've done
similar things before:
#define do_something(FOO) \
{ \
... /* lines of stuff ending with backslashes */ \
}

....
do_something(0);
do_something(1);
Simple, right?
(Searchers for more complicated stuff, or stuff involving loops
from 0 to 15126 instead of 0 to 1, might be advised to write a
program that could generate the tricky bits of the program you
want. Basically what Glen said, but you really don't need a
whole preprocessor if you want to do only one thing!)

-Arthur

Nov 13 '05 #12
On Mon, 15 Sep 2003 15:49:32 +0200, Capstar <sp***@eg.homeip.net> wrote:
pete wrote:
idx = 1;
do {
idx = !idx;
do_something_with(foo[idx]);
} while (!somestatement);


Yes, I appologise for my example being to much simplified perhaps. The
if(somestatement) break; is somewhat longer. It is actually if
(somestatement) { wait_event_interruptible_timeout(...); break;);


So make it this:

idx = 1;
for (;;) // or while (1)
{
idx = !idx;
do_something_with(foo[idx]);
if (somestatement)
break;
}
Six of one, half a gross of the other.
--
#include <standard.disclaimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Per the FCA, this address may not be added to any commercial mail list
Nov 13 '05 #13
Capstar wrote:
LibraryUser wrote:
Capstar wrote:

... snip ...
int idx;
for(idx=0;;idx=!idx)
{
do_something_with(foo[idx]);
if(somestatement) break;
}

my question is: Does the standard guarantee that idx=!idx
always result in a 1 or a 0?

Yes. However you can have a much simpler and clearer loop with:

for (idx = 0; idx < 2; idx++) {
do_something_with(foo[idx]);
}


I agree that this is a much simpler and clearer loop, but it
doesn't meet my requerements, I don't want the loop to end
after 2 iterations. It should continue until a certain condition
is met. So the values for idx shoudl be 0,1,0,1,0,1,0,1,....


Then let me suggest:

idx = 0;
do {
dosomethingwith(foo[idx]);
idx = !idx;
} while (somestatement);
(Note that the blank shortage ended several years ago)


What is "the blank shortage"?


The thing that entices some programmers to write:

for(idx=0;;idx=!idx)

in place of:

for (idx = 0; ; idx = !idx)

--
Replies should be to the newsgroup
Chuck Falconer, on vacation.
Nov 13 '05 #14
LibraryUser wrote:

Capstar wrote:
LibraryUser wrote:
Capstar wrote:

... snip ...

>int idx;
>for(idx=0;;idx=!idx)
>{
> do_something_with(foo[idx]);
> if(somestatement) break;
>}
>
> my question is: Does the standard guarantee that idx=!idx
> always result in a 1 or a 0?
Yes. However you can have a much simpler and clearer loop with:

for (idx = 0; idx < 2; idx++) {
do_something_with(foo[idx]);
}


I agree that this is a much simpler and clearer loop, but it
doesn't meet my requerements, I don't want the loop to end
after 2 iterations. It should continue until a certain condition
is met. So the values for idx shoudl be 0,1,0,1,0,1,0,1,....


Then let me suggest:

idx = 0;
do {
dosomethingwith(foo[idx]);
idx = !idx;
} while (somestatement);


That leaves idx with a different value after the loop concludes,
than it had in the dosomethingwith() call, unlike the original code.
OP didn't specify if it was important,
but I wouldn't take it upon myself to assume that it wasn't.

--
pete
Nov 13 '05 #15

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

Similar topics

9
by: Martin Goldman | last post by:
Hello all, I've been struggling for a few days with the question of how to convert "smart" (curly) quotes into straight quotes. I tried playing with the htmlentities() function, but all that is...
2
by: Tim Hochberg | last post by:
During the recent, massive, painful Lisp-Python crossposting thread the evils of Python's whitespace based indentation were once again brought to light. Since Python' syntax is so incredibly...
14
by: David B. Held | last post by:
I wanted to post this proposal on c.l.c++.m, but my news server apparently does not support that group any more. I propose a new class of exception safety known as the "smart guarantee". ...
11
by: Ron | last post by:
Hello, I'm having an aggravating time getting the "html" spewed by Word 2003 to display correctly in a webpage. The situation here is that the people creating the documents only know Word, and...
2
by: BobAchgill | last post by:
Is there a way to let the User click on a button on a web site and have that download and install my prepackaged compressed data directory and place it nicely under my existing VB .Net Form...
3
by: red floyd | last post by:
I've got some code where somebody cut&pasted some comments from MS Word, and so these comments have "smart quotes" (in particular apostrophes) embedded. The apostrophe is character hex 0x92. ...
5
by: Noozer | last post by:
I'm looking for a "smart folder" program to run on my Windows XP machine. I'm not having any luck finding it and think the logic behind the program is pretty simple, but I'm not sure how I'd...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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
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...

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.