1214152Sed/* ===-- floatuntidf.c - Implement __floatuntidf ---------------------------===
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 __floatuntidf for the compiler_rt library.
11214152Sed *
12214152Sed * ===----------------------------------------------------------------------===
13214152Sed */
14214152Sed
15239138Sandrew#include "int_lib.h"
16239138Sandrew
17263763Sdim#ifdef CRT_HAS_128BIT
18214152Sed
19214152Sed/* Returns: convert a to a double, rounding toward even. */
20214152Sed
21214152Sed/* Assumption: double is a IEEE 64 bit floating point type
22214152Sed *             tu_int is a 128 bit integral type
23214152Sed */
24214152Sed
25214152Sed/* seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm */
26214152Sed
27214152Sedsi_int __clzti2(ti_int a);
28214152Sed
29214152Seddouble
30214152Sed__floatuntidf(tu_int a)
31214152Sed{
32214152Sed    if (a == 0)
33214152Sed        return 0.0;
34214152Sed    const unsigned N = sizeof(tu_int) * CHAR_BIT;
35214152Sed    int sd = N - __clzti2(a);  /* number of significant digits */
36214152Sed    int e = sd - 1;             /* exponent */
37214152Sed    if (sd > DBL_MANT_DIG)
38214152Sed    {
39214152Sed        /*  start:  0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
40214152Sed         *  finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
41214152Sed         *                                                12345678901234567890123456
42214152Sed         *  1 = msb 1 bit
43214152Sed         *  P = bit DBL_MANT_DIG-1 bits to the right of 1
44214152Sed         *  Q = bit DBL_MANT_DIG bits to the right of 1
45214152Sed         *  R = "or" of all bits to the right of Q
46263763Sdim         */
47214152Sed        switch (sd)
48214152Sed        {
49214152Sed        case DBL_MANT_DIG + 1:
50214152Sed            a <<= 1;
51214152Sed            break;
52214152Sed        case DBL_MANT_DIG + 2:
53214152Sed            break;
54214152Sed        default:
55214152Sed            a = (a >> (sd - (DBL_MANT_DIG+2))) |
56214152Sed                ((a & ((tu_int)(-1) >> ((N + DBL_MANT_DIG+2) - sd))) != 0);
57214152Sed        };
58214152Sed        /* finish: */
59214152Sed        a |= (a & 4) != 0;  /* Or P into R */
60214152Sed        ++a;  /* round - this step may add a significant bit */
61214152Sed        a >>= 2;  /* dump Q and R */
62214152Sed        /* a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits */
63214152Sed        if (a & ((tu_int)1 << DBL_MANT_DIG))
64214152Sed        {
65214152Sed            a >>= 1;
66214152Sed            ++e;
67214152Sed        }
68214152Sed        /* a is now rounded to DBL_MANT_DIG bits */
69214152Sed    }
70214152Sed    else
71214152Sed    {
72214152Sed        a <<= (DBL_MANT_DIG - sd);
73214152Sed        /* a is now rounded to DBL_MANT_DIG bits */
74214152Sed    }
75214152Sed    double_bits fb;
76214152Sed    fb.u.s.high = ((e + 1023) << 20)      |        /* exponent */
77214152Sed                ((su_int)(a >> 32) & 0x000FFFFF); /* mantissa-high */
78214152Sed    fb.u.s.low = (su_int)a;                         /* mantissa-low */
79214152Sed    return fb.f;
80214152Sed}
81214152Sed
82263763Sdim#endif /* CRT_HAS_128BIT */
83