1/* ===-- floatundidf.c - Implement __floatundidf ---------------------------===
2 *
3 *                     The LLVM Compiler Infrastructure
4 *
5 * This file is dual licensed under the MIT and the University of Illinois Open
6 * Source Licenses. See LICENSE.TXT for details.
7 *
8 * ===----------------------------------------------------------------------===
9 *
10 * This file implements __floatundidf for the compiler_rt library.
11 *
12 * ===----------------------------------------------------------------------===
13 */
14
15/* Returns: convert a to a double, rounding toward even. */
16
17/* Assumption: double is a IEEE 64 bit floating point type
18 *             du_int is a 64 bit integral type
19 */
20
21/* seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm */
22
23#include "int_lib.h"
24
25#ifndef __SOFT_FP__
26/* Support for systems that have hardware floating-point; we'll set the inexact flag
27 * as a side-effect of this computation.
28 */
29
30COMPILER_RT_ABI double
31__floatundidf(du_int a)
32{
33	static const double twop52 = 4503599627370496.0; // 0x1.0p52
34	static const double twop84 = 19342813113834066795298816.0; // 0x1.0p84
35	static const double twop84_plus_twop52 = 19342813118337666422669312.0; // 0x1.00000001p84
36
37	union { uint64_t x; double d; } high = { .d = twop84 };
38	union { uint64_t x; double d; } low = { .d = twop52 };
39
40	high.x |= a >> 32;
41	low.x |= a & UINT64_C(0x00000000ffffffff);
42
43	const double result = (high.d - twop84_plus_twop52) + low.d;
44	return result;
45}
46
47#else
48/* Support for systems that don't have hardware floating-point; there are no flags to
49 * set, and we don't want to code-gen to an unknown soft-float implementation.
50 */
51
52COMPILER_RT_ABI double
53__floatundidf(du_int a)
54{
55    if (a == 0)
56        return 0.0;
57    const unsigned N = sizeof(du_int) * CHAR_BIT;
58    int sd = N - __builtin_clzll(a);  /* number of significant digits */
59    int e = sd - 1;             /* exponent */
60    if (sd > DBL_MANT_DIG)
61    {
62        /*  start:  0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
63         *  finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
64         *                                                12345678901234567890123456
65         *  1 = msb 1 bit
66         *  P = bit DBL_MANT_DIG-1 bits to the right of 1
67         *  Q = bit DBL_MANT_DIG bits to the right of 1
68         *  R = "or" of all bits to the right of Q
69         */
70        switch (sd)
71        {
72        case DBL_MANT_DIG + 1:
73            a <<= 1;
74            break;
75        case DBL_MANT_DIG + 2:
76            break;
77        default:
78            a = (a >> (sd - (DBL_MANT_DIG+2))) |
79                ((a & ((du_int)(-1) >> ((N + DBL_MANT_DIG+2) - sd))) != 0);
80        };
81        /* finish: */
82        a |= (a & 4) != 0;  /* Or P into R */
83        ++a;  /* round - this step may add a significant bit */
84        a >>= 2;  /* dump Q and R */
85        /* a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits */
86        if (a & ((du_int)1 << DBL_MANT_DIG))
87        {
88            a >>= 1;
89            ++e;
90        }
91        /* a is now rounded to DBL_MANT_DIG bits */
92    }
93    else
94    {
95        a <<= (DBL_MANT_DIG - sd);
96        /* a is now rounded to DBL_MANT_DIG bits */
97    }
98    double_bits fb;
99    fb.u.s.high = ((e + 1023) << 20)      |        /* exponent */
100                ((su_int)(a >> 32) & 0x000FFFFF); /* mantissa-high */
101    fb.u.s.low = (su_int)a;                         /* mantissa-low  */
102    return fb.f;
103}
104#endif
105
106#if defined(__ARM_EABI__)
107#if defined(COMPILER_RT_ARMHF_TARGET)
108AEABI_RTABI double __aeabi_ul2d(du_int a) {
109  return __floatundidf(a);
110}
111#else
112AEABI_RTABI double __aeabi_ul2d(du_int a) COMPILER_RT_ALIAS(__floatundidf);
113#endif
114#endif
115