Initial revision
[fw/sdcc] / device / lib / _fssub.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) & SIGNBIT)
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 /* the following deal with IEEE double-precision numbers */
66 #define EXCESSD         1022
67 #define HIDDEND         (1 << 20)
68 #define EXPD(fp)        (((fp.l.upper) >> 20) & 0x7FF)
69 #define SIGND(fp)       ((fp.l.upper) & SIGNBIT)
70 #define MANTD(fp)       (((((fp.l.upper) & 0xFFFFF) | HIDDEND) << 10) | \
71                                 (fp.l.lower >> 22))
72
73 union float_long
74   {
75     float f;
76     long l;
77   };
78
79 /* subtract two floats */
80 float
81 __fssub (float a1, float a2)
82 {
83   volatile union float_long fl1, fl2;
84
85   fl1.f = a1;
86   fl2.f = a2;
87
88   /* check for zero args */
89   if (!fl2.l)
90     return (fl1.f);
91   if (!fl1.l)
92     return (-fl2.f);
93
94   /* twiddle sign bit and add */
95   fl2.l ^= SIGNBIT;
96   return fl1.f + fl2.f; 
97 }