<?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; math</title>
	<atom:link href="http://www.southwindsgames.com/blog/tag/math/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>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>

