* device/lib/pic/libsdcc/fs2ulong.c, device/lib/pic/libsdcc/fsadd.c,
[fw/sdcc] / device / lib / pic / libsdcc / fsdiv.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     long l;
29   };
30
31 /* divide two floats */
32 float __fsdiv (float a1, float a2) _FS_REENTRANT
33 {
34   volatile union float_long fl1, fl2;
35   long result;
36   unsigned long mask;
37   long mant1, mant2;
38   int exp;
39   char sign;
40
41   fl1.f = a1;
42   fl2.f = a2;
43
44   /* subtract exponents */
45   exp = EXP (fl1.l) ;
46   exp -= EXP (fl2.l);
47   exp += EXCESS;
48
49   /* compute sign */
50   sign = SIGN (fl1.l) ^ SIGN (fl2.l);
51
52   /* divide by zero??? */
53   if (!fl2.l)
54     {/* return NaN or -NaN */
55       fl2.l = 0x7FC00000;
56       return (fl2.f);
57     }
58
59   /* numerator zero??? */
60   if (!fl1.l)
61     return (0);
62
63   /* now get mantissas */
64   mant1 = MANT (fl1.l);
65   mant2 = MANT (fl2.l);
66
67   /* this assures we have 25 bits of precision in the end */
68   if (mant1 < mant2)
69     {
70       mant1 <<= 1;
71       exp--;
72     }
73
74   /* now we perform repeated subtraction of fl2.l from fl1.l */
75   mask = 0x1000000;
76   result = 0;
77   while (mask)
78     {
79       if (mant1 >= mant2)
80         {
81           result |= mask;
82           mant1 -= mant2;
83         }
84       mant1 <<= 1;
85       mask >>= 1;
86     }
87
88   /* round */
89   result += 1;
90
91   /* normalize down */
92   exp++;
93   result >>= 1;
94
95   result &= ~HIDDEN;
96
97   /* pack up and go home */
98   if (exp >= 0x100)
99     fl1.l = (sign ? SIGNBIT : 0) | 0x7F800000;
100   else if (exp < 0)
101     fl1.l = 0;
102   else
103     fl1.l = PACK (sign ? SIGNBIT : 0 , exp, result);
104   return (fl1.f);
105 }
106