home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / MSB2IEEE.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  64 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /***  MSBIN conversion routines    ***/
  4. /***  public domain by Jeffery Foy ***/
  5.  
  6. #include "snipmath.h"
  7.  
  8. union Converter {
  9.       unsigned char uc[10];
  10.       unsigned int  ui[5];
  11.       unsigned long ul[2];
  12.       float          f[2];
  13.       double         d[1];
  14. };
  15.  
  16. /* MSBINToIEEE - Converts an MSBIN floating point number */
  17. /*               to IEEE floating point format           */
  18. /*                                                       */
  19. /*  Input: f - floating point number in MSBIN format     */
  20. /* Output: Same number in IEEE format                    */
  21.  
  22. float MSBINToIEEE(float f)
  23. {
  24.       union Converter t;
  25.       int sign, exp;       /* sign and exponent */
  26.  
  27.       t.f[0] = f;
  28.  
  29.       /* extract the sign & move exponent bias from 0x81 to 0x7f */
  30.  
  31.       sign = t.uc[2] / 0x80;
  32.       exp  = (t.uc[3] - 0x81 + 0x7f) & 0xff;
  33.  
  34.       /* reassemble them in IEEE 4 byte real number format */
  35.  
  36.       t.ui[1] = (t.ui[1] & 0x7f) | (exp << 7) | (sign << 15);
  37.       return t.f[0];
  38. } /* End of MSBINToIEEE */
  39.  
  40.  
  41. /* IEEEToMSBIN - Converts an IEEE floating point number  */
  42. /*               to MSBIN floating point format          */
  43. /*                                                       */
  44. /*  Input: f - floating point number in IEEE format      */
  45. /* Output: Same number in MSBIN format                   */
  46.  
  47. float IEEEToMSBIN(float f)
  48. {
  49.       union Converter t;
  50.       int sign, exp;       /* sign and exponent */
  51.  
  52.       t.f[0] = f;
  53.  
  54.       /* extract sign & change exponent bias from 0x7f to 0x81 */
  55.  
  56.       sign = t.uc[3] / 0x80;
  57.       exp  = ((t.ui[1] >> 7) - 0x7f + 0x81) & 0xff;
  58.  
  59.       /* reassemble them in MSBIN format */
  60.  
  61.       t.ui[1] = (t.ui[1] & 0x7f) | (sign << 7) | (exp << 8);
  62.       return t.f[0];
  63. } /* End of IEEEToMSBIN */
  64.