1*7c3d14c8STreehugger Robot//===-- lib/fixdfsi.c - Double-precision -> integer conversion ----*- C -*-===// 2*7c3d14c8STreehugger Robot// 3*7c3d14c8STreehugger Robot// The LLVM Compiler Infrastructure 4*7c3d14c8STreehugger Robot// 5*7c3d14c8STreehugger Robot// This file is dual licensed under the MIT and the University of Illinois Open 6*7c3d14c8STreehugger Robot// Source Licenses. See LICENSE.TXT for details. 7*7c3d14c8STreehugger Robot// 8*7c3d14c8STreehugger Robot//===----------------------------------------------------------------------===// 9*7c3d14c8STreehugger Robot// 10*7c3d14c8STreehugger Robot// This file implements float to unsigned integer conversion for the 11*7c3d14c8STreehugger Robot// compiler-rt library. 12*7c3d14c8STreehugger Robot// 13*7c3d14c8STreehugger Robot//===----------------------------------------------------------------------===// 14*7c3d14c8STreehugger Robot 15*7c3d14c8STreehugger Robot#include "fp_lib.h" 16*7c3d14c8STreehugger Robot 17*7c3d14c8STreehugger Robotstatic __inline fixuint_t __fixuint(fp_t a) { 18*7c3d14c8STreehugger Robot // Break a into sign, exponent, significand 19*7c3d14c8STreehugger Robot const rep_t aRep = toRep(a); 20*7c3d14c8STreehugger Robot const rep_t aAbs = aRep & absMask; 21*7c3d14c8STreehugger Robot const int sign = aRep & signBit ? -1 : 1; 22*7c3d14c8STreehugger Robot const int exponent = (aAbs >> significandBits) - exponentBias; 23*7c3d14c8STreehugger Robot const rep_t significand = (aAbs & significandMask) | implicitBit; 24*7c3d14c8STreehugger Robot 25*7c3d14c8STreehugger Robot // If either the value or the exponent is negative, the result is zero. 26*7c3d14c8STreehugger Robot if (sign == -1 || exponent < 0) 27*7c3d14c8STreehugger Robot return 0; 28*7c3d14c8STreehugger Robot 29*7c3d14c8STreehugger Robot // If the value is too large for the integer type, saturate. 30*7c3d14c8STreehugger Robot if ((unsigned)exponent >= sizeof(fixuint_t) * CHAR_BIT) 31*7c3d14c8STreehugger Robot return ~(fixuint_t)0; 32*7c3d14c8STreehugger Robot 33*7c3d14c8STreehugger Robot // If 0 <= exponent < significandBits, right shift to get the result. 34*7c3d14c8STreehugger Robot // Otherwise, shift left. 35*7c3d14c8STreehugger Robot if (exponent < significandBits) 36*7c3d14c8STreehugger Robot return significand >> (significandBits - exponent); 37*7c3d14c8STreehugger Robot else 38*7c3d14c8STreehugger Robot return (fixuint_t)significand << (exponent - significandBits); 39*7c3d14c8STreehugger Robot} 40