]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/builtins/floattitf.c
Upgrade Unbound to 1.6.5. More to follow.
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / builtins / floattitf.c
1 //===-- lib/floattitf.c - int128 -> quad-precision conversion -----*- C -*-===//
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 ti_int to quad-precision conversion for the
11 // compiler-rt library in the IEEE-754 default round-to-nearest, ties-to-even
12 // mode.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #define QUAD_PRECISION
17 #include "fp_lib.h"
18 #include "int_lib.h"
19
20 /* Returns: convert a ti_int to a fp_t, rounding toward even. */
21
22 /* Assumption: fp_t is a IEEE 128 bit floating point type
23  *             ti_int is a 128 bit integral type
24  */
25
26 /* seee eeee eeee eeee mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm |
27  * mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm
28  */
29
30 #if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT)
31 COMPILER_RT_ABI fp_t
32 __floattitf(ti_int a) {
33     if (a == 0)
34         return 0.0;
35     const unsigned N = sizeof(ti_int) * CHAR_BIT;
36     const ti_int s = a >> (N-1);
37     a = (a ^ s) - s;
38     int sd = N - __clzti2(a);  /* number of significant digits */
39     int e = sd - 1;            /* exponent */
40     if (sd > LDBL_MANT_DIG) {
41         /*  start:  0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
42          *  finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
43          *                                                12345678901234567890123456
44          *  1 = msb 1 bit
45          *  P = bit LDBL_MANT_DIG-1 bits to the right of 1
46          *  Q = bit LDBL_MANT_DIG bits to the right of 1
47          *  R = "or" of all bits to the right of Q
48          */
49         switch (sd) {
50         case LDBL_MANT_DIG + 1:
51             a <<= 1;
52             break;
53         case LDBL_MANT_DIG + 2:
54             break;
55         default:
56             a = ((tu_int)a >> (sd - (LDBL_MANT_DIG+2))) |
57                 ((a & ((tu_int)(-1) >> ((N + LDBL_MANT_DIG+2) - sd))) != 0);
58         };
59         /* finish: */
60         a |= (a & 4) != 0;  /* Or P into R */
61         ++a;  /* round - this step may add a significant bit */
62         a >>= 2;  /* dump Q and R */
63         /* a is now rounded to LDBL_MANT_DIG or LDBL_MANT_DIG+1 bits */
64         if (a & ((tu_int)1 << LDBL_MANT_DIG)) {
65             a >>= 1;
66             ++e;
67         }
68         /* a is now rounded to LDBL_MANT_DIG bits */
69     } else {
70         a <<= (LDBL_MANT_DIG - sd);
71         /* a is now rounded to LDBL_MANT_DIG bits */
72     }
73
74     long_double_bits fb;
75     fb.u.high.all = (s & 0x8000000000000000LL)           /* sign */
76                   | (du_int)(e + 16383) << 48            /* exponent */
77                   | ((a >> 64) & 0x0000ffffffffffffLL);  /* significand */
78     fb.u.low.all = (du_int)(a);
79     return fb.f;
80 }
81
82 #endif