<?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>chys&#039;s random notes &#187; VC</title>
	<atom:link href="http://en.chys.info/tag/vc/feed/" rel="self" type="application/rss+xml" />
	<link>http://en.chys.info</link>
	<description>Study more problems; Talk less of isms.</description>
	<lastBuildDate>Tue, 27 Dec 2011 11:56:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Counterpart of __assume in GCC</title>
		<link>http://en.chys.info/2010/07/counterpart-of-assume-in-gcc/</link>
		<comments>http://en.chys.info/2010/07/counterpart-of-assume-in-gcc/#comments</comments>
		<pubDate>Sat, 24 Jul 2010 09:35:53 +0000</pubDate>
		<dc:creator>chys</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[GCC]]></category>
		<category><![CDATA[VC]]></category>

		<guid isPermaLink="false">http://en.chys.info/?p=828</guid>
		<description><![CDATA[Microsoft Visual C++ has a keyword __assume, which is used to pass a hint to the compiler for optimization. It can be useful when optimizing hotspots in a program. For example, if we know a loop will run at least once, we can add an __assume hint: __assume (0 &#60; n); for (i=0; i &#60; [...]<hr/>
No related posts.]]></description>
			<content:encoded><![CDATA[<p>Microsoft Visual C++ has a keyword <a href="http://msdn.microsoft.com/en-us/library/1b3fsfxw%28VS.80%29.aspx"><code>__assume</code></a>, which is used to pass a hint to the compiler for optimization. It can be useful when optimizing hotspots in a program. For example, if we know a loop will run at least once, we can add an <code>__assume</code> hint:</p>
<blockquote><p>
<code>__assume (0 &lt; n);</code><br />
<code>for (i=0; i &lt; n; i++)</code><br />
<code>&nbsp;&nbsp;&nbsp;&nbsp;/* .... */</code>
</p></blockquote>
<p>This eliminates the comparison of <code>n</code> against zero before entering the loop body for the first time, without changing it to a <code>do-while</code> structure, which is usually less preferable than a <code>for</code> loop. Another typical use is to tell the compiler the <code>default</code> branch of a <code>switch</code>-<code>case</code> statement is unreachable.</p>
<p>GCC does not have a counterpart. But now that GCC 4.5.0 introduced <code>__builtin_unreachable</code>, we can do everything in GCC what we do with <code>__assume</code> in MSVC. My tests show the following macro works:</p>
<blockquote><p><code>#define __assume(cond) do { if (!(cond)) __builtin_unreachable(); } while (0)</code></p></blockquote>
<p>(The “<code>do { ... } while (0)</code>” statement is just <a href="http://stackoverflow.com/questions/154136/why-are-there-sometimes-meaningless-dowhile-and-ifelse-statements-in-cc-macros">a small trick in defining macros</a>. Actually it&#8217;s been so well known and widely used that it can hardly be called a trick anymore.)</p>
<hr/><p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://en.chys.info/2010/07/counterpart-of-assume-in-gcc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

