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