<?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>optimisation | Learn C Games Programming Blog</title>
	<atom:link href="https://learncgames.com/tag/optimisation/feed/" rel="self" type="application/rss+xml" />
	<link>https://learncgames.com</link>
	<description>A blog about C, programming games and my ebook(s).</description>
	<lastBuildDate>Fri, 04 Dec 2020 20:44:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://learncgames.com/wp-content/uploads/2020/03/cropped-favicon-32x32.png</url>
	<title>optimisation | Learn C Games Programming Blog</title>
	<link>https://learncgames.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">181446779</site>	<item>
		<title>Fun with optimisation in C</title>
		<link>https://learncgames.com/fun-with-optimisation-in-c/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=fun-with-optimisation-in-c</link>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Mon, 07 Dec 2020 00:00:47 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Techniques]]></category>
		<category><![CDATA[branchless]]></category>
		<category><![CDATA[optimisation]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=2012</guid>

					<description><![CDATA[<p>Bit twiddling with C is quite a common thing but sometimes it can be hard to understand something quite simple.  Here&#8217;s a line of code. Can you say what it does? Note that a,b and c are all ints. c = b ^ ((a ^ b) &#38; -(a &#60; b)); Or how about this which [&#8230;]</p>
The post <a href="https://learncgames.com/fun-with-optimisation-in-c/">Fun with optimisation in C</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:335,&quot;href&quot;:&quot;https:\/\/pixabay.com\/users\/ar130405-423602\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=2082639&quot;,&quot;archived_href&quot;:&quot;&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[],&quot;broken&quot;:false,&quot;last_checked&quot;:null,&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:336,&quot;href&quot;:&quot;https:\/\/pixabay.com\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=2082639&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20260205102120\/https:\/\/pixabay.com\/?utm_source=link-attribution&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 14:10:24&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-10 17:39:37&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-04 16:30:52&quot;,&quot;http_code&quot;:403}],&quot;broken&quot;:true,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-04 16:30:52&quot;,&quot;http_code&quot;:403},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:337,&quot;href&quot;:&quot;https:\/\/graphics.stanford.edu\/~seander\/bithacks.html&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20260204174744\/https:\/\/graphics.stanford.edu\/~seander\/bithacks.html&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 14:10:26&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-10 17:39:37&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-04 16:30:52&quot;,&quot;http_code&quot;:206}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-04 16:30:52&quot;,&quot;http_code&quot;:206},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:338,&quot;href&quot;:&quot;https:\/\/en.wikipedia.org\/wiki\/Branch_predictor&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20260107112411\/https:\/\/en.wikipedia.org\/wiki\/Branch_predictor&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 14:10:29&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-10 17:39:37&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-04 16:30:54&quot;,&quot;http_code&quot;:429}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-04 16:30:54&quot;,&quot;http_code&quot;:429},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:339,&quot;href&quot;:&quot;https:\/\/en.wikipedia.org\/wiki\/Pipeline_stall&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20260102055110\/https:\/\/en.wikipedia.org\/wiki\/Pipeline_stall&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 14:11:50&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-10 17:39:37&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-04 16:30:52&quot;,&quot;http_code&quot;:429}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-04 16:30:52&quot;,&quot;http_code&quot;:429},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:13,&quot;href&quot;:&quot;https:\/\/github.com\/David-H-Bolton\/LearnC&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20250807013721\/https:\/\/github.com\/David-H-Bolton\/LearnC&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 10:35:09&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-09 16:40:16&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-12 18:24:15&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-15 22:16:30&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-18 22:19:31&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-21 22:22:05&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-25 04:17:25&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-28 04:17:49&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-03 04:22:25&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-06 04:38:11&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-09 08:48:04&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-12 09:17:10&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-15 10:18:23&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-18 12:43:50&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-21 15:14:39&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-24 15:19:12&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-27 16:38:15&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-30 19:05:39&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-02 22:49:21&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-06 04:18:17&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-09 04:19:40&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-12 09:53:38&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-15 10:17:36&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-18 13:35:54&quot;,&quot;http_code&quot;:503}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-18 13:35:54&quot;,&quot;http_code&quot;:503},&quot;process&quot;:&quot;done&quot;}]'></div>
<figure id="attachment_2013" aria-describedby="caption-attachment-2013" style="width: 300px" class="wp-caption alignleft"><img fetchpriority="high" decoding="async" class="size-medium wp-image-2013" src="https://learncgames.com/wp-content/uploads/2020/12/business-2082639_640-300x169.jpg" alt="Inner cogs" width="300" height="169" srcset="https://learncgames.com/wp-content/uploads/2020/12/business-2082639_640-300x169.jpg 300w, https://learncgames.com/wp-content/uploads/2020/12/business-2082639_640.jpg 640w" sizes="(max-width: 300px) 100vw, 300px" /><figcaption id="caption-attachment-2013" class="wp-caption-text">Image by <a href="https://pixabay.com/users/ar130405-423602/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=2082639">ar130405</a> from <a href="https://pixabay.com/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=2082639">Pixabay</a></figcaption></figure>
<p>Bit twiddling with C is quite a common thing but sometimes it can be hard to understand something quite simple.  Here&#8217;s a line of code. Can you say what it does? Note that a,b and c are all ints.</p>
<pre><code class="language-c" data-line="">c = b ^ ((a ^ b) &amp; -(a &lt; b));</code></pre>
<p>Or how about this which looks quite similar</p>
<pre><code class="language-c" data-line="">c = a ^ ((a ^ b) &amp; -(a &lt; b));</code></pre>
<p>The first line puts the minimum of a and b into c, while the second line puts the max of a and b. Both came from a page of <a title="Lin to Bit twiddling hacks" href="https://graphics.stanford.edu/~seander/bithacks.html" target="_blank" rel="nofollow noopener noreferrer">bit twiddling hacks</a> by Sean Eron Anderson at Stanford university. The significant thing about these is not that they provide two clever if incomprehensible ways of calculating min and max but they do it without branches.</p>
<p>If you look at the source of min and max in stdlib.h they are defined like this:</p>
<pre><code class="language-c" data-line="">#define max(a,b) (((a) &gt; (b)) ? (a) : (b))
#define min(a,b) (((a) &lt; (b)) ? (a) : (b))</code></pre>
<p>Because modern CPUs use instruction caches (pipelines), they try to keep them full through a technique called <a title="Libnk to Wikipedia on Branch prediction" href="https://en.wikipedia.org/wiki/Branch_predictor" target="_blank" rel="nofollow noopener noreferrer">branch prediction</a>. If a branch happens then the CPU has to read instructions from the wherever the branch goes to and this causes a <a title="Link to Wikipedia on pipeline stall" href="https://en.wikipedia.org/wiki/Pipeline_stall" target="_blank" rel="nofollow noopener noreferrer">pipeline stall</a> which slightly slows down processing. So branchless min and max should run faster than branching code, at least in theory.</p>
<p>I tried it on Windows using a small Visual C++ (<em>Well C</em>) program doing the four in a loop 10 million times. I also ran it in 32-bit and 64-bit.  All runs were done in release mode which lets the compiler optimise.</p>
<p>Curiously the 32-bit took almost no time at all while the 64-bit took a lot lot longer. There wasn&#8217;t much difference between branchless and branch. Possibly the code is too small and the benefits of branchless are less significant because the instructions fall inside the instruction cache for 32-bit while 64-bit may be too big and so is affected by instruction cache misses.</p>
<p>This is the 32-bit time.</p>
<pre>fastMin 0.0000000
fastMax 0.1000000
Min 0.1000000
Max 0.1000000
</pre>
<p>And this is the 64-bit. Quite a lot of difference!</p>
<pre>fastMin 1757.6000000
fastMax 1677.9000000
Min 2039.4000000
Max 2134.4000000
</pre>
<p>This is the code which compiles in Visual C++. You can get the <strong>hr_time</strong> timing code (<em>for WIndows</em>) from the <a title="Link to LearnC files on GitHub." href="https://github.com/David-H-Bolton/LearnC" target="_blank" rel="nofollow noopener noreferrer">sdlttf.zip</a> file. I increment and decrement a and b to stop the compiler possibly spotting that the values would be invariant otherwise and doing too-clever optimisation stuff.</p>
<pre><code class="language-c" data-line="">#include &lt;math.h&gt;
#include &lt;stdio.h&gt;
#include &quot;hr_time.h&quot;

#define NUMLOOPS 10000000
	int a = 1000;
	int b = 67;
	int c;
	double t;
	stopWatch s;

int main() {

	startTimer(&amp;s);
	for (int i = 0; i &lt; NUMLOOPS; i++) {
		a++;
		b++;
		c = b ^ ((a ^ b) &amp; -(a &lt; b));
	}
	stopTimer(&amp;s);
	t = getElapsedTime(&amp;s) * 1000000.0;
	printf( &quot;fastMin %8.7f\n&quot;, t);
	startTimer(&amp;s);
	for (int i = 0; i &lt; NUMLOOPS; i++) {
		a--;
		b--;
		c = a ^ ((a ^ b) &amp; -(a &lt; b));
	}
	stopTimer(&amp;s);
	t = getElapsedTime(&amp;s) * 1000000.0;
	printf(&quot;fastMax %8.7f\n&quot;, t);
	startTimer(&amp;s);
	for (int i = 0; i &lt; NUMLOOPS; i++) {
		a++;
		b++;
		c = min(a, b);
	}
	stopTimer(&amp;s);
	t = getElapsedTime(&amp;s) * 1000000.0;
	printf(&quot;Min %8.7f\n&quot;, t);
	startTimer(&amp;s);
	for (int i = 0; i &lt; NUMLOOPS; i++) {
		a--;
		b--;
		c = max(a, b);
	}
	stopTimer(&amp;s);
	t = getElapsedTime(&amp;s) * 1000000.0;
	printf(&quot;Max %8.7f\n&quot;, t);
}

</code></pre>The post <a href="https://learncgames.com/fun-with-optimisation-in-c/">Fun with optimisation in C</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2012</post-id>	</item>
	</channel>
</rss>
