1*0a6a1f1dSLionel Sambuc //===-- lib/fixdfsi.c - Double-precision -> integer conversion ----*- C -*-===//
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc // The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc //
5*0a6a1f1dSLionel Sambuc // This file is dual licensed under the MIT and the University of Illinois Open
6*0a6a1f1dSLionel Sambuc // Source Licenses. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc //
8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
9*0a6a1f1dSLionel Sambuc //
10*0a6a1f1dSLionel Sambuc // This file implements double-precision to integer conversion for the
11*0a6a1f1dSLionel Sambuc // compiler-rt library. No range checking is performed; the behavior of this
12*0a6a1f1dSLionel Sambuc // conversion is undefined for out of range values in the C standard.
13*0a6a1f1dSLionel Sambuc //
14*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
15*0a6a1f1dSLionel Sambuc
16*0a6a1f1dSLionel Sambuc #define DOUBLE_PRECISION
17*0a6a1f1dSLionel Sambuc #include "fp_lib.h"
18*0a6a1f1dSLionel Sambuc
19*0a6a1f1dSLionel Sambuc #include "int_lib.h"
20*0a6a1f1dSLionel Sambuc
ARM_EABI_FNALIAS(d2iz,fixdfsi)21*0a6a1f1dSLionel Sambuc ARM_EABI_FNALIAS(d2iz, fixdfsi)
22*0a6a1f1dSLionel Sambuc
23*0a6a1f1dSLionel Sambuc COMPILER_RT_ABI int
24*0a6a1f1dSLionel Sambuc __fixdfsi(fp_t a) {
25*0a6a1f1dSLionel Sambuc
26*0a6a1f1dSLionel Sambuc // Break a into sign, exponent, significand
27*0a6a1f1dSLionel Sambuc const rep_t aRep = toRep(a);
28*0a6a1f1dSLionel Sambuc const rep_t aAbs = aRep & absMask;
29*0a6a1f1dSLionel Sambuc const int sign = aRep & signBit ? -1 : 1;
30*0a6a1f1dSLionel Sambuc const int exponent = (aAbs >> significandBits) - exponentBias;
31*0a6a1f1dSLionel Sambuc const rep_t significand = (aAbs & significandMask) | implicitBit;
32*0a6a1f1dSLionel Sambuc
33*0a6a1f1dSLionel Sambuc // If 0 < exponent < significandBits, right shift to get the result.
34*0a6a1f1dSLionel Sambuc if ((unsigned int)exponent < significandBits) {
35*0a6a1f1dSLionel Sambuc return sign * (significand >> (significandBits - exponent));
36*0a6a1f1dSLionel Sambuc }
37*0a6a1f1dSLionel Sambuc
38*0a6a1f1dSLionel Sambuc // If exponent is negative, the result is zero.
39*0a6a1f1dSLionel Sambuc else if (exponent < 0) {
40*0a6a1f1dSLionel Sambuc return 0;
41*0a6a1f1dSLionel Sambuc }
42*0a6a1f1dSLionel Sambuc
43*0a6a1f1dSLionel Sambuc // If significandBits < exponent, left shift to get the result. This shift
44*0a6a1f1dSLionel Sambuc // may end up being larger than the type width, which incurs undefined
45*0a6a1f1dSLionel Sambuc // behavior, but the conversion itself is undefined in that case, so
46*0a6a1f1dSLionel Sambuc // whatever the compiler decides to do is fine.
47*0a6a1f1dSLionel Sambuc else {
48*0a6a1f1dSLionel Sambuc return sign * (significand << (exponent - significandBits));
49*0a6a1f1dSLionel Sambuc }
50*0a6a1f1dSLionel Sambuc }
51