Initial revision
[fw/sdcc] / device / lib / _fsdiv.c
1 /*
2 ** libgcc support for software floating point.
3 ** Copyright (C) 1991 by Pipeline Associates, Inc.  All rights reserved.
4 ** Permission is granted to do *anything* you want with this file,
5 ** commercial or otherwise, provided this message remains intact.  So there!
6 ** I would appreciate receiving any updates/patches/changes that anyone
7 ** makes, and am willing to be the repository for said changes (am I
8 ** making a big mistake?).
9
10 Warning! Only single-precision is actually implemented.  This file
11 won't really be much use until double-precision is supported.
12
13 However, once that is done, this file might eventually become a
14 replacement for libgcc1.c.  It might also make possible
15 cross-compilation for an IEEE target machine from a non-IEEE
16 host such as a VAX.
17
18 If you'd like to work on completing this, please talk to rms@gnu.ai.mit.edu.
19
20
21 **
22 ** Pat Wood
23 ** Pipeline Associates, Inc.
24 ** pipeline!phw@motown.com or
25 ** sun!pipeline!phw or
26 ** uunet!motown!pipeline!phw
27 **
28 ** 05/01/91 -- V1.0 -- first release to gcc mailing lists
29 ** 05/04/91 -- V1.1 -- added float and double prototypes and return values
30 **                  -- fixed problems with adding and subtracting zero
31 **                  -- fixed rounding in truncdfsf2
32 **                  -- fixed SWAP define and tested on 386
33 */
34
35 /*
36 ** The following are routines that replace the libgcc soft floating point
37 ** routines that are called automatically when -msoft-float is selected.
38 ** The support single and double precision IEEE format, with provisions
39 ** for byte-swapped machines (tested on 386).  Some of the double-precision
40 ** routines work at full precision, but most of the hard ones simply punt
41 ** and call the single precision routines, producing a loss of accuracy.
42 ** long long support is not assumed or included.
43 ** Overall accuracy is close to IEEE (actually 68882) for single-precision
44 ** arithmetic.  I think there may still be a 1 in 1000 chance of a bit
45 ** being rounded the wrong way during a multiply.  I'm not fussy enough to
46 ** bother with it, but if anyone is, knock yourself out.
47 **
48 ** Efficiency has only been addressed where it was obvious that something
49 ** would make a big difference.  Anyone who wants to do this right for
50 ** best speed should go in and rewrite in assembler.
51 **
52 ** I have tested this only on a 68030 workstation and 386/ix integrated
53 ** in with -msoft-float.
54 */
55
56 /* the following deal with IEEE single-precision numbers */
57 #define EXCESS          126
58 #define SIGNBIT         ((unsigned long)0x80000000)
59 #define HIDDEN          (unsigned long)(1 << 23)
60 #define SIGN(fp)        ((fp >> (8*sizeof(fp)-1)) & 1)
61 #define EXP(fp)         (((fp) >> 23) & (unsigned int)0x00FF)
62 #define MANT(fp)        (((fp) & (unsigned long)0x007FFFFF) | HIDDEN)
63 #define PACK(s,e,m)     ((s) | ((e) << 23) | (m))
64
65 union float_long
66   {
67     float f;
68     long l;
69   };
70
71 /* divide two floats */
72 float
73 __fsdiv (float a1, float a2)
74 {
75   volatile union float_long fl1, fl2;
76   volatile long result;
77   volatile unsigned long mask;
78   volatile long mant1, mant2;
79   volatile int exp ;
80   short sign;
81
82   fl1.f = a1;
83   fl2.f = a2;
84
85   /* subtract exponents */
86   exp = EXP (fl1.l) ;
87   exp -= EXP (fl2.l);
88   exp += EXCESS;
89
90   /* compute sign */
91   sign = SIGN (fl1.l) ^ SIGN (fl2.l);
92
93   /* divide by zero??? */
94   if (!fl2.l)
95     /* return NaN or -NaN */
96     return (-1.0);
97
98   /* numerator zero??? */
99   if (!fl1.l)
100     return (0);
101
102   /* now get mantissas */
103   mant1 = MANT (fl1.l);
104   mant2 = MANT (fl2.l);
105
106   /* this assures we have 25 bits of precision in the end */
107   if (mant1 < mant2)
108     {
109       mant1 <<= 1;
110       exp--;
111     }
112
113   /* now we perform repeated subtraction of fl2.l from fl1.l */
114   mask = 0x1000000;
115   result = 0;
116   while (mask)
117     {
118       if (mant1 >= mant2)
119         {
120           result |= mask;
121           mant1 -= mant2;
122         }
123       mant1 <<= 1;
124       mask >>= 1;
125     }
126
127   /* round */
128   result += 1;
129
130   /* normalize down */
131   exp++;
132   result >>= 1;
133
134   result &= ~HIDDEN;
135
136   /* pack up and go home */
137   fl1.l = PACK (sign, (unsigned long) exp, result);
138   return (fl1.f);
139 }