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