315a04d618b85ecd3ae6cb213194ab6b0fb825c3
[fw/sdcc] / device / lib / pic16 / libsdcc / float / 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)
33 // reentrant
34 {
35   volatile union float_long fl1, fl2;
36   volatile unsigned long result;
37   volatile int exp;
38   char sign;
39   
40   fl1.f = a1;
41   fl2.f = a2;
42
43   if (!fl1.l || !fl2.l)
44     return (0);
45
46   /* compute sign and exponent */
47   sign = SIGN (fl1.l) ^ SIGN (fl2.l);
48   exp = EXP (fl1.l) - EXCESS;
49   exp += EXP (fl2.l);
50
51   fl1.l = MANT (fl1.l);
52   fl2.l = MANT (fl2.l);
53
54   /* the multiply is done as one 16x16 multiply and two 16x8 multiples */
55   result = (fl1.l >> 8) * (fl2.l >> 8);
56   result += ((fl1.l & (unsigned long) 0xFF) * (fl2.l >> 8)) >> 8;
57   result += ((fl2.l & (unsigned long) 0xFF) * (fl1.l >> 8)) >> 8;
58
59   if (result & SIGNBIT)
60     {
61       /* round */
62       result += 0x80;
63       result >>= 8;
64     }
65   else
66     {
67       /* round */
68       result += 0x40;
69       result >>= 7;
70       exp--;
71     }
72
73   result &= ~HIDDEN;
74
75   /* pack up and go home */
76   fl1.l = PACK (sign ? SIGNBIT : 0 , (unsigned long)exp, result);  
77   return (fl1.f);
78 }
79
80
81
82