Rev 731 | Rev 804 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 731 | Rev 732 | ||
---|---|---|---|
Line 35... | Line 35... | ||
35 | float32 addFloat32(float32 a, float32 b) |
35 | float32 addFloat32(float32 a, float32 b) |
36 | { |
36 | { |
37 | int expdiff; |
37 | int expdiff; |
38 | __u32 exp1, exp2,mant1, mant2; |
38 | __u32 exp1, exp2,mant1, mant2; |
39 | 39 | ||
40 | expdiff=a.parts.exp - b.parts.exp; |
40 | expdiff = a.parts.exp - b.parts.exp; |
41 | if (expdiff<0) { |
41 | if (expdiff < 0) { |
42 | if (isFloat32NaN(b)) { |
42 | if (isFloat32NaN(b)) { |
43 | //TODO: fix SigNaN |
43 | //TODO: fix SigNaN |
44 | if (isFloat32SigNaN(b)) { |
44 | if (isFloat32SigNaN(b)) { |
45 | }; |
45 | }; |
46 | 46 | ||
47 | return b; |
47 | return b; |
48 | }; |
48 | }; |
49 | 49 | ||
50 | if (b.parts.exp==0xFF) { |
50 | if (b.parts.exp == FLOAT32_MAX_EXPONENT) { |
51 | return b; |
51 | return b; |
52 | } |
52 | } |
53 | 53 | ||
54 | mant1=b.parts.mantisa; |
54 | mant1 = b.parts.mantisa; |
55 | exp1=b.parts.exp; |
55 | exp1 = b.parts.exp; |
56 | mant2=a.parts.mantisa; |
56 | mant2 = a.parts.mantisa; |
57 | exp2=a.parts.exp; |
57 | exp2 = a.parts.exp; |
58 | expdiff*=-1; |
58 | expdiff *= -1; |
59 | } else { |
59 | } else { |
60 | if (isFloat32NaN(a)) { |
60 | if (isFloat32NaN(a)) { |
61 | //TODO: fix SigNaN |
61 | //TODO: fix SigNaN |
62 | if ((isFloat32SigNaN(a))||(isFloat32SigNaN(b))) { |
62 | if (isFloat32SigNaN(a) || isFloat32SigNaN(b)) { |
63 | }; |
63 | }; |
64 | return a; |
64 | return a; |
65 | }; |
65 | }; |
66 | 66 | ||
67 | if (a.parts.exp==0xFF) { |
67 | if (a.parts.exp == FLOAT32_MAX_EXPONENT) { |
68 | return a; |
68 | return a; |
69 | } |
69 | } |
70 | 70 | ||
71 | mant1=a.parts.mantisa; |
71 | mant1 = a.parts.mantisa; |
72 | exp1=a.parts.exp; |
72 | exp1 = a.parts.exp; |
73 | mant2=b.parts.mantisa; |
73 | mant2 = b.parts.mantisa; |
74 | exp2=b.parts.exp; |
74 | exp2 = b.parts.exp; |
75 | }; |
75 | }; |
76 | 76 | ||
77 | if (exp1 == 0) { |
77 | if (exp1 == 0) { |
78 | /* both are denormalized */ |
78 | /* both are denormalized */ |
79 | mant1 += mant2; |
79 | mant1 += mant2; |
Line 97... | Line 97... | ||
97 | 97 | ||
98 | /* create some space for rounding */ |
98 | /* create some space for rounding */ |
99 | mant1 <<= 6; |
99 | mant1 <<= 6; |
100 | mant2 <<= 6; |
100 | mant2 <<= 6; |
101 | 101 | ||
102 | if (expdiff > 24) { |
102 | if (expdiff > (FLOAT32_MANTISA_SIZE + 1) ) { |
103 | goto done; |
103 | goto done; |
104 | }; |
104 | }; |
105 | 105 | ||
106 | mant2>>=expdiff; |
106 | mant2 >>= expdiff; |
107 | mant1+=mant2; |
107 | mant1 += mant2; |
108 | done: |
108 | done: |
109 | if (mant1 & (FLOAT32_HIDDEN_BIT_MASK << 6) ) { |
109 | if (mant1 & (FLOAT32_HIDDEN_BIT_MASK << 7) ) { |
110 | ++exp1; |
110 | ++exp1; |
111 | mant1 >>= 1; |
111 | mant1 >>= 1; |
112 | }; |
112 | }; |
113 | 113 | ||
114 | /* rounding - if first bit after mantisa is set then round up */ |
114 | /* rounding - if first bit after mantisa is set then round up */ |
115 | mant1 += (0x1 << 5); |
115 | mant1 += (0x1 << 5); |
116 | 116 | ||
117 | if (mant1 & (FLOAT32_HIDDEN_BIT_MASK << 6)) { |
117 | if (mant1 & (FLOAT32_HIDDEN_BIT_MASK << 7)) { |
118 | ++exp1; |
118 | ++exp1; |
119 | mant1 >> =1; |
119 | mant1 >>= 1; |
120 | }; |
120 | }; |
121 | 121 | ||
122 | a.parts.exp = exp1; |
122 | a.parts.exp = exp1; |
123 | 123 | ||
124 | /*Clear hidden bit and shift */ |
124 | /*Clear hidden bit and shift */ |
125 | a.parts.mantisa = ((mant1>>6) & (~FLOAT32_HIDDEN_BIT_MASK)) ; |
125 | a.parts.mantisa = ((mant1 >> 6) & (~FLOAT32_HIDDEN_BIT_MASK)) ; |
126 | return a; |
126 | return a; |
127 | } |
127 | } |
128 | 128 | ||
129 | 129 | ||
130 | /** Add two Float64 numbers with same signs |
130 | /** Add two Float64 numbers with same signs |
131 | */ |
131 | */ |
132 | float64 addFloat64(float64 a, float64 b) |
132 | float64 addFloat64(float64 a, float64 b) |
133 | { |
133 | { |
134 | int expdiff; |
134 | int expdiff; |
135 | __u64 exp1,exp2,mant1,mant2; |
135 | __u64 exp1, exp2, mant1, mant2; |
136 | 136 | ||
137 | expdiff=a.parts.exp - b.parts.exp; |
137 | expdiff = a.parts.exp - b.parts.exp; |
138 | if (expdiff<0) { |
138 | if (expdiff < 0) { |
139 | if (isFloat64NaN(b)) { |
139 | if (isFloat64NaN(b)) { |
140 | //TODO: fix SigNaN |
140 | //TODO: fix SigNaN |
141 | if (isFloat64SigNaN(b)) { |
141 | if (isFloat64SigNaN(b)) { |
142 | }; |
142 | }; |
143 | 143 | ||
144 | return b; |
144 | return b; |
145 | }; |
145 | }; |
146 | 146 | ||
147 | /* b is infinity and a not */ |
147 | /* b is infinity and a not */ |
148 | if (b.parts.exp==0x8FF) { |
148 | if (b.parts.exp == FLOAT64_MAX_EXPONENT ) { |
149 | return b; |
149 | return b; |
150 | } |
150 | } |
151 | 151 | ||
152 | mant1=b.parts.mantisa; |
152 | mant1 = b.parts.mantisa; |
153 | exp1=b.parts.exp; |
153 | exp1 = b.parts.exp; |
154 | mant2=a.parts.mantisa; |
154 | mant2 = a.parts.mantisa; |
155 | exp2=a.parts.exp; |
155 | exp2 = a.parts.exp; |
156 | expdiff*=-1; |
156 | expdiff *= -1; |
157 | } else { |
157 | } else { |
158 | if (isFloat64NaN(a)) { |
158 | if (isFloat64NaN(a)) { |
159 | //TODO: fix SigNaN |
159 | //TODO: fix SigNaN |
160 | if ((isFloat64SigNaN(a))||(isFloat64SigNaN(b))) { |
160 | if (isFloat64SigNaN(a) || isFloat64SigNaN(b)) { |
161 | }; |
161 | }; |
162 | return a; |
162 | return a; |
163 | }; |
163 | }; |
164 | 164 | ||
165 | /* a is infinity and b not */ |
165 | /* a is infinity and b not */ |
166 | if (a.parts.exp == 0x8FF) { |
166 | if (a.parts.exp == FLOAT64_MAX_EXPONENT ) { |
167 | return a; |
167 | return a; |
168 | } |
168 | } |
169 | 169 | ||
170 | mant1 = a.parts.mantisa; |
170 | mant1 = a.parts.mantisa; |
171 | exp1 = a.parts.exp; |
171 | exp1 = a.parts.exp; |
Line 198... | Line 198... | ||
198 | 198 | ||
199 | /* create some space for rounding */ |
199 | /* create some space for rounding */ |
200 | mant1 <<= 6; |
200 | mant1 <<= 6; |
201 | mant2 <<= 6; |
201 | mant2 <<= 6; |
202 | 202 | ||
203 | if (expdiff > 53) { |
203 | if (expdiff > (FLOAT64_MANTISA_SIZE + 1) ) { |
204 | goto done; |
204 | goto done; |
205 | }; |
205 | }; |
206 | 206 | ||
207 | mant2 >>= expdiff; |
207 | mant2 >>= expdiff; |
208 | mant1 += mant2; |
208 | mant1 += mant2; |
209 | done: |
209 | done: |
210 | if (mant1 & (FLOAT64_HIDDEN_BIT_MASK << 6) ) { |
210 | if (mant1 & (FLOAT64_HIDDEN_BIT_MASK << 7) ) { |
211 | ++exp1; |
211 | ++exp1; |
212 | mant1 >>= 1; |
212 | mant1 >>= 1; |
213 | }; |
213 | }; |
214 | 214 | ||
215 | /* rounding - if first bit after mantisa is set then round up */ |
215 | /* rounding - if first bit after mantisa is set then round up */ |
216 | mant1 += (0x1 << 5); |
216 | mant1 += (0x1 << 5); |
217 | 217 | ||
218 | if (mant1 & (FLOAT64_HIDDEN_BIT_MASK << 6)) { |
218 | if (mant1 & (FLOAT64_HIDDEN_BIT_MASK << 7)) { |
219 | ++exp1; |
219 | ++exp1; |
220 | mant1 >> =1; |
220 | mant1 >>= 1; |
221 | }; |
221 | }; |
222 | 222 | ||
223 | a.parts.exp = exp1; |
223 | a.parts.exp = exp1; |
224 | /*Clear hidden bit and shift */ |
224 | /*Clear hidden bit and shift */ |
225 | a.parts.mantisa = ( (mant1 >>6 ) & (~ (FLOAT64_HIDDEN_BIT_MASK))); |
225 | a.parts.mantisa = ( (mant1 >> 6 ) & (~FLOAT64_HIDDEN_BIT_MASK)); |
226 | return a; |
226 | return a; |
227 | } |
227 | } |
228 | 228 | ||
229 | 229 |