Rev 661 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 661 | Rev 688 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2005 Josef Cejka |
2 | * Copyright (C) 2005 Josef Cejka |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | #include<sftypes.h> |
29 | #include<sftypes.h> |
30 | #include<arithmetic.h> |
30 | #include<arithmetic.h> |
31 | #include<comparison.h> |
31 | #include<comparison.h> |
32 | 32 | ||
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,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 | return b; |
47 | return b; |
47 | }; |
48 | }; |
48 | 49 | ||
49 | if (b.parts.exp==0xFF) { |
50 | if (b.parts.exp==0xFF) { |
50 | return b; |
51 | return b; |
51 | } |
52 | } |
52 | 53 | ||
53 | mant1=b.parts.mantisa; |
54 | mant1=b.parts.mantisa; |
54 | exp1=b.parts.exp; |
55 | exp1=b.parts.exp; |
55 | mant2=a.parts.mantisa; |
56 | mant2=a.parts.mantisa; |
56 | exp2=a.parts.exp; |
57 | exp2=a.parts.exp; |
57 | expdiff*=-1; |
58 | expdiff*=-1; |
58 | } else { |
59 | } else { |
59 | if (isFloat32NaN(a)) { |
60 | if (isFloat32NaN(a)) { |
60 | //TODO: fix SigNaN |
61 | //TODO: fix SigNaN |
61 | if ((isFloat32SigNaN(a))||(isFloat32SigNaN(b))) { |
62 | if ((isFloat32SigNaN(a))||(isFloat32SigNaN(b))) { |
62 | }; |
63 | }; |
63 | return a; |
64 | return a; |
64 | }; |
65 | }; |
65 | 66 | ||
66 | if (a.parts.exp==0xFF) { |
67 | if (a.parts.exp==0xFF) { |
67 | return a; |
68 | return a; |
68 | } |
69 | } |
69 | 70 | ||
70 | mant1=a.parts.mantisa; |
71 | mant1=a.parts.mantisa; |
71 | exp1=a.parts.exp; |
72 | exp1=a.parts.exp; |
72 | mant2=b.parts.mantisa; |
73 | mant2=b.parts.mantisa; |
73 | exp2=b.parts.exp; |
74 | exp2=b.parts.exp; |
74 | }; |
75 | }; |
75 | 76 | ||
76 | if (exp1==0) { |
77 | if (exp1==0) { |
77 | //both are denormalized |
78 | //both are denormalized |
78 | mant1+=mant2; |
79 | mant1+=mant2; |
79 | if (mant1&0xF00000) { |
80 | if (mant1&0xF00000) { |
80 | a.parts.exp=1; |
81 | a.parts.exp=1; |
81 | }; |
82 | }; |
82 | a.parts.mantisa=mant1; |
83 | a.parts.mantisa=mant1; |
83 | return a; |
84 | return a; |
84 | }; |
85 | }; |
85 | 86 | ||
86 | // create some space for rounding |
87 | // create some space for rounding |
87 | mant1<<=6; |
88 | mant1<<=6; |
88 | mant2<<=6; |
89 | mant2<<=6; |
89 | 90 | ||
90 | mant1|=0x20000000; //add hidden bit |
91 | mant1|=0x20000000; //add hidden bit |
91 | 92 | ||
92 | 93 | ||
93 | if (exp2==0) { |
94 | if (exp2==0) { |
94 | --expdiff; |
95 | --expdiff; |
95 | } else { |
96 | } else { |
96 | mant2|=0x20000000; //hidden bit |
97 | mant2|=0x20000000; //hidden bit |
97 | }; |
98 | }; |
98 | 99 | ||
99 | if (expdiff>24) { |
100 | if (expdiff>24) { |
100 | goto done; |
101 | goto done; |
101 | }; |
102 | }; |
102 | 103 | ||
103 | mant2>>=expdiff; |
104 | mant2>>=expdiff; |
104 | mant1+=mant2; |
105 | mant1+=mant2; |
105 | done: |
106 | done: |
106 | if (mant1&0x40000000) { |
107 | if (mant1&0x40000000) { |
107 | ++exp1; |
108 | ++exp1; |
108 | mant1>>=1; |
109 | mant1>>=1; |
109 | }; |
110 | }; |
110 | 111 | ||
111 | //rounding - if first bit after mantisa is set then round up |
112 | //rounding - if first bit after mantisa is set then round up |
112 | mant1+=0x20; |
113 | mant1+=0x20; |
113 | 114 | ||
114 | if (mant1&0x40000000) { |
115 | if (mant1&0x40000000) { |
115 | ++exp1; |
116 | ++exp1; |
116 | mant1>>=1; |
117 | mant1>>=1; |
117 | }; |
118 | }; |
118 | 119 | ||
119 | a.parts.exp=exp1; |
120 | a.parts.exp=exp1; |
120 | a.parts.mantisa = ((mant1&(~0x20000000))>>6); /*Clear hidden bit and shift */ |
121 | a.parts.mantisa = ((mant1&(~0x20000000))>>6); /*Clear hidden bit and shift */ |
121 | return a; |
122 | return a; |
122 | }; |
123 | }; |
123 | 124 | ||
124 | /** Subtract two float32 numbers with same signs |
125 | /** Subtract two float32 numbers with same signs |
125 | */ |
126 | */ |
126 | float32 subFloat32(float32 a, float32 b) |
127 | float32 subFloat32(float32 a, float32 b) |
127 | { |
128 | { |
128 | int expdiff; |
129 | int expdiff; |
129 | __u32 exp1,exp2,mant1,mant2; |
130 | __u32 exp1,exp2,mant1,mant2; |
130 | float32 result; |
131 | float32 result; |
131 | 132 | ||
132 | result.f = 0; |
133 | result.f = 0; |
133 | 134 | ||
134 | expdiff=a.parts.exp - b.parts.exp; |
135 | expdiff=a.parts.exp - b.parts.exp; |
135 | if ((expdiff<0)||((expdiff==0)&&(a.parts.mantisa<b.parts.mantisa))) { |
136 | if ((expdiff<0)||((expdiff==0)&&(a.parts.mantisa<b.parts.mantisa))) { |
136 | if (isFloat32NaN(b)) { |
137 | if (isFloat32NaN(b)) { |
137 | //TODO: fix SigNaN |
138 | //TODO: fix SigNaN |
138 | if (isFloat32SigNaN(b)) { |
139 | if (isFloat32SigNaN(b)) { |
139 | }; |
140 | }; |
140 | return b; |
141 | return b; |
141 | }; |
142 | }; |
142 | 143 | ||
143 | if (b.parts.exp==0xFF) { |
144 | if (b.parts.exp==0xFF) { |
144 | b.parts.sign=!b.parts.sign; /* num -(+-inf) = -+inf */ |
145 | b.parts.sign=!b.parts.sign; /* num -(+-inf) = -+inf */ |
145 | return b; |
146 | return b; |
146 | } |
147 | } |
147 | 148 | ||
148 | result.parts.sign = !a.parts.sign; |
149 | result.parts.sign = !a.parts.sign; |
149 | 150 | ||
150 | mant1=b.parts.mantisa; |
151 | mant1=b.parts.mantisa; |
151 | exp1=b.parts.exp; |
152 | exp1=b.parts.exp; |
152 | mant2=a.parts.mantisa; |
153 | mant2=a.parts.mantisa; |
153 | exp2=a.parts.exp; |
154 | exp2=a.parts.exp; |
154 | expdiff*=-1; |
155 | expdiff*=-1; |
155 | } else { |
156 | } else { |
156 | if (isFloat32NaN(a)) { |
157 | if (isFloat32NaN(a)) { |
157 | //TODO: fix SigNaN |
158 | //TODO: fix SigNaN |
158 | if ((isFloat32SigNaN(a))||(isFloat32SigNaN(b))) { |
159 | if ((isFloat32SigNaN(a))||(isFloat32SigNaN(b))) { |
159 | }; |
160 | }; |
160 | return a; |
161 | return a; |
161 | }; |
162 | }; |
162 | 163 | ||
163 | if (a.parts.exp==0xFF) { |
164 | if (a.parts.exp==0xFF) { |
164 | if (b.parts.exp==0xFF) { |
165 | if (b.parts.exp==0xFF) { |
165 | /* inf - inf => nan */ |
166 | /* inf - inf => nan */ |
166 | //TODO: fix exception |
167 | //TODO: fix exception |
167 | result.binary = FLOAT32_NAN; |
168 | result.binary = FLOAT32_NAN; |
168 | return result; |
169 | return result; |
169 | }; |
170 | }; |
170 | return a; |
171 | return a; |
171 | } |
172 | } |
172 | 173 | ||
173 | result.parts.sign = a.parts.sign; |
174 | result.parts.sign = a.parts.sign; |
174 | 175 | ||
175 | mant1=a.parts.mantisa; |
176 | mant1=a.parts.mantisa; |
176 | exp1=a.parts.exp; |
177 | exp1=a.parts.exp; |
177 | mant2=b.parts.mantisa; |
178 | mant2=b.parts.mantisa; |
178 | exp2=b.parts.exp; |
179 | exp2=b.parts.exp; |
179 | 180 | ||
180 | 181 | ||
181 | 182 | ||
182 | }; |
183 | }; |
183 | 184 | ||
184 | if (exp1==0) { |
185 | if (exp1==0) { |
185 | //both are denormalized |
186 | //both are denormalized |
186 | result.parts.mantisa=mant1-mant2; |
187 | result.parts.mantisa=mant1-mant2; |
187 | if (result.parts.mantisa>mant1) { |
188 | if (result.parts.mantisa>mant1) { |
188 | //TODO: underflow exception |
189 | //TODO: underflow exception |
189 | return result; |
190 | return result; |
190 | }; |
191 | }; |
191 | result.parts.exp=0; |
192 | result.parts.exp=0; |
192 | return result; |
193 | return result; |
193 | }; |
194 | }; |
194 | 195 | ||
195 | // create some space for rounding |
196 | // create some space for rounding |
196 | mant1<<=6; |
197 | mant1<<=6; |
197 | mant2<<=6; |
198 | mant2<<=6; |
198 | 199 | ||
199 | mant1|=0x20000000; //add hidden bit |
200 | mant1|=0x20000000; //add hidden bit |
200 | 201 | ||
201 | 202 | ||
202 | if (exp2==0) { |
203 | if (exp2==0) { |
203 | --expdiff; |
204 | --expdiff; |
204 | } else { |
205 | } else { |
205 | mant2|=0x20000000; //hidden bit |
206 | mant2|=0x20000000; //hidden bit |
206 | }; |
207 | }; |
207 | 208 | ||
208 | if (expdiff>24) { |
209 | if (expdiff>24) { |
209 | goto done; |
210 | goto done; |
210 | }; |
211 | }; |
211 | 212 | ||
212 | mant1 = mant1-(mant2>>expdiff); |
213 | mant1 = mant1-(mant2>>expdiff); |
213 | done: |
214 | done: |
214 | 215 | ||
215 | //TODO: find first nonzero digit and shift result and detect possibly underflow |
216 | //TODO: find first nonzero digit and shift result and detect possibly underflow |
216 | while ((exp1>0)&&(!(mant1&0x20000000))) { |
217 | while ((exp1>0)&&(!(mant1&0x20000000))) { |
217 | exp1--; |
218 | exp1--; |
218 | mant1 <<= 1; |
219 | mant1 <<= 1; |
219 | if(mant1 == 0) { |
220 | if(mant1 == 0) { |
220 | /* Realy is it an underflow? ... */ |
221 | /* Realy is it an underflow? ... */ |
221 | //TODO: fix underflow |
222 | /* TODO: fix underflow */ |
222 | }; |
223 | }; |
223 | }; |
224 | }; |
224 | 225 | ||
225 | //rounding - if first bit after mantisa is set then round up |
226 | //rounding - if first bit after mantisa is set then round up |
226 | mant1 += 0x20; |
227 | mant1 += 0x20; |
227 | 228 | ||
228 | if (mant1&0x40000000) { |
229 | if (mant1&0x40000000) { |
229 | ++exp1; |
230 | ++exp1; |
230 | mant1>>=1; |
231 | mant1>>=1; |
231 | }; |
232 | }; |
232 | 233 | ||
233 | result.parts.mantisa = ((mant1&(~0x20000000))>>6); /*Clear hidden bit and shift */ |
234 | result.parts.mantisa = ((mant1&(~0x20000000))>>6); /*Clear hidden bit and shift */ |
234 | result.parts.exp = exp1; |
235 | result.parts.exp = exp1; |
235 | 236 | ||
236 | return result; |
237 | return result; |
237 | }; |
238 | }; |
238 | 239 | ||
- | 240 | /** Multiply two 32 bit float numbers |
|
- | 241 | * |
|
- | 242 | */ |
|
- | 243 | float32 mulFloat32(float32 a, float32 b) |
|
- | 244 | { |
|
- | 245 | float32 result; |
|
- | 246 | __u64 mant1, mant2; |
|
- | 247 | __s32 exp; |
|
- | 248 | ||
- | 249 | result.parts.sign = a.parts.sign ^ b.parts.sign; |
|
- | 250 | ||
- | 251 | if ((isFloat32NaN(a))||(isFloat32NaN(b))) { |
|
- | 252 | /* TODO: fix SigNaNs */ |
|
- | 253 | if (isFloat32SigNaN(a)) { |
|
- | 254 | result.parts.mantisa = a.parts.mantisa; |
|
- | 255 | result.parts.exp = a.parts.exp; |
|
- | 256 | return result; |
|
- | 257 | }; |
|
- | 258 | if (isFloat32SigNaN(b)) { /* TODO: fix SigNaN */ |
|
- | 259 | result.parts.mantisa = b.parts.mantisa; |
|
- | 260 | result.parts.exp = b.parts.exp; |
|
- | 261 | return result; |
|
- | 262 | }; |
|
- | 263 | /* set NaN as result */ |
|
- | 264 | result.parts.mantisa = 0x1; |
|
- | 265 | result.parts.exp = 0xFF; |
|
- | 266 | return result; |
|
- | 267 | }; |
|
- | 268 | ||
- | 269 | if (isFloat32Infinity(a)) { |
|
- | 270 | if (isFloat32Zero(b)) { |
|
- | 271 | /* FIXME: zero * infinity */ |
|
- | 272 | result.parts.mantisa = 0x1; |
|
- | 273 | result.parts.exp = 0xFF; |
|
- | 274 | return result; |
|
- | 275 | } |
|
- | 276 | result.parts.mantisa = a.parts.mantisa; |
|
- | 277 | result.parts.exp = a.parts.exp; |
|
- | 278 | return result; |
|
- | 279 | } |
|
- | 280 | ||
- | 281 | if (isFloat32Infinity(b)) { |
|
- | 282 | if (isFloat32Zero(a)) { |
|
- | 283 | /* FIXME: zero * infinity */ |
|
- | 284 | result.parts.mantisa = 0x1; |
|
- | 285 | result.parts.exp = 0xFF; |
|
- | 286 | return result; |
|
- | 287 | } |
|
- | 288 | result.parts.mantisa = b.parts.mantisa; |
|
- | 289 | result.parts.exp = b.parts.exp; |
|
- | 290 | return result; |
|
- | 291 | } |
|
- | 292 | ||
- | 293 | /* exp is signed so we can easy detect underflow */ |
|
- | 294 | exp = a.parts.exp + b.parts.exp; |
|
- | 295 | exp -= FLOAT32_BIAS; |
|
- | 296 | ||
- | 297 | if (exp >= 0xFF ) { |
|
- | 298 | /* FIXME: overflow */ |
|
- | 299 | /* set infinity as result */ |
|
- | 300 | result.parts.mantisa = 0x0; |
|
- | 301 | result.parts.exp = 0xFF; |
|
- | 302 | return result; |
|
- | 303 | }; |
|
- | 304 | ||
- | 305 | if (exp < 0) { |
|
- | 306 | /* FIXME: underflow */ |
|
- | 307 | /* return signed zero */ |
|
- | 308 | result.parts.mantisa = 0x0; |
|
- | 309 | result.parts.exp = 0x0; |
|
- | 310 | return result; |
|
- | 311 | }; |
|
- | 312 | ||
- | 313 | mant1 = a.parts.mantisa; |
|
- | 314 | if (a.parts.exp>0) { |
|
- | 315 | mant1 |= 0x800000; |
|
- | 316 | } else { |
|
- | 317 | ++exp; |
|
- | 318 | }; |
|
- | 319 | ||
- | 320 | mant2 = b.parts.mantisa; |
|
- | 321 | if (b.parts.exp>0) { |
|
- | 322 | mant2 |= 0x800000; |
|
- | 323 | } else { |
|
- | 324 | ++exp; |
|
- | 325 | }; |
|
- | 326 | ||
- | 327 | mant1 <<= 1; /* one bit space for rounding */ |
|
- | 328 | ||
- | 329 | mant1 = mant1 * mant2; |
|
- | 330 | /* round and return */ |
|
- | 331 | ||
- | 332 | while ((exp < 0xFF )&&(mant1 > 0x1FFFFFF )) { /* 0xFFFFFF is 23 bits of mantisa + one more for hidden bit (all shifted 1 bit left)*/ |
|
- | 333 | ++exp; |
|
- | 334 | mant1 >>= 1; |
|
- | 335 | }; |
|
- | 336 | ||
- | 337 | /* rounding */ |
|
- | 338 | //++mant1; /* FIXME: not works - without it is ok */ |
|
- | 339 | mant1 >>= 1; /* shift off rounding space */ |
|
- | 340 | ||
- | 341 | if ((exp < 0xFF )&&(mant1 > 0xFFFFFF )) { |
|
- | 342 | ++exp; |
|
- | 343 | mant1 >>= 1; |
|
- | 344 | }; |
|
- | 345 | ||
- | 346 | if (exp >= 0xFF ) { |
|
- | 347 | /* TODO: fix overflow */ |
|
- | 348 | /* return infinity*/ |
|
- | 349 | result.parts.exp = 0xFF; |
|
- | 350 | result.parts.mantisa = 0x0; |
|
- | 351 | return result; |
|
- | 352 | } |
|
- | 353 | ||
- | 354 | exp -= FLOAT32_MANTISA_SIZE; |
|
- | 355 | ||
- | 356 | if (exp <= FLOAT32_MANTISA_SIZE) { |
|
- | 357 | /* denormalized number */ |
|
- | 358 | mant1 >>= 1; /* denormalize */ |
|
- | 359 | while ((mant1 > 0) && (exp < 0)) { |
|
- | 360 | mant1 >>= 1; |
|
- | 361 | ++exp; |
|
- | 362 | }; |
|
- | 363 | if (mant1 == 0) { |
|
- | 364 | /* FIXME : underflow */ |
|
- | 365 | result.parts.exp = 0; |
|
- | 366 | result.parts.mantisa = 0; |
|
- | 367 | return result; |
|
- | 368 | }; |
|
- | 369 | }; |
|
- | 370 | result.parts.exp = exp; |
|
- | 371 | result.parts.mantisa = mant1 & 0x7FFFFF; |
|
- | 372 | ||
- | 373 | return result; |
|
- | 374 | ||
- | 375 | }; |
|
- | 376 | ||
- | 377 | ||
239 | 378 |