views:

430

answers:

2

Before .Net, before math coprocessors, before IEEE-574, Microsoft defined a bit pattern for floating-point numbers. Old versions of the C++ compiler happily used that definition.

I am writing a C# app that needs to read/write such floating-point numbers in a file. How can I do the conversions between the 2 bit formats? I need conversion methods in both directions.

This app is going to run in a PocketPC/WinCE environment.

Changing the structure of the file is out-of-scope for this project.

Is there a C++ compiler option that instructs it to use the old FP format? That would be ideal. I could then exchange data between the C# code and C++ code by using a null-terminated text string, and the C++ methods would be simple wrappers around sprintf and atof functions.

At the very least, I'm hoping someone can reply with the bit definitions for the old FP format, so I can put together a low-level bit manipulation algorithm if necessary.

Thanks.

+2  A: 

Based on Johannes's answer, you can go to http://support.microsoft.com/kb/140520 to download the source code for a conversion .dll from mbf to IEEE.

EDIT: Actually, that isn't helpful for you. But the actual MBF format is documented here: http://support.microsoft.com/kb/35826:

  -------------------------------------------------
 |              |    |                             |
 |8 Bit Exponent|Sign|   55 Bit Mantissa           |
 |              | Bit|                             |
  -------------------------------------------------
MSN
That link says that the source code is indeed available, but it requires the old version of the C++ compiler for proper storage of the FP variables. And I can't use the DLL for obvious reasons: this is supposed to run on a PocketPC/WinCE platform.
Richard Kucia
The Microsoft link leaves out important details, like the bias of the exponent and whether there's an implied "1" bit at the top of the mantissa.
Mark Ransom
@Mark, who would care about that? :) And yes, I upvoted your post.
MSN
@Richard: if you have access to MSDN subscriber downloads, you can get an old MSC compiler, Visual C++ 1.52 or 2.0, that may support the functions in question (`_fmsbintoieee()` and friends). They might even have source for the functions provided. I don't know for sure that they'll be useful, but it might be worth a look if you have access to them anyway.
Michael Burr
+2  A: 

I followed the bread crumbs from Johannes Rössel's Wikipedia link and found a Python implementation that shouldn't be too hard to translate: http://groups.google.com/group/comp.lang.python/browse_thread/thread/42150ccc20a1d8d5/4aadc71be8aeddbe

Here's the documentation of the format from Bengt Richter in that link:

According to an old MASM 5.0 programmer's guide, there was a Microsoft Binary format for encoding real numbers, both short (32 bits) and long (64 bits).

There were 3 parts:

  1. Biased 8-bit exponent in the highest byte (last in the little-endian view we've been using) It says the bias is 0x81 for short numbers and 0x401 for long, but I'm not sure where that lines up. I just got there by experimentation.

  2. Sign bit (0 for +, 1 for -) in upper bit of second highest byte.

  3. All except the first set bit of the mantissa in the remaining 7 bits of the second highest byte, and the rest of the bytes. And since the most signficant bit for non-zero numbers is 1, it is not represented. But if if were, it would share the same bit position where the sign is (that's why I or-ed it in there to complete the actual mantissa).

MASM also supported a 10-byte format similar to IEEE. I didn't see anything in that section on NaNs and INFs.

Mark Ransom