1 //===-- lib/fixdfsi.c - Double-precision -> integer conversion ----*- C -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements float to integer conversion for the
11 // compiler-rt library.
13 //===----------------------------------------------------------------------===//
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;
27 // If exponent is negative, the result is zero.
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;
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));
40 return sign * ((fixint_t)significand << (exponent - significandBits));