2 Copyright © 1995-2004, The AROS Development Team. All rights reserved.
6 #include "mathffp_intern.h"
10 Calculate the sum of two ffp numbers
13 sum of fnum1 and fnum2.
17 <li><item>zero</item>result is zero</li>
18 <li><item>negative</item>result is negative</li>
19 <li><item>overflow</item>result is too large or too small for ffp format</li>
32 <p>Adapt the exponent of the ffp-number with the smaller
33 exponent to the ffp-number with the larger exponent.
34 Therefore rotate the mantisse of the ffp-number with the
35 smaller exponents by n bits, where n is the absolute value
36 of the difference of the exponents.</p>
38 <p>The exponent of the target ffp-number is set to the larger
41 <p>Additionally rotate both numbers by one bit to the right so
42 you can catch a result > 1 in the MSB.</p>
44 <p>If the signs of the two numbers are equal then simply add
45 the two mantisses. The result of the mantisses will be
46 [0.5 .. 2[. Check the MSB. If zero, then the result is < 1
47 and therefore subtract 1 from the exponent. Normalize the
48 mantisse of the result by rotating it one bit to the left.
49 Check the mantisse for 0.</p>
51 <p>If the signs of the two numbers are different then subtract
52 the ffp-number with the neagtive sign from the other one.
53 The result of the mantisse will be [-1..1[. If the MSB of
54 the result is set, then the result is below zero and therefore
55 you have to calculate the absolute value of the mantisse.
56 Check the mantisse for zero. Normalize the mantisse by
57 rotating it to the left and decreasing the exponent for every
60 <p>Test the exponent of the result for an overflow.
66 AROS_LH2(float, SPAdd
,
67 AROS_LHA(float, fnum1
, D1
),
68 AROS_LHA(float, fnum2
, D0
),
69 struct LibHeader
*, MathBase
, 11, Mathffp
79 SetSR(0, Zero_Bit
| Overflow_Bit
| Negative_Bit
);
81 Mant1
= fnum1
& FFPMantisse_Mask
;
82 Mant2
= fnum2
& FFPMantisse_Mask
;
83 Shift
= ((char)fnum1
& FFPExponent_Mask
) -
84 ((char)fnum2
& FFPExponent_Mask
);
94 Mant2
>>= (Shift
+ 1);
97 Exponent
= (fnum1
& FFPExponent_Mask
) + 1;
107 Mant1
>>= (-Shift
+ 1);
110 Exponent
= (fnum2
& FFPExponent_Mask
) + 1;
113 /* sign(fnum1) == sign(fnum2)
117 if ( ((BYTE
) fnum1
& FFPSign_Mask
) - ((BYTE
) fnum2
& FFPSign_Mask
) == 0)
119 Res
= fnum1
& FFPSign_Mask
;
121 if ((LONG
) Mant1
> 0)
129 SetSR(Zero_Bit
, Zero_Bit
| Negative_Bit
| Overflow_Bit
);
133 /* second case: sign(fnum1) != sign(fnum2)
138 if ((char) fnum1
< 0)
140 Mant1
= Mant2
- Mant1
;
144 Mant1
= Mant1
- Mant2
;
146 /* if the result is below zero */
147 if ((LONG
) Mant1
< 0)
151 SetSR(Negative_Bit
, Zero_Bit
| Negative_Bit
| Overflow_Bit
);
157 /* test the result for zero, has to be done before normalizing
162 SetSR(Zero_Bit
, Zero_Bit
| Overflow_Bit
| Negative_Bit
);
165 /* normalize the mantisse */
166 while ((LONG
) Mant1
> 0)
168 Mant1
+= Mant1
; /* one bit to the left. */
173 if ((char) Exponent
< 0)
175 SetSR(Overflow_Bit
, Zero_Bit
| Overflow_Bit
);
176 /* do NOT change Negative_Bit! */
177 return (Res
| (FFPMantisse_Mask
| FFPExponent_Mask
));
180 Res
|= (Mant1
& FFPMantisse_Mask
) | Exponent
;
182 kprintf("SPAdd(%x,%x)=%x\n",fnum1
,fnum2
,Res
);