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 unsigned integer conversion for the
11 // compiler-rt library.
13 //===----------------------------------------------------------------------===//
17 static __inline fixuint_t __fixuint(fp_t a) {
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;
25 // If either the value or the exponent is negative, the result is zero.
26 if (sign == -1 || exponent < 0)
29 // If the value is too large for the integer type, saturate.
30 if ((unsigned)exponent >= sizeof(fixuint_t) * CHAR_BIT)
33 // If 0 <= exponent < significandBits, right shift to get the result.
34 // Otherwise, shift left.
35 if (exponent < significandBits)
36 return significand >> (significandBits - exponent);
38 return (fixuint_t)significand << (exponent - significandBits);