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?).
10 Warning! Only single-precision is actually implemented. This file
11 won't really be much use until double-precision is supported.
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
18 If you'd like to work on completing this, please talk to rms@gnu.ai.mit.edu.
23 ** Pipeline Associates, Inc.
24 ** pipeline!phw@motown.com or
25 ** sun!pipeline!phw or
26 ** uunet!motown!pipeline!phw
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
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.
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.
52 ** I have tested this only on a 68030 workstation and 386/ix integrated
53 ** in with -msoft-float.
56 /* the following deal with IEEE single-precision numbers */
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))
65 /* the following deal with IEEE double-precision numbers */
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) | \
79 /* multiply two floats */
81 __fsmul (float a1, float a2)
83 volatile union float_long fl1, fl2;
84 volatile unsigned long result;
94 /* compute sign and exponent */
95 sign = SIGN (fl1.l) ^ SIGN (fl2.l);
96 exp = EXP (fl1.l) - EXCESS;
100 fl2.l = MANT (fl2.l);
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;
107 if (result & (unsigned long)0x80000000)
123 /* pack up and go home */
124 fl1.l = PACK (sign ? ((unsigned long) 0x80000000) : 0 , (unsigned long)exp, result);