floattixf.c revision 263763
1/* ===-- floattixf.c - Implement __floattixf -------------------------------===
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 __floattixf for the compiler_rt library.
11 *
12 * ===----------------------------------------------------------------------===
13 */
14
15#include "int_lib.h"
16
17#ifdef CRT_HAS_128BIT
18
19/* Returns: convert a to a long double, rounding toward even. */
20
21/* Assumption: long double is a IEEE 80 bit floating point type padded to 128 bits
22 *             ti_int is a 128 bit integral type
23 */
24
25/* gggg gggg gggg gggg gggg gggg gggg gggg | gggg gggg gggg gggg seee eeee eeee eeee |
26 * 1mmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm
27 */
28
29si_int __clzti2(ti_int a);
30
31long double
32__floattixf(ti_int a)
33{
34    if (a == 0)
35        return 0.0;
36    const unsigned N = sizeof(ti_int) * CHAR_BIT;
37    const ti_int s = a >> (N-1);
38    a = (a ^ s) - s;
39    int sd = N - __clzti2(a);  /* number of significant digits */
40    int e = sd - 1;             /* exponent */
41    if (sd > LDBL_MANT_DIG)
42    {
43        /*  start:  0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
44         *  finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
45         *                                                12345678901234567890123456
46         *  1 = msb 1 bit
47         *  P = bit LDBL_MANT_DIG-1 bits to the right of 1
48         *  Q = bit LDBL_MANT_DIG bits to the right of 1
49         *  R = "or" of all bits to the right of Q
50         */
51        switch (sd)
52        {
53        case LDBL_MANT_DIG + 1:
54            a <<= 1;
55            break;
56        case LDBL_MANT_DIG + 2:
57            break;
58        default:
59            a = ((tu_int)a >> (sd - (LDBL_MANT_DIG+2))) |
60                ((a & ((tu_int)(-1) >> ((N + LDBL_MANT_DIG+2) - sd))) != 0);
61        };
62        /* finish: */
63        a |= (a & 4) != 0;  /* Or P into R */
64        ++a;  /* round - this step may add a significant bit */
65        a >>= 2;  /* dump Q and R */
66        /* a is now rounded to LDBL_MANT_DIG or LDBL_MANT_DIG+1 bits */
67        if (a & ((tu_int)1 << LDBL_MANT_DIG))
68        {
69            a >>= 1;
70            ++e;
71        }
72        /* a is now rounded to LDBL_MANT_DIG bits */
73    }
74    else
75    {
76        a <<= (LDBL_MANT_DIG - sd);
77        /* a is now rounded to LDBL_MANT_DIG bits */
78    }
79    long_double_bits fb;
80    fb.u.high.s.low = ((su_int)s & 0x8000) |        /* sign */
81                    (e + 16383);                  /* exponent */
82    fb.u.low.all = (du_int)a;                     /* mantissa */
83    return fb.f;
84}
85
86#endif /* CRT_HAS_128BIT */
87