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