1214152Sed//===-- lib/fixdfsi.c - Double-precision -> integer conversion ----*- C -*-===//
2214152Sed//
3214152Sed//                     The LLVM Compiler Infrastructure
4214152Sed//
5222656Sed// This file is dual licensed under the MIT and the University of Illinois Open
6222656Sed// Source Licenses. See LICENSE.TXT for details.
7214152Sed//
8214152Sed//===----------------------------------------------------------------------===//
9214152Sed//
10214152Sed// This file implements double-precision to integer conversion for the
11214152Sed// compiler-rt library.  No range checking is performed; the behavior of this
12214152Sed// conversion is undefined for out of range values in the C standard.
13214152Sed//
14214152Sed//===----------------------------------------------------------------------===//
15263560Sdim
16263560Sdim#define DOUBLE_PRECISION
17263764Sdim#include "fp_lib.h"
18214152Sed
19214152Sed#include "int_lib.h"
20214152Sed
21214152SedARM_EABI_FNALIAS(d2iz, fixdfsi)
22214152Sed
23214152Sedint __fixdfsi(fp_t a) {
24214152Sed
25214152Sed    // Break a into sign, exponent, significand
26214152Sed    const rep_t aRep = toRep(a);
27214152Sed    const rep_t aAbs = aRep & absMask;
28214152Sed    const int sign = aRep & signBit ? -1 : 1;
29214152Sed    const int exponent = (aAbs >> significandBits) - exponentBias;
30214152Sed    const rep_t significand = (aAbs & significandMask) | implicitBit;
31214152Sed
32214152Sed    // If 0 < exponent < significandBits, right shift to get the result.
33214152Sed    if ((unsigned int)exponent < significandBits) {
34214152Sed        return sign * (significand >> (significandBits - exponent));
35214152Sed    }
36214152Sed
37263764Sdim    // If exponent is negative, the result is zero.
38    else if (exponent < 0) {
39        return 0;
40    }
41
42    // If significandBits < exponent, left shift to get the result.  This shift
43    // may end up being larger than the type width, which incurs undefined
44    // behavior, but the conversion itself is undefined in that case, so
45    // whatever the compiler decides to do is fine.
46    else {
47        return sign * (significand << (exponent - significandBits));
48    }
49}
50