File: msb2ieee.c

package info (click to toggle)
c-cpp-reference 2.0.2-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 8,016 kB
  • ctags: 4,612
  • sloc: ansic: 26,960; sh: 11,014; perl: 1,854; cpp: 1,324; asm: 1,239; python: 258; makefile: 119; java: 77; awk: 34; csh: 9
file content (59 lines) | stat: -rwxr-xr-x 1,772 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/***  MSBIN conversion routines    ***/
/***  public domain by Jeffery Foy ***/

union Converter {
      unsigned char uc[10];
      unsigned int  ui[5];
      unsigned long ul[2];
      float          f[2];
      double         d[1];
};

/* MSBINToIEEE - Converts an MSBIN floating point number */
/*               to IEEE floating point format           */
/*                                                       */
/*  Input: f - floating point number in MSBIN format     */
/* Output: Same number in IEEE format                    */

float MSBINToIEEE(float f)
{
      union Converter t;
      int sign, exp;       /* sign and exponent */

      t.f[0] = f;

      /* extract the sign & move exponent bias from 0x81 to 0x7f */

      sign = t.uc[2] / 0x80;
      exp  = (t.uc[3] - 0x81 + 0x7f) & 0xff;

      /* reassemble them in IEEE 4 byte real number format */

      t.ui[1] = (t.ui[1] & 0x7f) | (exp << 7) | (sign << 15);
      return t.f[0];
} /* End of MSBINToIEEE */


/* IEEEToMSBIN - Converts an IEEE floating point number  */
/*               to MSBIN floating point format          */
/*                                                       */
/*  Input: f - floating point number in IEEE format      */
/* Output: Same number in MSBIN format                   */

float IEEEToMSBIN(float f)
{
      union Converter t;
      int sign, exp;       /* sign and exponent */

      t.f[0] = f;

      /* extract sign & change exponent bias from 0x7f to 0x81 */

      sign = t.uc[3] / 0x80;
      exp  = ((t.ui[1] >> 7) - 0x7f + 0x81) & 0xff;

      /* reassemble them in MSBIN format */

      t.ui[1] = (t.ui[1] & 0x7f) | (sign << 7) | (exp << 8);
      return t.f[0];
} /* End of IEEEToMSBIN */