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

Unrolling a loop

I have a recursive function like this:

void MyFunction(int i)
{
i++;
// do something with i
if (we're not finished) MyFunction(i);
}

I keep count of how far nested we are with i. Now, it would give the
compiler more chances at optimisation if I could give each loop its own
block of code, like this:

void MyFunctionNextedThrice()
{
// do something with the number two
// if (we're not finished) MyFunctionNestedFourTimes();
}

void MyFunctionNestedTwice()
{
// do something with the number zero
// if (we're not finished) MyFunctionNestedThrice();
}

void MyFunctionNextedOnce()
{
// do something with the number one
// if (we're not finished) MyFunctionNestedTwice();
}

void MyFunction()
{
// do something with the number zero
// if (we're not finished) MyFunctionNestedOnce();
}

What would be orgasmic is if I could 'inline' them inside each other.
Is there some way of creating a template (not in the C++ sense)? Do I
use macros (which, as the FAQ points out, are evil)?

Jul 23 '05 #1
3 2928
Richard Cavell wrote:
I have a recursive function like this:

void MyFunction(int i)
{
i++;
// do something with i
if (we're not finished) MyFunction(i);
}

I keep count of how far nested we are with i. Now, it would give the
compiler more chances at optimisation if I could give each loop its own
block of code, like this:

void MyFunctionNextedThrice()
{
// do something with the number two
// if (we're not finished) MyFunctionNestedFourTimes();
}

void MyFunctionNestedTwice()
{
// do something with the number zero
// if (we're not finished) MyFunctionNestedThrice();
}

void MyFunctionNextedOnce()
{
// do something with the number one
// if (we're not finished) MyFunctionNestedTwice();
}

void MyFunction()
{
// do something with the number zero
// if (we're not finished) MyFunctionNestedOnce();
}

What would be orgasmic is if I could 'inline' them inside each other. Is
there some way of creating a template (not in the C++ sense)?
Why "not in the C++ sense"? Recursive template definitions are very
common and form the foundation of "meta-programming".
Do I use
macros (which, as the FAQ points out, are evil)?


I don't know what you use. I use templates:

template<int depth> void MyFunction(int i) {
blahblah
if (something)
MyFunction<depth-1>(i);
}

template<> void MyFunction<0>(i) {
whatever you use at depth 0
// no recursion any more
}

Or you could go positive, then limit yourself from above:

template<int depth> void MyFunction(int i) {
blahblah
if (something)
MyFunction<depth+1>(i);
}

template<> void MyFunction<5>(i) {
whatever you use at depth 5
// no recursion any more
}

V
Jul 23 '05 #2
Richard Cavell wrote:

I have a recursive function like this:

void MyFunction(int i)
{
i++;
// do something with i
if (we're not finished) MyFunction(i);
}

I keep count of how far nested we are with i. Now, it would give the
compiler more chances at optimisation if I could give each loop its own
block of code, like this:


or like this:

void MyFunction( int i )
{
do {
i++;
// do something with i
} while( we're not finished )
}

Look up: Tail recursion elimination

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #3
Richard Cavell wrote:
["unrolling" a recursion cascade]
Well, the first (obvious) thing to check is whether it really is
necessary to use a recursive function to do the job. In most cases, a
loop will be just as good, or better.

If not - What would be orgasmic is if I could 'inline' them inside each other. Is
there some way of creating a template (not in the C++ sense)? Do I use
macros (which, as the FAQ points out, are evil)?


You've actually inadvertently hit the nail right on the head there -
it's possible to "unroll" recursion using C++ templates.

Unrolling is only possible if the recursion depth is known at compile
time, which is also necessary for using templates.

The technique is described extensively in [1], but the general gist of
it is to create a template and to pass your recursion data to the next
level using template parameters rather than function parameters.

[1] uses class templates, but for more involved operations you'll
probably want to use an inline function template. Using one of the
typical examples for recursion, (for which you'd never use recursion in
the real world...) calculating factorials:

////////////////////////////////////////
template <int i> inline int Factorial();
// termination specialisation
template <> inline int Factorial<1>();

template <int i> inline int Factorial()
{
return i * Factorial<i - 1>();
}

template <> inline int Factorial<1>()
{
return 1;
}
////////////////////////////////////////

Factorials can then be evaluated using

Factorial<5>();

Whether the technique is effective depends heavily on your compiler,
however; there is no *guarantee* that your compiler will expand it
properly. If in doubt, look at the compiler's assembly output.

[1] Mark DeLoura, Game Programming Gems. 2000, Charles River Media
Gem 1.2, "Fast Math Using Template Metaprogramming"

~phil
Jul 23 '05 #4

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

Similar topics

0
by: Charles Alexander | last post by:
Hello I am new to php & MySQL - I am trying to retrieve some records from a MySQL table and redisplay them. The data in list form looks like this: Sample_ID Marker_ID Variation ...
34
by: Jacek Generowicz | last post by:
I have a program in which I make very good use of a memoizer: def memoize(callable): cache = {} def proxy(*args): try: return cache except KeyError: return cache.setdefault(args,...
5
by: John Edwards | last post by:
Hello, I have sort of a newbie question. I'm trying to optimize a loop by breaking it into two passes. Example: for(i = 0; i < max; i++) {
1
by: Per Nordlöw | last post by:
Hi all I am using the boost::array template class trying to generalize my handcrafted vector specialization for dimensions 2 (class vec2), 3 (class vec3) etc. As performance is of greatest...
32
by: cj | last post by:
When I'm inside a do while loop sometimes it's necessary to jump out of the loop using exit do. I'm also used to being able to jump back and begin the loop again. Not sure which language my...
5
by: iapx86 | last post by:
My parser project calls for a computed goto (see code below). The C preprocessor delivers the desired result, but is ugly. Template metaprogramming delivers results I do not understand. Can...
2
ADezii
by: ADezii | last post by:
If you are executing a code segment for a fixed number of iterations, always use a For...Next Loop instead of a Do...Loop, since it is significantly faster. Each pass through a Do...Loop that...
9
by: V | last post by:
Hello: Consider the following nested for loop: uint64 TABLE; for (i=0; i<=7; i++) for (j=1; j<=7; j++) for (k=1; k<=(1<<j)-1; k++) TABLE = (TABLE) ^ (TABLE);
4
by: mark | last post by:
Why does the following excerpt of trivial code execute so quickly? #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv){ static const int SIZE = 1000000; long nops = 0; int...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...

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.