title: 80-bit floating point and subnormal numbers
body: <p>I am trying to convert an 80-bit extended precision floating point number (in a buffer) to double.
  The buffer basically contains the content of an x87 register.</p>
  
  <p><a href="https://stackoverflow.com/questions/2963055/msvc-win32-convert-extended-precision-float-80-bit-to-double-64-bit">This question</a> helped me get started as I wasn't all that familiar with the IEEE standard.
  Anyway, I am struggling to find useful info on subnormal (or denormalized) numbers in the 80-bit format.
  What I know is that unlike float32 or float64 it doesn't have a hidden bit in the mantissa (no implied addition of 1.0), so one way to know if a number is normalized is to check if the highest bit in the mantissa is set. That leaves me with the following question:</p>
  
  <p>From what wikipedia tells me, float32 and float64 indicate a subnormal number with a (biased) exponent of 0 and a non-zero mantissa.</p>
  
  <ul>
  <li>What does that tell me in an 80-bit float?</li>
  <li>Can 80-bit floats with a mantissa &lt; 1.0 even have a non-zero exponent?</li>
  <li>Alternatively, can 80-bit floats with an exponent of 0 even have a mantissa >= 1.0?</li>
  </ul>
  
  <p>EDIT: I guess the question boils down to:</p>
  
  <p><strong>Can I expect the FPU to sanitize exponent and highest mantissa bit in x87 registers?</strong></p>
  
  <p>If not, what kind of number should the conversion result in? Should I ignore the exponent altogether in that case? Or is it qNaN?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I read the FPU section in the Intel manual (<em>Intel® 64 and IA-32 Architectures Software Developer’s Manual, Volume 1: Basic Architecture</em>) which was less scary than I had feared. As it turns out the following values are not defined:</p>
  
  <ul>
  <li>exponent == 0 + mantissa with the highest bit set</li>
  <li>exponent != 0 + mantissa without the highest bit set</li>
  </ul>
  
  <p>It doesn't mention if these values can appear in the wild, nor if they are internally converted.
  So I actually dusted off Ollydbg and manually set bits in the x87 registers.
  I crafted ST(0) to contain all bits set in the exponent and a mantissa of 0. Then I made it execute</p>
  
  <pre><code>FSTP QWORD [ESP]
  FLD QWORD [ESP]
  </code></pre>
  
  <p>The  value stored at <code>[ESP]</code> was converted to a signaling NaN.
  After the <code>FLD</code>, <code>ST(0)</code> contained a quiet NaN.</p>
  
  <p>I guess that answers my question. I accepted <em>J-16 SDiZ</em>'s solution because it's the most straight forward solution (although it doesn't explicitly explain some of the finer details).</p>
  
  <p>Anyway, case solved. Thanks, everybody.</p>