1214152Sed/* ===-- floatundidf.c - Implement __floatundidf ---------------------------===
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 __floatundidf for the compiler_rt library.
11214152Sed *
12214152Sed * ===----------------------------------------------------------------------===
13214152Sed */
14214152Sed
15214152Sed/* Returns: convert a to a double, rounding toward even. */
16214152Sed
17214152Sed/* Assumption: double is a IEEE 64 bit floating point type
18214152Sed *             du_int is a 64 bit integral type
19214152Sed */
20214152Sed
21214152Sed/* seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm */
22214152Sed
23222656Sed#include "int_lib.h"
24222656Sed
25263560SdimARM_EABI_FNALIAS(ul2d, floatundidf)
26222656Sed
27214152Sed#ifndef __SOFT_FP__
28214152Sed/* Support for systems that have hardware floating-point; we'll set the inexact flag
29214152Sed * as a side-effect of this computation.
30214152Sed */
31214152Sed
32214152Sed
33222656SedCOMPILER_RT_ABI double
34214152Sed__floatundidf(du_int a)
35214152Sed{
36214152Sed	static const double twop52 = 0x1.0p52;
37214152Sed	static const double twop84 = 0x1.0p84;
38214152Sed	static const double twop84_plus_twop52 = 0x1.00000001p84;
39214152Sed
40214152Sed	union { uint64_t x; double d; } high = { .d = twop84 };
41214152Sed	union { uint64_t x; double d; } low = { .d = twop52 };
42214152Sed
43214152Sed	high.x |= a >> 32;
44214152Sed	low.x |= a & UINT64_C(0x00000000ffffffff);
45214152Sed
46214152Sed	const double result = (high.d - twop84_plus_twop52) + low.d;
47214152Sed	return result;
48214152Sed}
49214152Sed
50214152Sed#else
51214152Sed/* Support for systems that don't have hardware floating-point; there are no flags to
52214152Sed * set, and we don't want to code-gen to an unknown soft-float implementation.
53214152Sed */
54214152Sed
55222656SedCOMPILER_RT_ABI double
56214152Sed__floatundidf(du_int a)
57214152Sed{
58214152Sed    if (a == 0)
59214152Sed        return 0.0;
60214152Sed    const unsigned N = sizeof(du_int) * CHAR_BIT;
61214152Sed    int sd = N - __builtin_clzll(a);  /* number of significant digits */
62214152Sed    int e = sd - 1;             /* exponent */
63214152Sed    if (sd > DBL_MANT_DIG)
64214152Sed    {
65214152Sed        /*  start:  0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
66214152Sed         *  finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
67214152Sed         *                                                12345678901234567890123456
68214152Sed         *  1 = msb 1 bit
69214152Sed         *  P = bit DBL_MANT_DIG-1 bits to the right of 1
70214152Sed         *  Q = bit DBL_MANT_DIG bits to the right of 1
71214152Sed         *  R = "or" of all bits to the right of Q
72214152Sed         */
73214152Sed        switch (sd)
74214152Sed        {
75214152Sed        case DBL_MANT_DIG + 1:
76214152Sed            a <<= 1;
77214152Sed            break;
78214152Sed        case DBL_MANT_DIG + 2:
79214152Sed            break;
80214152Sed        default:
81214152Sed            a = (a >> (sd - (DBL_MANT_DIG+2))) |
82214152Sed                ((a & ((du_int)(-1) >> ((N + DBL_MANT_DIG+2) - sd))) != 0);
83214152Sed        };
84214152Sed        /* finish: */
85214152Sed        a |= (a & 4) != 0;  /* Or P into R */
86214152Sed        ++a;  /* round - this step may add a significant bit */
87214152Sed        a >>= 2;  /* dump Q and R */
88214152Sed        /* a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits */
89214152Sed        if (a & ((du_int)1 << DBL_MANT_DIG))
90214152Sed        {
91214152Sed            a >>= 1;
92214152Sed            ++e;
93214152Sed        }
94214152Sed        /* a is now rounded to DBL_MANT_DIG bits */
95214152Sed    }
96214152Sed    else
97214152Sed    {
98214152Sed        a <<= (DBL_MANT_DIG - sd);
99214152Sed        /* a is now rounded to DBL_MANT_DIG bits */
100214152Sed    }
101214152Sed    double_bits fb;
102214152Sed    fb.u.high = ((e + 1023) << 20)      |        /* exponent */
103214152Sed                ((su_int)(a >> 32) & 0x000FFFFF); /* mantissa-high */
104214152Sed    fb.u.low = (su_int)a;                         /* mantissa-low  */
105214152Sed    return fb.f;
106214152Sed}
107214152Sed#endif
108