<?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>ridiculous_fish</title>
	<atom:link href="http://ridiculousfish.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://ridiculousfish.com/blog</link>
	<description>serious code</description>
	<lastBuildDate>Wed, 17 Feb 2010 09:20:32 +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>Labor of Division (Episode 1)</title>
		<link>http://ridiculousfish.com/blog/archives/2010/02/15/labor-of-division-episode-1/</link>
		<comments>http://ridiculousfish.com/blog/archives/2010/02/15/labor-of-division-episode-1/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 23:04:47 +0000</pubDate>
		<dc:creator>ridiculous_fish</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://ridiculousfish.com/blog/?p=230</guid>
		<description><![CDATA[1321528399 is an example of a "magic number:" a number that lets you substitute speedy multiplication for pokey division, as if by magic.  In this post, I become a big fat spoilsport and ruin the trick for everyone.]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s how you divide an unsigned int by 13 in C:</p>
<pre>
unsigned divide(unsigned x) { return x / 13; }
</pre>
<p>It gets better, I promise.  Here&#8217;s the corresponding x64 assembly that gcc produces:</p>
<pre>
_divide:
        movl    $1321528399, %edx
        movl    %edi, %eax
        mull    %edx
        shrl    $2, %edx
        movl    %edx, %eax
        ret
</pre>
<p>Instead of division, there&#8217;s multiplication by a bizarre number: 1321528399.  What wickedness is this?</p>
<p>1321528399 is an example of a &#8220;magic number:&#8221; a number that lets you substitute speedy multiplication for pokey division, as if by magic.  In this post, I become a big fat spoilsport and ruin the trick for everyone.</p>
<p>Dividing by 13 is different than multiplying by 1321528399, so there must be more here than meets the eye.  Compiling it as PowerPC reveals a bit more:</p>
<pre>
_divide:
        lis r0,0x4ec4
        ori r0,r0,60495
        mulhwu r3,r3,r0
        srwi r3,r3,2
        blr
</pre>
<p>mulhwu means &#8220;multiply high word unsigned.&#8221;  It multiplies  by 1321528399, but takes the high 32 bits of the 64 bit result, not the low 32 bits like we are used to.  x86-64 does the same thing (notice the shift right instruction operates on %edx, which contains the high bits, not %eax which is the low bits).  After multiplying, it shifts the result right by two.   In C:</p>
<pre>
unsigned divide(unsigned x) {
   return (unsigned)(x*1321528399ULL >> 34);
}
</pre>
<p>So dividing by 13 is the same as multiplying by 1321528399 and then dividing by 2<sup>34</sup>.  That means that 2<sup>34</sup> divided by 13 would be 1321528399, right?  In fact, it&#8217;s 1321528398.769 and change.  Pretty close, but we&#8217;re not optimizing horseshoes, so how can we be sure this works all the time?</p>
<div class="dotbox">&#160;</div>
<h4>The answer to the question right above this line</h4>
<p>It&#8217;s provable, and hopefully we can get some intuition about the whole thing.  In the following proof, division is exact, not that integer-truncating stuff that C does; to round down I&#8217;ll use floor.  Also, this only covers unsigned division: every number that appears is non-negative.  (Signed division is broadly similar, though does differ in some details.)  Lastly, we&#8217;re going to assume that the denominator d is not a power of 2, which is weirdly important.  If d were a power of 2, we&#8217;d skip the multiplication and just use shifts, so we haven&#8217;t lost any generality.</p>
<p>We have some fixed positive denominator <i>d</i> and some variable nonegative numerator <i>n</i>, and we want to compute the quotient <img src='http://ridiculousfish.com/blog/wp-content/latex/59e/59e12d316eeddd2295c0f0142873c37b-T-383838-3.png' alt='\frac n d' title='\frac n d' class='latex' /> &#8211; no, wait, <img src='http://ridiculousfish.com/blog/wp-content/latex/ec9/ec9817221c158f28adb1f2d946f318f8-T-383838-3.png' alt='\lfloor \frac n d \rfloor' title='\lfloor \frac n d \rfloor' class='latex' />, since that&#8217;s what C does.  We&#8217;ll multiply the top and bottom by 2<sup>k</sup>, where k is some positive integer power &#8211; it represents the precision in a sense, and we&#8217;ll pick its value later:</p>
<img src='http://ridiculousfish.com/blog/wp-content/latex/480/48002b9485630bf107ce60691f138940-T-383838-3.png' alt='\lfloor \frac n d \rfloor = \lfloor  \frac n d \times \frac {2^k} {2^k} \rfloor = \lfloor \frac {2^k} d \times \frac n {2^k} \rfloor' title='\lfloor \frac n d \rfloor = \lfloor  \frac n d \times \frac {2^k} {2^k} \rfloor = \lfloor \frac {2^k} d \times \frac n {2^k} \rfloor' class='latex' />
<p>We&#8217;re going to call that <img src='http://ridiculousfish.com/blog/wp-content/latex/a7d/a7d9e2b7aa4a81de972b3bb8e70c46c8-T-383838-3.png' alt='\frac {2^k} d' title='\frac {2^k} d' class='latex' /> term <i>m<sub>exact</sub></i>, because it&#8217;s our &#8220;magic number&#8221; that lets us compute division through multiplication.  So we have:<br />
<br /><img src='http://ridiculousfish.com/blog/wp-content/latex/694/6941ece6b2e820ef305936273a3ad483-T-383838-3.png' alt='m_{exact} = \frac {2^k} d \\\\ \lfloor \frac n d \rfloor = \lfloor m_{exact} \times \frac n {2^k} \rfloor' title='m_{exact} = \frac {2^k} d \\\\ \lfloor \frac n d \rfloor = \lfloor m_{exact} \times \frac n {2^k} \rfloor' class='latex' /></p>
<p>This equality looks promising, because we&#8217;ve hammered our expression into the shape we want; but we haven&#8217;t really done anything yet.  The problem is that m<sub>exact</sub> is a fraction, which is hard for computers to multiply.  (We know it&#8217;s a fraction because d is not a power of 2).  The tricky part is finding an integer approximation of m<sub>exact</sub> that gives us the same result for any dividend up to the largest possible one (UINT_MAX in our case).  Call this approximation <i>m</i>.  So we have <img src='http://ridiculousfish.com/blog/wp-content/latex/2b1/2b193f3574a07d2f5766e62a9cea05de-T-383838-2.png' alt='m \approx m_{exact} ' title='m \approx m_{exact} ' class='latex' />, where m is an integer.</p>
<p>When it comes to approximation, closer is better, so let&#8217;s start by just rounding m down: <img src='http://ridiculousfish.com/blog/wp-content/latex/ca9/ca923a5c67b7f5a366f23d759846abd2-T-383838-2.png' alt='m = \lfloor m_{exact} \rfloor' title='m = \lfloor m_{exact} \rfloor' class='latex' />.  Since m<sub>exact</sub> cannot be an integer, m must be strictly less than m<sub>exact</sub>.  Does this m work, which is to say, does it behave the same as m<sub>exact</sub> over the range we care about?</p>
<p>We&#8217;ll try it when the numerator equals the denominator: n = d.  Of course then the quotient is supposed to be 1.  This leads to:</p>
<img src='http://ridiculousfish.com/blog/wp-content/latex/b50/b507f0c43f50ae90844821ce5f75f9e4-T-383838-3.png' alt='\frac d d = m_{exact} \times \frac d {2^k} \\ \implies \frac d d &gt; m \times \frac d {2^k} \\ \implies 1 &gt; \lfloor m \times \frac d {2^k} \rfloor' title='\frac d d = m_{exact} \times \frac d {2^k} \\ \implies \frac d d &gt; m \times \frac d {2^k} \\ \implies 1 &gt; \lfloor m \times \frac d {2^k} \rfloor' class='latex' />
<p>That&#8217;s bad, because that last expression is supposed to be 1.  The approximation <img src='http://ridiculousfish.com/blog/wp-content/latex/ca9/ca923a5c67b7f5a366f23d759846abd2-T-383838-3.png' alt='m = \lfloor m_{exact} \rfloor' title='m = \lfloor m_{exact} \rfloor' class='latex' /> is too small, no matter what k is.</p>
<p>Ok, let&#8217;s round up instead: <img src='http://ridiculousfish.com/blog/wp-content/latex/636/636e4e2eca84606ef1761bd9ba69ec68-T-383838-3.png' alt='m = \lceil m_{exact} \rceil = \lceil \frac {2^k} d \rceil' title='m = \lceil m_{exact} \rceil = \lceil \frac {2^k} d \rceil' class='latex' />.  Does this value for m work?</p>
<p>Now, <img src='http://ridiculousfish.com/blog/wp-content/latex/d07/d0798ffe1819a0515843df53641ed55c-T-383838-3.png' alt='\lceil \frac {2^k} d \rceil' title='\lceil \frac {2^k} d \rceil' class='latex' /> just means dividing 2<sup>k</sup> by d and then rounding up.  That&#8217;s the same as rounding up 2<sup>k</sup> to the next multiple of d <i>first</i>, and then dividing exactly by d.  (We know that 2<sup>k</sup> is not itself a multiple of d because d is not a power of 2!)  So we can write:</p>
<img src='http://ridiculousfish.com/blog/wp-content/latex/1c2/1c22ae4eb32c9088da65bf151d20fc7c-T-383838-3.png' alt='m = \lceil \frac {2^k} d \rceil = \frac {2^k + e} d' title='m = \lceil \frac {2^k} d \rceil = \frac {2^k + e} d' class='latex' />. </p>
<p>where e is the &#8220;thing to add to get to the next multiple of d,&#8221; which means that 0 < e < d.  (Formally, e = d - 2<sup>k</sup> mod d.)  This value e is sort of a measure of how much &#8220;damage&#8221; the ceil does &#8211; that is, how bad the approximation m is.</p>
<p>So let&#8217;s plug this new m into our division expression, and then apply some algebra:</p>
<p><img src='http://ridiculousfish.com/blog/wp-content/latex/968/96832a1b77f9ff6210f41dded30ace10-T-383838-3.png' alt='\lfloor m \times \frac n {2^k} \rfloor = \lfloor \frac {2^k + e} d \times \frac n {2^k} \rfloor = \lfloor \frac n d + \frac e d \times \frac n {2^k} \rfloor  ' title='\lfloor m \times \frac n {2^k} \rfloor = \lfloor \frac {2^k + e} d \times \frac n {2^k} \rfloor = \lfloor \frac n d + \frac e d \times \frac n {2^k} \rfloor  ' class='latex' />
<p>This last expression is really cool, because it shows the wholesome result we&#8217;re after, <img src='http://ridiculousfish.com/blog/wp-content/latex/ec9/ec9817221c158f28adb1f2d946f318f8-T-383838-3.png' alt='\lfloor \frac n d \rfloor' title='\lfloor \frac n d \rfloor' class='latex' />, and also an insidious &#8220;error term:&#8221; that <img src='http://ridiculousfish.com/blog/wp-content/latex/3c9/3c9fd9765ef1a7478f911662bb3c8fb8-T-383838-3.png' alt='\frac e d \times \frac n {2^k}' title='\frac e d \times \frac n {2^k}' class='latex' />, which represents how much our ceil is causing us to overestimate.  It also shows that we can make the error term smaller by cranking up k: that&#8217;s the sense in which k is a precision.</p>
<div class="dotbox">&#160;</div>
<h4>K&#8217;s Choice</h4>
<p>We could pick some huge precision k, get a very precise result, and be done.  But we defined m to be <img src='http://ridiculousfish.com/blog/wp-content/latex/a58/a58675af20c1e09b9888691fd9d19e87-T-383838-3.png' alt='\lceil \frac {2^k}  d \rceil' title='\lceil \frac {2^k}  d \rceil' class='latex' />, so if k is too big, then m will be too big to fit in a machine register and we can&#8217;t efficiently multiply by it.  So instead let&#8217;s pick the smallest k that&#8217;s precise enough, and hope that fits into a register.  Since there are several variables involved, our strategy will be to find upper bounds for them, replace the variables with their upper bounds, and simplify the expression until we can solve for k.</p>
<p>So we want the smallest k so that <img src='http://ridiculousfish.com/blog/wp-content/latex/3fa/3fa7ece085313c0e286229cd2ba4673b-T-383838-3.png' alt='\lfloor \frac n d \rfloor = \lfloor \frac n d + \frac e d \times \frac n {2^k} \rfloor ' title='\lfloor \frac n d \rfloor = \lfloor \frac n d + \frac e d \times \frac n {2^k} \rfloor ' class='latex' />.  This will be true if the error term <img src='http://ridiculousfish.com/blog/wp-content/latex/3c9/3c9fd9765ef1a7478f911662bb3c8fb8-T-383838-3.png' alt='\frac e d \times \frac n {2^k}' title='\frac e d \times \frac n {2^k}' class='latex' /> is less than 1, because then the floor operation will wipe it out, right?  Oops, not quite, because there&#8217;s also a fractional contribution from <img src='http://ridiculousfish.com/blog/wp-content/latex/59e/59e12d316eeddd2295c0f0142873c37b-T-383838-3.png' alt='\frac n d' title='\frac n d' class='latex' />.   We need to be sure that the error term, <i>plus</i> the fractional contribution of <img src='http://ridiculousfish.com/blog/wp-content/latex/59e/59e12d316eeddd2295c0f0142873c37b-T-383838-3.png' alt='\frac n d' title='\frac n d' class='latex' />, is less than 1 for that equality to be true.</p>
<p>We&#8217;ll start by putting an upper bound on the fractional contribution.  How big can it be?  If you divide any integer by d, the fractional part of the result is no more than <img src='http://ridiculousfish.com/blog/wp-content/latex/8ce/8ceacb7c74fdffddd4ad2abbb520dc96-T-383838-3.png' alt='\frac {d - 1} d' title='\frac {d - 1} d' class='latex' />.  Therefore the fractional contribution of <img src='http://ridiculousfish.com/blog/wp-content/latex/59e/59e12d316eeddd2295c0f0142873c37b-T-383838-3.png' alt='\frac n d' title='\frac n d' class='latex' /> is at most <img src='http://ridiculousfish.com/blog/wp-content/latex/8ce/8ceacb7c74fdffddd4ad2abbb520dc96-T-383838-3.png' alt='\frac {d - 1} d' title='\frac {d - 1} d' class='latex' />, and so if our error term is less than <img src='http://ridiculousfish.com/blog/wp-content/latex/458/458bb428a5f88ca082971f3a7ef29220-T-383838-3.png' alt='\frac 1 d' title='\frac 1 d' class='latex' />, it will be erased by the floor and our equality will be true.</p>
<p>So k will be big enough when &#160;&#160;<img src='http://ridiculousfish.com/blog/wp-content/latex/f08/f08bb1701255a3dca746a7e8863ed410-T-383838-3.png' alt='\frac e d \times \frac n {2^k} &lt; \frac 1 d' title='\frac e d \times \frac n {2^k} &lt; \frac 1 d' class='latex' />.</p>
<p>Next we&#8217;ll tackle <img src='http://ridiculousfish.com/blog/wp-content/latex/1a5/1a516c84c4570ebb2f78455e8d71e4ab-T-383838-2.png' alt='\frac e d' title='\frac e d' class='latex' />.  We know from up above that e is at most d-1, so we have <img src='http://ridiculousfish.com/blog/wp-content/latex/1eb/1ebcf5b028ed2f33d5d9da58ad24feaa-T-383838-3.png' alt='\frac e d &lt; 1' title='\frac e d &lt; 1' class='latex' />.  So we can ignore that factor: <img src='http://ridiculousfish.com/blog/wp-content/latex/54e/54e9fb98b5193a33a28f6e06e83ba3a9-T-383838-3.png' alt='\frac n {2^k} &lt; \frac 1 d \implies \frac e d \times \frac n {2^k}  &lt; \frac 1 d' title='\frac n {2^k} &lt; \frac 1 d \implies \frac e d \times \frac n {2^k}  &lt; \frac 1 d' class='latex' />.</p>
<p>The term <img src='http://ridiculousfish.com/blog/wp-content/latex/81b/81bc11f37bf632ab89a77145edeed427-T-383838-3.png' alt='\frac n {2^k}' title='\frac n {2^k}' class='latex' /> has a dependence on n, the numerator.  How big can the numerator be?  Let&#8217;s say we&#8217;re dividing 32 bit unsigned integers: n &#8804; 2<sup>32</sup>.  (The result is easily extended to other widths).  We can make <img src='http://ridiculousfish.com/blog/wp-content/latex/81b/81bc11f37bf632ab89a77145edeed427-T-383838-3.png' alt='\frac n {2^k}' title='\frac n {2^k}' class='latex' /> less than 1 by picking k = 32.  To make it even smaller, less than <img src='http://ridiculousfish.com/blog/wp-content/latex/458/458bb428a5f88ca082971f3a7ef29220-T-383838-3.png' alt='\frac 1 d' title='\frac 1 d' class='latex' />, we must increase k by <img src='http://ridiculousfish.com/blog/wp-content/latex/1d3/1d39f8da0da91abb9ac9b99a2d8c53f3-T-383838-2.png' alt='\log_2 d' title='\log_2 d' class='latex' />.  That is,</p>
<img src='http://ridiculousfish.com/blog/wp-content/latex/6df/6df2316bfd028b5ea1d20bca1ff7fc80-T-383838-3.png' alt='k &gt; 32 + \log_2 d \implies \frac n {2^k} &lt; \frac 1 d' title='k &gt; 32 + \log_2 d \implies \frac n {2^k} &lt; \frac 1 d' class='latex' />
<p>Since we want k to be an integer, we have to round this up: <img src='http://ridiculousfish.com/blog/wp-content/latex/95a/95a4340f886b84e6cfeb00bb42e6e6ca-T-383838-2.png' alt='k = 32 + \lceil \log_2 d \rceil' title='k = 32 + \lceil \log_2 d \rceil' class='latex' />.  We know that value for k works.</p>
<p>What does that precision do to m, our magic number?  Maybe this k makes m too big to fit into a 32 bit register!  We defined <img src='http://ridiculousfish.com/blog/wp-content/latex/158/1580addff7c79ceb71c6bcebe8f160ce-T-383838-3.png' alt='m = \lceil \frac {2^k} d \rceil' title='m = \lceil \frac {2^k} d \rceil' class='latex' />; plugging in k we have <img src='http://ridiculousfish.com/blog/wp-content/latex/584/584824cd897be477ee61f5fd3f88e22f-T-383838-3.png' alt='m = \lceil \frac {2^{32 + \lceil log_2 d \rceil} } d \rceil ' title='m = \lceil \frac {2^{32 + \lceil log_2 d \rceil} } d \rceil ' class='latex' />.</p>
<p>This is a gross expression, but we can put an upper bound on it.  Note that <img src='http://ridiculousfish.com/blog/wp-content/latex/6f4/6f477be756aca87badbd51390414e188-T-383838-3.png' alt='d &lt; {2 ^ { \lfloor log_2 d \rfloor } }' title='d &lt; {2 ^ { \lfloor log_2 d \rfloor } }' class='latex' />, so we can write the following:</p>
<img src='http://ridiculousfish.com/blog/wp-content/latex/402/402e0de8ddfc593ff0043d2d5121b1a1-T-383838-3.png' alt='m &lt;= \lceil \frac {2^{32 + \lceil log_2 d \rceil} } {2 ^ { \lfloor log_2 d \rfloor } } \rceil = \lceil {2^{32 + \lceil log_2 d \rceil - \lfloor log_2 d \rfloor} } \rceil \\ \\ = \lceil {2 ^ { 32 + 1}} \rceil = 2^{33}' title='m &lt;= \lceil \frac {2^{32 + \lceil log_2 d \rceil} } {2 ^ { \lfloor log_2 d \rfloor } } \rceil = \lceil {2^{32 + \lceil log_2 d \rceil - \lfloor log_2 d \rfloor} } \rceil \\ \\ = \lceil {2 ^ { 32 + 1}} \rceil = 2^{33}' class='latex' />
<p>Nuts!  Our approximation m just barely does <i>not</i> fit into a 32 bit word!</p>
<p>Since we rounded some stuff, you might think that we were too lazy and a more careful analysis would produce a tighter upper bound.  But in fact our upper bound is exact: the magic number for an N-bit division really may need to be as large as N+1 bits.  The good news is, there may be smaller numbers that fit in N bits for specific divisors.  More on that later!</p>
<div class="dotbox">&#160;</div>
<h4>We are fruitful and multiply</h4>
<p>In any case, we now have our magic number m.  To perform the division, we need to compute <img src='http://ridiculousfish.com/blog/wp-content/latex/184/184018b76e4ebf3f8eecd2891c2dea9a-T-383838-3.png' alt=' \lfloor \frac {m n} {2^k} \rfloor' title=' \lfloor \frac {m n} {2^k} \rfloor' class='latex' /> as efficiently as possible.  m is a 33 bit number, so how can a 32 bit processor efficiently multiply by that?  One way would be to use a bignum library, but&#8217;s probably slow.  A better approach is to multiply by the low 32 bits, and handle the 33rd bit specially, like this:</p>
<img src='http://ridiculousfish.com/blog/wp-content/latex/816/816312e47a930b4bb9bd9a2ba2f12d26-T-383838-2.png' alt='m n \\ \\ = (m - 2^{32} + 2^{32})n \\ \\ = (m - 2^{32})n + 2^{32} n' title='m n \\ \\ = (m - 2^{32} + 2^{32})n \\ \\ = (m - 2^{32})n + 2^{32} n' class='latex' />
<p>This looks very promising: m &#8211; 2<sup>32</sup> is definitely a 32 bit number.  Furthermore, we can compute that 2<sup>32</sup>n term very easily: we don&#8217;t even need to shift, just add n to the high word of the product.  Unfortunately, it&#8217;s not right: a 32 bit number times a 33 bit number is 65 bits, so the addition overflows.</p>
<p>But we can prevent the overflow by distributing one of the 2s in the denominator, through the following trick:</p>
<img src='http://ridiculousfish.com/blog/wp-content/latex/e0c/e0c75bcfc6fead1d28e712616652a1e4-T-383838-3.png' alt=' \lfloor \frac {n + q} {2^k} \rfloor = \lfloor  \frac {n - q + 2q} {2^k} \rfloor = \lfloor  ( \frac {n - q + 2q} 2 ) / {2^{k-1}} \rfloor \\ = \lfloor ( \lfloor \frac {n - q} 2 \rfloor + q ) / {2^{k-1}} \rfloor ' title=' \lfloor \frac {n + q} {2^k} \rfloor = \lfloor  \frac {n - q + 2q} {2^k} \rfloor = \lfloor  ( \frac {n - q + 2q} 2 ) / {2^{k-1}} \rfloor \\ = \lfloor ( \lfloor \frac {n - q} 2 \rfloor + q ) / {2^{k-1}} \rfloor ' class='latex' />
<p>Here q is the magic number (minus that 33rd bit) times n, and n is just n, the numerator.  (Everything should be multiplied by 2<sup>32</sup>, but that&#8217;s sort of implicit in the fact that we are working in the register containing the high product, so we can ignore it.)</p>
<p>Can the subtraction n &#8211; q underflow?  No, because q is just n times the 32 bit magic number, and then divided by 2<sup>32</sup>.  The magic number (now bereft of its 33rd bit) is less than 2<sup>32</sup>, so we have q < n, so n - q cannot underflow.</p>
<p>What about that +q?  Can that overflow?  No, because we can just throw out the floor to get an upper bound of <img src='http://ridiculousfish.com/blog/wp-content/latex/8d5/8d583e0f06d3398886382a01714756e9-T-383838-2.png' alt='\frac {n + q} 2' title='\frac {n + q} 2' class='latex' /> which is a 32 bit number.</p>
<p>So we have a practical algorithm.  Given a dividend n and a fixed divisor d, where 0 < d < 2<sup>32</sup> and 0 &#8804; n < 2<sup>32</sup>:</p>
<ol>
<li style="margin: 3px">Precompute <img src='http://ridiculousfish.com/blog/wp-content/latex/fb6/fb6d08b00779a83af70821f7c0eb9f92-T-383838-2.png' alt='p = \lceil log_2 d \rceil' title='p = \lceil log_2 d \rceil' class='latex' />
<li style="margin: 3px">Precompute <img src='http://ridiculousfish.com/blog/wp-content/latex/e7a/e7a8c1f954a90ff7b094180351d0b65f-T-383838-2.png' alt='m = \lceil \frac {2^{32 + p}} d \rceil' title='m = \lceil \frac {2^{32 + p}} d \rceil' class='latex' />.  This will be a 33 bit number, so keep only the low 32 bits.</li>
<li style="margin: 3px">Compute <img src='http://ridiculousfish.com/blog/wp-content/latex/55c/55cc14ecfddd69093c4f67a96f4b6f69-T-383838-2.png' alt='q = (m \times n) &gt;&gt; 32' title='q = (m \times n) &gt;&gt; 32' class='latex' />.  Most processors can do this with one &#8220;high multiply&#8221; instruction.</li>
<li style="margin: 3px">Compute <img src='http://ridiculousfish.com/blog/wp-content/latex/2ff/2ffc875be0ed5d79591b1f0b57cf719b-T-383838-2.png' alt='t = ((n - q) &gt;&gt; 2) + q' title='t = ((n - q) &gt;&gt; 2) + q' class='latex' />, which is an overflow-safe way of computing (n + q) >> 1.  This extra addition corrects for dropping the 33rd bit of m.</li>
<li style="margin: 3px">Perform the remaining shift p: <img src='http://ridiculousfish.com/blog/wp-content/latex/884/884c5f696caa60ffcbbc63ac6c1d6d7a-T-383838-2.png' alt='t = t &gt;&gt; (p-1)' title='t = t &gt;&gt; (p-1)' class='latex' /></li>
</ol>
<p>That&#8217;s it!  We know how to efficiently replace division with multiplication.  We can see all these steps in action.  Here is the assembly that Clang generates for division by 7, along with my commentary:</p>
<pre style="font-size: small">
_divide_by_7:
 movl $613566757, %ecx  <i>613566757 is the low 32 bits of (2**35)/7, rounded up</i>
 movl %edi, %eax
 mull %ecx              <i>Multiply the dividend by the magic number</i>
 subl %edx, %edi        <i>The dividend minus the high 32 bits of the product in %edx (%eax has the low)</i>
 shrl %edi              <i>Initial shift by 1 to prevent overflow</i>
 addl %edx, %edi        <i>Add in the high 32 bits again, correcting for the 33rd bit of the magic number</i>
 movl %edi, %eax        <i>Move the result into the return register</i>
 shrl $2, %eax          <i>Final shift right of floor(log_2(7))</i>
 ret
</pre>
<div class="dotbox">&#160;</div>
<h4>Improving the algorithm for particular divisors</h4>
<p>This algorithm is the best we can do for the general case, but we may be able to improve on this for certain divisors.  The key is that e term: the value we add to get to the next multiple of d.  We reasoned that e was less than d, so we used <img src='http://ridiculousfish.com/blog/wp-content/latex/1eb/1ebcf5b028ed2f33d5d9da58ad24feaa-T-383838-2.png' alt='\frac e d &lt; 1' title='\frac e d &lt; 1' class='latex' /> as an upper bound for e.  But it may happen that a multiple of d is only slightly larger than a power of 2.  In that case, e will be small, and if we're lucky, it will be small enough to push the entire "error term" under <img src='http://ridiculousfish.com/blog/wp-content/latex/458/458bb428a5f88ca082971f3a7ef29220-T-383838-2.png' alt='\frac 1 d' title='\frac 1 d' class='latex' />.</p>
<p>For example, let&#8217;s try it for the divisor 11.  With the general algorithm, we would need <img src='http://ridiculousfish.com/blog/wp-content/latex/50c/50c1f37d61a519ab80fd4a2b8e965031-T-383838-2.png' alt='k = 32 + \lceil log_2 11 \rceil = 36' title='k = 32 + \lceil log_2 11 \rceil = 36' class='latex' />.  But what if we choose k = 35 instead?  2<sup>35</sup> + 1 is a multiple of 11, so we have e = 1, and we compute:</p>
<img src='http://ridiculousfish.com/blog/wp-content/latex/475/47542166e7c0d75007179db77028675d-T-383838-3.png' alt='\frac e d \times \frac n {2^k} = \frac 1 d \times \frac n {2^{35}} &lt; \frac 1 d' title='\frac e d \times \frac n {2^k} = \frac 1 d \times \frac n {2^{35}} &lt; \frac 1 d' class='latex' />
<p>So k = 35 forces the error term to be less than <img src='http://ridiculousfish.com/blog/wp-content/latex/458/458bb428a5f88ca082971f3a7ef29220-T-383838-3.png' alt='\frac 1 d' title='\frac 1 d' class='latex' />, and therefore k = 35 is a good enough approximation.  This is good news, because then <img src='http://ridiculousfish.com/blog/wp-content/latex/b9d/b9dc954a7efb31a7697076fead2c1515-T-383838-2.png' alt='m = \lceil \frac {2^{35}} {11} \rceil = 3123612579 &lt; 2^{32}' title='m = \lceil \frac {2^{35}} {11} \rceil = 3123612579 &lt; 2^{32}' class='latex' />, so our magic number fits in 32 bits, and we don't need to worry about overflow!  We can avoid the subtraction, addition, and extra shifting in the general algorithm.  Indeed, clang outputs:</p>
<pre>_divide_by_11:
  movl  $-1171354717, %ecx
  movl  %edi, %eax
  mull  %ecx
  movl  %edx, %eax
  shrl  $3, %eax
  ret
</pre>
<p>The code for dividing by 11 is shorter than for dividing by 7, because a multiple of 11 happens to be very close to a power of 2.  This shows the way to an improved algorithm: We can simply try successive powers of 2, from 32 up to <img src='http://ridiculousfish.com/blog/wp-content/latex/89a/89a3997d0743c8d6b8f87fa4e151e54a-T-383838-2.png' alt='32 + \lfloor log_2 d \rfloor' title='32 + \lfloor log_2 d \rfloor' class='latex' />, and see if any of them happen to be close enough to a multiple of d to force the error term below <img src='http://ridiculousfish.com/blog/wp-content/latex/458/458bb428a5f88ca082971f3a7ef29220-T-383838-3.png' alt='\frac 1 d' title='\frac 1 d' class='latex' />.  If so, the corresponding magic number fits in 32 bits and we can generate very efficient code.  If not, we can always fall back to the general algorithm.</p>
<p>The power 32 will work if <img src='http://ridiculousfish.com/blog/wp-content/latex/8f8/8f893f52b47a9474046a75bc7f1efcf3-T-383838-3.png' alt='\frac e d \leq \frac 1 d' title='\frac e d \leq \frac 1 d' class='latex' />, the power 33 will work if <img src='http://ridiculousfish.com/blog/wp-content/latex/d5a/d5a63e669fb2f673b4731ca4128f06c7-T-383838-3.png' alt='\frac e d \leq \frac 2 d' title='\frac e d \leq \frac 2 d' class='latex' />, the power 34 will work if <img src='http://ridiculousfish.com/blog/wp-content/latex/ee2/ee2c7bae71553684da78a1097c8a3385-T-383838-3.png' alt='\frac e d \leq \frac 4 d' title='\frac e d \leq \frac 4 d' class='latex' />, etc., up to <img src='http://ridiculousfish.com/blog/wp-content/latex/89a/89a3997d0743c8d6b8f87fa4e151e54a-T-383838-2.png' alt='32 + \lfloor log_2 d \rfloor' title='32 + \lfloor log_2 d \rfloor' class='latex' />, which will work if <img src='http://ridiculousfish.com/blog/wp-content/latex/110/110dbc4addb8c7b9217fc832d32a2534-T-383838-3.png' alt='\frac e d \leq \frac 1 2' title='\frac e d \leq \frac 1 2' class='latex' />.  By "chance", we'd expect this last power to work half the time, the previous power to work 25% of the time, etc.  Since we only need one,  most divisors actually should have an efficient, 32 bit or fewer magic number.  The infinite series <img src='http://ridiculousfish.com/blog/wp-content/latex/212/212776ec94134cf04a6d226c88e04b78-T-383838-3.png' alt='\frac 1 2 + \frac 1 4 + \frac 1 8' title='\frac 1 2 + \frac 1 4 + \frac 1 8' class='latex' /> sums to 1, so our chances get better with increasing divisors.</p>
<p>It's interesting to note that if a divisor d has one of these more efficient magic numbers for a power <img src='http://ridiculousfish.com/blog/wp-content/latex/13d/13dfea4ae8562a9b97cfef7927c60d60-T-383838-2.png' alt='k &lt; 32 + \lceil log_2 d \rceil' title='k &lt; 32 + \lceil log_2 d \rceil' class='latex' />, it also has one for all higher powers.  This is easy to see: if 2<sup>k</sup> + e is a multiple of d, then 2<sup>k+1</sup> + 2e is also a multiple of d. </p>
<img src='http://ridiculousfish.com/blog/wp-content/latex/51c/51c855ec66eaca90563af49fd97ef043-T-383838-3.png' alt='\frac e d \times \frac n {2^k} \leq \frac 1 d \implies \frac {2e} d \times \frac n {2^{k+1}} \leq \frac 1 d ' title='\frac e d \times \frac n {2^k} \leq \frac 1 d \implies \frac {2e} d \times \frac n {2^{k+1}} \leq \frac 1 d ' class='latex' />
<p>This is good news.  It means that we only have to check one case, <img src='http://ridiculousfish.com/blog/wp-content/latex/2df/2df4de24d737adff7425a68a22083fde-T-383838-3.png' alt=' \lceil \frac {2^{32 + \lfloor log_2 d \rfloor}} d \rceil' title=' \lceil \frac {2^{32 + \lfloor log_2 d \rfloor}} d \rceil' class='latex' /> (a 32 bit value) to see if there is a more efficient magic number, because if any smaller power works, that one works too.  If that that power fails, we can go to the 33 bit number, which we know must work.  This is useful information in case we are computing magic numbers at runtime.</p>
<p>Still, gcc and LLVM don't settle for just any magic number - both try to find the <i>smallest</i> magic number.  Why?  There's a certain aesthetic appeal in not using bigger numbers than necessary, and most likely the resulting smaller multiplier and smaller shifts are a bit faster.  In fact, in a very few cases, the resulting shift may be zero!  For example, the code for diving by 641:</p>
<pre>
_divide_by_641:
        movl    $6700417, %ecx
        movl    %edi, %eax
        mull    %ecx
        movl    %edx, %eax
        ret
</pre>
<p>No shifts at all!  For this to happen, we must have <img src='http://ridiculousfish.com/blog/wp-content/latex/316/316ec7739545b7a20303cf8393e56274-T-383838-3.png' alt='\frac e d \le \frac 1 d' title='\frac e d \le \frac 1 d' class='latex' />, which of course means that e = 1, so 641 must evenly divide 2<sup>32</sup> + 1.  Indeed it does.</p>
<p>This inspires a way to find the other "super-efficient" shiftless divisors: compute the factors of 2<sup>32</sup> + 1.  Sadly, the only other factor is 6700417.  I have yet to discover an occasion to divide by this factor, but if I do, I'll be ready.</p>
<div class="dotbox">&#160;</div>
<h4>So that's how it works</h4>
<p>Did you skip ahead?  It's OK.  Here's the summary.</p>
<p>Every divisor has a magic number, and most have more than one!  A magic number for <i>d</i> is nothing more than a precomputed quotient: a power of 2 divided by <i>d</i> and then rounded up.  At runtime, we do the same thing, except backwards: multiply by this magic number and then divide by the power of 2, rounding down.  The tricky part is finding a power big enough that the "rounding up" part doesn't hurt anything.  If we are lucky, a multiple of <i>d</i> will happen to be only slightly larger than a power of 2, so rounding up doesn't change much and our magic number will fit in 32 bits.  If we are unlucky, well, we can always fall back to a 33 bit number, which is almost as efficient.</p>
<p>I humbly acknowledge the legendary <del>Guy Steele</del> <b>Henry Warren</b> (must have been a while) and his book Hacker's Delight, which introduced me to this line of proof.  (Except his version has, you know, rigor.)</p>
]]></content:encoded>
			<wfw:commentRss>http://ridiculousfish.com/blog/archives/2010/02/15/labor-of-division-episode-1/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
		<item>
		<title>cdecl</title>
		<link>http://ridiculousfish.com/blog/archives/2009/11/12/cdecl/</link>
		<comments>http://ridiculousfish.com/blog/archives/2009/11/12/cdecl/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 17:41:22 +0000</pubDate>
		<dc:creator>ridiculous_fish</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://ridiculousfish.com/blog/?p=197</guid>
		<description><![CDATA[Quick, what is &#8220;char (*(*(* const x[3])())[5])(int)?&#8221;
If you immediately blurted out &#8220;x&#173;is&#173;an&#173;array&#173;of&#173;three&#173;const&#173;pointers&#173;to&#173;functions&#173;returning&#173;pointers&#173;to&#173;arrays&#173;of&#173;five&#173;pointers&#173;to&#173;functions&#173;taking&#173;int&#173;returning&#173;char&#8220;, and your last name doesn&#8217;t end with &#8220;itchie,&#8221; then chances are you used my new website:
www.cdecl.org
Yes, the venerable cdecl &#8211; the C gibberish to English translator &#8211; is now online.  With AJAX!  Every C declaration will be as an open book [...]]]></description>
			<content:encoded><![CDATA[<p>Quick, what is &#8220;<span style="font-family:  Courier, mono;<br />
;">char (*(*(* const x[3])())[5])(int)</span>?&#8221;</p>
<p>If you immediately blurted out &#8220;<span style="font-size: smaller;">x&shy;is&shy;an&shy;array&shy;of&shy;three&shy;const&shy;pointers&shy;to&shy;functions&shy;returning&shy;pointers&shy;to&shy;arrays&shy;of&shy;five&shy;pointers&shy;to&shy;functions&shy;taking&shy;int&shy;returning&shy;char</span>&#8220;, and your last name doesn&#8217;t end with &#8220;itchie,&#8221; then chances are you used my new website:</p>
<p><span style="font-size: x-large"><a href="http://www.cdecl.org">www.cdecl.org</a></span></p>
<p>Yes, the venerable cdecl &#8211; the C gibberish to English translator &#8211; is now online.  With AJAX!  Every C declaration will be as an open book to you!  Your coworkers&#8217; scruffy beards and suspenders will be nigh useless!</p>
<p>Write C casts and declarations in plain English!  Write plain English in the chicken scratchings and line noise we call C!  The possibilities are twain!</p>
<p>(<a href="http://cdecl.org/?q=char+%28*%28*%28*+const+x%5B3%5D%29%28%29%29%5B5%5D%29%28int%29">Click here</a> to try it with that declaration!)</p>
<p>ANNNNND…it gets better!  Did I mention that <i>my</i> version of cdecl <b>supports blocks?</b>  That&#8217;s right, now you can totally nail that API that requires a <span style="font-size: x-small"><a href="http://cdecl.org/?q=declare+foo+as+block+%28pointer+to+block+%28block%28pointer+to+int%29+returning+int%29+returning+void%29+returning+pointer+to+block%28void%29+returning+int">block taking a pointer to a block taking a block taking a pointer to an int returning an int returning a pointer to a block taking void returning int</a></span>!</p>
<p>This site:</p>
<ul>
<li>Converts readable English variable declarations or typecasts to C</li>
<li>Converts C variable declarations or typecasts to English</li>
<li>Supports <a href="http://developer.apple.com/mac/articles/cocoa/introblocksgcd.html">Apple&#8217;s blocks extension</a> to C</li>
<li>Uses AJAX and has nifty effects</li>
<li>Allows you to generate permalinks, so you can send <a href="http://cdecl.ridiculousfish.com/?q=declare+broiled+as+char">hilarious</a> declarations to your friends, or add them gratuitously to your blog</li>
</ul>
<p>A note on licensing. The cdecl readme states:</p>
<blockquote style="border-left: 2px solid #369; padding-left: 10px; font-size: smaller;"><p>You may well be wondering what the status of cdecl is.  So am I.  It was twice posted to comp.sources.unix, but neither edition carried any mention of copyright.  This version is derived from the second edition.  I have no reason to believe there are any limitations on its use, and strongly believe it to be in the Public Domain.</p></blockquote>
<p>I hereby place my blocks changes to cdecl in the public domain.  The cdecl source code, including my changes, is available for download on the site.</p>
]]></content:encoded>
			<wfw:commentRss>http://ridiculousfish.com/blog/archives/2009/11/12/cdecl/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>I Didn&#8217;t Order That, So Why Is It On My Bill, Episode 2</title>
		<link>http://ridiculousfish.com/blog/archives/2009/09/17/i-didnt-order-that-so-why-is-it-on-my-bill-episode-2/</link>
		<comments>http://ridiculousfish.com/blog/archives/2009/09/17/i-didnt-order-that-so-why-is-it-on-my-bill-episode-2/#comments</comments>
		<pubDate>Thu, 17 Sep 2009 08:17:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://ridiculousfish.com/blog/?p=161</guid>
		<description><![CDATA[This is the second episode of I Didn&#8217;t Order That, So Why Is It On My Bill: C++ features you never use, but pay for anyways.  See episode one here.
For those who like their punchlines first: std::string is designed to allow copy-on-write.  But once operator[] is called, that string is ruined for COW [...]]]></description>
			<content:encoded><![CDATA[<p>This is the second episode of I Didn&#8217;t Order That, So Why Is It On My Bill: C++ features you never use, but pay for anyways.  <a href="blog/archives/2009/06/22/i-didnt-order-that-so-why-is-it-on-my-bill-episode-1/#fish_made_a_mess">See episode one here</a>.</p>
<p>For those who like their punchlines first: std::string is designed to allow copy-on-write.  But once operator[] is called, that string is ruined for COW forevermore. The reason is that it has to guard against a future write at any time, because you may have stashed away the internal pointer that operator[] returns &#8211; a feature that you surely know better than to use, but that costs you nevertheless.</p>
<p>The C++ standard string class is std::string. Here&#8217;s a string containing 10 megabytes of the letter &#8216;x&#8217;:</p>
<pre>
#include &lt;string&gt;
using std::string;
int main(void) {
    string the_base(1024 * 1024 * 10, 'x');
}
</pre>
<p>Big string.  Now let&#8217;s make a hundred copies, one by one:</p>
<pre>
#include &lt;string&gt;
using std::string;
int main(void) {
    string the_base(1024 * 1024 * 10, 'x');
    <span style="color: red">for (int i = 0; i &lt; 100; i++) {
        string the_copy = the_base;
    }</span>
}
</pre>
<p>This runs in .02 seconds, which is very fast.  Suspiciously fast!  This old iMac can&#8217;t schlep a gigabyte of data in two hundredths of a second.  It&#8217;s using copy-on-write.</p>
<p>So let&#8217;s write and measure the copy:</p>
<pre>
#include &lt;string&gt;
using std::string;
int main(void) {
    string the_base(1024 * 1024 * 10, 'x');
    for (int i = 0; i &lt; 100; i++) {
        string the_copy = the_base;
        <span style="color: red">the_copy[0] = 'y';</span>
    }
}
</pre>
<p>The program now runs in 2.5 seconds.  These scant 100 writes resulted in a 100x slowdown.  Not surprising: it&#8217;s exactly what we would expect from a COW string.</p>
<p>the_copy[0] returns a reference to a char.  But maybe we aren&#8217;t going to write into it &#8211; perhaps we only want to read.  How can these copy-on-write strings know when the write occurs, so they can copy?  The answer is that they cannot &#8211; they must be pessimistic and assume you will write, even if you never do.  We can trick it: don&#8217;t assign anything, and still see the performance hit:</p>
<pre>
#include &lt;string&gt;
using std::string;
int main(void) {
    string the_base(1024 * 1024 * 10, 'x');
    for (int i = 0; i &lt; 100; i++) {
        string the_copy = the_base;
        <span style="color: red">the_copy[0];</span>
    }
}
</pre>
<p>No writes!  Just a read!  But same performance as before.  This isn&#8217;t copy on write &#8211; it&#8217;s copy on read!</p>
<p>Things get even worse.  operator[] returns a reference: basically a sugary pointer.  You can read from it or write to it.  Or you can be a jerk and just hang onto it:</p>
<pre>
string original = "hello";
char &#038; ref = original[0];
string clone = original;
ref = 'y';
</pre>
<p>In case that&#8217;s not clear, we made a string, stashed away a reference to the first character, <i>copied</i> the string, and then wrote to the string&#8217;s guts.  The write is just a normal store to a memory location, so the poor string is caught completely unaware.  If the copy-on-write were naive, then we would have modified both the original and the copy.</p>
<p>Oh man, is that evil.  But the string&#8217;s got to be paranoid and guard against it anyways &#8211; in fact, the C++ standard explicitly calls out this case as something that has to work!  In other words, when you get a reference to a char inside a string, the string is tainted.  It can never participate in copy-on-write again, because you just might have stashed away the internal pointer it handed back, and write into the string as a completely unexpected time.</p>
<p>Of course you know better than to hang onto internal pointers.  You <i>probably</i> don&#8217;t need this feature.  But you pay for it anyways:</p>
<pre>
#include &lt;string&gt;
using std::string;
int main(void) {
    string the_base(1024 * 1024 * 10, 'x');
    <span style="color: red">the_base[0];</span>
    for (int i = 0; i &lt; 100; i++) {
        string the_copy = the_base;
    }
}
</pre>
<p>This takes the 2.5 seconds.  Just one silly read wrecked all of the COW machinery for all the future copies.  You&#8217;re paying for writes that never come!</p>
<p>Could the C++ standard have fixed this?  Sure &#8211; the C++ standard goes into great detail about when iterators and references into strings are invalidated, and all it would have taken would be to add the copy constructor and operator=() to the list of reference-invalidating functions (section 23.1.5, for those who are following along in the standard).  A second option would have been to declare that operator=() invalidates existing references for writing, but not for reading.  But the standards committee preferred simplicity, convenience and safety to performance &#8211; uncharacteristically, if you&#8217;ll forgive a smidgen of editorializing.</p>
<p>If you call a reference-invalidating function, you can, in principle, get back in the COW fast lane.  Although GNU STL does not take advantage of this in all cases, it does when resizing strings:</p>
<pre>
#include &lt;string&gt;
using std::string;
int main(void) {
    string the_base(1024 * 1024 * 10, 'x');
    the_base[0];
    <span style="color: red">the_base.push_back('x');</span>
    for (int i = 0; i < 100; i++) {
        string the_copy = the_base;
    }
}
</pre>
<p>Allowing the string to modify itself allowed it to know that any previous references were invalidated.  We're fast again!  It's called a COW, and it's not about to blow it now.</p>
<p>The moral is that you should avoid using operator[], the at() function, iterators, etc. to read from a non-const string - especially a long string - if it may be copied in the future.  If you do use these functions, you risk paying for a write that you don't make, and for hanging onto an internal pointer that you released.  Unfortunately, there's no good function for just reading from a non-const string.  You can do it by adding a crazy cast:</p>
<pre>
#include &lt;string&gt;
using std::string;
int main(void) {
	string the_base(1024 * 1024 * 10, 'x');
	<span style="color: red">const_cast&lt;const string &&gt;(the_base)[0];</span>
	for (int i = 0; i < 100; i++) {
		string the_copy = the_base;
	}
}
</pre>
<p>This hits the fast path too.</p>
<p>Scene.  I hope you enjoyed this post.  As usual the point is not to rag on C++, but to explore aspects of its design that, for whatever reason, tilt a tradeoff away from performance.</p>
]]></content:encoded>
			<wfw:commentRss>http://ridiculousfish.com/blog/archives/2009/09/17/i-didnt-order-that-so-why-is-it-on-my-bill-episode-2/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>I&#8217;m Bringing Hexy Back</title>
		<link>http://ridiculousfish.com/blog/archives/2009/07/31/im-bringing-hexy-back/</link>
		<comments>http://ridiculousfish.com/blog/archives/2009/07/31/im-bringing-hexy-back/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 05:45:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://ridiculousfish.com/blog/?p=127</guid>
		<description><![CDATA[
Hooray, it&#8217;s Hex Fiend 2, a nearly complete rewrite of Hex Fiend that incorporates even better techniques for working with big files.  Hex Fiend is my fast and clever hex editor for Mac OS X.
Click On Me To Get Hex Fiend
This app is about exploring the implementation of standard desktop UI features in the [...]]]></description>
			<content:encoded><![CDATA[<p><a href="/hexfiend/"><img src="/images/hex_icon.png" style="float: right; margin-left: 10px;" /></a></p>
<p>Hooray, it&#8217;s Hex Fiend 2, a nearly complete rewrite of Hex Fiend that incorporates even better techniques for working with big files.  Hex Fiend is my fast and clever hex editor for Mac OS X.</p>
<p style="font-size: larger"><a href="/hexfiend/">Click On Me To Get Hex Fiend</a></p>
<p>This app is about exploring the implementation of standard desktop UI features in the realm of files too large to fully read into main memory.  Is it possible to do copy and paste, find and replace, undo and redo, on a document that may top a hundred gigabytes, and make it feel natural?  Where do we run into trouble?</p>
<p>More on that later.  For now, here&#8217;s what&#8217;s better about Hex Fiend 2:</p>
<ul>
<li style="margin-top: 10px"><b>It&#8217;s embeddable.</b>  The most requested feature was &quot;I want a hex view in my app.&quot;  Now it&#8217;s really easy: Hex Fiend 2 is built as a relatively slim shell on top of a bundle-embeddable .framework.  There&#8217;s <a href="/hexfiend/docs/">a real API</a>, sample code, and everything.  See the Developer section of the Hex Fiend page.</p>
<li style="margin-top: 10px"><b>It&#8217;s faster.</b>  All around. Text rendering is speedier, and the backing data representation more efficient.  It needs less I/O too.
<p>The save algorithm is especially improved.  For example, inserting one byte at the beginning of a 340 MB file and hitting Save would take 52 seconds and 340 additional MB of temporary disk space with Hex Fiend.  In Hex Fiend 2 it&#8217;s reduced to 22 seconds and requires no temporary disk space.</p>
<li style="margin-top: 10px"><b>Better UI.</b>  It supports discontiguous selection.  Scroll-wheel scrolling no longer feels weird.  You can group the bytes into blocks (credit to Blake), and you can hide and show different views (thanks to bbum).  The big dorky line number view now shrinks to fit.  The data inspector panel is inline.  It has Safari-style inline find and replace and &#8220;pop out&#8221; selection highlighting.</li>
<li style="margin-top: 10px"><b>Long operations support progress reporting, cancellation, and don&#8217;t block the UI.</b>   For example, find and replace now has a progress bar and a cancel button, and you can keep using your document (or others) while it works.
</li>
<li style="margin-top: 10px"><b>Longstanding bugfixes</b>.  Backwards searching is now optimized.  Certain coalesced undo bugs have been addressed.  There&#8217;s some basic cross-file dependency tracking.</li>
</ul>
<p>I hope you find it useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://ridiculousfish.com/blog/archives/2009/07/31/im-bringing-hexy-back/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>I Didn&#8217;t Order That, So Why Is It On My Bill, Episode 1</title>
		<link>http://ridiculousfish.com/blog/archives/2009/06/22/i-didnt-order-that-so-why-is-it-on-my-bill-episode-1/</link>
		<comments>http://ridiculousfish.com/blog/archives/2009/06/22/i-didnt-order-that-so-why-is-it-on-my-bill-episode-1/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 21:32:49 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://pammon.webfactional.com/blog/?p=106</guid>
		<description><![CDATA[ C++ features you don't use but produce a performance drag at runtime, episode 1: Inline Functions.]]></description>
			<content:encoded><![CDATA[<p>C++ was designed to ensure you &quot;only pay for what you use,&quot; and at that it is mostly successful.  But it&#8217;s not <i>entirely</i> successful, and I thought it might be interesting to explore some counterexamples: C++ features you don&#8217;t use but still negatively impact performance, and ways in which other languages avoid those issues.</p>
<p>So here&#8217;s the first episode of I Didn&#8217;t Order That, So Why Is It On My Bill, to be continued until I can&#8217;t think of any more.</p>
<p><b>Inline Functions</b></p>
<p>The C++ standard says this:</p>
<p><span style="font: 12pt Monaco, Courier, mono;">A static local variable in an extern inline function always refers to the same object.</span></p>
<p>In other words, static variables in inline functions work just like static variables in any function. That&#8217;s reasonable, because that&#8217;s probably what you want.  That&#8217;s what statics are <i>for</i>, after all.</p>
<p>But wait, it also says this:</p>
<p><span style="font: 12pt Monaco, Courier, mono;">An inline function with external linkage shall have the same address in all translation units.</span></p>
<p>That&#8217;s borderline silly.  Who cares what the address of the function is?  When&#8217;s the last time you used the function pointer address for anything except calling it?  Dollars to donuts says never.  You aren&#8217;t using a function pointer as a hash table key.  You aren&#8217;t comparing the same function via pointers from different files.  And if you did, you&#8217;d probably have the good sense to ensure the functions are defined exactly once (i.e. not inline).  You can easily live without this feature.</p>
<p>So hopefully I&#8217;ve established that you don&#8217;t use this feature.  Now I have to show how you&#8217;re paying for it anyways.  Well, this feature complicates linking.  Ordinarily, there should be only one copy of a function, and if the linker finds more than one it gives you a multiple definition error.  But with inline functions, the compiler is expected to generates multiple copies of the code and it&#8217;s up to the linker to sort it out.  See <a href="http://gcc.gnu.org/onlinedocs/gcc/Vague-Linkage.html">vague linkage</a>.</p>
<p>So the linker has more work to do, and link time is increased.  Again, who cares?  Linking occurs after compilation, and I promised you a runtime cost.  But ah &#8211; link time is runtime when we&#8217;re using dynamic libraries!</p>
<p>Every time you start up a program, the linker has to make sure that your implementation of vector&lt;int&gt;::capacity() has the same address as the vector&lt;int&gt;::capacity() defined in libWhoGivesAHoot.dylib <i>just in case</i> you decide to go and compare their function pointers.</p>
<p>And it gets a little worse.  You know how class templates have to live in header files?  The function definition goes right there in the class declaration, and that makes them inline automatically.  So nearly every function in a template gets this inline uniquing treatment.  Every template function in a class that cannot or should not be inlined &#8211; because its address is taken, because it is recursive, because you&#8217;re optimizing for size, or simply because it&#8217;s too darn big and inlining is counterproductive &#8211; will incur a launch time cost.</p>
<p>The situation is dire enough that gcc added a &quot;-fvisibility-inlines-hidden&quot; flag.  This is a minor violation of the C++ standard, but will improve launch time in cases with a lot of dynamic libraries.  In other words, this flag says: I&#8217;m not using this feature, please take it off my bill.</p>
<p>C does not have this problem.  Why not?  C (in the C99 standard) has very different semantics for inline functions.  To oversimplify a bit, a C99 inline function acts as sort of an alias for a real function, which must be defined once and only once.  Furthermore, inline functions can&#8217;t have static variables, or have their addresses taken.  This approach is more limiting, but doesn&#8217;t require the linker to bend over backwards, and so avoids the performance penalty of the C++ approach.</p>
]]></content:encoded>
			<wfw:commentRss>http://ridiculousfish.com/blog/archives/2009/06/22/i-didnt-order-that-so-why-is-it-on-my-bill-episode-1/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Roundy</title>
		<link>http://ridiculousfish.com/blog/archives/2009/06/01/roundy/</link>
		<comments>http://ridiculousfish.com/blog/archives/2009/06/01/roundy/#comments</comments>
		<pubDate>Mon, 01 Jun 2009 08:13:12 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pammon.webfactional.com/blog/?p=47</guid>
		<description><![CDATA[As any usability expert will tell you, the foundation of sound web page design is rounded corners.  Their smooth curves lend an organic, almost sensual feel to a site, while pages without rounded corners are completely un-navigable morasses of right angles.]]></description>
			<content:encoded><![CDATA[<p>This post is in homage to my old page, <a href="/images/sleep_roundly.png">a triumph of curvaceousness</a>.  May it sleep roundly.</p>
<p>As any usability expert will tell you, the foundation of sound web page design is rounded corners. Their smooth curves lend an organic, almost sensual feel to a site, while other pages are un-navigable morasses of right angles.</p>
<p>Rounded corners are easy with tables and images!  But tables make your page Web 2.0-incompatible, so most browsers cannot render it.</p>
<p>So when I decided to get some roundy corners for myself, I looked around, and the best I was able to find was <a href="http://www.html.it/articoli/niftycube/index.html">Nifty Corners</a>. There&#8217;s a lot to like about Nifty Corners, but look what happens if you try to make them overlap or put them on a, say, not-fixed-color background:</p>
<div style="text-align: center; margin: 15px; font-size: small; ">
        <a href="/images/roundy/nifty.html"><img style="border: 1px solid gray;" src="/images/roundy/pain_agony.png" alt="pain_agony.png" /><br />
        Click on the image to see the actual page.</a>
    </div>
<p>Notice that the corners of the boxes suffer.  The problem is that the area around the corner cannot be drawn transparently &#8211; it needs to be over a fixed color. <a href="http://developer.mozilla.org/en/CSS/-moz-border-radius">Browser specific extensions</a> would be perfect if they weren&#8217;t browser specific.</p>
<p>But I like crazy backgrounds and overlapping boxes.  So in a half-hearted effort for geek cred, here&#8217;s how my roundy corners worked.</p>
<table cellpadding="10px" style="margin-left: auto; margin-right: auto; border: solid; border-width: 1px; border-color: gray;">
<tr>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: right; width: 400px">This is a div:</td>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: left; width: 310px">
<div style="width: 200px; height: 20px;"></div>
</td>
</tr>
<tr>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: right; width: 400px">This is a bordered div:</td>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: left; width: 310px">
<div style="width: 200px; height: 20px; border: 2px solid blue"></div>
</td>
</tr>
<tr>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: right; width: 400px">This is a thickly bordered div:</td>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: left; width: 310px">
<div style="width: 200px; height: 20px; border: 15px solid blue"></div>
</td>
</tr>
<tr>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: right; width: 400px">This is a thickly bordered div with differently colored borders:</td>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: left; width: 310px">
<div style="width: 200px; height: 20px; border: 15px solid; border-right-color: red; border-left-color: green; border-top-color: blue; border-bottom-color: yellow"></div>
</td>
</tr>
<tr>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: right; width: 400px">This is a div bordered only on the left and right:</td>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: left; width: 310px">
<div style="width: 200px; height: 20px; border-left: 10px solid green; border-right: 10px solid red"></div>
</td>
</tr>
<tr>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: right; width: 400px">This is a div bordered more thickly on the right:</td>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: left; width: 310px">
<div style="width: 200px; height: 20px; border-left: 10px solid green; border-right: 80px solid red"></div>
</td>
</tr>
<tr>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: right; width: 400px">Closer:</td>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: left; width: 310px">
<div style="width: 100px; height: 20px; border-left: 10px solid green; border-right: 80px solid red"></div>
</td>
</tr>
<tr>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: right; width: 400px">Closer:</td>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: left; width: 310px">
<div style="width: 20px; height: 20px; border-left: 10px solid green; border-right: 80px solid red"></div>
</td>
</tr>
<tr>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: right; width: 400px">Contact:</td>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: left; width: 310px">
<div style="width: 0px; height: 20px; border-left: 10px solid green; border-right: 80px solid red"></div>
</td>
</tr>
<tr>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: right; width: 400px">This is a bunch of divs in a stack:</td>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: left; width: 310px">
<div style="width: 0px; height: 20px; border-left: 10px solid green; border-right: 80px solid red; margin-bottom: 2px"></div>
<div style="width: 0px; height: 20px; border-left: 10px solid green; border-right: 80px solid red; margin-bottom: 2px"></div>
<div style="width: 0px; height: 20px; border-left: 10px solid green; border-right: 80px solid red; margin-bottom: 2px"></div>
<div style="width: 0px; height: 20px; border-left: 10px solid green; border-right: 80px solid red; margin-bottom: 2px"></div>
<div style="width: 0px; height: 20px; border-left: 10px solid green; border-right: 80px solid red; margin-bottom: 2px"></div>
<div style="width: 0px; height: 20px; border-left: 10px solid green; border-right: 80px solid red; margin-bottom: 2px"></div>
<div style="width: 0px; height: 20px; border-left: 10px solid green; border-right: 80px solid red; margin-bottom: 2px"></div>
<div style="width: 0px; height: 20px; border-left: 10px solid green; border-right: 80px solid red; margin-bottom: 2px"></div>
</td>
</tr>
<tr>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: right; width: 400px">This is a bunch of divs in a stack with different right border widths:</td>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: left; width: 310px">
<div style="width: 0px; height: 20px; border-left: 10px solid green; border-right: 80px solid red; margin-bottom: 2px"></div>
<div style="width: 0px; height: 20px; border-left: 10px solid green; border-right: 95px solid red; margin-bottom: 2px"></div>
<div style="width: 0px; height: 20px; border-left: 10px solid green; border-right: 101px solid red; margin-bottom: 2px"></div>
<div style="width: 0px; height: 20px; border-left: 10px solid green; border-right: 105px solid red; margin-bottom: 2px"></div>
<div style="width: 0px; height: 20px; border-left: 10px solid green; border-right: 108px solid red; margin-bottom: 2px"></div>
<div style="width: 0px; height: 20px; border-left: 10px solid green; border-right: 110px solid red; margin-bottom: 2px"></div>
<div style="width: 0px; height: 20px; border-left: 10px solid green; border-right: 112px solid red; margin-bottom: 2px"></div>
<div style="width: 0px; height: 20px; border-left: 10px solid green; border-right: 113px solid red; margin-bottom: 2px"></div>
</td>
</tr>
<tr>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: right; width: 400px">This is a bunch of divs in a stack with different right border widths, set to float right:</td>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: left; width: 310px">
<div style="width: 130px">
<div style="width: 0px; height: 20px; border-left: 10px solid green; border-right: 80px solid red; margin-bottom: 2px; float: right"></div>
<div style="width: 0px; height: 20px; border-left: 10px solid green; border-right: 95px solid red; margin-bottom: 2px; float: right"></div>
<div style="width: 0px; height: 20px; border-left: 10px solid green; border-right: 101px solid red; margin-bottom: 2px; float: right"></div>
<div style="width: 0px; height: 20px; border-left: 10px solid green; border-right: 105px solid red; margin-bottom: 2px; float: right"></div>
<div style="width: 0px; height: 20px; border-left: 10px solid green; border-right: 108px solid red; margin-bottom: 2px; float: right"></div>
<div style="width: 0px; height: 20px; border-left: 10px solid green; border-right: 110px solid red; margin-bottom: 2px; float: right"></div>
<div style="width: 0px; height: 20px; border-left: 10px solid green; border-right: 112px solid red; margin-bottom: 2px; float: right"></div>
<div style="width: 0px; height: 20px; border-left: 10px solid green; border-right: 113px solid red; margin-bottom: 2px; float: right"></div>
</p></div>
</td>
</tr>
<tr>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: right; width: 400px">Smaller:</td>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: left; width: 310px">
<div style="width: 65px">
<div style="width: 0px; height: 10px; border-left: 10px solid green; border-right: 40px solid red; margin-bottom: 1px; float: right"></div>
<div style="width: 0px; height: 10px; border-left: 10px solid green; border-right: 48px solid red; margin-bottom: 1px; float: right"></div>
<div style="width: 0px; height: 10px; border-left: 10px solid green; border-right: 50px solid red; margin-bottom: 1px; float: right"></div>
<div style="width: 0px; height: 10px; border-left: 10px solid green; border-right: 52px solid red; margin-bottom: 1px; float: right"></div>
<div style="width: 0px; height: 10px; border-left: 10px solid green; border-right: 54px solid red; margin-bottom: 1px; float: right"></div>
<div style="width: 0px; height: 10px; border-left: 10px solid green; border-right: 55px solid red; margin-bottom: 1px; float: right"></div>
<div style="width: 0px; height: 10px; border-left: 10px solid green; border-right: 55px solid red; margin-bottom: 1px; float: right"></div>
<div style="width: 0px; height: 10px; border-left: 10px solid green; border-right: 56px solid red; margin-bottom: 1px; float: right"></div>
</p></div>
</td>
</tr>
<tr>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: right; width: 400px">Smaller:</td>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: left; width: 310px">
<div style="width: 34px">
<div style="width: 0px; height: 5px; border-left: 5px solid green; border-right: 16px solid red; margin-bottom: 1px; float: right"></div>
<div style="width: 0px; height: 5px; border-left: 5px solid green; border-right: 20px solid red; margin-bottom: 1px; float: right"></div>
<div style="width: 0px; height: 5px; border-left: 5px solid green; border-right: 24px solid red; margin-bottom: 1px; float: right"></div>
<div style="width: 0px; height: 5px; border-left: 5px solid green; border-right: 26px solid red; margin-bottom: 1px; float: right"></div>
<div style="width: 0px; height: 5px; border-left: 5px solid green; border-right: 27px solid red; margin-bottom: 1px; float: right"></div>
<div style="width: 0px; height: 5px; border-left: 5px solid green; border-right: 27px solid red; margin-bottom: 1px; float: right"></div>
<div style="width: 0px; height: 5px; border-left: 5px solid green; border-right: 28px solid red; margin-bottom: 1px; float: right"></div>
<div style="width: 0px; height: 5px; border-left: 5px solid green; border-right: 28px solid red; margin-bottom: 1px; float: right"></div>
</p></div>
</td>
</tr>
<tr>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: right; width: 400px">One pixel high!</td>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: left; width: 310px">
<div style="width: 23px">
<div style="width: 0px; height: 1px; border-left: 1px solid green; border-right: 11px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid green; border-right: 13px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid green; border-right: 14px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid green; border-right: 15px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid green; border-right: 15px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid green; border-right: 16px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid green; border-right: 16px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid green; border-right: 16px solid red; margin-bottom: 0px; float: right"></div>
</p></div>
</td>
</tr>
<tr>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: right; width: 400px">Make the left border pastel red to soften the edge. Tweak the border widths to get the desired curve. Add a solid div on top:</td>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: left; width: 310px">
<div style="width: 21px">
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 8px solid #FF6060; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 10px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 12px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 14px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 15px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 16px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 17px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 17px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
</p></div>
</td>
</tr>
<tr>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: right; width: 400px">Four corners:</td>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: left; width: 310px">
<div style="width: 21px; float: left">
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 8px solid #FF6060; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 10px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 12px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 14px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 15px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 16px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 17px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 17px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
</p></div>
<div style="width: 21px; float: right">
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 8px solid #FF6060; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 10px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 12px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 14px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 15px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 16px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 17px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 17px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
</p></div>
<div style="width: 50px; height: 50px"></div>
<div>
<div style="width: 21px; float: left">
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 17px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 17px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 16px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 15px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 14px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 12px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 10px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 8px solid #FF6060; margin-bottom: 0px; float: right"></div>
</p></div>
<div style="width: 21px; float: right">
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 17px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 17px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 16px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 15px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 14px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 12px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 10px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 8px solid #FF6060; margin-bottom: 0px; float: left"></div>
</p></div>
</p></div>
</td>
</tr>
<tr>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: right; width: 400px">Connect the top and bottom with one div each:</td>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: left; width: 310px">
<div style="width: 21px; float: left;">
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 8px solid #FF6060; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 10px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 12px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 14px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 15px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 16px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 17px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 17px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
</p></div>
<div style="width: 21px; float: right">
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 8px solid #FF6060; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 10px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 12px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 14px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 15px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 16px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 17px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 17px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
</p></div>
<div style="border-style: solid; border-width: 1px 0px 10px 0px; border-color: red; border-top-color: #FF6060; margin: 0 12px 0 12px"></div>
<div style="width: 50px; height: 50px"></div>
<div>
<div style="width: 21px; float: left">
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 17px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 17px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 16px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 15px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 14px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 12px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 10px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 8px solid #FF6060; margin-bottom: 0px; float: right"></div>
</p></div>
<div style="width: 21px; float: right">
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 17px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 17px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 16px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 15px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 14px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 12px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 10px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 8px solid #FF6060; margin-bottom: 0px; float: left"></div>
</p></div>
<div style="border-style: solid; border-width: 10px 0px 1px 0px; border-color: red; border-bottom-color: #FF6060; margin: 0 12px 0 12px"></div>
</p></div>
</td>
</tr>
<tr>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: right; width: 400px">Add a div in the center where stuff goes:</td>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: left; width: 310px">
<div style="width: 21px; float: left;">
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 8px solid #FF6060; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 10px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 12px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 14px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 15px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 16px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 17px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 17px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
</p></div>
<div style="width: 21px; float: right">
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 8px solid #FF6060; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 10px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 12px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 14px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 15px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 16px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 17px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 17px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
</p></div>
<div style="border-style: solid; border-width: 1px 0px 10px 0px; border-color: red; border-top-color: #FF6060; margin: 0 12px 0 12px"></div>
<div style="height: 50px; border-width: 0 1px 0 1px; border-color: #FF6060; border-style: solid; background-color: red; margin-left: 1px; margin-right: 1px"></div>
<div>
<div style="width: 21px; float: left">
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 17px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 17px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 16px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 15px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 14px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 12px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 10px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 8px solid #FF6060; margin-bottom: 0px; float: right"></div>
</p></div>
<div style="width: 21px; float: right">
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 17px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 17px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 16px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 15px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 14px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 12px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 10px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 8px solid #FF6060; margin-bottom: 0px; float: left"></div>
</p></div>
<div style="border-style: solid; border-width: 10px 0px 1px 0px; border-color: red; border-bottom-color: #FF6060; margin: 0 12px 0 12px"></div>
</p></div>
</td>
</tr>
<tr>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: right; width: 400px">And the stuff doth go:</td>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: left; width: 310px">
<div style="width: 21px; float: left;">
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 8px solid #FF6060; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 10px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 12px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 14px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 15px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 16px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 17px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 17px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
</p></div>
<div style="width: 21px; float: right">
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 8px solid #FF6060; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 10px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 12px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 14px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 15px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 16px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 17px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 17px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
</p></div>
<div style="border-style: solid; border-width: 1px 0px 10px 0px; border-color: red; border-top-color: #FF6060; margin: 0 12px 0 12px"></div>
<div style="height: 50px; border-width: 0 1px 0 1px; border-color: #FF6060; border-style: solid; background-color: red; margin-left: 1px; margin-right: 1px">
<div style="text-align: center; color: white; font-size: 24pt; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold">
                        Yo mama!
                    </div>
</p></div>
<div>
<div style="width: 21px; float: left">
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 17px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 17px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 16px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 15px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 14px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 12px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 10px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 8px solid #FF6060; margin-bottom: 0px; float: right"></div>
</p></div>
<div style="width: 21px; float: right">
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 17px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 17px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 16px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 15px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 14px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 12px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 10px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 8px solid #FF6060; margin-bottom: 0px; float: left"></div>
</p></div>
<div style="border-style: solid; border-width: 10px 0px 1px 0px; border-color: red; border-bottom-color: #FF6060; margin: 0 12px 0 12px"></div>
</p></div>
</td>
</tr>
<tr>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: right; width: 400px">Broken apart to show some of the pieces:</td>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: left; width: 310px">
<div style="width: 21px; float: left;">
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 8px solid #FF6060; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 10px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 12px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 14px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 15px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 16px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 17px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 17px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
</p></div>
<div style="width: 21px; float: right">
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 8px solid #FF6060; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 10px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 12px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 14px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 15px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 16px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 17px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 17px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
</p></div>
<div style="border-style: solid; border-width: 1px 0px 10px 0px; border-color: red; border-top-color: #FF6060; margin: 0 30px 10px 30px"></div>
<div style="height: 50px; border-width: 0 1px 0 1px; border-color: #FF6060; border-style: solid; background-color: red; margin-left: 1px; margin-right: 1px">
<div style="text-align: center; color: white; font-size: 24pt; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold">
                        Yo mama!
                    </div>
</p></div>
<div>
<div style="width: 21px; float: left">
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 17px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 17px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 16px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 15px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 14px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 12px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 10px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 8px solid #FF6060; margin-bottom: 0px; float: right"></div>
</p></div>
<div style="width: 21px; float: right">
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 17px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 17px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 16px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 15px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 14px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 12px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 10px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 8px solid #FF6060; margin-bottom: 0px; float: left"></div>
</p></div>
<div style="border-style: solid; border-width: 10px 0px 1px 0px; border-color: red; border-bottom-color: #FF6060; margin: 10px 30px 0 30px"></div>
</p></div>
</td>
</tr>
<tr>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: right; width: 400px">These overlap nicely (at least in Safari, IE6, and Firefox), respect z-order, and work well with non-solid backgrounds.  They do not require images, tables, or JavaScript.</td>
<td style="border: solid; border-width: 1px; border-color: gray; text-align: left; background: url(/images/roundy/noisy.png);">
<div style="height: 130px; overflow: hidden;">
<div style="width: 21px; float: left;">
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 8px solid #FF6060; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 10px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 12px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 14px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 15px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 16px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 17px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 17px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
</p></div>
<div style="width: 21px; float: right">
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 8px solid #FF6060; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 10px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 12px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 14px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 15px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 16px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 17px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 17px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
</p></div>
<div style="border-style: solid; border-width: 1px 0px 10px 0px; border-color: red; border-top-color: #FF6060; margin: 0 12px 0 12px"></div>
<div style="height: 50px; border-width: 0 1px 0 1px; border-color: #FF6060; border-style: solid; background-color: red; margin-left: 1px; margin-right: 1px">
<div style="text-align: center; color: white; font-size: 24pt; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold">
						Yo mama!
					</div>
</p></div>
<div>
<div style="width: 21px; float: left">
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 18px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 17px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 17px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 16px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 15px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #FF6060; border-right: 14px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 12px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 10px solid red; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #FF6060; border-right: 8px solid #FF6060; margin-bottom: 0px; float: right"></div>
</p></div>
<div style="width: 21px; float: right">
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 18px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 17px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 17px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 16px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 15px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #FF6060; border-left: 14px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 12px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 10px solid red; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #FF6060; border-left: 8px solid #FF6060; margin-bottom: 0px; float: left"></div>
</p></div>
<div style="border-style: solid; border-width: 10px 0px 1px 0px; border-color: red; border-bottom-color: #FF6060; margin: 0 12px 0 12px"></div>
</p></div>
<div style="position: relative; left: 120px; bottom: 55px; width: 120px; margin-bottom: -40px">
<div style="width: 21px; float: left;">
<div style="width: 0px; height: 1px; border-left: 2px solid blue; border-right: 8px solid blue; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid blue; border-right: 10px solid blue; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid blue; border-right: 12px solid blue; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid blue; border-right: 14px solid blue; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid blue; border-right: 15px solid blue; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid blue; border-right: 16px solid blue; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid blue; border-right: 17px solid blue; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid blue; border-right: 17px solid blue; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid blue; border-right: 18px solid blue; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid blue; border-right: 18px solid blue; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid blue; border-right: 18px solid blue; margin-bottom: 0px; float: right"></div>
</p></div>
<div style="width: 21px; float: right">
<div style="width: 0px; height: 1px; border-right: 2px solid blue; border-left: 8px solid blue; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid blue; border-left: 10px solid blue; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid blue; border-left: 12px solid blue; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid blue; border-left: 14px solid blue; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid blue; border-left: 15px solid blue; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid blue; border-left: 16px solid blue; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid blue; border-left: 17px solid blue; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid blue; border-left: 17px solid blue; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid blue; border-left: 18px solid blue; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid blue; border-left: 18px solid blue; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid blue; border-left: 18px solid blue; margin-bottom: 0px; float: left"></div>
</p></div>
<div style="border-style: solid; border-width: 1px 0px 10px 0px; border-color: blue; border-top-color: blue; margin: 0 12px 0 12px"></div>
<div style="height: 40px; border-width: 0 1px 0 1px; border-color: blue; border-style: solid; background-color: blue; margin-left: 1px; margin-right: 1px">
<div style="text-align: center; color: white; font-size: 14pt; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold">
							No, yo mama!
						</div>
</p></div>
<div>
<div style="width: 21px; float: left">
<div style="width: 0px; height: 1px; border-left: 1px solid blue; border-right: 18px solid blue; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid blue; border-right: 18px solid blue; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid blue; border-right: 18px solid blue; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid blue; border-right: 17px solid blue; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid blue; border-right: 17px solid blue; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid blue; border-right: 16px solid blue; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid blue; border-right: 15px solid blue; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid blue; border-right: 14px solid blue; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid blue; border-right: 12px solid blue; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid blue; border-right: 10px solid blue; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid blue; border-right: 8px solid blue; margin-bottom: 0px; float: right"></div>
</p></div>
<div style="width: 21px; float: right">
<div style="width: 0px; height: 1px; border-right: 1px solid blue; border-left: 18px solid blue; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid blue; border-left: 18px solid blue; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid blue; border-left: 18px solid blue; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid blue; border-left: 17px solid blue; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid blue; border-left: 17px solid blue; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid blue; border-left: 16px solid blue; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid blue; border-left: 15px solid blue; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid blue; border-left: 14px solid blue; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid blue; border-left: 12px solid blue; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid blue; border-left: 10px solid blue; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid blue; border-left: 8px solid blue; margin-bottom: 0px; float: left"></div>
</p></div>
<div style="border-style: solid; border-width: 10px 0px 1px 0px; border-color: blue; border-bottom-color: blue; margin: 0 12px 0 12px"></div>
</p></div>
</p></div>
<div style="position: relative; left: 8px;  width: 120px; margin-top: 80px; bottom: 135px; margin-bottom: 0">
<div style="width: 21px; float: left;">
<div style="width: 0px; height: 1px; border-left: 2px solid green; border-right: 8px solid green; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid green; border-right: 10px solid green; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid green; border-right: 12px solid green; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid green; border-right: 14px solid green; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid green; border-right: 15px solid green; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid green; border-right: 16px solid green; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid green; border-right: 17px solid green; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid green; border-right: 17px solid green; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid green; border-right: 18px solid green; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid green; border-right: 18px solid green; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid green; border-right: 18px solid green; margin-bottom: 0px; float: right"></div>
</p></div>
<div style="width: 21px; float: right">
<div style="width: 0px; height: 1px; border-right: 2px solid green; border-left: 8px solid green; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid green; border-left: 10px solid green; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid green; border-left: 12px solid green; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid green; border-left: 14px solid green; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid green; border-left: 15px solid green; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid green; border-left: 16px solid green; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid green; border-left: 17px solid green; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid green; border-left: 17px solid green; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid green; border-left: 18px solid green; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid green; border-left: 18px solid green; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid green; border-left: 18px solid green; margin-bottom: 0px; float: left"></div>
</p></div>
<div style="border-style: solid; border-width: 1px 0px 10px 0px; border-color: green; border-top-color: green; margin: 0 12px 0 12px"></div>
<div style="height: 20px; border-width: 0 1px 0 1px; border-color: green; border-style: solid; background-color: green; margin-left: 1px; margin-right: 1px">
<div style="text-align: center; color: white; font-size: 12pt; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold">
							Touch&eacute;!
						</div>
</p></div>
<div>
<div style="width: 21px; float: left">
<div style="width: 0px; height: 1px; border-left: 1px solid green; border-right: 18px solid green; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid green; border-right: 18px solid green; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid green; border-right: 18px solid green; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid green; border-right: 17px solid green; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid green; border-right: 17px solid green; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid green; border-right: 16px solid green; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid green; border-right: 15px solid green; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid green; border-right: 14px solid green; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid green; border-right: 12px solid green; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid green; border-right: 10px solid green; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid green; border-right: 8px solid green; margin-bottom: 0px; float: right"></div>
</p></div>
<div style="width: 21px; float: right">
<div style="width: 0px; height: 1px; border-right: 1px solid green; border-left: 18px solid green; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid green; border-left: 18px solid green; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid green; border-left: 18px solid green; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid green; border-left: 17px solid green; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid green; border-left: 17px solid green; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid green; border-left: 16px solid green; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid green; border-left: 15px solid green; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid green; border-left: 14px solid green; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid green; border-left: 12px solid green; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid green; border-left: 10px solid green; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid green; border-left: 8px solid green; margin-bottom: 0px; float: left"></div>
</p></div>
<div style="border-style: solid; border-width: 10px 0px 1px 0px; border-color: green; border-bottom-color: green; margin: 0 12px 0 12px"></div>
</p></div>
</p></div>
</p></div>
</td>
</tr>
</table>
<p>
	Hey!  Flexible widths!</p>
<div style="margin-left: 15%; margin-right: 15%; margin-top: 10px">
<div style="width: 21px; float: left;">
<div style="width: 0px; height: 1px; border-left: 2px solid #67CAFF; border-right: 8px solid #67CAFF; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #67CAFF; border-right: 10px solid #009fff; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #67CAFF; border-right: 12px solid #009fff; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #67CAFF; border-right: 14px solid #009fff; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #67CAFF; border-right: 15px solid #009fff; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #67CAFF; border-right: 16px solid #009fff; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #67CAFF; border-right: 17px solid #009fff; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #67CAFF; border-right: 17px solid #009fff; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #67CAFF; border-right: 18px solid #009fff; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #67CAFF; border-right: 18px solid #009fff; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #67CAFF; border-right: 18px solid #009fff; margin-bottom: 0px; float: right"></div>
</p></div>
<div style="width: 21px; float: right">
<div style="width: 0px; height: 1px; border-right: 2px solid #67CAFF; border-left: 8px solid #67CAFF; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #67CAFF; border-left: 10px solid #009fff; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #67CAFF; border-left: 12px solid #009fff; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #67CAFF; border-left: 14px solid #009fff; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #67CAFF; border-left: 15px solid #009fff; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #67CAFF; border-left: 16px solid #009fff; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #67CAFF; border-left: 17px solid #009fff; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #67CAFF; border-left: 17px solid #009fff; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #67CAFF; border-left: 18px solid #009fff; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #67CAFF; border-left: 18px solid #009fff; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #67CAFF; border-left: 18px solid #009fff; margin-bottom: 0px; float: left"></div>
</p></div>
<div style="border-style: solid; border-width: 1px 0px 10px 0px; border-color: #009fff; border-top-color: #67CAFF; margin: 0 12px 0 12px"></div>
<div style="height: 50px; border-width: 0 1px 0 1px; border-color: #67CAFF; border-style: solid; background-color: #009fff; margin-left: 1px; margin-right: 1px">
<div style="text-align: center; color: white; font-size: 24pt; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold">
			Resize me!
		    </div>
</p></div>
<div>
<div style="width: 21px; float: left">
<div style="width: 0px; height: 1px; border-left: 1px solid #67CAFF; border-right: 18px solid #009fff; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #67CAFF; border-right: 18px solid #009fff; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #67CAFF; border-right: 18px solid #009fff; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #67CAFF; border-right: 17px solid #009fff; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #67CAFF; border-right: 17px solid #009fff; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #67CAFF; border-right: 16px solid #009fff; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #67CAFF; border-right: 15px solid #009fff; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 1px solid #67CAFF; border-right: 14px solid #009fff; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #67CAFF; border-right: 12px solid #009fff; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #67CAFF; border-right: 10px solid #009fff; margin-bottom: 0px; float: right"></div>
<div style="width: 0px; height: 1px; border-left: 2px solid #67CAFF; border-right: 8px solid #67CAFF; margin-bottom: 0px; float: right"></div>
</p></div>
<div style="width: 21px; float: right">
<div style="width: 0px; height: 1px; border-right: 1px solid #67CAFF; border-left: 18px solid #009fff; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #67CAFF; border-left: 18px solid #009fff; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #67CAFF; border-left: 18px solid #009fff; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #67CAFF; border-left: 17px solid #009fff; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #67CAFF; border-left: 17px solid #009fff; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #67CAFF; border-left: 16px solid #009fff; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #67CAFF; border-left: 15px solid #009fff; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 1px solid #67CAFF; border-left: 14px solid #009fff; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #67CAFF; border-left: 12px solid #009fff; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #67CAFF; border-left: 10px solid #009fff; margin-bottom: 0px; float: left"></div>
<div style="width: 0px; height: 1px; border-right: 2px solid #67CAFF; border-left: 8px solid #67CAFF; margin-bottom: 0px; float: left"></div>
</p></div>
<div style="border-style: solid; border-width: 10px 0px 1px 0px; border-color: #009fff; border-bottom-color: #67CAFF; margin: 0 12px 0 12px"></div>
</p></div>
</p></div>
<h3>But but but&#8230;</h3>
<ul>
<li>Isn&#8217;t that, like, a humongous flood of markup?  Won&#8217;t the page take a long time to download?</li>
</ul>
<p style="margin-left: 55px">
    	Well, I worried about that too.  But happily, all modern browsers support gzip compression &#8211; and boy does it work!  Even though my last posting with comments is 382 KB in size, you only need to download 19.8 KB (Mac users can look in Safari&#8217;s Activity Viewer for the download size).  So it ends up being smaller than, say, Yahoo!&#8217;s home page &#8211; and that&#8217;s just the HTML, not even counting all of its images.  As a result, the page loads quite fast.
    </p>
<ul>
<li>Well, don&#8217;t all those divs make the page unmaintainable?</li>
</ul>
<p style="margin-left: 55px">
    	Yeah, I suppose so.  I use a script to generate these boxes, so the source is readable to me &#8211; even if the generated HTML isn&#8217;t.
    </p>
]]></content:encoded>
			<wfw:commentRss>http://ridiculousfish.com/blog/archives/2009/06/01/roundy/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Buzz</title>
		<link>http://ridiculousfish.com/blog/archives/2007/04/25/buzz/</link>
		<comments>http://ridiculousfish.com/blog/archives/2007/04/25/buzz/#comments</comments>
		<pubDate>Thu, 26 Apr 2007 05:44:04 +0000</pubDate>
		<dc:creator>ridiculous_fish</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://ridiculousfish.com/blog/archives/2007/04/25/buzz/</guid>
		<description><![CDATA[Farewell, Buzz]]></description>
			<content:encoded><![CDATA[<p><a href="http://buzz.vox.com/library/post/leaving-apple.html">Buzz</a> is leaving Apple, has already left.  Buzz joined Apple two months after I joined, on the same team.  He was the first candidate I ever interviewed, although I can&#8217;t remember what I asked him.  Without Buzz&#8217;s encouragement and inspiration, I&#8217;d have never started this blog, and he was first to link to me.  So thank you and farewell, Buzz.</p>
<p>On another thread, I&#8217;ve fixed the <a href="/angband">Angband screensaver</a> to remember the last character it played with.  The user defaults had to be synchronized.  Thanks to everyone who pointed this out.</p>
]]></content:encoded>
			<wfw:commentRss>http://ridiculousfish.com/blog/archives/2007/04/25/buzz/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Angband</title>
		<link>http://ridiculousfish.com/blog/archives/2007/04/13/42/</link>
		<comments>http://ridiculousfish.com/blog/archives/2007/04/13/42/#comments</comments>
		<pubDate>Fri, 13 Apr 2007 09:57:38 +0000</pubDate>
		<dc:creator>ridiculous_fish</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://ridiculousfish.com/blog/archives/2007/04/13/42/</guid>
		<description><![CDATA[So Angband iss back - so fissh, the fish, raw and wriggling, he hasst brought its back!]]></description>
			<content:encoded><![CDATA[<p><img src="/angband/images/gollum.png" style="float: left"></p>
<p><b>Guest blogger: Gollum (Smeagol)</b></p>
<p>Where isst it?  So it iss back &#8211; so <i>fissh</i>, the fish, raw and wriggling, he hasst brought its back!</p>
<p>Angband, the Hells of Iron, yest, the ancient ASCII roguelike, child spawn of Moria and VMS, it iss here once more.  We wants it!</p>
<p>You hasst not seen it, Angband?  But perhapss you have seen one like it, the filthy, filthy <b>NetHack</b>, yess, full of such stupids and jokeses, and no Smeagol!  We hates it for ever!  But we loves Angband, yess, Angband has Smeagol and the fat hobbitses and yes, it has my preciouss!  We loves it, Angband!</p>
<p>We hass thought it lost.  When OS X was a little child and hungry for software, we knows how it calls to <i>fissh</i>.  We knows how <i>fissh</i> missed Angband, dearly missed Smeagol, yes.  And we knows how <i>fissh</i> stoles it, and worked his tricksy little magic, so he could go down into a Carbonized Angband on OS X.  And we knows <i>fissh</i> did, and it was easy, Carbon madess it simple.</p>
<p>But <i>fissh</i> hasst brought it to Cocoa, now, all fresh and it glitterses so subpixel pretty with Quartz, and resizesss so smooth, and animates with pretty graphics!  So precious to <i>fissh</i>.</p>
<p>And ssuch love for Angband so <i>fissh</i> made a <b>borg screensaveres</b>!  So now you can visits Smeagol and wring the filthy little neck of Saruman and murderes the fat Morgoth in your sleep!  The screensaveres, yes!</p>
<p><a href="/angband/">Goes there now</a>, fat hobbitses!  The <b>game</b> and the <b>screensaveres</b> and the <b>source codess</b>!  Angband need you, yess!</p>
<div style="width: 216px; height: 227px; margin-left: auto; margin-right: auto; text-align: center;">
<a href="/angband/"><img src="/angband/images/logo_bitty.png" style="border: none"></a><br />
<a href="/angband/">Angband for Mac OS X</a>
</div>
<p><b>Edit:</b> <i>fissh</i> has fixed the problem with the screensaveres needing Angband to have been launched first.  It should work no problems now.</p>
]]></content:encoded>
			<wfw:commentRss>http://ridiculousfish.com/blog/archives/2007/04/13/42/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Barrier</title>
		<link>http://ridiculousfish.com/blog/archives/2007/02/17/barrier/</link>
		<comments>http://ridiculousfish.com/blog/archives/2007/02/17/barrier/#comments</comments>
		<pubDate>Sun, 18 Feb 2007 05:24:04 +0000</pubDate>
		<dc:creator>ridiculous_fish</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://ridiculousfish.com/blog/archives/2007/02/17/barrier/</guid>
		<description><![CDATA[What's this multithreading thing everyone keeps talking about?]]></description>
			<content:encoded><![CDATA[<div style="background-color: #fff0cb; border: 1px solid #c0c030; font-size: larger; padding: 5px; margin: 5px;">
<p><img src="http://ridiculousfish.com/images/barrier/teraflop.jpg" style="float: right"></p>
<p>&quot;<a href="http://www.intel.com/pressroom/archive/releases/20060926corp_b.htm">That&#8217;s a lot of cores.</a>  <span style="color: #43174c">And while 80-core floating point monsters like that aren&#8217;t likely to show up in an iMac any time soon, multicore chips in multiprocessor computers are here today.  Single chip machines are so 2004.  Programmers better get crackin&#8217;.  The megahertz free ride is over &#8211; and we have work to do.&quot;</span></div>
<p>There, that&#8217;s the noisome little pitch everyone&#8217;s been spreading like so much thermal paste.  As if multiprocessing is something new!  But of course it&#8217;s not &#8211; heck, I remember Apple shipping dualies more than ten years ago, as the Power Macintosh 9500.  Multiprocessing is more <i>accessible</i> (read: cheaper) now, but it&#8217;s anything save new.  It&#8217;s been around long enough that we should have it figured out by now.</p>
<p>So what&#8217;s my excuse?  I admit it &#8211; I don&#8217;t get multiprocessing, not, you know, really get it, and that&#8217;s gone on long enough.  It&#8217;s time to get to the bottom of it &#8211; or if not to the bottom, at least deep enough that my ears start popping.</p>
<h3>Threadinology</h3>
<p>Where to start, where to start&#8230;well, let&#8217;s define our terms.  Ok, here&#8217;s the things I mean when I say the following, uh, things:</p>
<ul>
<li style="margin-bottom: 6px"><b>Threads</b> are just preemptively scheduled contexts of execution that share an address space.  But you already know what threads are.  Frankly, for my purposes, they&#8217;re all pretty much the same whether you&#8217;re using Objective-C or C++ or Java on Mac OS X or Linux or Windows&#8230;</li>
<li style="margin-bottom: 6px"><b>Threading</b> means creating multiple threads.  But you often create multiple threads for simpler control flow or to get around blocking system calls, not to improve performance through true simultaneous execution.
<li><b>Multithreading</b> is the physically simultaneous execution of multiple threads for increased performance, which requires a dualie or more.  Now things get hard.
</ul>
<p>Yeah, I know.  &quot;Multithreading is hard&quot; is a clich&eacute;, and it bugs me, because it is not some truism describing a fundamental property of nature, but it&#8217;s something <i>we did</i>.  We made multithreading hard because we optimized so heavily for the single threaded case.</p>
<p>What do I mean?  Well, processor speeds outrun memory so much that we started <b>guessing</b> at what&#8217;s in memory so the processor doesn&#8217;t have to waste time checking.  &quot;Guessing&quot; is a loaded term; a nicer phrase might be &quot;make increasingly aggressive assumptions&quot; about the state of memory.  And by &quot;we,&quot; I mean both the compiler and the processor &#8211; both make things hard, as we&#8217;ll see.  We&#8217;ll figure this stuff out &#8211; but they&#8217;re going to try to confuse us.  Oh well.  Right or wrong, this is the bed we&#8217;ve made, and now we get to lie in it.</p>
<div style="background-color: #fff2d4; font-size: 14px; border: 1px solid #909050; width: 180px; float: right; padding: 5px; margin: 5px;">
<div style="font-family: Monaco, Courier, mono; margin-left: 10px;">
while (1) {<br />
&nbsp;&nbsp;&nbsp;x++;<br />
&nbsp;&nbsp;&nbsp;y++;<br />
}</div>
<p></p>
<p><span style="color: #000080; font-size: 16px">x should always be at least as big as y, right?  Right?</span></div>
<p>Blah blah.  Let&#8217;s look at some code.  We have two variables, variable1 and variable2, that both start out at 0.  The writer thread will do this:</p>
<p><b>Writer thread</b></p>
<pre class="code">
while (1) {
   variable1++;
   variable2++;
}
</pre>
<p>They both started out at zero, so therefore variable1, at every point in time, will always be the same as variable2, or larger &#8211; but never smaller.  Right?</p>
<p>The reader thread will do this:</p>
<p><b>Reader thread</b></p>
<pre class="code">
while (1) {
   local2 = variable2;
   local1 = variable1;
   if (local2 > local1) {
      print("Error!");
   }
}
</pre>
<p>That&#8217;s odd &#8211; why does the reader thread load the second variable before the first?  That&#8217;s the <i>opposite</i> order from the writer thread!  But it makes sense if you think about it.</p>
<p>See, it&#8217;s possible that variable1 and/or variable2 will be incremented by the writer thread between the loads from the reader thread.  If variable2 gets incremented, that doesn&#8217;t matter &#8211; variable2 has already been read.  If variable1 gets incremented, then that makes variable1 appear larger.  So we conclude that variable2 should never be seen as larger than variable1, in the reader thread.  If we loaded variable1 before variable2, then variable2 might be incremented after the load of variable1, and we would see variable 2 as larger.</p>
<div style="background-color: #ffdbf5; border: 1px solid #FF6bA3; padding: 5px; margin: 5px;">
Analogy to the rescue.  Imagine some bratty kid playing polo while his uptight father looks on with a starched collar and monocle (which can only see one thing at a time, and not even very well, dontcha know).  The kid smacks the ball, and then gallops after it, and so on.  Now the squinting, snobby father first finds the ball, then looks around for his sproggen.  But!  If in the meantime, the kid hits it and takes off after it, Dad will find the kid ahead of where he first found the ball.  Dad doesn&#8217;t realize the ball has moved, and concludes that his budding athlete is ahead of the ball, and running the wrong way!  If, however, Dad finds the kid first, and <i>then</i> the ball, things will always appear in the right order, if not the right place.  The order of action (move ball, move kid) has to be the <i>opposite</i> from the order of observation (find kid, find ball).</div>
<p>
<h3>Threadalogy</h3>
<p>So we&#8217;ve got two threads operating on two variables, and we think we know what&#8217;ll happen.  Let&#8217;s try it out, on my G5:</p>
<p>
<pre class="code">
<span class="note">unsigned variable1 = 0;</span>
<span class="note">unsigned variable2 = 0;</span>

#define ITERATIONS 50000000

void *<span class="note">writer</span>(void *unused) {
        for (;;) {
<span class="note">                variable1 = variable1 + 1;
                variable2 = variable2 + 1;</span>
        }
}

void *<span class="note">reader</span>(void *unused) {
        struct timeval start, end;
        gettimeofday(&#038;start, NULL);
        unsigned i, failureCount = 0;
        for (i=0; i < ITERATIONS; i++) {
<span class="note">                unsigned v2 = variable2;
                unsigned v1 = variable1;
                if (v2 > v1) failureCount++;</span>
        }
        gettimeofday(&#038;end, NULL);
        double seconds = end.tv_sec + end.tv_usec / 1000000. - start.tv_sec - start.tv_usec / 1000000.;
        printf("%u failure%s (%2.1f percent of the time) in %2.1f seconds\n",
               failureCount, failureCount == 1 ? "" : "s",
               (100. * failureCount) / ITERATIONS, seconds);
        exit(0);
        return NULL;
}

int main(void) {
        pthread_t thread1, thread2;
        pthread_create(&#038;thread1, NULL, writer, NULL);
        pthread_create(&#038;thread2, NULL, reader, NULL);
        for (;;) sleep(1000000);
        return 0;
}
</pre>
<p>What do we get when we run this?</p>
<pre class="code">
fish ) ./a.out
0 failures (0.0 percent of the time) in 0.1 seconds
</pre>
<div style="background-color: #fff0cb; border: 1px solid #c0c030; width: 300px; float: right; padding: 5px; margin: 5px;">How do we know that the reader thread won&#8217;t see a variable in some intermediate state, midway through being updated?  We have to know that these particular operations are atomic.  On PowerPC and x86 machines, 32 bit writes to <i>aligned</i> addresses are guaranteed atomic.  Other types of memory accesses are not always atomic &#8211; in particular, 64 bit writes (say, of a double precision floating point value) on a 32 bit PowerPC are <i>not</i> atomic.  We have to check the documentation to know.</div>
<h3>So, we&#8217;re done?</h3>
<p>Our expectations were confirmed!  The writer thread ordered its writes so that the first variable would always be at least as big as the second, and the reader thread ordered its reads the opposite way to preserve that invariant, and everything worked as planned.</p>
<p>But we might just be getting lucky, right?  I mean, if thread1 and thread2 were always scheduled on the same processor, then we wouldn&#8217;t see any failures &#8211; a processor is always <i>self</i>-consistent with how it appears to order reads and writes.  In other words, a particular processor remembers where and what it pretends to have written, so if you read from that location <i>with that same processor</i>, you get what you expect.  It&#8217;s only when you read with processor1 from the same address where processor2 wrote &#8211; or pretended to write &#8211; that you might get into trouble.</p>
<p>So let&#8217;s try to force thread1 and thread2 to run on separate processors.  We can do that with the utilBindThreadToCPU() function, in the CHUD framework.  That function should never go in a shipping app, but it&#8217;s useful for debugging.  Here it is:</p>
<pre class="code">

void *writer(void *unused) {
        <span class="note">utilBindThreadToCPU(0);</span>
        for (;;) {
                variable1 = variable1 + 1;
                variable2 = variable2 + 1;
        }
}

void *reader(void *unused) {
        <span class="note">utilBindThreadToCPU(1);</span>
        struct timeval start, end;
        gettimeofday(&#038;start, NULL);
        ...

int main(void) {
        pthread_t thread1, thread2;
        <span class="note">chudInitialize();</span>
        unsigned variable2 = 0;
        pthread_create(&#038;thread1, NULL, writer, &#038;variable2);
        pthread_create(&#038;thread2, NULL, reader, &#038;variable2);
        while (1) sleep(1000000);
        return 0;
}
</pre>
<p>To run it:</p>
<pre class="code">
fish ) ./a.out
0 failures (0.0 percent of the time) in 0.1 seconds
</pre>
<h3>NOW are we done?</h3>
<p>Still no failures.  Hmm&#8230;  But wait &#8211; processors operate on cache lines, and variable1 and variable2 are right next to each other, so they probably share the same cache line &#8211; that is, they get brought in together and treated the same by each processor.  What if we separate them?  We&#8217;ll put one on the stack and leave the other where it is.</p>
<pre class="code">
unsigned variable1 = 0;

#define ITERATIONS 50000000

void *writer(<span class="note">unsigned *variable2</span>) {
        utilBindThreadToCPU(0);
        for (;;) {
                variable1 = variable1 + 1;
                <span class="note">*variable2 = *variable2 + 1;</span>
        }
        return NULL;
}

void *reader(<span class="note">unsigned *variable2</span>) {
        utilBindThreadToCPU(1);
        struct timeval start, end;
        gettimeofday(&#038;start, NULL);
        unsigned i;
        unsigned failureCount = 0;
        for (i=0; i < ITERATIONS; i++) {
                unsigned v2 = <span class="note">*variable2</span>;
                unsigned v1 = variable1;
                if (v2 > v1) failureCount++;
        }
        gettimeofday(&#038;end, NULL);
        double seconds = end.tv_sec + end.tv_usec / 1000000. - start.tv_sec - start.tv_usec / 1000000.;
        printf("%u failure%s (%2.1f percent of the time) in %2.1f seconds\n", failureCount, failureCount == 1 ? "" : "s", (100. * failureCount) / ITERATIONS, seconds);
        exit(0);
        return NULL;
}

int main(void) {
        pthread_t thread1, thread2;
        <span class="note">unsigned variable2 = 0;</span>
        chudInitialize();
        pthread_create(&#038;thread1, NULL, writer, <span class="note">&#038;variable2</span>);
        pthread_create(&#038;thread2, NULL, reader, <span class="note">&#038;variable2</span>);
        while (1) sleep(1000000);
        return 0;
}
</pre>
<p>So now, one variable is way high up on the stack and the other is way down low in the .data section.  Does this change anything?</p>
<pre class="code">
fish ) ./a.out
0 failures (0.0 percent of the time) in 0.1 seconds
</pre>
<p>Still nothing!   I&#8217;m not going to have an article after all!  Arrrghhh **BANG BANG BANG BANG**</p>
<pre class="code">
fish ) ./a.out
0 failures (0.0 percent of the time) in 0.1 seconds
fish ) ./a.out
0 failures (0.0 percent of the time) in 0.1 seconds
fish ) ./a.out
0 failures (0.0 percent of the time) in 0.1 seconds
fish ) ./a.out
50000000 failures (100.0 percent of the time) in 0.1 seconds
</pre>
<p>Hey, there it is!  Most of the time, every test passes, but that last time, every test failed.</p>
<h3>Our Enemy the Compiler</h3>
<p>The lesson here is something you already knew, but I&#8217;ll state it anyways:  <b>Multithreading bugs are very delicate.</b>  There is a real bug here, but it was masked by the fact that the kernel scheduled them on the same processor, and <i>then</i> by the fact that the variables were too close together in memory, and once those two issues were removed, (un)lucky timing usually masked the bug <i>anyways</i>.  In fact, if I didn&#8217;t know there was a bug there, I&#8217;d never have found it &#8211; and I <i>still</i> have my doubts!</p>
<p>So first of all, why would every test pass or every test fail?  If there&#8217;s a subtle timing bug, we&#8217;d expect most tests to pass, with a few failing &#8211; not all or nothing.  Let&#8217;s look at what gcc is giving us for the reader function:</p>
<pre class="code">
        lis r9,0x2fa
        ori r2,r9,61568
        mtctr r2
<span style="color: #0000CC">L8:
        bdnz L8</span>
        lis r2,0x2fa
        ori r2,r2,61568
        mullw r2,r0,r2
</pre>
<p>Hey!  The entire extent of that big long 50 million iteration loop has been hoisted out, leaving just the blue bits &#8211; essentially fifty million no-ops.  Instead of adding one or zero each time through the loop, it calculates the one or zero once, and then multiplies it by 50 million.</p>
<p>gcc is loading from variable1 and variable2 exactly once, and comparing them exactly once, and assuming their values do not change throughout the function &#8211; which would be a fine assumption if there weren&#8217;t also other threads manipulating those variables.</p>
<p>This is an example of what I mentioned above, about the compiler making things difficult by optimizing so aggressively for the single threaded case.</p>
<p>Well, you know the punchline &#8211; to stop gcc from optimizing aggressively, you use the volatile keyword.  So let&#8217;s do that:</p>
<pre class="code">
<span class="note">volatile</span> unsigned variable1 = 0;

#define ITERATIONS 50000000

void *writer(<span class="note">volatile</span> unsigned *variable2) {
        utilBindThreadToCPU(0);
        for (;;) {
                variable1 = variable1 + 1;
                *variable2 = *variable2 + 1;
        }
        return NULL;
}

void *reader(<span class="note">volatile</span> unsigned *variable2) {
        utilBindThreadToCPU(1);
        struct timeval start, end;
        ...
</pre>
<p>What does this change get us?</p>
<pre class="code">
fish ) ./a.out
12462711 failures (24.9 percent of the time) in 3.7 seconds
</pre>
<p>It&#8217;s much slower (expected, since volatile defeats optimizations), but more importantly, it fails intermittently instead of all or nothing.  Inspection of the assembly shows that gcc is generating the straightforward sequence of loads and stores that you&#8217;d expect.</p>
<h3>Our Enemy the Processor</h3>
<p>Is this really the cross-processor synchronization issues we&#8217;re trying to investigate?  We can find out by binding both threads to the same CPU:</p>
<pre class="code">

void *writer(unsigned *variable2) {
        <span class="note">utilBindThreadToCPU(0);</span>
        ...

void *reader(unsigned *variable2) {
        <span class="note">utilBindThreadToCPU(0);</span>
        ...
</pre>
<pre class="code">
fish ) ./a.out
0 failures (0.0 percent of the time) in 0.4 seconds
</pre>
<p>The tests pass all the time &#8211; this really is a cross-processor issue.</p>
<p>So somehow variable2 is becoming larger than variable1 even though variable1 is always incremented first.  How&#8217;s that possible?  It&#8217;s possible that the writer thread, on processor 0, is writing in the wrong order &#8211; it&#8217;s writing variable2 before variable1 even though we explicitly say to write variable1 first.  It&#8217;s also possible that the reader thread, on processor 1, is reading variable1 before variable 2, even though we tell it to do things in the opposite order.  In other words, the processors could be reading and writing those variables in any order they feel like instead of the order we tell them to.</p>
<div style="background-color: #ffdeb8; border: 1px solid #303030; width: 440px; float: right; padding: 5px; margin: 5px;">
<h3>Pop and Lock?</h3>
<p>What&#8217;s the usual response to cross-processor synchronization issues like this?  A mutex!  Let&#8217;s try it.</p>
<pre class="code">
fish ) ./a.out
0 failures (0.0 percent of the time) in 479.5 seconds
</pre>
<p>It made the tests pass, all right &#8211; but it was 130 times slower!  A spinlock does substantially better, at 20 seconds, but that&#8217;s still 440% worse than no locking &#8211; and spinlocks won&#8217;t scale.  Surely we can do better.
</p></div>
<h3>Even the kitchen</h3>
<p>Our problem is this: our processors are doing things in a different order than we tell them to, and not informing each other.  Each processor is only keeping track of its own shenanigans!  For shame!  We know of two super-horrible ways to fix this: force both threads onto the same CPU, which is a very bad idea, or to use a lock, which is a very slow idea.  So what&#8217;s the right way to make this work?</p>
<p>What we really want is a way to turn off the reordering for that particular sequence of loads and stores.  They don&#8217;t call it &quot;turn off reordering&quot;, of course, because that might imply that reordering is bad.  So instead they call it just plain &quot;ordering&quot;.  We want to order the reads and writes.  Ask and ye shall receive &#8211; the mechanism for that is called a &quot;memory barrier&quot;.</p>
<p>And boy, does the PowerPC have them.  I count at least three: sync, lwsync, and the hilariously named eieio.  Here&#8217;s what they do:</p>
<ul>
<li><b>sync</b> is the sledgehammer of the bunch &#8211; it orders all reads and writes, no matter what.  It works, but it&#8217;s slow.</li>
<li><b>lwsync</b> (for &quot;lightweight sync&quot;) is the newest addition.  It&#8217;s limited to plain ol&#8217; system memory, but it&#8217;s also faster than sync.
<li><b>eieio</b> (&quot;Enforce In-Order execution of I/O&quot;) is weird &#8211; it orders writes to &quot;device&quot; memory (like a memory mapped peripheral) and regular ol&#8217; system memory, but each separately.  We only care about system memory, and IBM says not to use eieio just for that.  Nevertheless, it should still order our reads and writes like we want.</li>
</ul>
<p>Because we&#8217;re not working with devices, lwsync is what we&#8217;re after.  Processor 0 is writing variable2 after variable1, so we&#8217;ll insert a memory barrier to prevent that:</p>
<div style="background-color: #d0ffdd; border: 1px solid #309030; width: 310px; float: right; padding: 5px; margin: 5px;">Do we need a memory barrier after the write to variable2 as well?  No &#8211; that would guard against the possibility of the <i>next</i> increment landing on variable1 <i>before</i> the <i>previous</i> increment hits variable2.  But the goal is to make sure that variable1 is larger than variable2, so it&#8217;s OK if that happens.</div>
<pre class="code">
volatile unsigned variable1 = 0;

<span class="note">#define barrier() __asm__ volatile ("lwsync")</span>

#define ITERATIONS 50000000

void *writer(volatile unsigned *variable2) {
        utilBindThreadToCPU(0);
        for (;;) {
                variable1 = variable1 + 1;
                <span class="note">barrier();</span>
                *variable2 = *variable2 + 1;
        }
        return NULL;
}
</pre>
<p>So!  Let&#8217;s run it!</p>
<pre class="code">
fish ) ./a.out
260 failures (0.0 percent of the time) in 0.9 seconds
</pre>
<p>So we reduced the failure count from 12462711 to 260.  Much better, but still not perfect.  Why are we still failing at times?  The answer, of course, is that just because processor 0 writes in the order we want is no guarantee that processor1 will read in the desired order.  Processor 1 may issue the reads in the wrong order, and processor 0 would write in between those two reads.  We need a memory barrier in the reader thread, to force the reads into the right order as well:</p>
<pre class="code">
void *reader(volatile unsigned *variable2) {
        struct timeval start, end;
        utilBindThreadToCPU(1);
        gettimeofday(&#038;start, NULL);
        unsigned i;
        unsigned failureCount = 0;
        for (i=0; i < ITERATIONS; i++) {
                unsigned v2 = *variable2;
                <span class="note">barrier();</span>
                unsigned v1 = variable1;
                if (v2 > v1) failureCount++;
        }
        gettimeofday(&#038;end, NULL);
        double seconds = end.tv_sec + end.tv_usec / 1000000. - start.tv_sec - start.tv_usec / 1000000.;
        printf("%u failure%s (%2.1f percent of the time) in %2.1f seconds\n",
               failureCount, failureCount == 1 ? "" : "s",
               (100. * failureCount) / ITERATIONS, seconds);
        exit(0);
        return NULL;
}
</pre>
<pre class="code">
fish ) ./a.out
0 failures (0.0 percent of the time) in 4.2 seconds
</pre>
<p>That did it!</p>
<p>The lesson here is that if you care about the order of reads or writes by one thread, it&#8217;s because you care about the order of writes or reads by <i>another</i> thread.  <i>Both</i> threads need a memory barrier.  <b>Memory barriers always come in pairs</b>, or triplets or more.  (Of course, if both threads are in the same function, there may only be one memory barrier that appears in your code &#8211; as long as both threads execute it.)</p>
<p>This should not come as a surprise: locks have the same behavior.  If only one thread ever locks, it&#8217;s not a very useful lock.</p>
<h3>31 Flavors</h3>
<p>What&#8217;s that?  You noticed that the PowerPC comes with three different kinds of memory barriers.  Right &#8211; as reads and writes get scheduled increasingly out of order, the more expensive it becomes to order them &#8211; so the PowerPC allows you to request various less expensive partial orderings, for performance.  Processors that  schedule I/O out of order more aggressively offer even more barrier flavors.  At the extreme end is the DEC Alpha, that sports read barriers with device memory ordering, read barriers without, write barriers with, write barriers without, page table barriers, and various birds in fruit trees.  The Alpha&#8217;s memory model guarantees so little that it is said to define the Linux kernel memory model &#8211; that is, the set of available barriers in the kernel source match the Alpha&#8217;s instruction set.  (Of course, many of them get compiled out when targetting a different processor.)</p>
<div style="background-color: #ffdbf5; font-size: 14px; border: 1px solid #ff6ba3; width: 240px; float: right; padding: 5px; margin: 5px;">Actually &#8211; and here my understanding is especially thin &#8211; while x86 is strongly ordered in general, I believe that Intel has managed to slip some weakly ordered operations in sideways, through the SIMD unit.  These are referred to as &quot;streaming&quot; or &quot;nontemporal&quot; instructions.  And when writing to specially tagged &quot;write combining&quot; memory, like, say, memory mapped VRAM, the rules are different still.</div>
<p>And on the other end, we have strongly ordered memory models that do very little reordering, like the &#8211; here it comes &#8211; x86.  No matter how many times I run that code, even on a double-dualie Intel Mac Pro, I never saw any failures.  Why not?  My understanding (and here it grows fuzzy) is that early multiprocessing setups were strongly ordered because modern reordering tricks weren&#8217;t that useful &#8211; memory was still pretty fast, y&#8217;know, relatively speaking, so there wasn&#8217;t much win to be had.  So developers blithely assumed the good times would never end, and we&#8217;ve been wearing the backwards compatibility shackles ever since.</p>
<p>But that doesn&#8217;t answer the question of why x86_64, y&#8217;know, the 64 bit x86 implementation in the Core 2s and all, isn&#8217;t more weakly ordered &#8211; or at least, reserve the <i>right</i> to be weaker.  That&#8217;s what IA64 &#8211; remember Itanium? &#8211; did: early models were strongly ordered, but the official memory model was weak, for future proofing.  Why didn&#8217;t AMD follow suit with x86_64?  My only guess (emphasis on <i>guess</i>) is that it was a way of jockeying for position against Itanium, when the 64 bit future for the x86 was still fuzzy.  AMD&#8217;s strongly ordered memory model means better compatibility and less hair-pulling when porting x86 software to 64 bit, and that made x86_64 more attractive compared to the Itanium. A pity, at least for Apple, since of course all of Apple&#8217;s software runs on the weak PowerPC &#8211; there&#8217;s no compatibility benefit to be had.  So it goes.  Is my theory right?</p>
<h3>Makin&#8217; a lock, checkin&#8217; it twice</h3>
<p>Ok!  I think we&#8217;re ready to take on that perennial bugaboo of Java programmers &#8211; the double checked lock.  How does it go again?  Let&#8217;s see it in Objective-C:</p>
<pre class="code">
+ getSharedObject {
    static id sharedObject;
    if (! sharedObject) {
        LOCK;
        if (! sharedObject) {
            sharedObject = [[self alloc] init];
        }
        UNLOCK;
    }
    return sharedObject;
}
</pre>
<p>What&#8217;s the theory?  We want to create a single shared object, exactly once, while preventing conflict between multiple threads.  The hope is that we can do a quick test to avoid taking the expensive lock.  If the static variable is set, which it will be most of the time, we can return the object immediately, without taking the lock.</p>
<div style="background-color: #e4e2ff; border: 1px solid #b297ff; width: 240px; float: right; padding: 5px; margin: 5px;">Sometimes memory barriers are needed to guard against past or future reads and writes that occur in, say, the function that&#8217;s <i>calling</i> your function.  Reordering can cross function and library boundaries!</div>
<p>This sounds good, but of course you already know it&#8217;s not.  Why not?  Well, if you&#8217;re creating this object, you&#8217;re probably initializing it in some way &#8211; at the very least, you&#8217;re setting its isa (class) pointer.  And then you&#8217;re turning around and writing it back to the sharedObject variable.  But these can happen in any order, as seen from another processor &#8211; so when the getSharedObject method is called from some other processor, it can see the sharedObject variable as set, and happily return the object <i>before its class pointer is even valid</i>.  Cripes.</p>
<p>But now you know we have the know-how to make this work, no?  How?  The problem is that we need to order the writes within the alloc and init methods relative to the write to the sharedObject variable &#8211; the alloc and init writes must come first, the write to sharedObject last.  So we store the object into a temporary local variable, insert a memory barrier, and then copy from the temporary to the shared object.  This time, I&#8217;ll use Apple&#8217;s portable memory barrier function:</p>
<pre class="code">
+ getSharedObject {
    static id sharedObject;
    if (! sharedObject) {
        LOCK;
        if (! sharedObject) {
            <span class="note">id temp</span> = [[self alloc] init];
            <span class="note">OSMemoryBarrier();</span>
            sharedObject = <span class="note">temp</span>;
        }
        UNLOCK;
    }
    return sharedObject;
}
</pre>
<p>There!  Now we&#8217;re guaranteed that the initializing thread really will write to sharedObject after the object is fully initialized.  All done.</p>
<p>Hmm?  Oh, nuts!  I forgot my rule &#8211; write barriers come in <i>pairs</i>.  If thread A initializes the object, it goes through a memory barrier, but if thread B then comes along, it will see the object and return it without any barrier at all.  Our rule tells us that something is wrong, but what?  Why&#8217;s that bad?</p>
<p>Well, thread B&#8217;s going to <i>do</i> something with the shared object, like send it a message, and that requires at the very least accessing the isa class pointer.  But we know the isa pointer really was written to memory first, before the sharedObject pointer, and thread B got ahold of the sharedObject pointer, so logically, the isa pointer should be written, right?  The laws of physics require it!  Isn&#8217;t that, like, you put an object in a box and hand it to me, and then I open the box to find that you haven&#8217;t put something into it yet!  It&#8217;s a temporal paradox!</p>
<p>The answer is that, yes, amazingly, dependent reads like that can be performed seemingly out of order, but not on any processors that Apple ships.  I&#8217;ve only heard of it happening in the &#8211; you guessed it &#8211; the Alpha.  Crazy, huh?</p>
<p>So where should the memory barrier go?  The goal is to order future reads &#8211; reads that occur after this sharedObject function returns &#8211; against the read from the sharedObject variable.  So it&#8217;s gotta go here:</p>
<pre class="code">
+ getSharedObject {
    static id sharedObject;
    if (! sharedObject) {
        LOCK;
        if (! sharedObject) {
            id temp = [[self alloc] init];
            OSMemoryBarrier();
            sharedObject = temp;
        }
        UNLOCK;
    }
    <span class="note">OSMemoryBarrier();</span>
    return sharedObject;
}
</pre>
<p>Now, this differs slightly from the <a href="http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html">usual solution</a>, which stores the static variable into a temporary in all cases.  However, for the life of me I can&#8217;t figure out why that&#8217;s necessary &#8211; the placement of the second memory barrier above seems correct to me, assuming the compiler doesn&#8217;t hoist the final read of sharedObject above the memory barrier (which it shouldn&#8217;t).  If I screwed it up, let me know how, please!</p>
<h3>Do we want it?</h3>
<p>That <i>second</i> memory barrier makes the double checked lock correct &#8211; but is it wise?  As we discussed, it&#8217;s not technically necessary on any machine you or I are likely to encounter.   And, after all, it does incur a real performance hit if we leave it in.  What to do?</p>
<p>The Linux kernel defines a set of fine-grained barrier macros that get compiled in or out appropriately (we would want a &quot;data dependency barrier&quot; in that case).  You could go that route, but my suggestion is to just leave a semi-standard comment to help you locate these places in the future.  That will help future-proof your code, but more importantly, it forces you to reason carefully about the threading issues, and to record your thoughts.  You&#8217;re more likely to get it right.</p>
<pre class="code">
+ getSharedObject {
    static id sharedObject;
    if (! sharedObject) {
        LOCK;
        if (! sharedObject) {
            id temp = [[self alloc] init];
            OSMemoryBarrier();
            sharedObject = temp;
        }
        UNLOCK;
    }
    <span class="note">/* data dependency memory barrier here */</span>
    return sharedObject;
}
</pre>
<div style="background-color: #ffdbf5; font-size: 14px; border: 1px solid #ff6ba3; width: 220px; float: right; padding: 5px; margin: 5px;">
<b>Wrapping up!</b><br />  Skimmers skip to here.
</div>
<h3>Now are we done?</h3>
<p>I think so, Mr. Subheading.  Let&#8217;s see if we can summarize all this:</p>
<div style="background-color: #A0A0B0; border: 1px solid #404040; text-align: center; width: 380px; float: right; padding: 5px; margin: 5px; margin-left: 15px">
<img src="http://ridiculousfish.com/images/barrier/stuck.jpg" style="margin-bottom: 5px; border: 1px solid #202020;"><br />
Locks are like tanks &#8211; powerful, slow, safe, expensive, and prone to getting you stuck.
</div>
<ul>
<li class="tall">The compiler and the processor both conspire to <b>defeat your threads</b> by moving your code around!  Be warned and wary!  You will have to do battle with both.</li>
<li class="tall">Even so, it is very easy to mask serious threading bugs.  We had to work hard, even in highly contrived circumstances, to get our bug to poke its head out even occasionally.</li>
<li class="tall">Ergo, <b>testing probably won&#8217;t catch</b> these types of bugs.  That makes it more important to get it right the first time.</li>
<li class="tall">Locks are the heavy tanks of threading tools &#8211; powerful, but slow and expensive, and if you&#8217;re not careful, you&#8217;ll get yourself stuck in a deadlock.  </li>
<li class="tall">Memory barriers are a faster, non-blocking, deadlock free alternative to locks. They take more thought, and aren&#8217;t always applicable, but your code&#8217;ll be faster and scale better.</li>
<li class="tall">Memory barriers <b>always come in logical pairs</b> or more.  Understanding where the second barrier has to go will help you reason about your code, even if that particular architecture doesn&#8217;t require a second barrier.</li>
</ul>
<h3>Further reading</h3>
<p>Seriously?  You want to know more?  Ok &#8211; the best technical source I know of is actually a document called &#8220;memory-barriers.txt&#8221; that comes with the Linux kernel source.  You can get it <a href="http://www.gelato.unsw.edu.au/lxr/source/Documentation/memory-barriers.txt">here</a>.  Thanks to my co-worker for finding it and directing me to it.</p>
<h3>Things I wanna know</h3>
<p>I&#8217;m still scratching my head about some things.  Maybe you can help me out.</p>
<ul>
<li>Why is x86_64 strongly ordered?  Is my theory about gaining a competitive edge over Itanium reasonable?</li>
<li>Is my double checked lock right, even though it doesn&#8217;t use a temporary variable in the usual place?</li>
<li>What&#8217;s up with the so-called &quot;nontemporal&quot; streaming instructions on x86?</li>
</ul>
<p>Leave a comment if you know!  Thanks!</p>
]]></content:encoded>
			<wfw:commentRss>http://ridiculousfish.com/blog/archives/2007/02/17/barrier/feed/</wfw:commentRss>
		<slash:comments>44</slash:comments>
		</item>
		<item>
		<title>Logos</title>
		<link>http://ridiculousfish.com/blog/archives/2006/12/11/logos/</link>
		<comments>http://ridiculousfish.com/blog/archives/2006/12/11/logos/#comments</comments>
		<pubDate>Mon, 11 Dec 2006 21:26:09 +0000</pubDate>
		<dc:creator>ridiculous_fish</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://ridiculousfish.com/blog/archives/2006/12/11/logos/</guid>
		<description><![CDATA[Hair care, or digital audio?]]></description>
			<content:encoded><![CDATA[<style type="text/css">
td.ahleft, td.ahright {
	padding-top: 10px;
	padding-bottom: 10px;
	padding-left: 10px;
	padding-right: 10px;
}</p>
<p>td.ahleft {
	text-align: right;
}</p>
<p>td.ahright {
	padding-top: 20px;
	font-size: 15pt;
	padding-left: 50px;
	line-height: 30px;
	text-align: left;
}</p>
<p>tr.aheven {
	background-color: #DDEEFF;
}</p>
<p>tr.ahodd {
	background-color: white;
}</p>
<p>img.ahproduct {
	vertical-align: middle;
}</p>
</style>
<div style="text-align: center">
<form method="post" action="/kay/results.php">
<table cellspacing=0 style="border-style: groove; margin-left: auto; margin-right: auto;">
<tr class="aheven">
<td class="ahleft"><img class="product" src="/images/hair/GreatClips.png"></td>
<td class="ahright">
<input type="radio" name="answers[0]" value="0">Hair care!<br />
<input type="radio" name="answers[0]" value="1">Digital audio!</td>
</tr>
<tr class="ahodd">
<td class="ahleft"><img class="product" src="/images/hair/Logics.png"></td>
<td class="ahright">
<input type="radio" name="answers[1]" value="0">Hair care!<br />
<input type="radio" name="answers[1]" value="1">Digital audio!</td>
</tr>
<tr class="aheven">
<td class="ahleft"><img class="product" src="/images/hair/LogicPro.png"></td>
<td class="ahright">
<input type="radio" name="answers[2]" value="0">Hair care!<br />
<input type="radio" name="answers[2]" value="1">Digital audio!</td>
</tr>
<tr class="ahodd">
<td class="ahleft"><img class="product" src="/images/hair/protools.jpg"></td>
<td class="ahright">
<input type="radio" name="answers[3]" value="0">Hair care!<br />
<input type="radio" name="answers[3]" value="1">Digital audio!</td>
</tr>
<tr class="aheven">
<td class="ahleft"><img class="product" src="/images/hair/Biolage.png"></td>
<td class="ahright">
<input type="radio" name="answers[4]" value="0">Hair care!<br />
<input type="radio" name="answers[4]" value="1">Digital audio!</td>
</tr>
<tr class="ahodd">
<td class="ahleft"><img class="product" src="/images/hair/Amplify.gif"></td>
<td class="ahright">
<input type="radio" name="answers[5]" value="0">Hair care!<br />
<input type="radio" name="answers[5]" value="1">Digital audio!</td>
</tr>
<tr class="aheven">
<td class="ahleft"><img class="product" src="/images/hair/live.png"></td>
<td class="ahright">
<input type="radio" name="answers[6]" value="0">Hair care!<br />
<input type="radio" name="answers[6]" value="1">Digital audio!</td>
</tr>
<tr class="ahodd">
<td class="ahleft"><img class="product" src="/images/hair/matrix.png"></td>
<td class="ahright">
<input type="radio" name="answers[7]" value="0">Hair care!<br />
<input type="radio" name="answers[7]" value="1">Digital audio!</td>
</tr>
<tr class="aheven">
<td class="ahleft"><img class="product" src="/images/hair/bias.png"></td>
<td class="ahright">
<input type="radio" name="answers[8]" value="0">Hair care!<br />
<input type="radio" name="answers[8]" value="1">Digital audio!</td>
</tr>
<tr class="ahodd">
<td class="ahleft"><img class="product" src="/images/hair/ashampoo.gif"></td>
<td class="ahright">
<input type="radio" name="answers[9]" value="0">Hair care!<br />
<input type="radio" name="answers[9]" value="1">Digital audio!</td>
</tr>
<tr class="aheven">
<td class="ahleft"><img class="product" src="/images/hair/waves.gif"></td>
<td class="ahright">
<input type="radio" name="answers[10]" value="0">Hair care!<br />
<input type="radio" name="answers[10]" value="1">Digital audio!</td>
</tr>
<tr class="ahodd">
<td class="ahleft"><img class="product" src="/images/hair/nexxus.png"></td>
<td class="ahright">
<input type="radio" name="answers[11]" value="0">Hair care!<br />
<input type="radio" name="answers[11]" value="1">Digital audio!</td>
</tr>
<tr class="aheven">
<td class="ahleft"><img class="product" src="/images/hair/pureology.png"></td>
<td class="ahright">
<input type="radio" name="answers[12]" value="0">Hair care!<br />
<input type="radio" name="answers[12]" value="1">Digital audio!</td>
</tr>
<tr class="ahodd">
<td class="ahleft"><img class="product" src="/images/hair/sonalksis.png"></td>
<td class="ahright">
<input type="radio" name="answers[13]" value="0">Hair care!<br />
<input type="radio" name="answers[13]" value="1">Digital audio!</td>
</tr>
</table>
<input type="submit" value="How did I do?">
</form>
</div>
]]></content:encoded>
			<wfw:commentRss>http://ridiculousfish.com/blog/archives/2006/12/11/logos/feed/</wfw:commentRss>
		<slash:comments>44</slash:comments>
		</item>
	</channel>
</rss>
