<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2.1" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Branchless selections</title>
	<link>http://realtimecollisiondetection.net/blog/?p=90</link>
	<description>Coding wisdom and rants of Christer Ericson</description>
	<pubDate>Fri, 10 Sep 2010 22:04:00 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.1</generator>

	<item>
		<title>By: EddieEdwards</title>
		<link>http://realtimecollisiondetection.net/blog/?p=90#comment-2734</link>
		<author>EddieEdwards</author>
		<pubDate>Wed, 13 May 2009 15:21:56 +0000</pubDate>
		<guid>http://realtimecollisiondetection.net/blog/?p=90#comment-2734</guid>
		<description>I like the XOR trick.  Very appropriate for SSE2.</description>
		<content:encoded><![CDATA[<p>I like the XOR trick.  Very appropriate for SSE2.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Branch free Clamp() &#171; Miles Macklin</title>
		<link>http://realtimecollisiondetection.net/blog/?p=90#comment-2624</link>
		<author>Branch free Clamp() &#171; Miles Macklin</author>
		<pubDate>Fri, 09 Jan 2009 23:05:27 +0000</pubDate>
		<guid>http://realtimecollisiondetection.net/blog/?p=90#comment-2624</guid>
		<description>[...] http://realtimecollisiondetection.net/blog/?p=90 [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;] <a href="http://realtimecollisiondetection.net/blog/?p=90" rel="nofollow">http://realtimecollisiondetection.net/blog/?p=90</a> [&#8230;]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tony</title>
		<link>http://realtimecollisiondetection.net/blog/?p=90#comment-2567</link>
		<author>tony</author>
		<pubDate>Mon, 15 Dec 2008 05:49:39 +0000</pubDate>
		<guid>http://realtimecollisiondetection.net/blog/?p=90#comment-2567</guid>
		<description>GoW3 teaser,

Neogaf has gone bizarre over the footage. Underwhelming is what they are saying, thanks to Jaffe's comment about art coming to life.
But I hope you guys will deliver it, no doubt.</description>
		<content:encoded><![CDATA[<p>GoW3 teaser,</p>
<p>Neogaf has gone bizarre over the footage. Underwhelming is what they are saying, thanks to Jaffe&#8217;s comment about art coming to life.<br />
But I hope you guys will deliver it, no doubt.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SolomonS</title>
		<link>http://realtimecollisiondetection.net/blog/?p=90#comment-2560</link>
		<author>SolomonS</author>
		<pubDate>Thu, 11 Dec 2008 05:23:57 +0000</pubDate>
		<guid>http://realtimecollisiondetection.net/blog/?p=90#comment-2560</guid>
		<description>Sorry, it got cut off.  Let's try again.

unsigned int _max(unsigned int x, unsigned int y)
{
unsigned int r = x - ((x-y) &#38; -(x </description>
		<content:encoded><![CDATA[<p>Sorry, it got cut off.  Let&#8217;s try again.</p>
<p>unsigned int _max(unsigned int x, unsigned int y)<br />
{<br />
unsigned int r = x - ((x-y) &amp; -(x</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SolomonS</title>
		<link>http://realtimecollisiondetection.net/blog/?p=90#comment-2559</link>
		<author>SolomonS</author>
		<pubDate>Thu, 11 Dec 2008 05:21:36 +0000</pubDate>
		<guid>http://realtimecollisiondetection.net/blog/?p=90#comment-2559</guid>
		<description>supzi, this is not my trick,  but it is branchless, since the cmp instruction isn't a branch.

unsigned int _max(unsigned int x, unsigned int y)
{
    unsigned int r = x - ((x - y) &#38; -(x </description>
		<content:encoded><![CDATA[<p>supzi, this is not my trick,  but it is branchless, since the cmp instruction isn&#8217;t a branch.</p>
<p>unsigned int _max(unsigned int x, unsigned int y)<br />
{<br />
    unsigned int r = x - ((x - y) &amp; -(x</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: supzi</title>
		<link>http://realtimecollisiondetection.net/blog/?p=90#comment-2558</link>
		<author>supzi</author>
		<pubDate>Wed, 10 Dec 2008 23:24:35 +0000</pubDate>
		<guid>http://realtimecollisiondetection.net/blog/?p=90#comment-2558</guid>
		<description>ooops, there's an error my previous post, this is how it should be :

const unsigned int uint_bit_mask = unsigned int(-1)/2;
uint first_bit_sign = sign( diff( r1 &#62;&#62; uint_bit_shift, r2 &#62;&#62; uint_bit_shift ) );
uint last_bits_sign = sign( diff( r1 &#38; uint_bit_mask, r2 &#38; uint_bit_mask ) );</description>
		<content:encoded><![CDATA[<p>ooops, there&#8217;s an error my previous post, this is how it should be :</p>
<p>const unsigned int uint_bit_mask = unsigned int(-1)/2;<br />
uint first_bit_sign = sign( diff( r1 &gt;&gt; uint_bit_shift, r2 &gt;&gt; uint_bit_shift ) );<br />
uint last_bits_sign = sign( diff( r1 &amp; uint_bit_mask, r2 &amp; uint_bit_mask ) );</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: supzi</title>
		<link>http://realtimecollisiondetection.net/blog/?p=90#comment-2557</link>
		<author>supzi</author>
		<pubDate>Wed, 10 Dec 2008 23:17:39 +0000</pubDate>
		<guid>http://realtimecollisiondetection.net/blog/?p=90#comment-2557</guid>
		<description>Solomon, 


If I can't use 64 bits ints or 33 bits, in other words there's no way to know if a subtraction did an overflow then I would consider splitting the 32 bits, check the first bit, then check the remaining bits, something like this :

const int uint_bit_shift = sizeof( uint )*8-1; 
const unsigned int uint_bit_mask = ~(1 &#62; uint_bit_shift, r2 &#62;&#62; uint_bit_shift ) );
uint last_bits_sign = sign( diff( r1 &#38; uint_bit_mask, r2 &#38; uint_bit_mask ) );
	
return branchless_sel(r1, r2, first_bit_sign &#124; last_bits_sign ); 

May be not the best solution ever but it is still branchless! 

I presume this is not the solution you are waiting for, perhaps you have a cool trick that you want to share?</description>
		<content:encoded><![CDATA[<p>Solomon, </p>
<p>If I can&#8217;t use 64 bits ints or 33 bits, in other words there&#8217;s no way to know if a subtraction did an overflow then I would consider splitting the 32 bits, check the first bit, then check the remaining bits, something like this :</p>
<p>const int uint_bit_shift = sizeof( uint )*8-1;<br />
const unsigned int uint_bit_mask = ~(1 &gt; uint_bit_shift, r2 &gt;&gt; uint_bit_shift ) );<br />
uint last_bits_sign = sign( diff( r1 &amp; uint_bit_mask, r2 &amp; uint_bit_mask ) );</p>
<p>return branchless_sel(r1, r2, first_bit_sign | last_bits_sign ); </p>
<p>May be not the best solution ever but it is still branchless! </p>
<p>I presume this is not the solution you are waiting for, perhaps you have a cool trick that you want to share?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: guardian</title>
		<link>http://realtimecollisiondetection.net/blog/?p=90#comment-2554</link>
		<author>guardian</author>
		<pubDate>Tue, 09 Dec 2008 12:04:05 +0000</pubDate>
		<guid>http://realtimecollisiondetection.net/blog/?p=90#comment-2554</guid>
		<description>Hello,

In today's applications, where would this trick find a use (apart from sprite code)?</description>
		<content:encoded><![CDATA[<p>Hello,</p>
<p>In today&#8217;s applications, where would this trick find a use (apart from sprite code)?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SolomonS</title>
		<link>http://realtimecollisiondetection.net/blog/?p=90#comment-2537</link>
		<author>SolomonS</author>
		<pubDate>Tue, 02 Dec 2008 08:31:59 +0000</pubDate>
		<guid>http://realtimecollisiondetection.net/blog/?p=90#comment-2537</guid>
		<description>supzi, can you think of a solution that doesn't require 64 bit ints?  or 33 bit ints for that matter.</description>
		<content:encoded><![CDATA[<p>supzi, can you think of a solution that doesn&#8217;t require 64 bit ints?  or 33 bit ints for that matter.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: supzi</title>
		<link>http://realtimecollisiondetection.net/blog/?p=90#comment-2536</link>
		<author>supzi</author>
		<pubDate>Mon, 01 Dec 2008 22:18:33 +0000</pubDate>
		<guid>http://realtimecollisiondetection.net/blog/?p=90#comment-2536</guid>
		<description>Sorry for the very late reply Solomon, 

I don't see why it is limited to a specific range, the solution itself is just pseudocode, you can always adapt it to your needs

Here's a c++ solution that is compatible with 32 bits unsigned int :

typedef long long int64;
typedef unsigned long long uint64;
typedef unsigned int uint;

int64 diff( uint a, uint b ) { return (int64( a ) - int64( b )); }
uint sign( int64 val ) { return uint( uint64( val ) &#62;&#62; ( (sizeof( uint64 ) * 8) -1 )); }
uint branchless_sel( uint r1, uint r2, uint val ) {  return r1*(1-val) + r2*val; }
uint max_branchless( uint r1, uint r2 ) { return branchless_sel(r1, r2, sign(diff(r1, r2)) ); }</description>
		<content:encoded><![CDATA[<p>Sorry for the very late reply Solomon, </p>
<p>I don&#8217;t see why it is limited to a specific range, the solution itself is just pseudocode, you can always adapt it to your needs</p>
<p>Here&#8217;s a c++ solution that is compatible with 32 bits unsigned int :</p>
<p>typedef long long int64;<br />
typedef unsigned long long uint64;<br />
typedef unsigned int uint;</p>
<p>int64 diff( uint a, uint b ) { return (int64( a ) - int64( b )); }<br />
uint sign( int64 val ) { return uint( uint64( val ) &gt;&gt; ( (sizeof( uint64 ) * 8) -1 )); }<br />
uint branchless_sel( uint r1, uint r2, uint val ) {  return r1*(1-val) + r2*val; }<br />
uint max_branchless( uint r1, uint r2 ) { return branchless_sel(r1, r2, sign(diff(r1, r2)) ); }</p>
]]></content:encoded>
	</item>
</channel>
</rss>
