1*0a6a1f1dSLionel Sambuc //===-- lib/fixsfsi.c - Single-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 single-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 SINGLE_PRECISION
17*0a6a1f1dSLionel Sambuc #include "fp_lib.h"
18*0a6a1f1dSLionel Sambuc
ARM_EABI_FNALIAS(f2iz,fixsfsi)19*0a6a1f1dSLionel Sambuc ARM_EABI_FNALIAS(f2iz, fixsfsi)
20*0a6a1f1dSLionel Sambuc
21*0a6a1f1dSLionel Sambuc COMPILER_RT_ABI int
22*0a6a1f1dSLionel Sambuc __fixsfsi(fp_t a) {
23*0a6a1f1dSLionel Sambuc // Break a into sign, exponent, significand
24*0a6a1f1dSLionel Sambuc const rep_t aRep = toRep(a);
25*0a6a1f1dSLionel Sambuc const rep_t aAbs = aRep & absMask;
26*0a6a1f1dSLionel Sambuc const int sign = aRep & signBit ? -1 : 1;
27*0a6a1f1dSLionel Sambuc const int exponent = (aAbs >> significandBits) - exponentBias;
28*0a6a1f1dSLionel Sambuc const rep_t significand = (aAbs & significandMask) | implicitBit;
29*0a6a1f1dSLionel Sambuc
30*0a6a1f1dSLionel Sambuc // If 0 < exponent < significandBits, right shift to get the result.
31*0a6a1f1dSLionel Sambuc if ((unsigned int)exponent < significandBits) {
32*0a6a1f1dSLionel Sambuc return sign * (significand >> (significandBits - exponent));
33*0a6a1f1dSLionel Sambuc }
34*0a6a1f1dSLionel Sambuc
35*0a6a1f1dSLionel Sambuc // If exponent is negative, the result is zero.
36*0a6a1f1dSLionel Sambuc else if (exponent < 0) {
37*0a6a1f1dSLionel Sambuc return 0;
38*0a6a1f1dSLionel Sambuc }
39*0a6a1f1dSLionel Sambuc
40*0a6a1f1dSLionel Sambuc // If significandBits < exponent, left shift to get the result. This shift
41*0a6a1f1dSLionel Sambuc // may end up being larger than the type width, which incurs undefined
42*0a6a1f1dSLionel Sambuc // behavior, but the conversion itself is undefined in that case, so
43*0a6a1f1dSLionel Sambuc // whatever the compiler decides to do is fine.
44*0a6a1f1dSLionel Sambuc else {
45*0a6a1f1dSLionel Sambuc return sign * (significand << (exponent - significandBits));
46*0a6a1f1dSLionel Sambuc }
47*0a6a1f1dSLionel Sambuc }
48