1 //===-- lib/floatuntitf.c - uint128 -> 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 tu_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 tu_int to a fp_t, rounding toward even. */
22 /* Assumption: fp_t is a IEEE 128 bit floating point type
23 * tu_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 __floatuntitf(tu_int a) {
35 const unsigned N = sizeof(tu_int) * CHAR_BIT;
36 int sd = N - __clzti2(a); /* number of significant digits */
37 int e = sd - 1; /* exponent */
38 if (sd > LDBL_MANT_DIG) {
39 /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
40 * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
41 * 12345678901234567890123456
43 * P = bit LDBL_MANT_DIG-1 bits to the right of 1
44 * Q = bit LDBL_MANT_DIG bits to the right of 1
45 * R = "or" of all bits to the right of Q
48 case LDBL_MANT_DIG + 1:
51 case LDBL_MANT_DIG + 2:
54 a = (a >> (sd - (LDBL_MANT_DIG+2))) |
55 ((a & ((tu_int)(-1) >> ((N + LDBL_MANT_DIG+2) - sd))) != 0);
58 a |= (a & 4) != 0; /* Or P into R */
59 ++a; /* round - this step may add a significant bit */
60 a >>= 2; /* dump Q and R */
61 /* a is now rounded to LDBL_MANT_DIG or LDBL_MANT_DIG+1 bits */
62 if (a & ((tu_int)1 << LDBL_MANT_DIG)) {
66 /* a is now rounded to LDBL_MANT_DIG bits */
68 a <<= (LDBL_MANT_DIG - sd);
69 /* a is now rounded to LDBL_MANT_DIG bits */
73 fb.u.high.all = (du_int)(e + 16383) << 48 /* exponent */
74 | ((a >> 64) & 0x0000ffffffffffffLL); /* significand */
75 fb.u.low.all = (du_int)(a);