Squashed commit of 'sdl-hidd' branch
[cake.git] / rom / mathffp / spdiv.c
blob6d38da5de887f792e94987cd409abbb1d7bcba83
1 /*
2 Copyright © 1995-2004, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include "mathffp_intern.h"
8 /*
9 FUNCTION
10 Divide two ffp numbers
11 fnum = fnum2 / fnum1;
13 RESULT
14 Flags:
15 zero : result is zero
16 negative : result is negative
17 overflow : result is out of range
19 NOTES
21 EXAMPLE
23 BUGS
24 The parameters are swapped !
26 SEE ALSO
29 INTERNALS
30 ALGORITHM:
31 Check if fnum2 == 0: result = 0;
32 Check if fnum1 == 0: result = overflow;
33 The further algorithm comes down to a pen & paper division
35 HISTORY
38 AROS_LH2(float, SPDiv,
39 AROS_LHA(float, fnum1, D1),
40 AROS_LHA(float, fnum2, D0),
41 struct LibHeader *, MathBase, 14, Mathffp
44 AROS_LIBFUNC_INIT
46 LONG Res = 0;
47 char Exponent = ((char) fnum2 & FFPExponent_Mask) -
48 ((char) fnum1 & FFPExponent_Mask) + 0x41;
50 LONG Mant2 = ((ULONG)fnum2 & FFPMantisse_Mask);
51 LONG Mant1 = ((ULONG)fnum1 & FFPMantisse_Mask);
52 ULONG Bit_Mask = 0x80000000;
54 /* check if the dividend is zero */
55 if (0 == fnum2)
57 SetSR(Zero_Bit, Zero_Bit | Negative_Bit | Overflow_Bit);
58 return 0;
61 /* check for division by zero */
62 if (0 == fnum1)
64 SetSR(Overflow_Bit, Zero_Bit | Negative_Bit | Overflow_Bit);
65 return 0;
68 while (Bit_Mask >= 0x40 && Mant2 != 0)
70 if (Mant2 - Mant1 >= 0)
72 Mant2 -= Mant1;
73 Res |= Bit_Mask;
75 while (Mant2 > 0)
77 Mant2 <<= 1;
78 Bit_Mask >>= 1;
81 while (Mant1 > 0)
83 Mant1 <<=1;
84 Bit_Mask <<=1;
86 } /* if */
87 else
89 Mant1 = (ULONG) Mant1 >> 1;
90 Bit_Mask >>= 1;
92 } /* while */
94 /* normalize the mantisse */
95 while (Res > 0)
97 Res += Res;
98 Exponent --;
101 if ((char) Res < 0)
103 Res += 0x00000100;
106 Res &= FFPMantisse_Mask;
107 Res |= (Exponent & 0x7f);
108 Res |= (fnum1 & FFPSign_Mask) ^ (fnum2 & FFPSign_Mask);
110 if ((char) Res < 0)
112 SetSR(Negative_Bit, Zero_Bit | Overflow_Bit | Negative_Bit);
115 if ((char) Exponent < 0)
117 SetSR(Overflow_Bit, Zero_Bit | Overflow_Bit | Negative_Bit);
118 return(Res | (FFPMantisse_Mask | FFPExponent_Mask));
121 kprintf("%x / %x =%x\n",fnum2,fnum1,Res);
123 return Res;
125 AROS_LIBFUNC_EXIT