* src/hc08/gen.c (genPlusIncr, genUminus, genMinusDec, genCmp,
[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 ** Pat Wood
11 ** Pipeline Associates, Inc.
12 ** pipeline!phw@motown.com or
13 ** sun!pipeline!phw or
14 ** uunet!motown!pipeline!phw
15 */
16
17 /* (c)2000/2001: hacked a little by johan.knol@iduna.nl for sdcc */
18
19 #include <float.h>
20
21 union float_long
22   {
23     float f;
24     unsigned long l;
25   };
26
27 /* multiply two floats */
28 float __fsmul (float a1, float a2) {
29   volatile union float_long fl1, fl2;
30   volatile unsigned long result;
31   volatile int exp;
32   char sign;
33   
34   fl1.f = a1;
35   fl2.f = a2;
36
37   if (!fl1.l || !fl2.l)
38     return (0);
39
40   /* compute sign and exponent */
41   sign = SIGN (fl1.l) ^ SIGN (fl2.l);
42   exp = EXP (fl1.l) - EXCESS;
43   exp += EXP (fl2.l);
44
45   fl1.l = MANT (fl1.l);
46   fl2.l = MANT (fl2.l);
47
48   /* the multiply is done as one 16x16 multiply and two 16x8 multiples */
49   result = (fl1.l >> 8) * (fl2.l >> 8);
50   result += ((fl1.l & (unsigned long) 0xFF) * (fl2.l >> 8)) >> 8;
51   result += ((fl2.l & (unsigned long) 0xFF) * (fl1.l >> 8)) >> 8;
52
53   if (result & SIGNBIT)
54     {
55       /* round */
56       result += 0x80;
57       result >>= 8;
58     }
59   else
60     {
61       /* round */
62       result += 0x40;
63       result >>= 7;
64       exp--;
65     }
66
67   result &= ~HIDDEN;
68
69   /* pack up and go home */
70   fl1.l = PACK (sign ? SIGNBIT : 0 , (unsigned long)exp, result);  
71   return (fl1.f);
72 }
73
74
75
76