34279826c506b6bfcb71013bf558d6408f3db849
[fw/sdcc] / device / lib / pic / libsdcc / 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 /*
18 ** $Id$
19 */
20
21 /* (c)2000/2001: hacked a little by johan.knol@iduna.nl for sdcc */
22
23 #include <float.h>
24
25 union float_long
26   {
27     float f;
28     unsigned long l;
29   };
30
31 /* multiply two floats */
32 float __fsmul (float a1, float a2) _FS_REENTRANT
33 {
34   FS_STATIC volatile union float_long fl1, fl2;
35   unsigned long result;
36   int exp;
37   char sign;
38   
39   fl1.f = a1;
40   fl2.f = a2;
41
42   if (!fl1.l || !fl2.l)
43     return (0);
44
45   /* compute sign and exponent */
46   sign = SIGN (fl1.l) ^ SIGN (fl2.l);
47   exp = EXP (fl1.l) - EXCESS;
48   exp += EXP (fl2.l);
49
50   fl1.l = MANT (fl1.l);
51   fl2.l = MANT (fl2.l);
52
53   /* the multiply is done as one 16x16 multiply and two 16x8 multiples */
54   result = (fl1.l >> 8) * (fl2.l >> 8);
55   result += ((fl1.l & (unsigned long) 0xFF) * (fl2.l >> 8)) >> 8;
56   result += ((fl2.l & (unsigned long) 0xFF) * (fl1.l >> 8)) >> 8;
57
58   if (0 != (result & SIGNBIT))
59     {
60       /* round */
61       result += 0x80;
62       result >>= 8;
63     }
64   else
65     {
66       /* round */
67       result += 0x40;
68       result >>= 7;
69       exp--;
70     }
71
72   result &= ~HIDDEN;
73
74   /* pack up and go home */
75   if (exp >= 0x100)
76     fl1.l = (sign ? SIGNBIT : 0) | 0x7F800000;
77   else if (exp < 0)
78     fl1.l = 0;
79   else
80     fl1.l = PACK (sign ? SIGNBIT : 0 , exp, result);
81   return (fl1.f);
82 }
83
84
85
86