blob: d68ccf27a79ca8923d7aca07a6ae90646e614576 [file] [log] [blame]
Joerg Sonnenberger91bd6982015-03-11 21:13:56 +00001//===-- 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
Saleem Abdulrasool911cfc12015-10-10 21:21:28 +000017static __inline fixuint_t __fixuint(fp_t a) {
Joerg Sonnenberger91bd6982015-03-11 21:13:56 +000018 // 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.
Sergey Dmitrouk195f3a12015-11-05 18:36:42 +000030 if ((unsigned)exponent >= sizeof(fixuint_t) * CHAR_BIT)
Joerg Sonnenberger91bd6982015-03-11 21:13:56 +000031 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}