<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>I Grok2! &#187; gcc</title>
	<atom:link href="http://www.grok2.com/blog/tag/gcc/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.grok2.com/blog</link>
	<description>Except when I don't....</description>
	<lastBuildDate>Sat, 04 Feb 2012 22:09:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>C Optimization Tips &#8211; Dealing with Aliasing</title>
		<link>http://www.grok2.com/blog/2010/06/07/c-optimization-tips-dealing-with-aliasing/</link>
		<comments>http://www.grok2.com/blog/2010/06/07/c-optimization-tips-dealing-with-aliasing/#comments</comments>
		<pubDate>Mon, 07 Jun 2010 18:27:35 +0000</pubDate>
		<dc:creator>grok2</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[optimization]]></category>

		<guid isPermaLink="false">http://www.grok2.com/blog/2010/06/07/c-optimization-tips-dealing-with-aliasing/</guid>
		<description><![CDATA[Though in general it is true that you should simply leave C code optimization to the compiler, it sometimes is useful to provide hints to the compiler. This is because when optimizing the compiler has to make a few basic global assumptions so as to generate correct code in all situations, but these assumptions may [...]]]></description>
			<content:encoded><![CDATA[<!-- Easy AdSense Redux V2.82 -->
<!-- Post[count: 2] -->
<div class="ezAdsense adsense adsense-leadin" style="float:right;margin:6px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-2859704241898192";
/* blogpostad */
google_ad_slot = "1221128987";
google_ad_width = 180;
google_ad_height = 150;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><p>Though in general it is true that you should simply leave C code optimization to the compiler, it sometimes is useful to provide hints to the compiler. This is because when optimizing the compiler has to make a few basic global assumptions so as to generate correct code in all situations, but these assumptions may not be true in your specific code. </p>
<p>A case in point is <a href="http://lbrandy.com/blog/2010/06/you-cant-beat-a-good-compile/">providing <b>aliasing </b>hints through the use of the C99 <b>restrict</b> keyword or the GCC extension <b>__restrict__</b></a>. See <a href="http://lbrandy.com/blog/2010/06/you-cant-beat-a-good-compile/">this excellent article</a> with an easy example that will help you understand aliasing and how to deal with it.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.grok2.com%2Fblog%2F2010%2F06%2F07%2Fc-optimization-tips-dealing-with-aliasing%2F&amp;title=C%20Optimization%20Tips%20%26%238211%3B%20Dealing%20with%20Aliasing" id="wpa2a_2"><img src="http://www.grok2.com/blog/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.grok2.com/blog/2010/06/07/c-optimization-tips-dealing-with-aliasing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C if-else Optimization</title>
		<link>http://www.grok2.com/blog/2009/03/23/c-if-else-optimization/</link>
		<comments>http://www.grok2.com/blog/2009/03/23/c-if-else-optimization/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 04:39:05 +0000</pubDate>
		<dc:creator>grok2</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[optimization]]></category>

		<guid isPermaLink="false">http://www.grok2.com/blog/?p=13</guid>
		<description><![CDATA[When doing some maintenance work on C code (refactoring!), I wondered if I should code C if-else clauses in some particular way to take advantage of modern processor pipelines and caching. It seems obvious that if you have multiple else-if clauses, then putting the most likely to execute code in the first if clause would [...]]]></description>
			<content:encoded><![CDATA[<p>When doing some maintenance work on C code (refactoring!), I wondered if I should code C <i>if-else</i> clauses in some particular way to take advantage of modern processor pipelines and caching. </p>
<p>It seems obvious that if you have multiple <i>else-if</i> clauses, then putting the most likely to execute code in the first <i>if</i> clause would be the best (since it avoids subsequent checks most of the time), but what about the case that you only have a two-way <i>if (cond) {&#8230;} else {&#8230;}&nbsp;</i> type of code, should you place the code most likely to be executed in the <i>if</i> clause or in the <i>else</i> clause? It would seem that it shouldn&#8217;t matter either way since there is only one condition being checked. But&#8230;</p>
<p>I did some experimentation on a system at hand (an Intel Pentium system with a small program compiled using <a href="http://gcc.gnu.org/">gcc</a>) and found that putting the most likely to be executed code as part of the else clause consistently had better timings. Be warned that this was just a simplistic &#8220;add-two-values-stored-in-variables-in-one-line&#8221; kind of code &#8212; definitely not a good test, but sufficient for me to satisfy my curiousity for the time being.</p>
<p>Google searching seemed to indicate that the performance depended on the &#8216;<a href="http://en.wikibooks.org/wiki/Optimizing_C%2B%2B/Code_optimization/Pipeline">branch prediction</a>&#8216; capability of the processor. I also found out that <a href="http://gcc.gnu.org/">gcc</a> has a compiler directive (<a href="http://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Other-Builtins.html#index-g_t_005f_005fbuiltin_005fexpect-2122">__builtin_expect()</a>) that one can use to provide a hint to the compiler so it knows what is the most likely outcome of a if conditional check and generate code suitable for the branch prediction capability of the target processor. The gcc manual recommends against using this directive though! See more details regarding this directive <a href="http://kerneltrap.org/node/4705">here</a>.</p>
<div class="zemanta-pixie"><img class="zemanta-pixie-img" src="http://img.zemanta.com/pixy.gif?x-id=85d5d344-79d0-4c5e-ae6b-c27c2fee7476" /></div>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.grok2.com%2Fblog%2F2009%2F03%2F23%2Fc-if-else-optimization%2F&amp;title=C%20if-else%20Optimization" id="wpa2a_4"><img src="http://www.grok2.com/blog/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.grok2.com/blog/2009/03/23/c-if-else-optimization/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

