1 //===-- lib/fixdfsi.c - Double-precision -> integer conversion ----*- C -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements float to unsigned integer conversion for the
10 // compiler-rt library.
12 //===----------------------------------------------------------------------===//
16 static __inline fixuint_t __fixuint(fp_t a) {
17 // Break a into sign, exponent, significand parts.
18 const rep_t aRep = toRep(a);
19 const rep_t aAbs = aRep & absMask;
20 const int sign = aRep & signBit ? -1 : 1;
21 const int exponent = (aAbs >> significandBits) - exponentBias;
22 const rep_t significand = (aAbs & significandMask) | implicitBit;
24 // If either the value or the exponent is negative, the result is zero.
25 if (sign == -1 || exponent < 0)
28 // If the value is too large for the integer type, saturate.
29 if ((unsigned)exponent >= sizeof(fixuint_t) * CHAR_BIT)
32 // If 0 <= exponent < significandBits, right shift to get the result.
33 // Otherwise, shift left.
34 if (exponent < significandBits)
35 return significand >> (significandBits - exponent);
37 return (fixuint_t)significand << (exponent - significandBits);