<?xml version="1.0" encoding="UTF-8" standalone="yes"?><oembed><version><![CDATA[1.0]]></version><provider_name><![CDATA[The ryg blog]]></provider_name><provider_url><![CDATA[https://fgiesen.wordpress.com]]></provider_url><author_name><![CDATA[fgiesen]]></author_name><author_url><![CDATA[https://fgiesen.wordpress.com/author/fgiesen/]]></author_url><title><![CDATA[Carry-save adders and averaging bit-packed&nbsp;values]]></title><type><![CDATA[link]]></type><html><![CDATA[<p>Long time no update. Got a few things to write about, but I thought I&#8217;d start with a quick post on a simple trick that I&#8217;ve never seen documented in full: An interesting identity for addition of integer values is that <code>a + b = (a^b) + ((a&amp;b)&lt;&lt;1) = (a^b) + 2*(a&amp;b)</code>. You can find this on a lot of pages that collect bit-twiddling tricks. This is basically the reduction formula for a 2-input Carry-Save Adder (Google it if you don&#8217;t know what that is). The more interesting 3-input carry save adder has the reduction formula</p>
<pre>a + b + c = S + (C &lt;&lt; 1) where
S = a ^ b ^ c
C = (a &amp; b) | (a &amp; c) | (b &amp; c)</pre>
<p>In Hardware, they are very useful as building blocks for integer multipliers: note that there&#8217;s no + in the computation of S and C, and that the three input numbers are &#8220;compressed&#8221; into two outputs. This makes them very useful to compute all the intermediate sums in a binary multiplier without heaving to deal with carries &#8211; using CSAs, you can reduce any multiple-term addition into a lot of very simple constant-delay logic elements with only one fast adder (the &#8220;completion adder&#8221;) placed at the very end. In Software, there is nothing to be gained by this &#8211; basically all current architectures have all basic integer ALU operations running at the same (fast) speed. But the reduction formula is still interesting, for another reason: Say you want to compute (a+b)/2 without overflow. Then you can apply the above identity to get <code>(a+b)/2 = (a^b)/2 + (a&amp;b)</code> which is guaranteed to be overflow-free. This trick is also well-known; it does tend to come in handy every once in a while for SIMD code, since a lot of SIMD instruction sets have a limited set of operand sizes and no output carry bit functionality. Unpacking a 4&#215;32 bit SIMD register to 2 2&#215;64 bit regs just to do a sum is a pain and a big waste of time.</p>
<p>But often you want not (a+b)/2, but (a+b+1)/2, i.e. with a rounding bias added into the mix. This is one of the reasons for this blog-post, since I haven&#8217;t run across a description of this yet. You can use the three-input CSA reduction for this case. It simplifies down to: <code>(a+b+1)/2 = (a^b)/2 + ((a&amp;b) | ((a|b)&amp;1))</code> Not as neat as the variant without rounding bias, but still better than temporary widening if you don&#8217;t have a SIMD add-with-carry instruction.</p>
<p>I&#8217;ve got one more: Let&#8217;s go back to the first variant again. A nice property of this is that it works well with bit-packed values, like A8R8G8B8 pixels in a 32-bit word. All we need to do is to make sure that our division (shift) doesn&#8217;t erroneously spill into adjacent channels. But that&#8217;s easily remedied with another bit masking operation: </p>
<pre>avg_pixel_a8r8g8b8(a,b) = (((a^b) &gt;&gt; 1) &amp; 0x7f7f7f7f) + (a&amp;b)</pre>
<p>In fact, why limit ourselves to 32-bit values? It works just as well for two 32-bit pixels inside a 64-bit register, just use 0x7f7f7f7f7f7f7f7f as mask. Nor does it say anywhere that the fields all have to have to same size. The trick works just as well for uneven partitions like the 11:11:10 bit format often used to store vertex normals or other unit vectors, or R5G6B5 pixels. All that changes is the bit mask.</p>
<p>Finally, the bitpacked stuff and the rounding bias fix are orthogonal &#8211; you can do both at the same time.</p>
<p>Sure, this isn&#8217;t <em>terribly</em> useful in practice, but it&#8217;s come in handy for me a couple times over the past few years, and I think it&#8217;s just fundamentally too <em>cool</em> not to have it properly documented.</p>
<p><b>UPDATE</b>: As Charles pointed out in the comments, a better way to compute (a+b+1)/2 is to use <code>(a | b) - ((a ^ b) &gt;&gt; 1)</code> &#8211; just as cheap as the version without rounding bias, and you can do the same masking trick to get rid of carries where you don&#8217;t want them.</p>
]]></html></oembed>