<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Playing with bits</title>
	<atom:link href="http://msinilo.pl/blog/?feed=rss2&#038;p=566" rel="self" type="application/rss+xml" />
	<link>http://msinilo.pl/blog/?p=566</link>
	<description>Random gamedev ramblings</description>
	<lastBuildDate>Sun, 05 Sep 2010 06:59:46 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: M</title>
		<link>http://msinilo.pl/blog/?p=566&#038;cpage=1#comment-19429</link>
		<dc:creator>M</dc:creator>
		<pubDate>Sun, 14 Feb 2010 12:44:58 +0000</pubDate>
		<guid isPermaLink="false">http://msinilo.pl/blog/?p=566#comment-19429</guid>
		<description>unsigned int mask = (m_start + size  &gt;= m_bottom)-1;</description>
		<content:encoded><![CDATA[<p>unsigned int mask = (m_start + size  &gt;= m_bottom)-1;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ed</title>
		<link>http://msinilo.pl/blog/?p=566&#038;cpage=1#comment-19376</link>
		<dc:creator>ed</dc:creator>
		<pubDate>Wed, 10 Feb 2010 20:13:19 +0000</pubDate>
		<guid isPermaLink="false">http://msinilo.pl/blog/?p=566#comment-19376</guid>
		<description>errata:
Mask is not reversed. OldStart equals m_start at the beginning, so instead of setting oldStart to m_start using a mask, we will clear to zero when condition is &quot;&quot;&quot;NOT&quot;&quot;&quot; true. Then we just mangle a bit with mask and size, and we clear size when cond = true.
And if I’m not wrong, then this will require one arithmetic less.</description>
		<content:encoded><![CDATA[<p>errata:<br />
Mask is not reversed. OldStart equals m_start at the beginning, so instead of setting oldStart to m_start using a mask, we will clear to zero when condition is &#8220;&#8221;"NOT&#8221;"&#8221; true. Then we just mangle a bit with mask and size, and we clear size when cond = true.<br />
And if I’m not wrong, then this will require one arithmetic less.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ed</title>
		<link>http://msinilo.pl/blog/?p=566&#038;cpage=1#comment-19375</link>
		<dc:creator>ed</dc:creator>
		<pubDate>Wed, 10 Feb 2010 20:11:58 +0000</pubDate>
		<guid isPermaLink="false">http://msinilo.pl/blog/?p=566#comment-19375</guid>
		<description>I took a bit different approach. 

unsigned char* oldStart = m_start;
m_start += size;
uint32 mask_sign = (m_start - m_bottom) &gt;&gt; 31;
oldStart &amp;= mask_sign;
size = (mask_sign ^ size) &amp; size;
m_start -= size;

Mask is not reversed. OldStart equals m_start at the beginning, so instead of setting oldStart to m_start using a mask, we will clear to zero when condition is true. Then we just mangle a bit with mask and size, and we clear size when cond = true.
And if I&#039;m not wrong, then this will require one arithmetic less.</description>
		<content:encoded><![CDATA[<p>I took a bit different approach. </p>
<p>unsigned char* oldStart = m_start;<br />
m_start += size;<br />
uint32 mask_sign = (m_start &#8211; m_bottom) &gt;&gt; 31;<br />
oldStart &amp;= mask_sign;<br />
size = (mask_sign ^ size) &amp; size;<br />
m_start -= size;</p>
<p>Mask is not reversed. OldStart equals m_start at the beginning, so instead of setting oldStart to m_start using a mask, we will clear to zero when condition is true. Then we just mangle a bit with mask and size, and we clear size when cond = true.<br />
And if I&#8217;m not wrong, then this will require one arithmetic less.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Arseny Kapoulkine</title>
		<link>http://msinilo.pl/blog/?p=566&#038;cpage=1#comment-19374</link>
		<dc:creator>Arseny Kapoulkine</dc:creator>
		<pubDate>Wed, 10 Feb 2010 18:21:20 +0000</pubDate>
		<guid isPermaLink="false">http://msinilo.pl/blog/?p=566#comment-19374</guid>
		<description>It&#039;s easy to make it 64-bit proof - just use intptr_t / uintptr_t instead of int/unsigned int and sizeof(intptr_t) * 8 - 1 instead of 31 (well, this assumes a 8-bit byte... :)</description>
		<content:encoded><![CDATA[<p>It&#8217;s easy to make it 64-bit proof &#8211; just use intptr_t / uintptr_t instead of int/unsigned int and sizeof(intptr_t) * 8 &#8211; 1 instead of 31 (well, this assumes a 8-bit byte&#8230; :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: admin</title>
		<link>http://msinilo.pl/blog/?p=566&#038;cpage=1#comment-19366</link>
		<dc:creator>admin</dc:creator>
		<pubDate>Wed, 10 Feb 2010 10:33:46 +0000</pubDate>
		<guid isPermaLink="false">http://msinilo.pl/blog/?p=566#comment-19366</guid>
		<description>Sander: almost portable now :) Will probably break with 64-bit pointers.</description>
		<content:encoded><![CDATA[<p>Sander: almost portable now :) Will probably break with 64-bit pointers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sander van Rossen</title>
		<link>http://msinilo.pl/blog/?p=566&#038;cpage=1#comment-19364</link>
		<dc:creator>Sander van Rossen</dc:creator>
		<pubDate>Wed, 10 Feb 2010 09:51:53 +0000</pubDate>
		<guid isPermaLink="false">http://msinilo.pl/blog/?p=566#comment-19364</guid>
		<description>Hmm.. not sure what happened to the above post, but it seems to have swallowed some text..

The essence of it all is the:
INTEGER_BITS = (sizeof(int) * 8),
INTEGER_MAX  = (1u &lt;&lt; (INTEGER_BITS - 1))

enum part</description>
		<content:encoded><![CDATA[<p>Hmm.. not sure what happened to the above post, but it seems to have swallowed some text..</p>
<p>The essence of it all is the:<br />
INTEGER_BITS = (sizeof(int) * 8),<br />
INTEGER_MAX  = (1u &lt;&lt; (INTEGER_BITS &#8211; 1))</p>
<p>enum part</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sander van Rossen</title>
		<link>http://msinilo.pl/blog/?p=566&#038;cpage=1#comment-19363</link>
		<dc:creator>Sander van Rossen</dc:creator>
		<pubDate>Wed, 10 Feb 2010 09:49:28 +0000</pubDate>
		<guid isPermaLink="false">http://msinilo.pl/blog/?p=566#comment-19363</guid>
		<description>enum IntegerTraits
{
  INTEGER_BITS = (sizeof(int) * 8),
  INTEGER_MAX  = (1u &lt;&gt; (INTEGER_BITS - 1);  
const unsigned int mask = ((unsigned int)INTEGER_MAX) - (sign - 1);  
oldStart = (unsigned char*)((size_t)m_start &amp; mask);  
m_start += size &amp; mask;

Not tested, but shouldn&#039;t this make it portable?</description>
		<content:encoded><![CDATA[<p>enum IntegerTraits<br />
{<br />
  INTEGER_BITS = (sizeof(int) * 8),<br />
  INTEGER_MAX  = (1u &lt;&gt; (INTEGER_BITS &#8211; 1);<br />
const unsigned int mask = ((unsigned int)INTEGER_MAX) &#8211; (sign &#8211; 1);<br />
oldStart = (unsigned char*)((size_t)m_start &amp; mask);<br />
m_start += size &amp; mask;</p>
<p>Not tested, but shouldn&#8217;t this make it portable?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: admin</title>
		<link>http://msinilo.pl/blog/?p=566&#038;cpage=1#comment-19361</link>
		<dc:creator>admin</dc:creator>
		<pubDate>Wed, 10 Feb 2010 08:25:34 +0000</pubDate>
		<guid isPermaLink="false">http://msinilo.pl/blog/?p=566#comment-19361</guid>
		<description>IIRC latest GCC is smart enough to use cmov__ whenever it can, I don&#039;t think MSVC will do it, though. It&#039;s not a &quot;real&quot; piece of code we&#039;re trying to optimize here, BTW, just a little brain exercise.
@Arseny: good call, I had a feeling my mask calculation looks a little bit too complicated.</description>
		<content:encoded><![CDATA[<p>IIRC latest GCC is smart enough to use cmov__ whenever it can, I don&#8217;t think MSVC will do it, though. It&#8217;s not a &#8220;real&#8221; piece of code we&#8217;re trying to optimize here, BTW, just a little brain exercise.<br />
@Arseny: good call, I had a feeling my mask calculation looks a little bit too complicated.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sopyer</title>
		<link>http://msinilo.pl/blog/?p=566&#038;cpage=1#comment-19359</link>
		<dc:creator>sopyer</dc:creator>
		<pubDate>Wed, 10 Feb 2010 07:37:46 +0000</pubDate>
		<guid isPermaLink="false">http://msinilo.pl/blog/?p=566#comment-19359</guid>
		<description>@Joshua

As far as I know x86 assembler has command cmov__ which is available from Pentium Pro processor family. But I am actually unaware about its support by compilers.</description>
		<content:encoded><![CDATA[<p>@Joshua</p>
<p>As far as I know x86 assembler has command cmov__ which is available from Pentium Pro processor family. But I am actually unaware about its support by compilers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joshua</title>
		<link>http://msinilo.pl/blog/?p=566&#038;cpage=1#comment-19356</link>
		<dc:creator>Joshua</dc:creator>
		<pubDate>Tue, 09 Feb 2010 23:21:59 +0000</pubDate>
		<guid isPermaLink="false">http://msinilo.pl/blog/?p=566#comment-19356</guid>
		<description>sopyer:

Actually, in many compilers (particularly targeting x86 and variants), using conditional assignment actually generates a branch instruction because short of a conditional move (which is only available on newer processors), branching is the fastest way to do it.  MIPS and PowerPC both have instructions to do this directly without a branch (slt for Set on Less Than).

Also, the conditional operator (?:) is no better than an if in terms of generating branching instructions.  It&#039;s just syntactic sugar.</description>
		<content:encoded><![CDATA[<p>sopyer:</p>
<p>Actually, in many compilers (particularly targeting x86 and variants), using conditional assignment actually generates a branch instruction because short of a conditional move (which is only available on newer processors), branching is the fastest way to do it.  MIPS and PowerPC both have instructions to do this directly without a branch (slt for Set on Less Than).</p>
<p>Also, the conditional operator (?:) is no better than an if in terms of generating branching instructions.  It&#8217;s just syntactic sugar.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
