1 //===-- lib/floattitf.c - int128 -> quad-precision conversion -----*- C -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
14 //===----------------------------------------------------------------------===//
16 #define QUAD_PRECISION
20 /* Returns: convert a ti_int to a fp_t, rounding toward even. */
22 /* Assumption: fp_t is a IEEE 128 bit floating point type
23 * ti_int is a 128 bit integral type
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
30 #if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT)
32 __floattitf(ti_int a) {
35 const unsigned N = sizeof(ti_int) * CHAR_BIT;
36 const ti_int s = a >> (N-1);
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
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
50 case LDBL_MANT_DIG + 1:
53 case LDBL_MANT_DIG + 2:
56 a = ((tu_int)a >> (sd - (LDBL_MANT_DIG+2))) |
57 ((a & ((tu_int)(-1) >> ((N + LDBL_MANT_DIG+2) - sd))) != 0);
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)) {
68 /* a is now rounded to LDBL_MANT_DIG bits */
70 a <<= (LDBL_MANT_DIG - sd);
71 /* a is now rounded to LDBL_MANT_DIG bits */
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);