Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [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 integer conversion for the |
| 11 | // compiler-rt library. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "fp_lib.h" |
| 16 | |
| 17 | static inline fixint_t __fixint(fp_t a) { |
| 18 | const fixint_t fixint_max = (fixint_t)((~(fixuint_t)0) / 2); |
| 19 | const fixint_t fixint_min = -fixint_max - 1; |
| 20 | // Break a into sign, exponent, significand |
| 21 | const rep_t aRep = toRep(a); |
| 22 | const rep_t aAbs = aRep & absMask; |
| 23 | const fixint_t sign = aRep & signBit ? -1 : 1; |
| 24 | const int exponent = (aAbs >> significandBits) - exponentBias; |
| 25 | const rep_t significand = (aAbs & significandMask) | implicitBit; |
| 26 | |
| 27 | // If exponent is negative, the result is zero. |
| 28 | if (exponent < 0) |
| 29 | return 0; |
| 30 | |
| 31 | // If the value is too large for the integer type, saturate. |
| 32 | if ((unsigned)exponent >= sizeof(fixint_t) * CHAR_BIT) |
| 33 | return sign == 1 ? fixint_max : fixint_min; |
| 34 | |
| 35 | // If 0 <= exponent < significandBits, right shift to get the result. |
| 36 | // Otherwise, shift left. |
| 37 | if (exponent < significandBits) |
| 38 | return sign * (significand >> (significandBits - exponent)); |
| 39 | else |
| 40 | return sign * ((fixint_t)significand << (exponent - significandBits)); |
| 41 | } |