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