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
17static 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;
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.
30    if ((unsigned)exponent > sizeof(fixuint_t) * CHAR_BIT)
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}
40