source: mainline/uspace/lib/softfloat/conversion.c@ f1380b7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f1380b7 was a35b458, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

style: Remove trailing whitespace on _all_ lines, including empty ones, for particular file types.

Command used: tools/srepl '\s\+$' '' -- *.c *.h *.py *.sh *.s *.S *.ag

Currently, whitespace on empty lines is very inconsistent.
There are two basic choices: Either remove the whitespace, or keep empty lines
indented to the level of surrounding code. The former is AFAICT more common,
and also much easier to do automatically.

Alternatively, we could write script for automatic indentation, and use that
instead. However, if such a script exists, it's possible to use the indented
style locally, by having the editor apply relevant conversions on load/save,
without affecting remote repository. IMO, it makes more sense to adopt
the simpler rule.

  • Property mode set to 100644
File size: 27.5 KB
Line 
1/*
2 * Copyright (c) 2005 Josef Cejka
3 * Copyright (c) 2011 Petr Koupy
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup softfloat
31 * @{
32 */
33/** @file Conversion of precision and conversion between integers and floats.
34 */
35
36#include "conversion.h"
37#include "comparison.h"
38#include "common.h"
39
40float64 float32_to_float64(float32 a)
41{
42 float64 result;
43 uint64_t frac;
44
45 result.parts.sign = a.parts.sign;
46 result.parts.fraction = a.parts.fraction;
47 result.parts.fraction <<= (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE);
48
49 if ((is_float32_infinity(a)) || (is_float32_nan(a))) {
50 result.parts.exp = FLOAT64_MAX_EXPONENT;
51 // TODO; check if its correct for SigNaNs
52 return result;
53 }
54
55 result.parts.exp = a.parts.exp + ((int) FLOAT64_BIAS - FLOAT32_BIAS);
56 if (a.parts.exp == 0) {
57 /* normalize denormalized numbers */
58
59 if (result.parts.fraction == 0) { /* fix zero */
60 result.parts.exp = 0;
61 return result;
62 }
63
64 frac = result.parts.fraction;
65
66 while (!(frac & FLOAT64_HIDDEN_BIT_MASK)) {
67 frac <<= 1;
68 --result.parts.exp;
69 }
70
71 ++result.parts.exp;
72 result.parts.fraction = frac;
73 }
74
75 return result;
76}
77
78float128 float32_to_float128(float32 a)
79{
80 float128 result;
81 uint64_t frac_hi, frac_lo;
82 uint64_t tmp_hi, tmp_lo;
83
84 result.parts.sign = a.parts.sign;
85 result.parts.frac_hi = 0;
86 result.parts.frac_lo = a.parts.fraction;
87 lshift128(result.parts.frac_hi, result.parts.frac_lo,
88 (FLOAT128_FRACTION_SIZE - FLOAT32_FRACTION_SIZE),
89 &frac_hi, &frac_lo);
90 result.parts.frac_hi = frac_hi;
91 result.parts.frac_lo = frac_lo;
92
93 if ((is_float32_infinity(a)) || (is_float32_nan(a))) {
94 result.parts.exp = FLOAT128_MAX_EXPONENT;
95 // TODO; check if its correct for SigNaNs
96 return result;
97 }
98
99 result.parts.exp = a.parts.exp + ((int) FLOAT128_BIAS - FLOAT32_BIAS);
100 if (a.parts.exp == 0) {
101 /* normalize denormalized numbers */
102
103 if (eq128(result.parts.frac_hi,
104 result.parts.frac_lo, 0x0ll, 0x0ll)) { /* fix zero */
105 result.parts.exp = 0;
106 return result;
107 }
108
109 frac_hi = result.parts.frac_hi;
110 frac_lo = result.parts.frac_lo;
111
112 and128(frac_hi, frac_lo,
113 FLOAT128_HIDDEN_BIT_MASK_HI, FLOAT128_HIDDEN_BIT_MASK_LO,
114 &tmp_hi, &tmp_lo);
115 while (!lt128(0x0ll, 0x0ll, tmp_hi, tmp_lo)) {
116 lshift128(frac_hi, frac_lo, 1, &frac_hi, &frac_lo);
117 --result.parts.exp;
118 }
119
120 ++result.parts.exp;
121 result.parts.frac_hi = frac_hi;
122 result.parts.frac_lo = frac_lo;
123 }
124
125 return result;
126}
127
128float128 float64_to_float128(float64 a)
129{
130 float128 result;
131 uint64_t frac_hi, frac_lo;
132 uint64_t tmp_hi, tmp_lo;
133
134 result.parts.sign = a.parts.sign;
135 result.parts.frac_hi = 0;
136 result.parts.frac_lo = a.parts.fraction;
137 lshift128(result.parts.frac_hi, result.parts.frac_lo,
138 (FLOAT128_FRACTION_SIZE - FLOAT64_FRACTION_SIZE),
139 &frac_hi, &frac_lo);
140 result.parts.frac_hi = frac_hi;
141 result.parts.frac_lo = frac_lo;
142
143 if ((is_float64_infinity(a)) || (is_float64_nan(a))) {
144 result.parts.exp = FLOAT128_MAX_EXPONENT;
145 // TODO; check if its correct for SigNaNs
146 return result;
147 }
148
149 result.parts.exp = a.parts.exp + ((int) FLOAT128_BIAS - FLOAT64_BIAS);
150 if (a.parts.exp == 0) {
151 /* normalize denormalized numbers */
152
153 if (eq128(result.parts.frac_hi,
154 result.parts.frac_lo, 0x0ll, 0x0ll)) { /* fix zero */
155 result.parts.exp = 0;
156 return result;
157 }
158
159 frac_hi = result.parts.frac_hi;
160 frac_lo = result.parts.frac_lo;
161
162 and128(frac_hi, frac_lo,
163 FLOAT128_HIDDEN_BIT_MASK_HI, FLOAT128_HIDDEN_BIT_MASK_LO,
164 &tmp_hi, &tmp_lo);
165 while (!lt128(0x0ll, 0x0ll, tmp_hi, tmp_lo)) {
166 lshift128(frac_hi, frac_lo, 1, &frac_hi, &frac_lo);
167 --result.parts.exp;
168 }
169
170 ++result.parts.exp;
171 result.parts.frac_hi = frac_hi;
172 result.parts.frac_lo = frac_lo;
173 }
174
175 return result;
176}
177
178float32 float64_to_float32(float64 a)
179{
180 float32 result;
181 int32_t exp;
182 uint64_t frac;
183
184 result.parts.sign = a.parts.sign;
185
186 if (is_float64_nan(a)) {
187 result.parts.exp = FLOAT32_MAX_EXPONENT;
188
189 if (is_float64_signan(a)) {
190 /* set first bit of fraction nonzero */
191 result.parts.fraction = FLOAT32_HIDDEN_BIT_MASK >> 1;
192 return result;
193 }
194
195 /* fraction nonzero but its first bit is zero */
196 result.parts.fraction = 0x1;
197 return result;
198 }
199
200 if (is_float64_infinity(a)) {
201 result.parts.fraction = 0;
202 result.parts.exp = FLOAT32_MAX_EXPONENT;
203 return result;
204 }
205
206 exp = (int) a.parts.exp - FLOAT64_BIAS + FLOAT32_BIAS;
207
208 if (exp >= FLOAT32_MAX_EXPONENT) {
209 /* FIXME: overflow */
210 result.parts.fraction = 0;
211 result.parts.exp = FLOAT32_MAX_EXPONENT;
212 return result;
213 } else if (exp <= 0) {
214 /* underflow or denormalized */
215
216 result.parts.exp = 0;
217
218 exp *= -1;
219 if (exp > FLOAT32_FRACTION_SIZE) {
220 /* FIXME: underflow */
221 result.parts.fraction = 0;
222 return result;
223 }
224
225 /* denormalized */
226
227 frac = a.parts.fraction;
228 frac |= FLOAT64_HIDDEN_BIT_MASK; /* denormalize and set hidden bit */
229
230 frac >>= (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE + 1);
231
232 while (exp > 0) {
233 --exp;
234 frac >>= 1;
235 }
236 result.parts.fraction = frac;
237
238 return result;
239 }
240
241 result.parts.exp = exp;
242 result.parts.fraction =
243 a.parts.fraction >> (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE);
244 return result;
245}
246
247float32 float128_to_float32(float128 a)
248{
249 float32 result;
250 int32_t exp;
251 uint64_t frac_hi, frac_lo;
252
253 result.parts.sign = a.parts.sign;
254
255 if (is_float128_nan(a)) {
256 result.parts.exp = FLOAT32_MAX_EXPONENT;
257
258 if (is_float128_signan(a)) {
259 /* set first bit of fraction nonzero */
260 result.parts.fraction = FLOAT32_HIDDEN_BIT_MASK >> 1;
261 return result;
262 }
263
264 /* fraction nonzero but its first bit is zero */
265 result.parts.fraction = 0x1;
266 return result;
267 }
268
269 if (is_float128_infinity(a)) {
270 result.parts.fraction = 0;
271 result.parts.exp = FLOAT32_MAX_EXPONENT;
272 return result;
273 }
274
275 exp = (int) a.parts.exp - FLOAT128_BIAS + FLOAT32_BIAS;
276
277 if (exp >= FLOAT32_MAX_EXPONENT) {
278 /* FIXME: overflow */
279 result.parts.fraction = 0;
280 result.parts.exp = FLOAT32_MAX_EXPONENT;
281 return result;
282 } else if (exp <= 0) {
283 /* underflow or denormalized */
284
285 result.parts.exp = 0;
286
287 exp *= -1;
288 if (exp > FLOAT32_FRACTION_SIZE) {
289 /* FIXME: underflow */
290 result.parts.fraction = 0;
291 return result;
292 }
293
294 /* denormalized */
295
296 frac_hi = a.parts.frac_hi;
297 frac_lo = a.parts.frac_lo;
298
299 /* denormalize and set hidden bit */
300 frac_hi |= FLOAT128_HIDDEN_BIT_MASK_HI;
301
302 rshift128(frac_hi, frac_lo,
303 (FLOAT128_FRACTION_SIZE - FLOAT32_FRACTION_SIZE + 1),
304 &frac_hi, &frac_lo);
305
306 while (exp > 0) {
307 --exp;
308 rshift128(frac_hi, frac_lo, 1, &frac_hi, &frac_lo);
309 }
310 result.parts.fraction = frac_lo;
311
312 return result;
313 }
314
315 result.parts.exp = exp;
316 frac_hi = a.parts.frac_hi;
317 frac_lo = a.parts.frac_lo;
318 rshift128(frac_hi, frac_lo,
319 (FLOAT128_FRACTION_SIZE - FLOAT32_FRACTION_SIZE + 1),
320 &frac_hi, &frac_lo);
321 result.parts.fraction = frac_lo;
322 return result;
323}
324
325float64 float128_to_float64(float128 a)
326{
327 float64 result;
328 int32_t exp;
329 uint64_t frac_hi, frac_lo;
330
331 result.parts.sign = a.parts.sign;
332
333 if (is_float128_nan(a)) {
334 result.parts.exp = FLOAT64_MAX_EXPONENT;
335
336 if (is_float128_signan(a)) {
337 /* set first bit of fraction nonzero */
338 result.parts.fraction = FLOAT64_HIDDEN_BIT_MASK >> 1;
339 return result;
340 }
341
342 /* fraction nonzero but its first bit is zero */
343 result.parts.fraction = 0x1;
344 return result;
345 }
346
347 if (is_float128_infinity(a)) {
348 result.parts.fraction = 0;
349 result.parts.exp = FLOAT64_MAX_EXPONENT;
350 return result;
351 }
352
353 exp = (int) a.parts.exp - FLOAT128_BIAS + FLOAT64_BIAS;
354
355 if (exp >= FLOAT64_MAX_EXPONENT) {
356 /* FIXME: overflow */
357 result.parts.fraction = 0;
358 result.parts.exp = FLOAT64_MAX_EXPONENT;
359 return result;
360 } else if (exp <= 0) {
361 /* underflow or denormalized */
362
363 result.parts.exp = 0;
364
365 exp *= -1;
366 if (exp > FLOAT64_FRACTION_SIZE) {
367 /* FIXME: underflow */
368 result.parts.fraction = 0;
369 return result;
370 }
371
372 /* denormalized */
373
374 frac_hi = a.parts.frac_hi;
375 frac_lo = a.parts.frac_lo;
376
377 /* denormalize and set hidden bit */
378 frac_hi |= FLOAT128_HIDDEN_BIT_MASK_HI;
379
380 rshift128(frac_hi, frac_lo,
381 (FLOAT128_FRACTION_SIZE - FLOAT64_FRACTION_SIZE + 1),
382 &frac_hi, &frac_lo);
383
384 while (exp > 0) {
385 --exp;
386 rshift128(frac_hi, frac_lo, 1, &frac_hi, &frac_lo);
387 }
388 result.parts.fraction = frac_lo;
389
390 return result;
391 }
392
393 result.parts.exp = exp;
394 frac_hi = a.parts.frac_hi;
395 frac_lo = a.parts.frac_lo;
396 rshift128(frac_hi, frac_lo,
397 (FLOAT128_FRACTION_SIZE - FLOAT64_FRACTION_SIZE + 1),
398 &frac_hi, &frac_lo);
399 result.parts.fraction = frac_lo;
400 return result;
401}
402
403/** Helper procedure for converting float32 to uint32.
404 *
405 * @param a Floating point number in normalized form
406 * (NaNs or Inf are not checked).
407 * @return Converted unsigned integer.
408 */
409static uint32_t _float32_to_uint32_helper(float32 a)
410{
411 uint32_t frac;
412
413 if (a.parts.exp < FLOAT32_BIAS) {
414 /* TODO: rounding */
415 return 0;
416 }
417
418 frac = a.parts.fraction;
419
420 frac |= FLOAT32_HIDDEN_BIT_MASK;
421 /* shift fraction to left so hidden bit will be the most significant bit */
422 frac <<= 32 - FLOAT32_FRACTION_SIZE - 1;
423
424 frac >>= 32 - (a.parts.exp - FLOAT32_BIAS) - 1;
425 if ((a.parts.sign == 1) && (frac != 0)) {
426 frac = ~frac;
427 ++frac;
428 }
429
430 return frac;
431}
432
433/*
434 * FIXME: Im not sure what to return if overflow/underflow happens
435 * - now its the biggest or the smallest int
436 */
437uint32_t float32_to_uint32(float32 a)
438{
439 if (is_float32_nan(a))
440 return UINT32_MAX;
441
442 if (is_float32_infinity(a) || (a.parts.exp >= (32 + FLOAT32_BIAS))) {
443 if (a.parts.sign)
444 return UINT32_MIN;
445
446 return UINT32_MAX;
447 }
448
449 return _float32_to_uint32_helper(a);
450}
451
452/*
453 * FIXME: Im not sure what to return if overflow/underflow happens
454 * - now its the biggest or the smallest int
455 */
456int32_t float32_to_int32(float32 a)
457{
458 if (is_float32_nan(a))
459 return INT32_MAX;
460
461 if (is_float32_infinity(a) || (a.parts.exp >= (32 + FLOAT32_BIAS))) {
462 if (a.parts.sign)
463 return INT32_MIN;
464
465 return INT32_MAX;
466 }
467
468 return _float32_to_uint32_helper(a);
469}
470
471/** Helper procedure for converting float32 to uint64.
472 *
473 * @param a Floating point number in normalized form
474 * (NaNs or Inf are not checked).
475 * @return Converted unsigned integer.
476 */
477static uint64_t _float32_to_uint64_helper(float32 a)
478{
479 uint64_t frac;
480
481 if (a.parts.exp < FLOAT32_BIAS) {
482 // TODO: rounding
483 return 0;
484 }
485
486 frac = a.parts.fraction;
487
488 frac |= FLOAT32_HIDDEN_BIT_MASK;
489 /* shift fraction to left so hidden bit will be the most significant bit */
490 frac <<= 64 - FLOAT32_FRACTION_SIZE - 1;
491
492 frac >>= 64 - (a.parts.exp - FLOAT32_BIAS) - 1;
493 if ((a.parts.sign == 1) && (frac != 0)) {
494 frac = ~frac;
495 ++frac;
496 }
497
498 return frac;
499}
500
501/*
502 * FIXME: Im not sure what to return if overflow/underflow happens
503 * - now its the biggest or the smallest int
504 */
505uint64_t float32_to_uint64(float32 a)
506{
507 if (is_float32_nan(a))
508 return UINT64_MAX;
509
510 if (is_float32_infinity(a) || (a.parts.exp >= (64 + FLOAT32_BIAS))) {
511 if (a.parts.sign)
512 return UINT64_MIN;
513
514 return UINT64_MAX;
515 }
516
517 return _float32_to_uint64_helper(a);
518}
519
520/*
521 * FIXME: Im not sure what to return if overflow/underflow happens
522 * - now its the biggest or the smallest int
523 */
524int64_t float32_to_int64(float32 a)
525{
526 if (is_float32_nan(a))
527 return INT64_MAX;
528
529 if (is_float32_infinity(a) || (a.parts.exp >= (64 + FLOAT32_BIAS))) {
530 if (a.parts.sign)
531 return INT64_MIN;
532
533 return INT64_MAX;
534 }
535
536 return _float32_to_uint64_helper(a);
537}
538
539/** Helper procedure for converting float64 to uint64.
540 *
541 * @param a Floating point number in normalized form
542 * (NaNs or Inf are not checked).
543 * @return Converted unsigned integer.
544 */
545static uint64_t _float64_to_uint64_helper(float64 a)
546{
547 uint64_t frac;
548
549 if (a.parts.exp < FLOAT64_BIAS) {
550 // TODO: rounding
551 return 0;
552 }
553
554 frac = a.parts.fraction;
555
556 frac |= FLOAT64_HIDDEN_BIT_MASK;
557 /* shift fraction to left so hidden bit will be the most significant bit */
558 frac <<= 64 - FLOAT64_FRACTION_SIZE - 1;
559
560 frac >>= 64 - (a.parts.exp - FLOAT64_BIAS) - 1;
561 if ((a.parts.sign == 1) && (frac != 0)) {
562 frac = ~frac;
563 ++frac;
564 }
565
566 return frac;
567}
568
569/*
570 * FIXME: Im not sure what to return if overflow/underflow happens
571 * - now its the biggest or the smallest int
572 */
573uint32_t float64_to_uint32(float64 a)
574{
575 if (is_float64_nan(a))
576 return UINT32_MAX;
577
578 if (is_float64_infinity(a) || (a.parts.exp >= (32 + FLOAT64_BIAS))) {
579 if (a.parts.sign)
580 return UINT32_MIN;
581
582 return UINT32_MAX;
583 }
584
585 return (uint32_t) _float64_to_uint64_helper(a);
586}
587
588/*
589 * FIXME: Im not sure what to return if overflow/underflow happens
590 * - now its the biggest or the smallest int
591 */
592int32_t float64_to_int32(float64 a)
593{
594 if (is_float64_nan(a))
595 return INT32_MAX;
596
597 if (is_float64_infinity(a) || (a.parts.exp >= (32 + FLOAT64_BIAS))) {
598 if (a.parts.sign)
599 return INT32_MIN;
600
601 return INT32_MAX;
602 }
603
604 return (int32_t) _float64_to_uint64_helper(a);
605}
606
607/*
608 * FIXME: Im not sure what to return if overflow/underflow happens
609 * - now its the biggest or the smallest int
610 */
611uint64_t float64_to_uint64(float64 a)
612{
613 if (is_float64_nan(a))
614 return UINT64_MAX;
615
616 if (is_float64_infinity(a) || (a.parts.exp >= (64 + FLOAT64_BIAS))) {
617 if (a.parts.sign)
618 return UINT64_MIN;
619
620 return UINT64_MAX;
621 }
622
623 return _float64_to_uint64_helper(a);
624}
625
626/*
627 * FIXME: Im not sure what to return if overflow/underflow happens
628 * - now its the biggest or the smallest int
629 */
630int64_t float64_to_int64(float64 a)
631{
632 if (is_float64_nan(a))
633 return INT64_MAX;
634
635 if (is_float64_infinity(a) || (a.parts.exp >= (64 + FLOAT64_BIAS))) {
636 if (a.parts.sign)
637 return INT64_MIN;
638
639 return INT64_MAX;
640 }
641
642 return _float64_to_uint64_helper(a);
643}
644
645/** Helper procedure for converting float128 to uint64.
646 *
647 * @param a Floating point number in normalized form
648 * (NaNs or Inf are not checked).
649 * @return Converted unsigned integer.
650 */
651static uint64_t _float128_to_uint64_helper(float128 a)
652{
653 uint64_t frac_hi, frac_lo;
654
655 if (a.parts.exp < FLOAT128_BIAS) {
656 // TODO: rounding
657 return 0;
658 }
659
660 frac_hi = a.parts.frac_hi;
661 frac_lo = a.parts.frac_lo;
662
663 frac_hi |= FLOAT128_HIDDEN_BIT_MASK_HI;
664 /* shift fraction to left so hidden bit will be the most significant bit */
665 lshift128(frac_hi, frac_lo,
666 (128 - FLOAT128_FRACTION_SIZE - 1), &frac_hi, &frac_lo);
667
668 rshift128(frac_hi, frac_lo,
669 (128 - (a.parts.exp - FLOAT128_BIAS) - 1), &frac_hi, &frac_lo);
670 if ((a.parts.sign == 1) && !eq128(frac_hi, frac_lo, 0x0ll, 0x0ll)) {
671 not128(frac_hi, frac_lo, &frac_hi, &frac_lo);
672 add128(frac_hi, frac_lo, 0x0ll, 0x1ll, &frac_hi, &frac_lo);
673 }
674
675 return frac_lo;
676}
677
678/*
679 * FIXME: Im not sure what to return if overflow/underflow happens
680 * - now its the biggest or the smallest int
681 */
682uint32_t float128_to_uint32(float128 a)
683{
684 if (is_float128_nan(a))
685 return UINT32_MAX;
686
687 if (is_float128_infinity(a) || (a.parts.exp >= (32 + FLOAT128_BIAS))) {
688 if (a.parts.sign)
689 return UINT32_MIN;
690
691 return UINT32_MAX;
692 }
693
694 return (uint32_t) _float128_to_uint64_helper(a);
695}
696
697/*
698 * FIXME: Im not sure what to return if overflow/underflow happens
699 * - now its the biggest or the smallest int
700 */
701int32_t float128_to_int32(float128 a)
702{
703 if (is_float128_nan(a))
704 return INT32_MAX;
705
706 if (is_float128_infinity(a) || (a.parts.exp >= (32 + FLOAT128_BIAS))) {
707 if (a.parts.sign)
708 return INT32_MIN;
709
710 return INT32_MAX;
711 }
712
713 return (int32_t) _float128_to_uint64_helper(a);
714}
715
716/*
717 * FIXME: Im not sure what to return if overflow/underflow happens
718 * - now its the biggest or the smallest int
719 */
720uint64_t float128_to_uint64(float128 a)
721{
722 if (is_float128_nan(a))
723 return UINT64_MAX;
724
725 if (is_float128_infinity(a) || (a.parts.exp >= (64 + FLOAT128_BIAS))) {
726 if (a.parts.sign)
727 return UINT64_MIN;
728
729 return UINT64_MAX;
730 }
731
732 return _float128_to_uint64_helper(a);
733}
734
735/*
736 * FIXME: Im not sure what to return if overflow/underflow happens
737 * - now its the biggest or the smallest int
738 */
739int64_t float128_to_int64(float128 a)
740{
741 if (is_float128_nan(a))
742 return INT64_MAX;
743
744 if (is_float128_infinity(a) || (a.parts.exp >= (64 + FLOAT128_BIAS))) {
745 if (a.parts.sign)
746 return INT64_MIN;
747
748 return INT64_MAX;
749 }
750
751 return _float128_to_uint64_helper(a);
752}
753
754float32 uint32_to_float32(uint32_t i)
755{
756 int counter;
757 int32_t exp;
758 float32 result;
759
760 result.parts.sign = 0;
761 result.parts.fraction = 0;
762
763 counter = count_zeroes32(i);
764
765 exp = FLOAT32_BIAS + 32 - counter - 1;
766
767 if (counter == 32) {
768 result.bin = 0;
769 return result;
770 }
771
772 if (counter > 0) {
773 i <<= counter - 1;
774 } else {
775 i >>= 1;
776 }
777
778 round_float32(&exp, &i);
779
780 result.parts.fraction = i >> (32 - FLOAT32_FRACTION_SIZE - 2);
781 result.parts.exp = exp;
782
783 return result;
784}
785
786float32 int32_to_float32(int32_t i)
787{
788 float32 result;
789
790 if (i < 0)
791 result = uint32_to_float32((uint32_t) (-i));
792 else
793 result = uint32_to_float32((uint32_t) i);
794
795 result.parts.sign = i < 0;
796
797 return result;
798}
799
800float32 uint64_to_float32(uint64_t i)
801{
802 int counter;
803 int32_t exp;
804 uint32_t j;
805 float32 result;
806
807 result.parts.sign = 0;
808 result.parts.fraction = 0;
809
810 counter = count_zeroes64(i);
811
812 exp = FLOAT32_BIAS + 64 - counter - 1;
813
814 if (counter == 64) {
815 result.bin = 0;
816 return result;
817 }
818
819 /* Shift all to the first 31 bits (31st will be hidden 1) */
820 if (counter > 33) {
821 i <<= counter - 1 - 32;
822 } else {
823 i >>= 1 + 32 - counter;
824 }
825
826 j = (uint32_t) i;
827 round_float32(&exp, &j);
828
829 result.parts.fraction = j >> (32 - FLOAT32_FRACTION_SIZE - 2);
830 result.parts.exp = exp;
831 return result;
832}
833
834float32 int64_to_float32(int64_t i)
835{
836 float32 result;
837
838 if (i < 0)
839 result = uint64_to_float32((uint64_t) (-i));
840 else
841 result = uint64_to_float32((uint64_t) i);
842
843 result.parts.sign = i < 0;
844
845 return result;
846}
847
848float64 uint32_to_float64(uint32_t i)
849{
850 int counter;
851 int32_t exp;
852 float64 result;
853 uint64_t frac;
854
855 result.parts.sign = 0;
856 result.parts.fraction = 0;
857
858 counter = count_zeroes32(i);
859
860 exp = FLOAT64_BIAS + 32 - counter - 1;
861
862 if (counter == 32) {
863 result.bin = 0;
864 return result;
865 }
866
867 frac = i;
868 frac <<= counter + 32 - 1;
869
870 round_float64(&exp, &frac);
871
872 result.parts.fraction = frac >> (64 - FLOAT64_FRACTION_SIZE - 2);
873 result.parts.exp = exp;
874
875 return result;
876}
877
878float64 int32_to_float64(int32_t i)
879{
880 float64 result;
881
882 if (i < 0)
883 result = uint32_to_float64((uint32_t) (-i));
884 else
885 result = uint32_to_float64((uint32_t) i);
886
887 result.parts.sign = i < 0;
888
889 return result;
890}
891
892float64 uint64_to_float64(uint64_t i)
893{
894 int counter;
895 int32_t exp;
896 float64 result;
897
898 result.parts.sign = 0;
899 result.parts.fraction = 0;
900
901 counter = count_zeroes64(i);
902
903 exp = FLOAT64_BIAS + 64 - counter - 1;
904
905 if (counter == 64) {
906 result.bin = 0;
907 return result;
908 }
909
910 if (counter > 0) {
911 i <<= counter - 1;
912 } else {
913 i >>= 1;
914 }
915
916 round_float64(&exp, &i);
917
918 result.parts.fraction = i >> (64 - FLOAT64_FRACTION_SIZE - 2);
919 result.parts.exp = exp;
920 return result;
921}
922
923float64 int64_to_float64(int64_t i)
924{
925 float64 result;
926
927 if (i < 0)
928 result = uint64_to_float64((uint64_t) (-i));
929 else
930 result = uint64_to_float64((uint64_t) i);
931
932 result.parts.sign = i < 0;
933
934 return result;
935}
936
937float128 uint32_to_float128(uint32_t i)
938{
939 int counter;
940 int32_t exp;
941 float128 result;
942 uint64_t frac_hi, frac_lo;
943
944 result.parts.sign = 0;
945 result.parts.frac_hi = 0;
946 result.parts.frac_lo = 0;
947
948 counter = count_zeroes32(i);
949
950 exp = FLOAT128_BIAS + 32 - counter - 1;
951
952 if (counter == 32) {
953 result.bin.hi = 0;
954 result.bin.lo = 0;
955 return result;
956 }
957
958 frac_hi = 0;
959 frac_lo = i;
960 lshift128(frac_hi, frac_lo, (counter + 96 - 1), &frac_hi, &frac_lo);
961
962 round_float128(&exp, &frac_hi, &frac_lo);
963
964 rshift128(frac_hi, frac_lo,
965 (128 - FLOAT128_FRACTION_SIZE - 2), &frac_hi, &frac_lo);
966 result.parts.frac_hi = frac_hi;
967 result.parts.frac_lo = frac_lo;
968 result.parts.exp = exp;
969
970 return result;
971}
972
973float128 int32_to_float128(int32_t i)
974{
975 float128 result;
976
977 if (i < 0)
978 result = uint32_to_float128((uint32_t) (-i));
979 else
980 result = uint32_to_float128((uint32_t) i);
981
982 result.parts.sign = i < 0;
983
984 return result;
985}
986
987
988float128 uint64_to_float128(uint64_t i)
989{
990 int counter;
991 int32_t exp;
992 float128 result;
993 uint64_t frac_hi, frac_lo;
994
995 result.parts.sign = 0;
996 result.parts.frac_hi = 0;
997 result.parts.frac_lo = 0;
998
999 counter = count_zeroes64(i);
1000
1001 exp = FLOAT128_BIAS + 64 - counter - 1;
1002
1003 if (counter == 64) {
1004 result.bin.hi = 0;
1005 result.bin.lo = 0;
1006 return result;
1007 }
1008
1009 frac_hi = 0;
1010 frac_lo = i;
1011 lshift128(frac_hi, frac_lo, (counter + 64 - 1), &frac_hi, &frac_lo);
1012
1013 round_float128(&exp, &frac_hi, &frac_lo);
1014
1015 rshift128(frac_hi, frac_lo,
1016 (128 - FLOAT128_FRACTION_SIZE - 2), &frac_hi, &frac_lo);
1017 result.parts.frac_hi = frac_hi;
1018 result.parts.frac_lo = frac_lo;
1019 result.parts.exp = exp;
1020
1021 return result;
1022}
1023
1024float128 int64_to_float128(int64_t i)
1025{
1026 float128 result;
1027
1028 if (i < 0)
1029 result = uint64_to_float128((uint64_t) (-i));
1030 else
1031 result = uint64_to_float128((uint64_t) i);
1032
1033 result.parts.sign = i < 0;
1034
1035 return result;
1036}
1037
1038#ifdef float32_t
1039
1040float32_t __floatsisf(int32_t i)
1041{
1042 float32_u res;
1043 res.data = int32_to_float32(i);
1044
1045 return res.val;
1046}
1047
1048float32_t __floatdisf(int64_t i)
1049{
1050 float32_u res;
1051 res.data = int64_to_float32(i);
1052
1053 return res.val;
1054}
1055
1056float32_t __floatunsisf(uint32_t i)
1057{
1058 float32_u res;
1059 res.data = uint32_to_float32(i);
1060
1061 return res.val;
1062}
1063
1064float32_t __floatundisf(uint64_t i)
1065{
1066 float32_u res;
1067 res.data = uint64_to_float32(i);
1068
1069 return res.val;
1070}
1071
1072int32_t __fixsfsi(float32_t a)
1073{
1074 float32_u ua;
1075 ua.val = a;
1076
1077 return float32_to_int32(ua.data);
1078}
1079
1080int64_t __fixsfdi(float32_t a)
1081{
1082 float32_u ua;
1083 ua.val = a;
1084
1085 return float32_to_int64(ua.data);
1086}
1087
1088uint32_t __fixunssfsi(float32_t a)
1089{
1090 float32_u ua;
1091 ua.val = a;
1092
1093 return float32_to_uint32(ua.data);
1094}
1095
1096uint64_t __fixunssfdi(float32_t a)
1097{
1098 float32_u ua;
1099 ua.val = a;
1100
1101 return float32_to_uint64(ua.data);
1102}
1103
1104int32_t __aeabi_f2iz(float32_t a)
1105{
1106 float32_u ua;
1107 ua.val = a;
1108
1109 return float32_to_int32(ua.data);
1110}
1111
1112uint32_t __aeabi_f2uiz(float32_t a)
1113{
1114 float32_u ua;
1115 ua.val = a;
1116
1117 return float32_to_uint32(ua.data);
1118}
1119
1120float32_t __aeabi_i2f(int32_t i)
1121{
1122 float32_u res;
1123 res.data = int32_to_float32(i);
1124
1125 return res.val;
1126}
1127
1128float32_t __aeabi_l2f(int64_t i)
1129{
1130 float32_u res;
1131 res.data = int64_to_float32(i);
1132
1133 return res.val;
1134}
1135
1136float32_t __aeabi_ui2f(uint32_t i)
1137{
1138 float32_u res;
1139 res.data = uint32_to_float32(i);
1140
1141 return res.val;
1142}
1143
1144float32_t __aeabi_ul2f(uint64_t i)
1145{
1146 float32_u res;
1147 res.data = uint64_to_float32(i);
1148
1149 return res.val;
1150}
1151
1152#endif
1153
1154#ifdef float64_t
1155
1156float64_t __floatsidf(int32_t i)
1157{
1158 float64_u res;
1159 res.data = int32_to_float64(i);
1160
1161 return res.val;
1162}
1163
1164float64_t __floatdidf(int64_t i)
1165{
1166 float64_u res;
1167 res.data = int64_to_float64(i);
1168
1169 return res.val;
1170}
1171
1172float64_t __floatunsidf(uint32_t i)
1173{
1174 float64_u res;
1175 res.data = uint32_to_float64(i);
1176
1177 return res.val;
1178}
1179
1180float64_t __floatundidf(uint64_t i)
1181{
1182 float64_u res;
1183 res.data = uint64_to_float64(i);
1184
1185 return res.val;
1186}
1187
1188uint32_t __fixunsdfsi(float64_t a)
1189{
1190 float64_u ua;
1191 ua.val = a;
1192
1193 return float64_to_uint32(ua.data);
1194}
1195
1196uint64_t __fixunsdfdi(float64_t a)
1197{
1198 float64_u ua;
1199 ua.val = a;
1200
1201 return float64_to_uint64(ua.data);
1202}
1203
1204int32_t __fixdfsi(float64_t a)
1205{
1206 float64_u ua;
1207 ua.val = a;
1208
1209 return float64_to_int32(ua.data);
1210}
1211
1212int64_t __fixdfdi(float64_t a)
1213{
1214 float64_u ua;
1215 ua.val = a;
1216
1217 return float64_to_int64(ua.data);
1218}
1219
1220float64_t __aeabi_i2d(int32_t i)
1221{
1222 float64_u res;
1223 res.data = int32_to_float64(i);
1224
1225 return res.val;
1226}
1227
1228float64_t __aeabi_ui2d(uint32_t i)
1229{
1230 float64_u res;
1231 res.data = uint32_to_float64(i);
1232
1233 return res.val;
1234}
1235
1236float64_t __aeabi_l2d(int64_t i)
1237{
1238 float64_u res;
1239 res.data = int64_to_float64(i);
1240
1241 return res.val;
1242}
1243
1244int32_t __aeabi_d2iz(float64_t a)
1245{
1246 float64_u ua;
1247 ua.val = a;
1248
1249 return float64_to_int32(ua.data);
1250}
1251
1252int64_t __aeabi_d2lz(float64_t a)
1253{
1254 float64_u ua;
1255 ua.val = a;
1256
1257 return float64_to_int64(ua.data);
1258}
1259
1260uint32_t __aeabi_d2uiz(float64_t a)
1261{
1262 float64_u ua;
1263 ua.val = a;
1264
1265 return float64_to_uint32(ua.data);
1266}
1267
1268#endif
1269
1270#ifdef float128_t
1271
1272float128_t __floatsitf(int32_t i)
1273{
1274 float128_u res;
1275 res.data = int32_to_float128(i);
1276
1277 return res.val;
1278}
1279
1280float128_t __floatditf(int64_t i)
1281{
1282 float128_u res;
1283 res.data = int64_to_float128(i);
1284
1285 return res.val;
1286}
1287
1288float128_t __floatunsitf(uint32_t i)
1289{
1290 float128_u res;
1291 res.data = uint32_to_float128(i);
1292
1293 return res.val;
1294}
1295
1296float128_t __floatunditf(uint64_t i)
1297{
1298 float128_u res;
1299 res.data = uint64_to_float128(i);
1300
1301 return res.val;
1302}
1303
1304int32_t __fixtfsi(float128_t a)
1305{
1306 float128_u ua;
1307 ua.val = a;
1308
1309 return float128_to_int32(ua.data);
1310}
1311
1312int64_t __fixtfdi(float128_t a)
1313{
1314 float128_u ua;
1315 ua.val = a;
1316
1317 return float128_to_uint64(ua.data);
1318}
1319
1320uint32_t __fixunstfsi(float128_t a)
1321{
1322 float128_u ua;
1323 ua.val = a;
1324
1325 return float128_to_uint32(ua.data);
1326}
1327
1328uint64_t __fixunstfdi(float128_t a)
1329{
1330 float128_u ua;
1331 ua.val = a;
1332
1333 return float128_to_uint64(ua.data);
1334}
1335
1336int32_t _Qp_qtoi(float128_t *a)
1337{
1338 return __fixtfsi(*a);
1339}
1340
1341int64_t _Qp_qtox(float128_t *a)
1342{
1343 return __fixunstfdi(*a);
1344}
1345
1346uint32_t _Qp_qtoui(float128_t *a)
1347{
1348 return __fixunstfsi(*a);
1349}
1350
1351uint64_t _Qp_qtoux(float128_t *a)
1352{
1353 return __fixunstfdi(*a);
1354}
1355
1356void _Qp_itoq(float128_t *c, int32_t a)
1357{
1358 *c = __floatsitf(a);
1359}
1360
1361void _Qp_xtoq(float128_t *c, int64_t a)
1362{
1363 *c = __floatditf(a);
1364}
1365
1366void _Qp_uitoq(float128_t *c, uint32_t a)
1367{
1368 *c = __floatunsitf(a);
1369}
1370
1371void _Qp_uxtoq(float128_t *c, uint64_t a)
1372{
1373 *c = __floatunditf(a);
1374}
1375
1376#endif
1377
1378#if (defined(float32_t) && defined(float64_t))
1379
1380float32_t __truncdfsf2(float64_t a)
1381{
1382 float64_u ua;
1383 ua.val = a;
1384
1385 float32_u res;
1386 res.data = float64_to_float32(ua.data);
1387
1388 return res.val;
1389}
1390
1391float64_t __extendsfdf2(float32_t a)
1392{
1393 float32_u ua;
1394 ua.val = a;
1395
1396 float64_u res;
1397 res.data = float32_to_float64(ua.data);
1398
1399 return res.val;
1400}
1401
1402float64_t __aeabi_f2d(float32_t a)
1403{
1404 float32_u ua;
1405 ua.val = a;
1406
1407 float64_u res;
1408 res.data = float32_to_float64(ua.data);
1409
1410 return res.val;
1411}
1412
1413float32_t __aeabi_d2f(float64_t a)
1414{
1415 float64_u ua;
1416 ua.val = a;
1417
1418 float32_u res;
1419 res.data = float64_to_float32(ua.data);
1420
1421 return res.val;
1422}
1423
1424#endif
1425
1426#if (defined(float32_t) && defined(float128_t))
1427
1428float32_t __trunctfsf2(float128_t a)
1429{
1430 float128_u ua;
1431 ua.val = a;
1432
1433 float32_u res;
1434 res.data = float128_to_float32(ua.data);
1435
1436 return res.val;
1437}
1438
1439float128_t __extendsftf2(float32_t a)
1440{
1441 float32_u ua;
1442 ua.val = a;
1443
1444 float128_u res;
1445 res.data = float32_to_float128(ua.data);
1446
1447 return res.val;
1448}
1449
1450void _Qp_stoq(float128_t *c, float32_t a)
1451{
1452 *c = __extendsftf2(a);
1453}
1454
1455float32_t _Qp_qtos(float128_t *a)
1456{
1457 return __trunctfsf2(*a);
1458}
1459
1460#endif
1461
1462#if (defined(float64_t) && defined(float128_t))
1463
1464float64_t __trunctfdf2(float128_t a)
1465{
1466 float128_u ua;
1467 ua.val = a;
1468
1469 float64_u res;
1470 res.data = float128_to_float64(ua.data);
1471
1472 return res.val;
1473}
1474
1475float128_t __extenddftf2(float64_t a)
1476{
1477 float64_u ua;
1478 ua.val = a;
1479
1480 float128_u res;
1481 res.data = float64_to_float128(ua.data);
1482
1483 return res.val;
1484}
1485
1486void _Qp_dtoq(float128_t *c, float64_t a)
1487{
1488 *c = __extenddftf2(a);
1489}
1490
1491float64_t _Qp_qtod(float128_t *a)
1492{
1493 return __trunctfdf2(*a);
1494}
1495
1496#endif
1497
1498/** @}
1499 */
Note: See TracBrowser for help on using the repository browser.