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