Talk:Loop unrolling

message: "Loop unwinding"?  Who uses that term?  Not many people. There are 1,450 Google hits to that term, and 85,800 to "loop unrolling."  —Preceding unsigned comment added by 83.249.241.102 (talk) 20:57, 1 July 2008 (UTC)
thread_title:  Wrong primary name 

message: Should the code example include a cleanup loop? The unwound loop now doesn't give the right answer unless you're looking for a multiple of five (which, in this case, is happening). 
 int max = 101;
 int remainder = max % 5;
 int x;
 for ( x = 0; x < max - 4; x += 5)
 {
     cout << (x)  << endl;
     cout << (x+1)<< endl;
     cout << (x+2)<< endl;
     cout << (x+3)<< endl;
     cout << (x+4)<< endl;
 }
 for ( int i = 0; i < remainder; i++)
     cout << x++ << endl;
Here, max can be set to any value and will print out the right answer.
(I replaced delete with cout so I could get feedback. delete could be put back in ("delete(x++)"))
Also, loops are typically unrolled in powers of two, at least in my experience
DevastatorIIC 19:00, 29 November 2005 (UTC)
thread_title:  Cleanup Loop 

message: The comments that loop unrolling causes the compiler to allocate more significantly more registers is silly,  and in most cases bogus.   Whoever wrote that probably doesn't understand how their compiler works very well.  (or they use a compiler that just isn't very good.)
Of course,  it depends upon which compiler you use.   However, in most decent compilers the example given shouldn't use more than one extra register,  if that.  For example,  compiling the example with GCC, with optimization,  and dumping ARM assembly code,  the loop and the "unrolled" versions use exactly the same number of registers.  Obscuranym (talk) 01:42, 23 October 2008 (UTC)
thread_title:  Register Usage 

message: I think whoever originally devised the C examples did not really appreciate that there is a really large benefit in avoiding run time pointer arithmetic - since the benefits were (originally) only stated as elimination of 'end of loop' tests. The C examples, which currently use variables ('x', 'x+1') etc in the unrolled loop, are not optimal and may only rid the generated code of additional run time calculations if absolute index values are used instead.
Someone should replace these examples with ones where the C compiler is able perform assignments etc without repeated index/pointer recalculations for each iteration, as performed in the IBM/360 and Z/Architecture example. ken (talk) 13:40, 4 December 2009 (UTC)
replies: text: I have now added a C example for dynamic unrolling . This however still suffers from the disadvantage that a variable (i) is used within both the While loop and Switch statements, still requiring pointer/index arithmetic to be generated by the compiler, negating part of the benefit of loop unrolling, even in the supposedly 'low level' C language ken (talk) 15:07, 11 December 2009 (UTC)
thread_title:  C examples do not demonstrate optimal unrolling 

message: I have amended the article to present program code before & after unrolling/unwinding side-by-side for ease of viewing the differemces.ken (talk) 10:04, 13 December 2009 (UTC)
thread_title:  Side by side comparisons 

message: I think we could differenciate the "total" unroll that is available only when the number of iterations in the loops is known, and the "partial" unroll that is available quite every time. Moreover, when unrolling partially, there are many way to do it. Given the following code to unroll partially: 
  //N is unknown at compile time
  for (int i = 0; i < N; i++) { 
    somestuffwith(i);
  }
we want to unroll this loop partially by a factor x. But we don't know if N mod x equals to 0. Then we have many options, such as : 
  
  for (int i = 0; i < N; i+=x) { 
    somestuffwith(i+0); if (i+1 >= N) break;
    somestuffwith(i+1); if (i+2 >= N) break;
    somestuffwith(i+2); if (i+3 >= N) break;
    //...
    somestuffwith(i+(x-3)); if (i+(x-2) >= N) break;
    somestuffwith(i+(x-2)); if (i+(x-1) >= N) break;
    somestuffwith(i+(x-1));
  }
In this case, the overhead of the if may break the pipeline, and add some overhead anyway. But the code is then more regular. We can use another kind of unrolling : 
  
  int N2 = N - N%x;
  for (int i = 0; i < N2; i+=x) { 
    somestuffwith(i); 
    somestuffwith(i+1);
    somestuffwith(i+2);
    //...
    somestuffwith(i+(x-3));
    somestuffwith(i+(x-2)); 
    somestuffwith(i+(x-1));
  }
  for (int i = N2; i < N; i++) { 
    somestuffwith(i);
  }
Here, we may use the pipeline of a superscalar architecture, but we then have a little overhead induced by the first line, and the last for that take at most (x-1) iterations. At the end, the speedup depends on the compiler and/or the architecture. Now suppose that we know N at compile time. Then we can enhance the unroll.
  for (int i = 0; i < N; i+=x) { 
    somestuffwith(i+0);
    somestuffwith(i+1);
    somestuffwith(i+2);
    //...
    //here N is known at compile time, and obviously the unroll factor x is known
    //then we know m = N%x and we know where the loop should break.
    somestuffwith(i+(x-m)); if (x-m+1 >= N) break;
    //...
    somestuffwith(i+(x-3));
    somestuffwith(i+(x-2));
    somestuffwith(i+(x-1));
  }
Using this method, we have now only one if, that still break the pipeline, but the overhead is now reduced. Finaly, we can use this method : 
  //N2 is here known at compile time : N2 = N - N%x
  for (int i = 0; i < N2; i+=x) { 
    somestuffwith(i); 
    somestuffwith(i+1);
    somestuffwith(i+2);
    //...
    somestuffwith(i+(x-3));
    somestuffwith(i+(x-2)); 
    somestuffwith(i+(x-1));
  }
  //here we still know m = N%x and we know how many iterations we have to add
  somestuffwith(i+(N2));
  somestuffwith(i+(N2+1));
  //...
  somestuffwith(i+(N2+m));
Here the pipeline may be used inside the loop's body, and at the end, when processing the remaining m last statements.  —Preceding unsigned comment added by 131.254.15.243 (talk) 09:51, 9 April 2010 (UTC)
thread_title:  Differenciate kinds of unrolling 

message: The Dynamic unrolling section appears to conflate two quite different ideas:
# JIT code generation
# A static unwound loop with a bunch of special cases to handle when n % N != 0
Surely these are completely unrelated?  In what way is the latter "dynamic"?  Oli Filth(talk|contribs) 22:35, 18 May 2010 (UTC)
thread_title:  Inconsistency in "Dynamic unrolling" 

message: * It shouldn't use "printf" as code of the loop body, better use something more abstract like "foo(i)".
* The switch statement is incorrect because it changes the order in which the index values are processed (e.g. i+6 before i+3). This can be very relevant depending on the code using i. Use entries-x.
* It is suboptimal to have both "i" and "repeat" as induction variables in the loop. Also the division to calculate "repeat" is superfluous. Use "i < entries-left" as loop condition.
Croorg (talk) 06:22, 3 March 2017 (UTC)
thread_title:  the final C example 

message: Hello fellow Wikipedians,
I have just modified one external link on Loop unrolling. Please take a moment to review my edit. If you have any questions, or need the bot to ignore the links, or the page altogether, please visit this simple FaQ for additional information. I made the following changes:
*Added archive https://web.archive.org/web/20081201132152/http://www.nondot.org/~sabre/Mirrored/GraphicsProgrammingBlackBook/gpbb7.pdf to http://nondot.org/~sabre/Mirrored/GraphicsProgrammingBlackBook/gpbb7.pdf
When you have finished reviewing my changes, you may follow the instructions on the template below to fix any issues with the URLs.
Cheers.—InternetArchiveBot (Report bug) 06:33, 21 December 2017 (UTC)
thread_title:  External links modified