klijo
klijo

Reputation: 16451

unrolling a while loop

original code

while(i<30){
// do something
i++;
}

unrolled while loop

while(i<15){
// do something twice
i+=2;
}

Cant we unroll it as shown above. Do we always have to do it like http://en.wikipedia.org/wiki/Loop_unrolling ?

Upvotes: 1

Views: 1989

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726849

In general, the answer is no. It works for 30 and 15 because 30 is even, but it would not work as easily for odd numbers. "Duff's device" was invented to deal with general case. It is quite ugly, though.

Upvotes: 2

Related Questions