<?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>சுதர்சன் சாந்தியப்பன் &#187; x86</title>
	<atom:link href="http://sudarsun.in/blog/tag/x86/feed/" rel="self" type="application/rss+xml" />
	<link>http://sudarsun.in/blog</link>
	<description>Dream of the Impossible™</description>
	<lastBuildDate>Sun, 20 May 2012 17:23:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Hash Overflow due to 64 bit upcasting</title>
		<link>http://sudarsun.in/blog/2011/10/hash-overflow-due-to-64-bit-upcasting/</link>
		<comments>http://sudarsun.in/blog/2011/10/hash-overflow-due-to-64-bit-upcasting/#comments</comments>
		<pubDate>Fri, 28 Oct 2011 12:36:24 +0000</pubDate>
		<dc:creator>sudarsun</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[Help]]></category>
		<category><![CDATA[KnowHow]]></category>
		<category><![CDATA[KnowWhat]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[32 bit]]></category>
		<category><![CDATA[64 bit]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[data types]]></category>
		<category><![CDATA[g++]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[hashing]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mint]]></category>
		<category><![CDATA[overflow]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[win32]]></category>
		<category><![CDATA[win64]]></category>
		<category><![CDATA[x64]]></category>
		<category><![CDATA[x86]]></category>

		<guid isPermaLink="false">http://sudarsun.in/blog/2011/10/hash-overflow-due-to-64-bit-upcasting/</guid>
		<description><![CDATA[&#160;&#160;&#160; Lately, I had to debug the following piece of code, where it caused overflow on the hash bucket design.&#160; The code worked perfectly on a Windows machine while compiled for Win32, but failed to work on a Linux Mint x64 machine.&#160; The code is listed below, which basically calculates hash value of an input [...]]]></description>
			<content:encoded><![CDATA[<div align="justify">&nbsp;&nbsp;&nbsp; Lately, I had to debug the following piece of code, where it caused overflow on the hash bucket design.&nbsp; The code worked perfectly on a Windows machine while compiled for Win32, but failed to work on a Linux Mint x64 machine.&nbsp; The code is listed below, which basically calculates hash value of an input 32 bit unsigned number, limiting the hash value to 2^10 (1Meg).</div>
<blockquote><p><font face="Courier New">hash = ( fpArray*2654404609 )&gt;&gt;12; // Calculate the hash and limit the value to 2^20 (1 Meg)</font></p></blockquote>
<div align="justify">&nbsp;&nbsp; When the input value for fpArray was 1724463449 (0x66C93959), the hash value generated was 1779068547 (0x6A0A6E83), which is more than (0x000FFFFF) to cause the hash bucket overflow.</div>
<blockquote><p> <font face="Courier New">unsigned hash = fpArray * 2654404609;<br /> hash = hash &gt;&gt; 12;</font></p></blockquote>
<div align="justify">&nbsp;&nbsp;&nbsp; When I rewrote the code like the above, the value of hash was 2800236889 (0xA6E83959).&nbsp; Upon shifting right by 12 yields 638651 (0x0009BEBB), which is the correct and expected hash value.</div>
<p>
<div align="justify">&nbsp;&nbsp;&nbsp; Overall, the first snippet of code appears to be correct.&nbsp; Do you see a problem there?&nbsp; I couldn&#8217;t find the issue, until I recalled the 32bit vs 64bit difference.&nbsp; If you carefully look at the multiplier 2654404609 (0x9E370001), although appears to be a valid 32 bit number, what is the default assignment of type to this number by the compiler?&nbsp; If it was assigned 64bits, what would happen to the results?&nbsp; To validate this, I changed the 2nd snippet as the following.</div>
<blockquote><p>    <font face="Courier New">unsigned long hash = (unsigned long)fpArray * 2654404609;<br />    hash = hash &gt;&gt; 12;<br />    unsigned h2 = (unsigned)hash;</font></p></blockquote>
<div align="justify">&nbsp;&nbsp;&nbsp; Now, when the input is the same 1724463449 (0x66C93959), the value of hash becomes 4577423727077636441 (0x3F8646A0A6E83959) and upon right shifting by 12 bits yields 1117535089618563 (0x0003F8646A0A6E83).  Followed by downcasting to unsigned yield 1779068547 (0x6A0A6E83). Bingo!</div>
<p>
<div align="justify">&nbsp;&nbsp;&nbsp; So, what is happening here? While performing (fpArray * 2654404609), the computation is upcasted to 64bit computation by the 64 bit compiler.&nbsp; So, what is the solution? Just put a &#8220;U&#8221; at the end of the constant.</div>
<blockquote><p><font face="Courier New">hash = ( fpArray*2654404609U )&gt;&gt;12; // Calculate the hash and limit the value to 2^20 (1 Meg)<br />(or)<br />const unsigned multipler = 2654404609; // here U suffix is not needed as the constant is explicitly made unsigned<br />hash = ( fpArray * multiplier ) &gt;&gt; 12;</font></p></blockquote>
<p>&nbsp;&nbsp;&nbsp; Now, the computation will happen with 32 bit numbers to get the expected outputs.</p>
<p><b>Lessons Learned here:</b>
<ol>
<li>While using constants, beware of the upcasting and downcasting. So use proper suffixes like U, L, F etc.</li>
<li>Instead of using constants directly in expressions, use them as constant variables.</li>
<li>Be conscious about the compiler type and the assumptions made by the compiler in different build modes.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://sudarsun.in/blog/2011/10/hash-overflow-due-to-64-bit-upcasting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Compile 32bit C/C++ Application in 64bit Linux</title>
		<link>http://sudarsun.in/blog/2009/10/compile-32bit-cc-application-in-64bit-linux/</link>
		<comments>http://sudarsun.in/blog/2009/10/compile-32bit-cc-application-in-64bit-linux/#comments</comments>
		<pubDate>Thu, 08 Oct 2009 12:59:19 +0000</pubDate>
		<dc:creator>sudarsun</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[KnowHow]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[-m32]]></category>
		<category><![CDATA[-m64]]></category>
		<category><![CDATA[32bit]]></category>
		<category><![CDATA[64bit]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[cross-compilation]]></category>
		<category><![CDATA[glibc]]></category>
		<category><![CDATA[libgcc]]></category>
		<category><![CDATA[libstdc++]]></category>
		<category><![CDATA[x64]]></category>
		<category><![CDATA[x86]]></category>

		<guid isPermaLink="false">http://sudarsun.in/blog/2009/10/compile-32bit-cc-application-in-64bit-linux/</guid>
		<description><![CDATA[To build (cross compilation) 32 bit C/C++ applications on 64 bit linux box, use &#8220;-m32&#8243; as a compiler argument.&#160; You would need the following 32bit libraries in place. Install glibc.i386, glibc-devel.i386 Install libgcc.i386 Install libstdc++.i386 #include &#60;cstdio&#62;#include &#60;iostream&#62;main(){&#160;&#160;&#160; std::cout &#60;&#60; &#8220;C++&#8221; &#60;&#60; std::endl;&#160;&#160;&#160; printf ( &#8220;long: %d\n&#8221;, sizeof(long) );&#160;&#160;&#160; printf ( &#8220;long long: %d\n&#8221;, sizeof(long [...]]]></description>
			<content:encoded><![CDATA[<p>To build (cross compilation) 32 bit C/C++ applications on 64 bit linux box, use &#8220;-m32&#8243; as a compiler argument.&nbsp; You would need the following 32bit libraries in place.
<ol>
<li>Install <b>glibc.i386</b>, <b>glibc-devel.i386</b></li>
<li>Install <b>libgcc.i386</b></li>
<li>Install <b>libstdc++.i386</b></li>
</ol>
<blockquote><p><font face="Courier New">#include &lt;cstdio&gt;<br />#include &lt;iostream&gt;<br />main()<br />{<br />&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; &#8220;C++&#8221; &lt;&lt; std::endl;<br />&nbsp;&nbsp;&nbsp; printf ( &#8220;long: %d\n&#8221;, sizeof(long) );<br />&nbsp;&nbsp;&nbsp; printf ( &#8220;long long: %d\n&#8221;, sizeof(long long) );<br />&nbsp;&nbsp;&nbsp; return 0;<br />}</font></p></blockquote>
<blockquote><p><b>To compile in native 64bit:</b><br /><font color="#000066">[sudar@tstsrv12 tmp]</font>$ g++ <font color="#990000"><b>-m64</b></font> -o out64 code.cpp</p></blockquote>
<blockquote><p><font color="#000066">[sudar@tstsrv12 tmp]</font>$ ./out64<br />C++<br />long: 8<br />long long: 8</p></blockquote>
<blockquote><p><font color="#000066">[sudar@tstsrv12 tmp]</font>$ ldd out64<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; linux-vdso.so.1 =&gt;&nbsp; (0x00007fffc43fe000)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; libstdc++.so.6 =&gt; /usr/lib64/libstdc++.so.6 (0x0000003e7be00000)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; libm.so.6 =&gt; /lib64/libm.so.6 (0&#215;0000000000601000)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; libgcc_s.so.1 =&gt; /lib64/libgcc_s.so.1 (0x0000003e79e00000)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; libc.so.6 =&gt; /lib64/libc.so.6 (0&#215;0000000000886000)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /lib64/ld-linux-x86-64.so.2 (0&#215;0000000000110000)</p></blockquote>
<p>
<blockquote><b>To compile in 32bit:</b><br /><font color="#000066">[sudar@tstsrv12 tmp]</font>$ g++ <font color="#990000"><b>-m32</b></font> -o out32 code.cpp</p></blockquote>
<p>
<blockquote><font color="#000066">[sudar@tstsrv12 tmp]</font>$ ./out32<br />C++<br />long: 4<br />long long: 8</p></blockquote>
<p>
<blockquote><font color="#000066">[sudar@tstsrv12 tmp]</font>$ ldd out32<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; linux-gate.so.1 =&gt;&nbsp; (0&#215;00130000)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; libstdc++.so.6 =&gt; /usr/lib/libstdc++.so.6 (0&#215;00133000)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; libm.so.6 =&gt; /lib/libm.so.6 (0&#215;00223000)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; libgcc_s.so.1 =&gt; /lib/libgcc_s.so.1 (0x0024c000)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; libc.so.6 =&gt; /lib/libc.so.6 (0x0025a000)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /lib/ld-linux.so.2 (0&#215;00110000)</p>
</blockquote>
<div class="zemanta-pixie"><img class="zemanta-pixie-img" alt="" src="http://img.zemanta.com/pixy.gif?x-id=396b62b8-86bd-8f41-b78b-d1bc6fa538b1" /></div>
]]></content:encoded>
			<wfw:commentRss>http://sudarsun.in/blog/2009/10/compile-32bit-cc-application-in-64bit-linux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  sudarsun.in/blog/tag/x86/feed/ ) in 0.30098 seconds, on May 22nd, 2012 at 10:12 pm UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on May 22nd, 2012 at 11:12 pm UTC -->
