Rev 734 | Rev 828 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 734 | Rev 804 | ||
---|---|---|---|
Line 33... | Line 33... | ||
33 | /** Add two Float32 numbers with same signs |
33 | /** Add two Float32 numbers with same signs |
34 | */ |
34 | */ |
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,frac1, frac2; |
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 == FLOAT32_MAX_EXPONENT) { |
50 | if (b.parts.exp == FLOAT32_MAX_EXPONENT) { |
51 | return b; |
51 | return b; |
52 | } |
52 | } |
53 | 53 | ||
54 | mant1 = b.parts.mantisa; |
54 | frac1 = b.parts.fraction; |
55 | exp1 = b.parts.exp; |
55 | exp1 = b.parts.exp; |
56 | mant2 = a.parts.mantisa; |
56 | frac2 = a.parts.fraction; |
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)) || (isFloat32NaN(b))) { |
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 (isFloat32NaN(a)?a:b); |
65 | }; |
65 | }; |
66 | 66 | ||
67 | if (a.parts.exp == FLOAT32_MAX_EXPONENT) { |
67 | if (a.parts.exp == FLOAT32_MAX_EXPONENT) { |
68 | return a; |
68 | return a; |
69 | } |
69 | } |
70 | 70 | ||
71 | mant1 = a.parts.mantisa; |
71 | frac1 = a.parts.fraction; |
72 | exp1 = a.parts.exp; |
72 | exp1 = a.parts.exp; |
73 | mant2 = b.parts.mantisa; |
73 | frac2 = b.parts.fraction; |
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 | frac1 += frac2; |
80 | if (mant1 & FLOAT32_HIDDEN_BIT_MASK ) { |
80 | if (frac1 & FLOAT32_HIDDEN_BIT_MASK ) { |
81 | /* result is not denormalized */ |
81 | /* result is not denormalized */ |
82 | a.parts.exp = 1; |
82 | a.parts.exp = 1; |
83 | }; |
83 | }; |
84 | a.parts.mantisa = mant1; |
84 | a.parts.fraction = frac1; |
85 | return a; |
85 | return a; |
86 | }; |
86 | }; |
87 | 87 | ||
88 | mant1 |= FLOAT32_HIDDEN_BIT_MASK; //add hidden bit |
88 | frac1 |= FLOAT32_HIDDEN_BIT_MASK; /* add hidden bit */ |
89 | 89 | ||
90 | if (exp2 == 0) { |
90 | if (exp2 == 0) { |
91 | /* second operand is denormalized */ |
91 | /* second operand is denormalized */ |
92 | --expdiff; |
92 | --expdiff; |
93 | } else { |
93 | } else { |
94 | /* add hidden bit to second operand */ |
94 | /* add hidden bit to second operand */ |
95 | mant2 |= FLOAT32_HIDDEN_BIT_MASK; |
95 | frac2 |= FLOAT32_HIDDEN_BIT_MASK; |
96 | }; |
96 | }; |
97 | 97 | ||
98 | /* create some space for rounding */ |
98 | /* create some space for rounding */ |
99 | mant1 <<= 6; |
99 | frac1 <<= 6; |
100 | mant2 <<= 6; |
100 | frac2 <<= 6; |
- | 101 | ||
- | 102 | if (expdiff < (FLOAT32_FRACTION_SIZE + 2) ) { |
|
- | 103 | frac2 >>= expdiff; |
|
- | 104 | frac1 += frac2; |
|
- | 105 | }; |
|
101 | 106 | ||
102 | if (expdiff > (FLOAT32_MANTISA_SIZE + 1) ) { |
- | |
103 | goto done; |
- | |
104 | }; |
- | |
105 | - | ||
106 | mant2 >>= expdiff; |
- | |
107 | mant1 += mant2; |
- | |
108 | done: |
- | |
109 | if (mant1 & (FLOAT32_HIDDEN_BIT_MASK << 7) ) { |
107 | if (frac1 & (FLOAT32_HIDDEN_BIT_MASK << 7) ) { |
110 | ++exp1; |
108 | ++exp1; |
111 | mant1 >>= 1; |
109 | frac1 >>= 1; |
112 | }; |
110 | }; |
113 | 111 | ||
114 | /* rounding - if first bit after mantisa is set then round up */ |
112 | /* rounding - if first bit after fraction is set then round up */ |
115 | mant1 += (0x1 << 5); |
113 | frac1 += (0x1 << 5); |
116 | 114 | ||
117 | if (mant1 & (FLOAT32_HIDDEN_BIT_MASK << 7)) { |
115 | if (frac1 & (FLOAT32_HIDDEN_BIT_MASK << 7)) { |
- | 116 | /* rounding overflow */ |
|
118 | ++exp1; |
117 | ++exp1; |
119 | mant1 >>= 1; |
118 | frac1 >>= 1; |
120 | }; |
119 | }; |
121 | 120 | ||
- | 121 | if ((a.parts.exp == FLOAT32_MAX_EXPONENT ) || (a.parts.exp < exp1)) { |
|
- | 122 | /* overflow - set infinity as result */ |
|
- | 123 | a.parts.exp = FLOAT32_MAX_EXPONENT; |
|
- | 124 | a.parts.fraction = 0; |
|
- | 125 | return a; |
|
- | 126 | } |
|
- | 127 | ||
122 | a.parts.exp = exp1; |
128 | a.parts.exp = exp1; |
123 | 129 | ||
124 | /*Clear hidden bit and shift */ |
130 | /*Clear hidden bit and shift */ |
125 | a.parts.mantisa = ((mant1 >> 6) & (~FLOAT32_HIDDEN_BIT_MASK)) ; |
131 | a.parts.fraction = ((frac1 >> 6) & (~FLOAT32_HIDDEN_BIT_MASK)) ; |
126 | return a; |
132 | return a; |
127 | } |
133 | } |
128 | 134 | ||
129 | 135 | ||
130 | /** Add two Float64 numbers with same signs |
136 | /** Add two Float64 numbers with same signs |
131 | */ |
137 | */ |
132 | float64 addFloat64(float64 a, float64 b) |
138 | float64 addFloat64(float64 a, float64 b) |
133 | { |
139 | { |
134 | int expdiff; |
140 | int expdiff; |
135 | __u32 exp1, exp2; |
141 | __u32 exp1, exp2; |
136 | __u64 mant1, mant2; |
142 | __u64 frac1, frac2; |
137 | 143 | ||
138 | expdiff = a.parts.exp - b.parts.exp; |
144 | expdiff = a.parts.exp - b.parts.exp; |
139 | if (expdiff < 0) { |
145 | if (expdiff < 0) { |
140 | if (isFloat64NaN(b)) { |
146 | if (isFloat64NaN(b)) { |
141 | //TODO: fix SigNaN |
147 | /* TODO: fix SigNaN */ |
142 | if (isFloat64SigNaN(b)) { |
148 | if (isFloat64SigNaN(b)) { |
143 | }; |
149 | }; |
144 | 150 | ||
145 | return b; |
151 | return b; |
146 | }; |
152 | }; |
Line 148... | Line 154... | ||
148 | /* b is infinity and a not */ |
154 | /* b is infinity and a not */ |
149 | if (b.parts.exp == FLOAT64_MAX_EXPONENT ) { |
155 | if (b.parts.exp == FLOAT64_MAX_EXPONENT ) { |
150 | return b; |
156 | return b; |
151 | } |
157 | } |
152 | 158 | ||
153 | mant1 = b.parts.mantisa; |
159 | frac1 = b.parts.fraction; |
154 | exp1 = b.parts.exp; |
160 | exp1 = b.parts.exp; |
155 | mant2 = a.parts.mantisa; |
161 | frac2 = a.parts.fraction; |
156 | exp2 = a.parts.exp; |
162 | exp2 = a.parts.exp; |
157 | expdiff *= -1; |
163 | expdiff *= -1; |
158 | } else { |
164 | } else { |
159 | if (isFloat64NaN(a)) { |
165 | if (isFloat64NaN(a)) { |
160 | //TODO: fix SigNaN |
166 | /* TODO: fix SigNaN */ |
161 | if (isFloat64SigNaN(a) || isFloat64SigNaN(b)) { |
167 | if (isFloat64SigNaN(a) || isFloat64SigNaN(b)) { |
162 | }; |
168 | }; |
163 | return a; |
169 | return a; |
164 | }; |
170 | }; |
165 | 171 | ||
166 | /* a is infinity and b not */ |
172 | /* a is infinity and b not */ |
167 | if (a.parts.exp == FLOAT64_MAX_EXPONENT ) { |
173 | if (a.parts.exp == FLOAT64_MAX_EXPONENT ) { |
168 | return a; |
174 | return a; |
169 | } |
175 | } |
170 | 176 | ||
171 | mant1 = a.parts.mantisa; |
177 | frac1 = a.parts.fraction; |
172 | exp1 = a.parts.exp; |
178 | exp1 = a.parts.exp; |
173 | mant2 = b.parts.mantisa; |
179 | frac2 = b.parts.fraction; |
174 | exp2 = b.parts.exp; |
180 | exp2 = b.parts.exp; |
175 | }; |
181 | }; |
176 | 182 | ||
177 | if (exp1 == 0) { |
183 | if (exp1 == 0) { |
178 | /* both are denormalized */ |
184 | /* both are denormalized */ |
179 | mant1 += mant2; |
185 | frac1 += frac2; |
180 | if (mant1 & FLOAT64_HIDDEN_BIT_MASK) { |
186 | if (frac1 & FLOAT64_HIDDEN_BIT_MASK) { |
181 | /* result is not denormalized */ |
187 | /* result is not denormalized */ |
182 | a.parts.exp = 1; |
188 | a.parts.exp = 1; |
183 | }; |
189 | }; |
184 | a.parts.mantisa = mant1; |
190 | a.parts.fraction = frac1; |
185 | return a; |
191 | return a; |
186 | }; |
192 | }; |
187 | 193 | ||
188 | /* add hidden bit - mant1 is sure not denormalized */ |
194 | /* add hidden bit - frac1 is sure not denormalized */ |
189 | mant1 |= FLOAT64_HIDDEN_BIT_MASK; |
195 | frac1 |= FLOAT64_HIDDEN_BIT_MASK; |
190 | 196 | ||
191 | /* second operand ... */ |
197 | /* second operand ... */ |
192 | if (exp2 == 0) { |
198 | if (exp2 == 0) { |
193 | /* ... is denormalized */ |
199 | /* ... is denormalized */ |
194 | --expdiff; |
200 | --expdiff; |
195 | } else { |
201 | } else { |
196 | /* is not denormalized */ |
202 | /* is not denormalized */ |
197 | mant2 |= FLOAT64_HIDDEN_BIT_MASK; |
203 | frac2 |= FLOAT64_HIDDEN_BIT_MASK; |
198 | }; |
204 | }; |
199 | 205 | ||
200 | /* create some space for rounding */ |
206 | /* create some space for rounding */ |
201 | mant1 <<= 6; |
207 | frac1 <<= 6; |
202 | mant2 <<= 6; |
208 | frac2 <<= 6; |
203 | 209 | ||
204 | if (expdiff > (FLOAT64_MANTISA_SIZE + 1) ) { |
210 | if (expdiff < (FLOAT64_FRACTION_SIZE + 2) ) { |
- | 211 | frac2 >>= expdiff; |
|
205 | goto done; |
212 | frac1 += frac2; |
206 | }; |
213 | }; |
207 | 214 | ||
208 | mant2 >>= expdiff; |
- | |
209 | mant1 += mant2; |
- | |
210 | done: |
- | |
211 | if (mant1 & (FLOAT64_HIDDEN_BIT_MASK << 7) ) { |
215 | if (frac1 & (FLOAT64_HIDDEN_BIT_MASK << 7) ) { |
212 | ++exp1; |
216 | ++exp1; |
213 | mant1 >>= 1; |
217 | frac1 >>= 1; |
214 | }; |
218 | }; |
215 | 219 | ||
216 | /* rounding - if first bit after mantisa is set then round up */ |
220 | /* rounding - if first bit after fraction is set then round up */ |
217 | mant1 += (0x1 << 5); |
221 | frac1 += (0x1 << 5); |
218 | 222 | ||
219 | if (mant1 & (FLOAT64_HIDDEN_BIT_MASK << 7)) { |
223 | if (frac1 & (FLOAT64_HIDDEN_BIT_MASK << 7)) { |
- | 224 | /* rounding overflow */ |
|
220 | ++exp1; |
225 | ++exp1; |
221 | mant1 >>= 1; |
226 | frac1 >>= 1; |
222 | }; |
227 | }; |
223 | 228 | ||
- | 229 | if ((a.parts.exp == FLOAT64_MAX_EXPONENT ) || (a.parts.exp < exp1)) { |
|
- | 230 | /* overflow - set infinity as result */ |
|
- | 231 | a.parts.exp = FLOAT64_MAX_EXPONENT; |
|
- | 232 | a.parts.fraction = 0; |
|
- | 233 | return a; |
|
- | 234 | } |
|
- | 235 | ||
224 | a.parts.exp = exp1; |
236 | a.parts.exp = exp1; |
225 | /*Clear hidden bit and shift */ |
237 | /*Clear hidden bit and shift */ |
226 | a.parts.mantisa = ( (mant1 >> 6 ) & (~FLOAT64_HIDDEN_BIT_MASK)); |
238 | a.parts.fraction = ( (frac1 >> 6 ) & (~FLOAT64_HIDDEN_BIT_MASK)); |
227 | return a; |
239 | return a; |
228 | } |
240 | } |
229 | 241 | ||
230 | 242 |