<?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>OJ's rants &#187; CLR</title>
	<atom:link href="http://buffered.io/category/clr/feed/" rel="self" type="application/rss+xml" />
	<link>http://buffered.io</link>
	<description>What would OJ do?</description>
	<lastBuildDate>Tue, 06 Jul 2010 20:32:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Extended-Precision Floating-Point Values in the CLR</title>
		<link>http://buffered.io/2007/06/27/extended-precision-floating-point-values-in-the-clr/</link>
		<comments>http://buffered.io/2007/06/27/extended-precision-floating-point-values-in-the-clr/#comments</comments>
		<pubDate>Wed, 27 Jun 2007 02:54:07 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[CLR]]></category>
		<category><![CDATA[Tips/Tricks]]></category>
		<category><![CDATA[floating point]]></category>

		<guid isPermaLink="false">http://buffered.io/2007/06/27/extended-precision-floating-point-values-in-the-clr/</guid>
		<description><![CDATA[While at work today I hit a problem that I've never hit before (which is quite rare these days ), and while it was frustrating it was also good to learn about something that I never knew was a problem. If you're having some issues marshalling double-precision floating-point information through managed components to unmanaged components, [...]]]></description>
			<content:encoded><![CDATA[<p>While at work today I hit a problem that I've never hit before (which is quite rare these days <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> ), and while it was frustrating it was also good to learn about something that I never knew was a problem. If you're having some issues marshalling double-precision floating-point information through managed components to unmanaged components, or you're just interested in learning something new, then read on <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> </p>
<p>Let me start by explaining the scenario:</p>
<p>One of our <acronym title="Visual Basic v6">VB6</acronym> applications passes a variant array of 64-bit doubles to a managed C++ component. The managed code does a bit of munging, before passing the same information on to another component which is an <em>un</em>managed C++ DLL written and maintained by a third party. This second DLL is responsible for doing boolean operations on 3D solids. When this component is invoked, the application dies with a nice obscure "screwed-up memory" error. Unfortunately, since the component is closed-source, we needed to pass the information on to the author of the code and wait for a response.</p>
<p>After the author had investigated the problem he informed us that the reason for the crash was that the values that are being passed in are <a href="http://en.wikipedia.org/wiki/Extended_precision" title="Extended Precision">80-bit extended-precision</a> values, and not the expected <a href="http://en.wikipedia.org/wiki/IEEE_754" title="IEEE 754">IEEE</a> <a href="http://en.wikipedia.org/wiki/Double_precision" title="Double Precision">64-bit double-precision</a> values.</p>
<p>Wierd! That didn't make any sense to me. On hearing this I started to investigate where the process was falling down. The original data was 64-bit, so how come it <em>wasn't</em> by the time it hit the final DLL? Something must be converting the 64-bit double-precision values to their 80-bit extended-precision equivalents. I spent a fair bit of time trying to find out as much as I could about the various floating-point representations, and where they're used, but it took me an eternity to locate the information I was searching for.</p>
<p>In the deep, dark depths of the web, I stumbled across this little nugget of information (thanks to <a href="http://www.extremeoptimization.com/resources/Articles/FPDotNetConceptsAndFormats.aspx#standards" title="FP .NET Concepts and Formats">ExtremeOptimization.com</a>):<br />
<blockquote>
<p>... the 'extended' format, for which the IEC 60559 standard defines minimum specifications, and which is used by the floating-point unit on Intel processors, and is also <strong>used internally by the CLR</strong>.</p>
</blockquote>
<p>Ah ha! You bastards <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> So we're always dealing with 80-bit values when we use <em>double</em> in the <a href="http://en.wikipedia.org/wiki/Common_Language_Runtime" title="Common Language Runtime">CLR</a>.</p>
<p>Now that we know this, we can take steps to sort it out. In a nutshell, we need to set the <a href="http://en.wikipedia.org/wiki/Floating_point_unit" title="Floating Point Unit">FPU</a> state to force the use of 64-bit values before invoking the unmanaged component. Thankfully there's an API function that we can use to do this, it's called <a href="http://msdn2.microsoft.com/en-us/library/e9b52ceh(VS.80).aspx" title="_controlfp">_controlfp()</a>. In VS 2005, this function is deprecated, so we're forced to use the 'secure' equivalent _controlfp_s(). Here's how you do it:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;float.h&gt;</span>
.
.
<span style="color: #0000ff;">void</span> ManagedClass<span style="color: #008080;">::</span><span style="color: #007788;">ManagedFunction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">double</span> dooby<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">128</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
  .
  <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> prevState<span style="color: #008080;">;</span>
  <span style="color: #666666;">// set the FPU state to 64-bit</span>
  _controlfp_s<span style="color: #008000;">&#40;</span> <span style="color: #000040;">&amp;</span>prevState, _PC_53, MCW_PC<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  .
  <span style="color: #666666;">// invoke unmanaged DLL - these values wil be 64-bit, not 80-bit</span>
  ExternalFunction<span style="color: #008000;">&#40;</span> dooby <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  .
  <span style="color: #666666;">// reset FPU state to previous value</span>
  _controlfp_s<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">NULL</span>, prevState, MCW_PC<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>So there you have it. I hope this information is useful <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2007/06/27/extended-precision-floating-point-values-in-the-clr/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
