<?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>South Winds Games :: Blog &#187; C++</title>
	<atom:link href="http://www.southwindsgames.com/blog/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.southwindsgames.com/blog</link>
	<description>Casual Games Development</description>
	<lastBuildDate>Sat, 05 Mar 2011 17:32:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Some of our bits &#8230;</title>
		<link>http://www.southwindsgames.com/blog/2011/02/28/some-of-our-bits/</link>
		<comments>http://www.southwindsgames.com/blog/2011/02/28/some-of-our-bits/#comments</comments>
		<pubDate>Mon, 28 Feb 2011 18:54:44 +0000</pubDate>
		<dc:creator>Juan Pablo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[C++ Template]]></category>
		<category><![CDATA[Euler Integrator]]></category>
		<category><![CDATA[Simple Integrator Template]]></category>

		<guid isPermaLink="false">http://www.southwindsgames.com/blog/?p=230</guid>
		<description><![CDATA[Hi!
Today I did some minors changes to our main site (please tell us if there is something wrong  ) and I thought it would be good to add another entry to the blog.
Well, since some time I was thinking about sharing this beautiful piece of code. It is beautiful because of the use we [...]]]></description>
			<content:encoded><![CDATA[<p>Hi!</p>
<p>Today I did some minors changes to our <a href="http://www.southwindsgames.com">main site</a> (<a href="http://southwindsgames.com/contact.php">please tell us if there is something wrong</a> <img src='http://www.southwindsgames.com/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> ) and I thought it would be good to add another entry to the blog.<br />
Well, since some time I was thinking about sharing this beautiful piece of code. It is beautiful because of the use we have given to it, we used this tool for almost everything.<br />
<span id="more-230"></span></p>
<div class="codecolorer-container cpp mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> T, <span style="color: #0000ff;">int</span> N<span style="color: #000080;">&gt;</span> <span style="color: #0000ff;">class</span> IntegratorBase <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">protected</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; T mV<span style="color: #008000;">&#91;</span>N<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; T<span style="color: #000040;">&amp;</span> operator<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> i<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">assert</span><span style="color: #008000;">&#40;</span>i<span style="color: #000080;">&gt;=</span><span style="color: #0000dd;">0</span> <span style="color: #000040;">&amp;&amp;</span> i<span style="color: #000080;">&lt;</span>N<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #0000ff;">return</span> mV<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">const</span> T<span style="color: #000040;">&amp;</span> operator<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> i<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">assert</span><span style="color: #008000;">&#40;</span>i<span style="color: #000080;">&gt;=</span><span style="color: #0000dd;">0</span> <span style="color: #000040;">&amp;&amp;</span> i<span style="color: #000080;">&lt;</span>N<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #0000ff;">return</span> mV<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> T<span style="color: #000080;">&gt;</span> <span style="color: #0000ff;">class</span> FirstOrderIntegrator <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> IntegratorBase<span style="color: #000080;">&lt;</span>T,<span style="color: #0000dd;">2</span><span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FirstOrderIntegrator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FirstOrderIntegrator<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> T<span style="color: #000040;">&amp;</span> x, <span style="color: #0000ff;">const</span> T<span style="color: #000040;">&amp;</span> v<span style="color: #008000;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>mV<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> x<span style="color: #008080;">;</span> &nbsp;this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>mV<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> v<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">void</span> Update<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">float</span> t<span style="color: #008000;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>mV<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000040;">+</span><span style="color: #000080;">=</span> this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>mV<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000040;">*</span> t<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> T<span style="color: #000080;">&gt;</span> <span style="color: #0000ff;">class</span> SecondOrderIntegrator <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> IntegratorBase<span style="color: #000080;">&lt;</span>T,<span style="color: #0000dd;">3</span><span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SecondOrderIntegrator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SecondOrderIntegrator<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> T<span style="color: #000040;">&amp;</span> x, <span style="color: #0000ff;">const</span> T<span style="color: #000040;">&amp;</span> v, <span style="color: #0000ff;">const</span> T<span style="color: #000040;">&amp;</span> a<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>mV<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> x<span style="color: #008080;">;</span> &nbsp;this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>mV<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> v<span style="color: #008080;">;</span> this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>mV<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> a<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">void</span> Update<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">float</span> t<span style="color: #008000;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; T vt <span style="color: #000080;">=</span> this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>mV<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #000040;">*</span> t<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>mV<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000040;">+</span><span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>mV<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000040;">+</span> vt <span style="color: #000040;">*</span> <span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> t<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>mV<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000040;">+</span><span style="color: #000080;">=</span> vt<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666;">// This is a fat one.</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> T<span style="color: #000080;">&gt;</span> <span style="color: #0000ff;">class</span> ThirdOrderIntegrator <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> IntegratorBase<span style="color: #000080;">&lt;</span>T,<span style="color: #0000dd;">4</span><span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ThirdOrderIntegrator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ThirdOrderIntegrator<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> T<span style="color: #000040;">&amp;</span> x, <span style="color: #0000ff;">const</span> T<span style="color: #000040;">&amp;</span> v, <span style="color: #0000ff;">const</span> T<span style="color: #000040;">&amp;</span> a, <span style="color: #0000ff;">const</span> T<span style="color: #000040;">&amp;</span> w<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>mV<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> x<span style="color: #008080;">;</span> &nbsp;this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>mV<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> v<span style="color: #008080;">;</span> this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>mV<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> a<span style="color: #008080;">;</span> this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>mV<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> w<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">void</span> Update<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">float</span> t<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; T mv3t <span style="color: #000080;">=</span> this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>mV<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#93;</span> <span style="color: #000040;">*</span> t<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; T mv2t <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>mV<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #000040;">+</span> mv3t <span style="color: #000040;">*</span> <span style="color:#800080;">0.5f</span> <span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> t<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>mV<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #000040;">+</span><span style="color: #000080;">=</span> mv3t<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>mV<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000040;">+</span><span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>mV<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000040;">+</span> mv2t <span style="color: #000040;">*</span> <span style="color:#800080;">0.5f</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> t<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>mV<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000040;">+</span><span style="color: #000080;">=</span> mv2t<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></div></div>
<p>It is a template structure for elements that can be integrated over time.<br />
It is Game development related and actually it is not as precise since it uses Euler integration. However, you can modify it to use the integration you want &#8230; and actually, it wouldn&#8217;t be as hard to create a RK4 version of the mechanic.</p>
<p>With this mechanic you will be able to access the derivatives with the index operator []:</p>
<p>X[0] -> The thing you are integrating<br />
X[1] -> First derivative over time<br />
X[2] -> Second derivative over time</p>
<p>Which is really really handy (you won&#8217;t believe me how much).</p>
<p>A tipical usage:</p>
<div class="codecolorer-container cpp mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">struct</span> Particle <span style="color: #008000;">&#123;</span><br />
<br />
&nbsp; &nbsp; FirstOrderIntegrator<span style="color: #000080;">&lt;</span>Vector2f<span style="color: #000080;">&gt;</span> mPosition<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; FirstOrderIntegrator<span style="color: #000080;">&lt;</span>Color4f<span style="color: #000080;">&gt;</span> mColor<span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> Update<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">float</span> t<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mPosition.<span style="color: #007788;">Update</span><span style="color: #008000;">&#40;</span>t<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mColor.<span style="color: #007788;">Update</span><span style="color: #008000;">&#40;</span>t<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> Draw<span style="color: #008000;">&#40;</span> GraphicThing<span style="color: #000040;">&amp;</span> gt <span style="color: #008000;">&#41;</span> &nbsp;<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; gt.<span style="color: #007788;">Draw</span><span style="color: #008000;">&#40;</span> mPosition<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span>, mColor<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> InvertDirection<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mPosition<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000040;">*</span><span style="color: #000080;">=</span> <span style="color: #000040;">-</span><span style="color:#800080;">1.0f</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></div></div>
<p>You probably know about templates and integrators but haven&#8217;t thought about using them that way &#8230; yeah, there are many situations where this structure cannot help you, but in many cases it will save you to write a lot of code <img src='http://www.southwindsgames.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>See you later!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.southwindsgames.com/blog/2011/02/28/some-of-our-bits/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fast Integer Log2 function in C/C++</title>
		<link>http://www.southwindsgames.com/blog/2009/01/19/fast-integer-log2-function-in-cc/</link>
		<comments>http://www.southwindsgames.com/blog/2009/01/19/fast-integer-log2-function-in-cc/#comments</comments>
		<pubDate>Tue, 20 Jan 2009 03:14:14 +0000</pubDate>
		<dc:creator>Juan Pablo</dc:creator>
				<category><![CDATA[Technical]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[log2]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[optimized]]></category>

		<guid isPermaLink="false">http://www.southwindsgames.com/blog/?p=48</guid>
		<description><![CDATA[Some times, it happens that you need to have a high performance function that you usually use in a not optimized way. And since you never needed to optimize it you hesitated before realising it can be severly improved. Probably, this is what you would think of the integer log2 function, in general this function [...]]]></description>
			<content:encoded><![CDATA[<p>Some times, it happens that you need to have a high performance function that you usually use in a not optimized way. And since you never needed to optimize it you hesitated before realising it can be severly improved. Probably, this is what you would think of the integer log2 function, in general this function is used on not critic code so it can be &#8220;slow&#8221; without creating any problem (often used to calculate the size of a power of 2 texture to hold an image)<span id="more-48"></span></p>
<p>The usual way of calculating this log2 function is testing until the base elevated to the log is bigger than the value. This is easy with the << operator.<br />
This function returns the <strong>truncated to the integer unit log2 of value</strong>:</p>
<div class="codecolorer-container cpp mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> iLog2<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> value<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> l <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span>value <span style="color: #000080;">&gt;&gt;</span> l<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&gt;</span> <span style="color: #0000dd;">1</span> <span style="color: #008000;">&#41;</span> <span style="color: #000040;">++</span>l<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> l<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Easy, isn&#8217;t it? And it is quite fast! you will do 8 comparations to return the log2 of 256.<br />
But what if we do a binary search instead of this linear search &#8230;</p>
<div class="codecolorer-container cpp mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> Log2<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> value<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> f<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span>, s<span style="color: #000080;">=</span><span style="color: #0000dd;">32</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span>s<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; s<span style="color: #000080;">&gt;&gt;=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> value <span style="color: #000080;">&gt;</span> <span style="color: #0000dd;">1</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #008000;">&#40;</span>f<span style="color: #000040;">+</span>s<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> f<span style="color: #000040;">+</span><span style="color: #000080;">=</span>s<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> f<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Even faster since we will be doing a fixed number of comparations.<br />
And so, if they are fixed &#8230;</p>
<div class="codecolorer-container cpp mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> ilog2<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">register</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> x<span style="color: #008000;">&#41;</span> <br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">register</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> l<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>x <span style="color: #000080;">&gt;=</span> <span style="color: #0000dd;">1</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #0000dd;">16</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> x<span style="color: #000080;">&gt;&gt;=</span><span style="color: #0000dd;">16</span><span style="color: #008080;">;</span> l<span style="color: #000040;">|</span><span style="color: #000080;">=</span><span style="color: #0000dd;">16</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>x <span style="color: #000080;">&gt;=</span> <span style="color: #0000dd;">1</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #0000dd;">8</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> x<span style="color: #000080;">&gt;&gt;=</span><span style="color: #0000dd;">8</span><span style="color: #008080;">;</span> l<span style="color: #000040;">|</span><span style="color: #000080;">=</span><span style="color: #0000dd;">8</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>x <span style="color: #000080;">&gt;=</span> <span style="color: #0000dd;">1</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> x<span style="color: #000080;">&gt;&gt;=</span><span style="color: #0000dd;">4</span><span style="color: #008080;">;</span> l<span style="color: #000040;">|</span><span style="color: #000080;">=</span><span style="color: #0000dd;">4</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>x <span style="color: #000080;">&gt;=</span> <span style="color: #0000dd;">1</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> x<span style="color: #000080;">&gt;&gt;=</span><span style="color: #0000dd;">2</span><span style="color: #008080;">;</span> l<span style="color: #000040;">|</span><span style="color: #000080;">=</span><span style="color: #0000dd;">2</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>x <span style="color: #000080;">&gt;=</span> <span style="color: #0000dd;">1</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> l<span style="color: #000040;">|</span><span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> l<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>You can hardcode the answers and the bit settings, and this one compiles really well!<br />
If you want to go further you can expand the <em>if</em>s to 32 branches with 32 results, and so you will collapse the search time to the minimal (I cannot think on something better), but it is up to you if you want to go further. As well this function can be bounded with template arguments, but I don&#8217;t think this will ever pay off.</p>
<p>Probably you won&#8217;t find this function useful (except if you are here because you were looking for it), but in any case I liked this function so much and I wanted to share it because it shows us that even the most every day used things can be improved, refined, and can be prepared for different contexts. <strong>It isn&#8217;t all invented, you can keep discovering and creating</strong> <img src='http://www.southwindsgames.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>- Juan Pablo</p>
]]></content:encoded>
			<wfw:commentRss>http://www.southwindsgames.com/blog/2009/01/19/fast-integer-log2-function-in-cc/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

