2 Copyright © 1995-2004, The AROS Development Team. All rights reserved.
6 #include "mathffp_intern.h"
8 /*****************************************************************************
12 AROS_LH2(float, SPAdd
,
15 AROS_LHA(float, fnum1
, D1
),
16 AROS_LHA(float, fnum2
, D0
),
19 struct LibHeader
*, MathBase
, 11, Mathffp
)
22 Calculate the sum of two ffp numbers
27 sum of fnum1 and fnum2.
31 negative : result is negative
32 overflow : result is too large or too small for ffp format
37 Adapt the exponent of the ffp-number with the smaller
38 exponent to the ffp-number with the larger exponent.
39 Therefore rotate the mantisse of the ffp-number with the
40 smaller exponents by n bits, where n is the absolute value
41 of the difference of the exponents.
43 The exponent of the target ffp-number is set to the larger
46 Additionally rotate both numbers by one bit to the right so
47 you can catch a result > 1 in the MSB.
49 If the signs of the two numbers are equal then simply add
50 the two mantisses. The result of the mantisses will be
51 [0.5 .. 2[. Check the MSB. If zero, then the result is < 1
52 and therefore subtract 1 from the exponent. Normalize the
53 mantisse of the result by rotating it one bit to the left.
54 Check the mantisse for 0.
56 If the signs of the two numbers are different then subtract
57 the ffp-number with the neagtive sign from the other one.
58 The result of the mantisse will be [-1..1[. If the MSB of
59 the result is set, then the result is below zero and therefore
60 you have to calculate the absolute value of the mantisse.
61 Check the mantisse for zero. Normalize the mantisse by
62 rotating it to the left and decreasing the exponent for every
65 Test the exponent of the result for an overflow.
68 *****************************************************************************/
77 SetSR(0, Zero_Bit
| Overflow_Bit
| Negative_Bit
);
79 Mant1
= fnum1
& FFPMantisse_Mask
;
80 Mant2
= fnum2
& FFPMantisse_Mask
;
81 Shift
= ((char)fnum1
& FFPExponent_Mask
) -
82 ((char)fnum2
& FFPExponent_Mask
);
92 Mant2
>>= (Shift
+ 1);
95 Exponent
= (fnum1
& FFPExponent_Mask
) + 1;
105 Mant1
>>= (-Shift
+ 1);
108 Exponent
= (fnum2
& FFPExponent_Mask
) + 1;
111 /* sign(fnum1) == sign(fnum2)
115 if ( ((BYTE
) fnum1
& FFPSign_Mask
) - ((BYTE
) fnum2
& FFPSign_Mask
) == 0)
117 Res
= fnum1
& FFPSign_Mask
;
119 if ((LONG
) Mant1
> 0)
127 SetSR(Zero_Bit
, Zero_Bit
| Negative_Bit
| Overflow_Bit
);
131 /* second case: sign(fnum1) != sign(fnum2)
136 if ((char) fnum1
< 0)
138 Mant1
= Mant2
- Mant1
;
142 Mant1
= Mant1
- Mant2
;
144 /* if the result is below zero */
145 if ((LONG
) Mant1
< 0)
149 SetSR(Negative_Bit
, Zero_Bit
| Negative_Bit
| Overflow_Bit
);
155 /* test the result for zero, has to be done before normalizing
160 SetSR(Zero_Bit
, Zero_Bit
| Overflow_Bit
| Negative_Bit
);
163 /* normalize the mantisse */
164 while ((LONG
) Mant1
> 0)
166 Mant1
+= Mant1
; /* one bit to the left. */
171 if ((char) Exponent
< 0)
173 SetSR(Overflow_Bit
, Zero_Bit
| Overflow_Bit
);
174 /* do NOT change Negative_Bit! */
175 return (Res
| (FFPMantisse_Mask
| FFPExponent_Mask
));
178 Res
|= (Mant1
& FFPMantisse_Mask
) | Exponent
;
180 D(kprintf("SPAdd(%x + %x) = %x\n", fnum1
, fnum2
, Res
));