Joerg Sonnenberger | 91bd698 | 2015-03-11 21:13:56 +0000 | [diff] [blame] | 1 | //===-- lib/fixdfsi.c - Double-precision -> integer conversion ----*- C -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements float to unsigned integer conversion for the |
| 11 | // compiler-rt library. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "fp_lib.h" |
| 16 | |
Saleem Abdulrasool | 911cfc1 | 2015-10-10 21:21:28 +0000 | [diff] [blame] | 17 | static __inline fixuint_t __fixuint(fp_t a) { |
Joerg Sonnenberger | 91bd698 | 2015-03-11 21:13:56 +0000 | [diff] [blame] | 18 | // Break a into sign, exponent, significand |
| 19 | const rep_t aRep = toRep(a); |
| 20 | const rep_t aAbs = aRep & absMask; |
| 21 | const int sign = aRep & signBit ? -1 : 1; |
| 22 | const int exponent = (aAbs >> significandBits) - exponentBias; |
| 23 | const rep_t significand = (aAbs & significandMask) | implicitBit; |
| 24 | |
| 25 | // If either the value or the exponent is negative, the result is zero. |
| 26 | if (sign == -1 || exponent < 0) |
| 27 | return 0; |
| 28 | |
| 29 | // If the value is too large for the integer type, saturate. |
Sergey Dmitrouk | 195f3a1 | 2015-11-05 18:36:42 +0000 | [diff] [blame] | 30 | if ((unsigned)exponent >= sizeof(fixuint_t) * CHAR_BIT) |
Joerg Sonnenberger | 91bd698 | 2015-03-11 21:13:56 +0000 | [diff] [blame] | 31 | return ~(fixuint_t)0; |
| 32 | |
| 33 | // If 0 <= exponent < significandBits, right shift to get the result. |
| 34 | // Otherwise, shift left. |
| 35 | if (exponent < significandBits) |
| 36 | return significand >> (significandBits - exponent); |
| 37 | else |
| 38 | return (fixuint_t)significand << (exponent - significandBits); |
| 39 | } |