Rev 1035 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
535 | cejka | 1 | /* |
2 | * Copyright (C) 2005 Josef Cejka |
||
3 | * All rights reserved. |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
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 |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
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 |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
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 |
||
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 |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
1657 | cejka | 29 | /** @defgroup sfl Softfloat |
30 | * @brief Software FPU emulation. |
||
31 | * @{ |
||
32 | * @} |
||
33 | */ |
||
34 | /** @addtogroup softfloat generic |
||
35 | * @ingroup sfl |
||
36 | * @brief Architecture independent parts of FPU software emulation library. |
||
37 | * @{ |
||
38 | */ |
||
39 | /** @file |
||
40 | */ |
||
41 | |||
535 | cejka | 42 | #include<softfloat.h> |
647 | cejka | 43 | #include<sftypes.h> |
731 | cejka | 44 | |
45 | #include<add.h> |
||
46 | #include<sub.h> |
||
47 | #include<mul.h> |
||
48 | #include<div.h> |
||
49 | |||
647 | cejka | 50 | #include<conversion.h> |
51 | #include<comparison.h> |
||
52 | #include<other.h> |
||
535 | cejka | 53 | |
857 | cejka | 54 | #include<functions.h> |
55 | |||
647 | cejka | 56 | /* Arithmetic functions */ |
542 | cejka | 57 | |
58 | float __addsf3(float a, float b) |
||
59 | { |
||
60 | float32 fa, fb; |
||
804 | cejka | 61 | fa.f = a; |
62 | fb.f = b; |
||
63 | if (fa.parts.sign != fb.parts.sign) { |
||
647 | cejka | 64 | if (fa.parts.sign) { |
804 | cejka | 65 | fa.parts.sign = 0; |
66 | return subFloat32(fb, fa).f; |
||
647 | cejka | 67 | }; |
804 | cejka | 68 | fb.parts.sign = 0; |
69 | return subFloat32(fa, fb).f; |
||
647 | cejka | 70 | } |
804 | cejka | 71 | return addFloat32(fa, fb).f; |
698 | cejka | 72 | } |
542 | cejka | 73 | |
734 | cejka | 74 | double __adddf3(double a, double b) |
75 | { |
||
76 | float64 da, db; |
||
804 | cejka | 77 | da.d = a; |
78 | db.d = b; |
||
79 | if (da.parts.sign != db.parts.sign) { |
||
734 | cejka | 80 | if (da.parts.sign) { |
804 | cejka | 81 | da.parts.sign = 0; |
82 | return subFloat64(db, da).d; |
||
734 | cejka | 83 | }; |
804 | cejka | 84 | db.parts.sign = 0; |
85 | return subFloat64(da, db).d; |
||
734 | cejka | 86 | } |
804 | cejka | 87 | return addFloat64(da, db).d; |
734 | cejka | 88 | } |
89 | |||
542 | cejka | 90 | float __subsf3(float a, float b) |
91 | { |
||
92 | float32 fa, fb; |
||
804 | cejka | 93 | fa.f = a; |
94 | fb.f = b; |
||
95 | if (fa.parts.sign != fb.parts.sign) { |
||
96 | fb.parts.sign = !fb.parts.sign; |
||
97 | return addFloat32(fa, fb).f; |
||
647 | cejka | 98 | } |
804 | cejka | 99 | return subFloat32(fa, fb).f; |
698 | cejka | 100 | } |
542 | cejka | 101 | |
734 | cejka | 102 | double __subdf3(double a, double b) |
103 | { |
||
104 | float64 da, db; |
||
105 | da.d = a; |
||
106 | db.d = b; |
||
107 | if (da.parts.sign != db.parts.sign) { |
||
108 | db.parts.sign = !db.parts.sign; |
||
109 | return addFloat64(da, db).d; |
||
110 | } |
||
111 | return subFloat64(da, db).d; |
||
112 | } |
||
113 | |||
688 | cejka | 114 | float __mulsf3(float a, float b) |
115 | { |
||
116 | float32 fa, fb; |
||
804 | cejka | 117 | fa.f = a; |
118 | fb.f = b; |
||
688 | cejka | 119 | return mulFloat32(fa, fb).f; |
120 | } |
||
121 | |||
737 | cejka | 122 | double __muldf3(double a, double b) |
123 | { |
||
124 | float64 da, db; |
||
125 | da.d = a; |
||
126 | db.d = b; |
||
127 | return mulFloat64(da, db).d; |
||
128 | } |
||
129 | |||
697 | cejka | 130 | float __divsf3(float a, float b) |
131 | { |
||
132 | float32 fa, fb; |
||
804 | cejka | 133 | fa.f = a; |
134 | fb.f = b; |
||
135 | return divFloat32(fa, fb).f; |
||
698 | cejka | 136 | } |
697 | cejka | 137 | |
828 | cejka | 138 | double __divdf3(double a, double b) |
139 | { |
||
140 | float64 da, db; |
||
141 | da.d = a; |
||
142 | db.d = b; |
||
143 | return divFloat64(da, db).d; |
||
144 | } |
||
145 | |||
542 | cejka | 146 | float __negsf2(float a) |
147 | { |
||
148 | float32 fa; |
||
804 | cejka | 149 | fa.f = a; |
150 | fa.parts.sign = !fa.parts.sign; |
||
542 | cejka | 151 | return fa.f; |
698 | cejka | 152 | } |
542 | cejka | 153 | |
154 | double __negdf2(double a) |
||
155 | { |
||
156 | float64 fa; |
||
804 | cejka | 157 | fa.d = a; |
158 | fa.parts.sign = !fa.parts.sign; |
||
563 | cejka | 159 | return fa.d; |
698 | cejka | 160 | } |
542 | cejka | 161 | |
647 | cejka | 162 | /* Conversion functions */ |
542 | cejka | 163 | |
697 | cejka | 164 | double __extendsfdf2(float a) |
165 | { |
||
166 | float32 fa; |
||
167 | fa.f = a; |
||
168 | return convertFloat32ToFloat64(fa).d; |
||
698 | cejka | 169 | } |
697 | cejka | 170 | |
171 | float __truncdfsf2(double a) |
||
172 | { |
||
173 | float64 da; |
||
174 | da.d = a; |
||
175 | return convertFloat64ToFloat32(da).f; |
||
176 | } |
||
698 | cejka | 177 | |
857 | cejka | 178 | int __fixsfsi(float a) |
179 | { |
||
180 | float32 fa; |
||
181 | fa.f = a; |
||
182 | |||
183 | return float32_to_int(fa); |
||
184 | } |
||
185 | int __fixdfsi(double a) |
||
186 | { |
||
865 | cejka | 187 | float64 da; |
188 | da.d = a; |
||
189 | |||
190 | return float64_to_int(da); |
||
857 | cejka | 191 | } |
192 | |||
193 | long __fixsfdi(float a) |
||
194 | { |
||
195 | float32 fa; |
||
196 | fa.f = a; |
||
197 | |||
198 | return float32_to_long(fa); |
||
199 | } |
||
200 | long __fixdfdi(double a) |
||
201 | { |
||
865 | cejka | 202 | float64 da; |
203 | da.d = a; |
||
204 | |||
205 | return float64_to_long(da); |
||
857 | cejka | 206 | } |
207 | |||
208 | long long __fixsfti(float a) |
||
209 | { |
||
865 | cejka | 210 | float32 fa; |
211 | fa.f = a; |
||
212 | |||
213 | return float32_to_longlong(fa); |
||
857 | cejka | 214 | } |
215 | long long __fixdfti(double a) |
||
216 | { |
||
865 | cejka | 217 | float64 da; |
218 | da.d = a; |
||
219 | |||
220 | return float64_to_longlong(da); |
||
857 | cejka | 221 | } |
222 | |||
223 | unsigned int __fixunssfsi(float a) |
||
224 | { |
||
225 | float32 fa; |
||
226 | fa.f = a; |
||
227 | |||
228 | return float32_to_uint(fa); |
||
229 | } |
||
230 | unsigned int __fixunsdfsi(double a) |
||
231 | { |
||
865 | cejka | 232 | float64 da; |
233 | da.d = a; |
||
234 | |||
235 | return float64_to_uint(da); |
||
857 | cejka | 236 | } |
237 | |||
238 | unsigned long __fixunssfdi(float a) |
||
239 | { |
||
240 | float32 fa; |
||
241 | fa.f = a; |
||
242 | |||
858 | cejka | 243 | return float32_to_ulong(fa); |
857 | cejka | 244 | } |
245 | unsigned long __fixunsdfdi(double a) |
||
246 | { |
||
865 | cejka | 247 | float64 da; |
248 | da.d = a; |
||
249 | |||
250 | return float64_to_ulong(da); |
||
857 | cejka | 251 | } |
252 | |||
253 | unsigned long long __fixunssfti(float a) |
||
254 | { |
||
865 | cejka | 255 | float32 fa; |
256 | fa.f = a; |
||
257 | |||
258 | return float32_to_ulonglong(fa); |
||
857 | cejka | 259 | } |
260 | unsigned long long __fixunsdfti(double a) |
||
261 | { |
||
865 | cejka | 262 | float64 da; |
263 | da.d = a; |
||
264 | |||
265 | return float64_to_ulonglong(da); |
||
857 | cejka | 266 | } |
267 | |||
268 | float __floatsisf(int i) |
||
269 | { |
||
874 | cejka | 270 | float32 fa; |
271 | |||
272 | fa = int_to_float32(i); |
||
273 | return fa.f; |
||
857 | cejka | 274 | } |
275 | double __floatsidf(int i) |
||
276 | { |
||
876 | cejka | 277 | float64 da; |
278 | |||
279 | da = int_to_float64(i); |
||
280 | return da.d; |
||
857 | cejka | 281 | } |
282 | |||
283 | float __floatdisf(long i) |
||
284 | { |
||
874 | cejka | 285 | float32 fa; |
286 | |||
287 | fa = long_to_float32(i); |
||
288 | return fa.f; |
||
857 | cejka | 289 | } |
290 | double __floatdidf(long i) |
||
291 | { |
||
876 | cejka | 292 | float64 da; |
293 | |||
294 | da = long_to_float64(i); |
||
295 | return da.d; |
||
857 | cejka | 296 | } |
297 | |||
298 | float __floattisf(long long i) |
||
299 | { |
||
874 | cejka | 300 | float32 fa; |
301 | |||
302 | fa = longlong_to_float32(i); |
||
303 | return fa.f; |
||
857 | cejka | 304 | } |
305 | double __floattidf(long long i) |
||
306 | { |
||
876 | cejka | 307 | float64 da; |
308 | |||
309 | da = longlong_to_float64(i); |
||
310 | return da.d; |
||
857 | cejka | 311 | } |
312 | |||
313 | float __floatunsisf(unsigned int i) |
||
314 | { |
||
874 | cejka | 315 | float32 fa; |
316 | |||
317 | fa = uint_to_float32(i); |
||
318 | return fa.f; |
||
857 | cejka | 319 | } |
320 | double __floatunsidf(unsigned int i) |
||
321 | { |
||
876 | cejka | 322 | float64 da; |
323 | |||
324 | da = uint_to_float64(i); |
||
325 | return da.d; |
||
857 | cejka | 326 | } |
327 | |||
328 | float __floatundisf(unsigned long i) |
||
329 | { |
||
874 | cejka | 330 | float32 fa; |
331 | |||
332 | fa = ulong_to_float32(i); |
||
333 | return fa.f; |
||
857 | cejka | 334 | } |
335 | double __floatundidf(unsigned long i) |
||
336 | { |
||
876 | cejka | 337 | float64 da; |
338 | |||
339 | da = ulong_to_float64(i); |
||
340 | return da.d; |
||
857 | cejka | 341 | } |
342 | |||
343 | float __floatuntisf(unsigned long long i) |
||
344 | { |
||
874 | cejka | 345 | float32 fa; |
346 | |||
347 | fa = ulonglong_to_float32(i); |
||
348 | return fa.f; |
||
857 | cejka | 349 | } |
350 | double __floatuntidf(unsigned long long i) |
||
351 | { |
||
876 | cejka | 352 | float64 da; |
353 | |||
354 | da = ulonglong_to_float64(i); |
||
355 | return da.d; |
||
857 | cejka | 356 | } |
357 | |||
647 | cejka | 358 | /* Comparison functions */ |
857 | cejka | 359 | /* Comparison functions */ |
542 | cejka | 360 | |
652 | cejka | 361 | /* a<b .. -1 |
362 | * a=b .. 0 |
||
363 | * a>b .. 1 |
||
364 | * */ |
||
365 | |||
660 | cejka | 366 | int __cmpsf2(float a, float b) |
652 | cejka | 367 | { |
804 | cejka | 368 | float32 fa, fb; |
369 | fa.f = a; |
||
370 | fb.f = b; |
||
371 | if ( (isFloat32NaN(fa)) || (isFloat32NaN(fb)) ) { |
||
652 | cejka | 372 | return 1; /* no special constant for unordered - maybe signaled? */ |
373 | }; |
||
374 | |||
375 | |||
804 | cejka | 376 | if (isFloat32eq(fa, fb)) { |
652 | cejka | 377 | return 0; |
378 | }; |
||
379 | |||
804 | cejka | 380 | if (isFloat32lt(fa, fb)) { |
652 | cejka | 381 | return -1; |
382 | }; |
||
383 | return 1; |
||
384 | } |
||
385 | |||
386 | int __unordsf2(float a, float b) |
||
387 | { |
||
804 | cejka | 388 | float32 fa, fb; |
389 | fa.f = a; |
||
390 | fb.f = b; |
||
391 | return ( (isFloat32NaN(fa)) || (isFloat32NaN(fb)) ); |
||
698 | cejka | 392 | } |
652 | cejka | 393 | |
394 | /** |
||
395 | * @return zero, if neither argument is a NaN and are equal |
||
396 | * */ |
||
397 | int __eqsf2(float a, float b) |
||
398 | { |
||
804 | cejka | 399 | float32 fa, fb; |
400 | fa.f = a; |
||
401 | fb.f = b; |
||
402 | if ( (isFloat32NaN(fa)) || (isFloat32NaN(fb)) ) { |
||
652 | cejka | 403 | /* TODO: sigNaNs*/ |
404 | return 1; |
||
405 | }; |
||
804 | cejka | 406 | return isFloat32eq(fa, fb) - 1; |
698 | cejka | 407 | } |
652 | cejka | 408 | |
409 | /* strange behavior, but it was in gcc documentation */ |
||
410 | int __nesf2(float a, float b) |
||
411 | { |
||
804 | cejka | 412 | return __eqsf2(a, b); |
698 | cejka | 413 | } |
653 | cejka | 414 | |
415 | /* return value >= 0 if a>=b and neither is NaN */ |
||
416 | int __gesf2(float a, float b) |
||
417 | { |
||
804 | cejka | 418 | float32 fa, fb; |
419 | fa.f = a; |
||
420 | fb.f = b; |
||
421 | if ( (isFloat32NaN(fa)) || (isFloat32NaN(fb)) ) { |
||
653 | cejka | 422 | /* TODO: sigNaNs*/ |
660 | cejka | 423 | return -1; |
653 | cejka | 424 | }; |
425 | |||
804 | cejka | 426 | if (isFloat32eq(fa, fb)) { |
653 | cejka | 427 | return 0; |
428 | }; |
||
429 | |||
804 | cejka | 430 | if (isFloat32gt(fa, fb)) { |
653 | cejka | 431 | return 1; |
432 | }; |
||
433 | |||
434 | return -1; |
||
435 | } |
||
436 | |||
437 | /** Return negative value, if a<b and neither is NaN*/ |
||
438 | int __ltsf2(float a, float b) |
||
439 | { |
||
804 | cejka | 440 | float32 fa, fb; |
441 | fa.f = a; |
||
442 | fb.f = b; |
||
443 | if ( (isFloat32NaN(fa)) || (isFloat32NaN(fb)) ) { |
||
653 | cejka | 444 | /* TODO: sigNaNs*/ |
445 | return 1; |
||
446 | }; |
||
447 | if (isFloat32lt(fa, fb)) { |
||
448 | return -1; |
||
449 | }; |
||
450 | return 0; |
||
451 | } |
||
452 | |||
453 | /* return value <= 0 if a<=b and neither is NaN */ |
||
454 | int __lesf2(float a, float b) |
||
455 | { |
||
804 | cejka | 456 | float32 fa, fb; |
457 | fa.f = a; |
||
458 | fb.f = b; |
||
459 | if ( (isFloat32NaN(fa)) || (isFloat32NaN(fb)) ) { |
||
653 | cejka | 460 | /* TODO: sigNaNs*/ |
461 | return 1; |
||
462 | }; |
||
463 | |||
804 | cejka | 464 | if (isFloat32eq(fa, fb)) { |
653 | cejka | 465 | return 0; |
466 | }; |
||
467 | |||
804 | cejka | 468 | if (isFloat32lt(fa, fb)) { |
653 | cejka | 469 | return -1; |
470 | }; |
||
471 | |||
472 | return 1; |
||
473 | } |
||
474 | |||
475 | /** Return positive value, if a>b and neither is NaN*/ |
||
660 | cejka | 476 | int __gtsf2(float a, float b) |
653 | cejka | 477 | { |
804 | cejka | 478 | float32 fa, fb; |
479 | fa.f = a; |
||
480 | fb.f = b; |
||
481 | if ( (isFloat32NaN(fa)) || (isFloat32NaN(fb)) ) { |
||
653 | cejka | 482 | /* TODO: sigNaNs*/ |
660 | cejka | 483 | return -1; |
653 | cejka | 484 | }; |
485 | if (isFloat32gt(fa, fb)) { |
||
486 | return 1; |
||
487 | }; |
||
488 | return 0; |
||
489 | } |
||
490 | |||
647 | cejka | 491 | /* Other functions */ |
542 | cejka | 492 | |
698 | cejka | 493 | float __powisf2(float a, int b) |
494 | { |
||
804 | cejka | 495 | /* TODO: */ |
1035 | cejka | 496 | float32 fa; |
497 | fa.binary = FLOAT32_NAN; |
||
498 | return fa.f; |
||
698 | cejka | 499 | } |
500 | |||
1657 | cejka | 501 | |
502 | /** @} |
||
503 | */ |
||
504 |