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//===----------------------------------------------------------------------===//
15214152Sed
16214152Sed#define DOUBLE_PRECISION
17214152Sed#include "fp_lib.h"
18214152Sed
19222656Sed#include "int_lib.h"
20222656Sed
21239138SandrewARM_EABI_FNALIAS(d2iz, fixdfsi)
22222656Sed
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
37214152Sed    // If exponent is negative, the result is zero.
38214152Sed    else if (exponent < 0) {
39214152Sed        return 0;
40214152Sed    }
41214152Sed
42214152Sed    // If significandBits < exponent, left shift to get the result.  This shift
43214152Sed    // may end up being larger than the type width, which incurs undefined
44214152Sed    // behavior, but the conversion itself is undefined in that case, so
45214152Sed    // whatever the compiler decides to do is fine.
46214152Sed    else {
47214152Sed        return sign * (significand << (exponent - significandBits));
48214152Sed    }
49214152Sed}
50