<?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>Some Assembly Required &#187; algorithms</title>
	<atom:link href="http://assemblyrequired.crashworks.org/tag/algorithms/feed/" rel="self" type="application/rss+xml" />
	<link>http://assemblyrequired.crashworks.org</link>
	<description>Technical Notes On Game Development</description>
	<lastBuildDate>Sun, 16 Oct 2011 04:04:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Making Division Faster</title>
		<link>http://assemblyrequired.crashworks.org/2008/07/03/making-division-faster/</link>
		<comments>http://assemblyrequired.crashworks.org/2008/07/03/making-division-faster/#comments</comments>
		<pubDate>Thu, 03 Jul 2008 08:16:19 +0000</pubDate>
		<dc:creator>Elan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[pipeline]]></category>

		<guid isPermaLink="false">http://assemblyrequired.wordpress.com/?p=6</guid>
		<description><![CDATA[If you want to divide an arbitrary X by an arbitrary Y, long division is pretty much the only way to go -- slow even in hardware. However, if the divisor is known at compile time, there is very often a much more efficient way to compute the quotient.]]></description>
			<content:encoded><![CDATA[<p>My friend <a href="http://cdwfs.livejournal.com/">Cort</a> pointed me at this interesting <em>Developing for Developers</em> article on <a title="Integer Division by Constants" href="http://blogs.msdn.com/devdev/archive/2005/12/12/502980.aspx">how to make integer division faster when the denominator is a fixed constant</a>. As Cort says, if your processor lacks a dedicated integer divide op, and</p>
<blockquote><p>&#8230;you want to divide an arbitrary X by an arbitrary Y, <a href="http://www.mathsisfun.com/long_division2.html">long division</a> is pretty much the only way to go. However, if the divisor <em>is</em> known at compile time, there is very often a much more efficient way to compute the quotient. The simplest example, familiar to most students of computer science, is the good old <tt>x / 2<sup>n</sup> = x &gt;&gt; n</tt> .</p>
<p>For those who are curious, <a href="http://blogs.msdn.com/devdev/archive/2005/12/12/502980.aspx">here&#8217;s an article</a> that&#8217;s chock full of similar tricks for efficient division by other small integers (3, 5, 7, 9, 10, 11, etc.). Even the non-assembly programmers in the audience might benefit from these, as they&#8217;re not as commonly implemented by compiler authors.</p></blockquote>
<p>These tricks are especially useful on the in-order PowerPC CPUs found in most modern game consoles, where the integer divide operation is <em>microcoded</em>: that is to say, even though it appears to be only a single opcode in the data stream, it actually causes the processor to execute a little subroutine stored in ROM. This means it stops the CPU pipeline dead in its tracks until the microcode is done executing (about twenty cycles): until the divide is finished, the processor can do nothing else.</p>
<p>In cases where a divide by a small constant can be replaced by five or six pipelined instructions, this can be a big speed benefit: first, because those five instructions will probably execute faster than the long-division stored in microcode; and second, because the compiler can interleave other useful work in between the dependent operations of the divide. (If that other work is on, say, the load or vector pipeline, it may even <a href="http://en.wikipedia.org/wiki/Superscalar">dual-dispatch</a> and get you two operations per clock cycle.)</p>
<p>Derrick Coetzee hasn&#8217;t updated <a href="http://blogs.msdn.com/devdev/default.aspx"><em>Developing for Developers</em></a> recently, but it looks like there&#8217;s quite a few interesting articles in the archives worth checking out.</p>
]]></content:encoded>
			<wfw:commentRss>http://assemblyrequired.crashworks.org/2008/07/03/making-division-faster/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

