floattidf.c revision 214152
1214152Sed/* ===-- floattidf.c - Implement __floattidf -------------------------------===
2214152Sed *
3214152Sed *                    The LLVM Compiler Infrastructure
4214152Sed *
5214152Sed * This file is distributed under the University of Illinois Open Source
6214152Sed * License. See LICENSE.TXT for details.
7214152Sed *
8214152Sed * ===----------------------------------------------------------------------===
9214152Sed *
10214152Sed * This file implements __floattidf for the compiler_rt library.
11214152Sed *
12214152Sed * ===----------------------------------------------------------------------===
13214152Sed */
14214152Sed
15214152Sed#if __x86_64
16214152Sed
17214152Sed#include "int_lib.h"
18214152Sed#include <float.h>
19214152Sed
20214152Sed/* Returns: convert a to a double, rounding toward even.*/
21214152Sed
22214152Sed/* Assumption: double is a IEEE 64 bit floating point type
23214152Sed *            ti_int is a 128 bit integral type
24214152Sed */
25214152Sed
26214152Sed/* seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm */
27214152Sed
28214152Sedsi_int __clzti2(ti_int a);
29214152Sed
30214152Seddouble
31214152Sed__floattidf(ti_int a)
32214152Sed{
33214152Sed    if (a == 0)
34214152Sed        return 0.0;
35214152Sed    const unsigned N = sizeof(ti_int) * CHAR_BIT;
36214152Sed    const ti_int s = a >> (N-1);
37214152Sed    a = (a ^ s) - s;
38214152Sed    int sd = N - __clzti2(a);  /* number of significant digits */
39214152Sed    int e = sd - 1;             /* exponent */
40214152Sed    if (sd > DBL_MANT_DIG)
41214152Sed    {
42214152Sed        /* start:  0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
43214152Sed         *  finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
44214152Sed         *                                               12345678901234567890123456
45214152Sed         * 1 = msb 1 bit
46214152Sed         * P = bit DBL_MANT_DIG-1 bits to the right of 1
47214152Sed         * Q = bit DBL_MANT_DIG bits to the right of 1
48214152Sed         * R = "or" of all bits to the right of Q
49214152Sed         */
50214152Sed        switch (sd)
51214152Sed        {
52214152Sed        case DBL_MANT_DIG + 1:
53214152Sed            a <<= 1;
54214152Sed            break;
55214152Sed        case DBL_MANT_DIG + 2:
56214152Sed            break;
57214152Sed        default:
58214152Sed            a = ((tu_int)a >> (sd - (DBL_MANT_DIG+2))) |
59214152Sed                ((a & ((tu_int)(-1) >> ((N + DBL_MANT_DIG+2) - sd))) != 0);
60214152Sed        };
61214152Sed        /* finish: */
62214152Sed        a |= (a & 4) != 0;  /* Or P into R */
63214152Sed        ++a;  /* round - this step may add a significant bit */
64214152Sed        a >>= 2;  /* dump Q and R */
65214152Sed        /* a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits */
66214152Sed        if (a & ((tu_int)1 << DBL_MANT_DIG))
67214152Sed        {
68214152Sed            a >>= 1;
69214152Sed            ++e;
70214152Sed        }
71214152Sed        /* a is now rounded to DBL_MANT_DIG bits */
72214152Sed    }
73214152Sed    else
74214152Sed    {
75214152Sed        a <<= (DBL_MANT_DIG - sd);
76214152Sed        /* a is now rounded to DBL_MANT_DIG bits */
77214152Sed    }
78214152Sed    double_bits fb;
79214152Sed    fb.u.s.high = ((su_int)s & 0x80000000) |        /* sign */
80214152Sed                ((e + 1023) << 20)      |        /* exponent */
81214152Sed                ((su_int)(a >> 32) & 0x000FFFFF); /* mantissa-high */
82214152Sed    fb.u.s.low = (su_int)a;                         /* mantissa-low */
83214152Sed    return fb.f;
84214152Sed}
85214152Sed
86214152Sed#endif
87