short (8 bit) replaced by char
[fw/sdcc] / device / lib / _fsmul.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 /* 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 /* multiply two floats */
80 float
81 __fsmul (float a1, float a2)
82 {
83   volatile union float_long fl1, fl2;
84   volatile unsigned long result;
85   volatile int exp;
86   char sign;
87
88   fl1.f = a1;
89   fl2.f = a2;
90
91   if (!fl1.l || !fl2.l)
92     return (0);
93
94   /* compute sign and exponent */
95   sign = SIGN (fl1.l) ^ SIGN (fl2.l);
96   exp = EXP (fl1.l) - EXCESS;
97   exp += EXP (fl2.l);
98
99   fl1.l = MANT (fl1.l);
100   fl2.l = MANT (fl2.l);
101
102   /* the multiply is done as one 16x16 multiply and two 16x8 multiples */
103   result = (fl1.l >> 8) * (fl2.l >> 8);
104   result += ((fl1.l & (unsigned long) 0xFF) * (fl2.l >> 8)) >> 8;
105   result += ((fl2.l & (unsigned long) 0xFF) * (fl1.l >> 8)) >> 8;
106
107   if (result & (unsigned long)0x80000000)
108     {
109       /* round */
110       result += 0x80;
111       result >>= 8;
112     }
113   else
114     {
115       /* round */
116       result += 0x40;
117       result >>= 7;
118       exp--;
119     }
120
121   result &= ~HIDDEN;
122
123   /* pack up and go home */
124   fl1.l = PACK (sign ? ((unsigned long) 0x80000000) : 0 , (unsigned long)exp, result);  
125   return (fl1.f);
126 }