fp_fixuint_impl.inc revision 287516
1251881Speter//===-- lib/fixdfsi.c - Double-precision -> integer conversion ----*- C -*-===//
2251881Speter//
3251881Speter//                     The LLVM Compiler Infrastructure
4251881Speter//
5251881Speter// This file is dual licensed under the MIT and the University of Illinois Open
6251881Speter// Source Licenses. See LICENSE.TXT for details.
7251881Speter//
8251881Speter//===----------------------------------------------------------------------===//
9251881Speter//
10251881Speter// This file implements float to unsigned integer conversion for the
11251881Speter// compiler-rt library.
12251881Speter//
13251881Speter//===----------------------------------------------------------------------===//
14251881Speter
15251881Speter#include "fp_lib.h"
16251881Speter
17251881Speterstatic inline fixuint_t __fixuint(fp_t a) {
18251881Speter    // Break a into sign, exponent, significand
19251881Speter    const rep_t aRep = toRep(a);
20251881Speter    const rep_t aAbs = aRep & absMask;
21251881Speter    const int sign = aRep & signBit ? -1 : 1;
22251881Speter    const int exponent = (aAbs >> significandBits) - exponentBias;
23251881Speter    const rep_t significand = (aAbs & significandMask) | implicitBit;
24251881Speter
25251881Speter    // If either the value or the exponent is negative, the result is zero.
26251881Speter    if (sign == -1 || exponent < 0)
27251881Speter        return 0;
28299742Sdim
29251881Speter    // If the value is too large for the integer type, saturate.
30299742Sdim    if ((unsigned)exponent > sizeof(fixuint_t) * CHAR_BIT)
31251881Speter        return ~(fixuint_t)0;
32251881Speter
33251881Speter    // If 0 <= exponent < significandBits, right shift to get the result.
34251881Speter    // Otherwise, shift left.
35251881Speter    if (exponent < significandBits)
36251881Speter        return significand >> (significandBits - exponent);
37251881Speter    else
38251881Speter        return (fixuint_t)significand << (exponent - significandBits);
39251881Speter}
40251881Speter