fp_fixuint_impl.inc revision 296417
1287516Sdim//===-- lib/fixdfsi.c - Double-precision -> integer conversion ----*- C -*-===// 2287516Sdim// 3287516Sdim// The LLVM Compiler Infrastructure 4287516Sdim// 5287516Sdim// This file is dual licensed under the MIT and the University of Illinois Open 6287516Sdim// Source Licenses. See LICENSE.TXT for details. 7287516Sdim// 8287516Sdim//===----------------------------------------------------------------------===// 9287516Sdim// 10287516Sdim// This file implements float to unsigned integer conversion for the 11287516Sdim// compiler-rt library. 12287516Sdim// 13287516Sdim//===----------------------------------------------------------------------===// 14287516Sdim 15287516Sdim#include "fp_lib.h" 16287516Sdim 17296417Sdimstatic __inline fixuint_t __fixuint(fp_t a) { 18287516Sdim // Break a into sign, exponent, significand 19287516Sdim const rep_t aRep = toRep(a); 20287516Sdim const rep_t aAbs = aRep & absMask; 21287516Sdim const int sign = aRep & signBit ? -1 : 1; 22287516Sdim const int exponent = (aAbs >> significandBits) - exponentBias; 23287516Sdim const rep_t significand = (aAbs & significandMask) | implicitBit; 24287516Sdim 25287516Sdim // If either the value or the exponent is negative, the result is zero. 26287516Sdim if (sign == -1 || exponent < 0) 27287516Sdim return 0; 28287516Sdim 29287516Sdim // If the value is too large for the integer type, saturate. 30296417Sdim if ((unsigned)exponent >= sizeof(fixuint_t) * CHAR_BIT) 31287516Sdim return ~(fixuint_t)0; 32287516Sdim 33287516Sdim // If 0 <= exponent < significandBits, right shift to get the result. 34287516Sdim // Otherwise, shift left. 35287516Sdim if (exponent < significandBits) 36287516Sdim return significand >> (significandBits - exponent); 37287516Sdim else 38287516Sdim return (fixuint_t)significand << (exponent - significandBits); 39287516Sdim} 40