<?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>Clusty&#039;s Blog &#187; Programming</title>
	<atom:link href="http://www.hexorcist.com/wordpress/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hexorcist.com/wordpress</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sat, 10 Jul 2010 22:05:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Duff&#8217;s device</title>
		<link>http://www.hexorcist.com/wordpress/2009/10/30/duff-device/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=duff-device</link>
		<comments>http://www.hexorcist.com/wordpress/2009/10/30/duff-device/#comments</comments>
		<pubDate>Fri, 30 Oct 2009 17:49:11 +0000</pubDate>
		<dc:creator>clusty</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://clusty1.wordpress.com/?p=36</guid>
		<description><![CDATA[Today I wanted to attract your attention to a funky contraption which dates from the 80&#8242;s, and no, it has no connection to Homer&#8217;s favourite brand of beer. I am talking about a nice exploit of the C programming language, related to the case-label fall-through invented by Tom Duff. This function performs the simple task [...]]]></description>
			<content:encoded><![CDATA[<p>Today I wanted to attract your attention to a funky contraption which dates from the 80&#8242;s, and no, it has no connection to Homer&#8217;s favourite brand of beer.</p>
<p>I am talking about a nice exploit of the C programming language, related to the <strong>case-label fall-through</strong> invented by Tom Duff. This function performs the simple task of copying data from one place to another in memory.</p>
<div class="codecolorer-container c vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">send<span style="color: #009900;">&#40;</span>to<span style="color: #339933;">,</span> from<span style="color: #339933;">,</span> count<span style="color: #009900;">&#41;</span><br />
<span style="color: #993333;">register</span> <span style="color: #993333;">short</span> <span style="color: #339933;">*</span>to<span style="color: #339933;">,</span> <span style="color: #339933;">*</span>from<span style="color: #339933;">;</span><br />
<span style="color: #993333;">register</span> count<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">register</span> n<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span>count<span style="color: #339933;">+</span><span style="color: #0000dd;">7</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #0000dd;">8</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span>count<span style="color: #339933;">%</span><span style="color:#800080;">8</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">:</span> <span style="color: #b1b100;">do</span><span style="color: #009900;">&#123;</span> <span style="color: #339933;">*</span>to<span style="color: #339933;">++</span> <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>from<span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">7</span><span style="color: #339933;">:</span> &nbsp; &nbsp; <span style="color: #339933;">*</span>to<span style="color: #339933;">++</span> <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>from<span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">6</span><span style="color: #339933;">:</span> &nbsp; &nbsp; <span style="color: #339933;">*</span>to<span style="color: #339933;">++</span> <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>from<span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">5</span><span style="color: #339933;">:</span> &nbsp; &nbsp; <span style="color: #339933;">*</span>to<span style="color: #339933;">++</span> <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>from<span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">:</span> &nbsp; &nbsp; <span style="color: #339933;">*</span>to<span style="color: #339933;">++</span> <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>from<span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">3</span><span style="color: #339933;">:</span> &nbsp; &nbsp; <span style="color: #339933;">*</span>to<span style="color: #339933;">++</span> <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>from<span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">:</span> &nbsp; &nbsp; <span style="color: #339933;">*</span>to<span style="color: #339933;">++</span> <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>from<span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">:</span> &nbsp; &nbsp; <span style="color: #339933;">*</span>to<span style="color: #339933;">++</span> <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>from<span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">--</span>n<span style="color: #339933;">&gt;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>In the dawn of C and compiler optimization, this function performed almost twice as fast as the naive looping approach due to the loop unrolling (unwinding).</p>
<p>Nowadays it is probably nothing more than a curiosity as <strong>memcopy</strong> is doing a better job on almost all platforms. Actually at some point the performance of XFree86 got a serious boost from replacing all Duffs. At least one good thing came from the awkward behaviour of C&#8217;s switch-case&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hexorcist.com/wordpress/2009/10/30/duff-device/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
