<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Tweak Your Code For Speed: Unroll Those Loops Part 1</title>
	<atom:link href="http://www.safercode.com/blog/2009/01/27/tweak-your-code-for-speed-unroll-those-loops-part-1.html/feed" rel="self" type="application/rss+xml" />
	<link>http://www.safercode.com/blog/2009/01/27/tweak-your-code-for-speed-unroll-those-loops-part-1.html</link>
	<description>Making Your Code Faster, Stronger, Safer…</description>
	<lastBuildDate>Tue, 31 Aug 2010 16:47:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Antriksh Pany</title>
		<link>http://www.safercode.com/blog/2009/01/27/tweak-your-code-for-speed-unroll-those-loops-part-1.html#comment-2405</link>
		<dc:creator>Antriksh Pany</dc:creator>
		<pubDate>Thu, 28 May 2009 17:32:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=15#comment-2405</guid>
		<description>Unrolling always may not help. One important demerit of unrolling that is not mentioned is that code size may get greatly impacted (would become larger that is).
The gcc optimization flag -O2 enables all optimizations but loop unrolling, because it does not want to optimize at the cost of code size.

And regarding point 7, are you sure that the compiler _always_ skips unrolling inner loops?
If a concoct a trivial example such as:
 for (i = 0; i &lt; 3; ++i)
   for (j = 0; j &lt; 2; ++j)
       ;
..would the compiler still unroll only the outer loop?</description>
		<content:encoded><![CDATA[<p>Unrolling always may not help. One important demerit of unrolling that is not mentioned is that code size may get greatly impacted (would become larger that is).<br />
The gcc optimization flag -O2 enables all optimizations but loop unrolling, because it does not want to optimize at the cost of code size.</p>
<p>And regarding point 7, are you sure that the compiler _always_ skips unrolling inner loops?<br />
If a concoct a trivial example such as:<br />
 for (i = 0; i &lt; 3; ++i)<br />
   for (j = 0; j &lt; 2; ++j)<br />
       ;<br />
..would the compiler still unroll only the outer loop?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zombie No. 5</title>
		<link>http://www.safercode.com/blog/2009/01/27/tweak-your-code-for-speed-unroll-those-loops-part-1.html#comment-690</link>
		<dc:creator>Zombie No. 5</dc:creator>
		<pubDate>Mon, 06 Apr 2009 19:37:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=15#comment-690</guid>
		<description>Of course, with trivial loops as in the examples you can expect that the compiler actually rewrites your code to count towards zero, especially if the counter isn&#039;t used inside of the loop. If it&#039;s also used as an index into an array for example, the compiler can&#039;t do that optimization for you.</description>
		<content:encoded><![CDATA[<p>Of course, with trivial loops as in the examples you can expect that the compiler actually rewrites your code to count towards zero, especially if the counter isn&#8217;t used inside of the loop. If it&#8217;s also used as an index into an array for example, the compiler can&#8217;t do that optimization for you.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: marcus</title>
		<link>http://www.safercode.com/blog/2009/01/27/tweak-your-code-for-speed-unroll-those-loops-part-1.html#comment-148</link>
		<dc:creator>marcus</dc:creator>
		<pubDate>Fri, 13 Feb 2009 15:36:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=15#comment-148</guid>
		<description>@agi
&gt; How does a loop counting backwards save a compare?

After every iteration, an up-counting loop will have to compare the loop variable with the loop limit. Pseudo code:
&lt;pre&gt;
loop
    ...
    ADD i, #1
    CMP i, 
    BNE loop
&lt;/pre&gt;
A down-counting loop decrements the loop variable. On most processor architectures, this will implicitly set condition code flags. The flags can be directly evaluated by a conditional branch. Plus there is no need to store the limit after initializing the loop variable.
&lt;pre&gt;
loop
    ...
    SUB i, #1
    BNZ loop
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>@agi<br />
&gt; How does a loop counting backwards save a compare?</p>
<p>After every iteration, an up-counting loop will have to compare the loop variable with the loop limit. Pseudo code:</p>
<pre>
loop
    ...
    ADD i, #1
    CMP i,
    BNE loop
</pre>
<p>A down-counting loop decrements the loop variable. On most processor architectures, this will implicitly set condition code flags. The flags can be directly evaluated by a conditional branch. Plus there is no need to store the limit after initializing the loop variable.</p>
<pre>
loop
    ...
    SUB i, #1
    BNZ loop
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: agi</title>
		<link>http://www.safercode.com/blog/2009/01/27/tweak-your-code-for-speed-unroll-those-loops-part-1.html#comment-145</link>
		<dc:creator>agi</dc:creator>
		<pubDate>Fri, 13 Feb 2009 04:31:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=15#comment-145</guid>
		<description>@marcus:

Just curious... How does a loop counting backwards save a compare?</description>
		<content:encoded><![CDATA[<p>@marcus:</p>
<p>Just curious&#8230; How does a loop counting backwards save a compare?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: marcus</title>
		<link>http://www.safercode.com/blog/2009/01/27/tweak-your-code-for-speed-unroll-those-loops-part-1.html#comment-136</link>
		<dc:creator>marcus</dc:creator>
		<pubDate>Tue, 10 Feb 2009 12:02:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.safercode.com/blog/?p=15#comment-136</guid>
		<description>When we talk about micro optimzation, I guess it would also help if the example loop counted backwards. Saves another compare (and often a register for storing the limit) if the compiler decides not to unroll a loop.</description>
		<content:encoded><![CDATA[<p>When we talk about micro optimzation, I guess it would also help if the example loop counted backwards. Saves another compare (and often a register for storing the limit) if the compiler decides not to unroll a loop.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
