xref: /aosp_15_r20/external/compiler-rt/lib/builtins/floattixf.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot /* ===-- floattixf.c - Implement __floattixf -------------------------------===
2*7c3d14c8STreehugger Robot  *
3*7c3d14c8STreehugger Robot  *      	       The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot  *
5*7c3d14c8STreehugger Robot  * This file is dual licensed under the MIT and the University of Illinois Open
6*7c3d14c8STreehugger Robot  * Source Licenses. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot  *
8*7c3d14c8STreehugger Robot  * ===----------------------------------------------------------------------===
9*7c3d14c8STreehugger Robot  *
10*7c3d14c8STreehugger Robot  * This file implements __floattixf for the compiler_rt library.
11*7c3d14c8STreehugger Robot  *
12*7c3d14c8STreehugger Robot  * ===----------------------------------------------------------------------===
13*7c3d14c8STreehugger Robot  */
14*7c3d14c8STreehugger Robot 
15*7c3d14c8STreehugger Robot #include "int_lib.h"
16*7c3d14c8STreehugger Robot 
17*7c3d14c8STreehugger Robot #ifdef CRT_HAS_128BIT
18*7c3d14c8STreehugger Robot 
19*7c3d14c8STreehugger Robot /* Returns: convert a to a long double, rounding toward even. */
20*7c3d14c8STreehugger Robot 
21*7c3d14c8STreehugger Robot /* Assumption: long double is a IEEE 80 bit floating point type padded to 128 bits
22*7c3d14c8STreehugger Robot  *             ti_int is a 128 bit integral type
23*7c3d14c8STreehugger Robot  */
24*7c3d14c8STreehugger Robot 
25*7c3d14c8STreehugger Robot /* gggg gggg gggg gggg gggg gggg gggg gggg | gggg gggg gggg gggg seee eeee eeee eeee |
26*7c3d14c8STreehugger Robot  * 1mmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm
27*7c3d14c8STreehugger Robot  */
28*7c3d14c8STreehugger Robot 
29*7c3d14c8STreehugger Robot COMPILER_RT_ABI long double
__floattixf(ti_int a)30*7c3d14c8STreehugger Robot __floattixf(ti_int a)
31*7c3d14c8STreehugger Robot {
32*7c3d14c8STreehugger Robot     if (a == 0)
33*7c3d14c8STreehugger Robot         return 0.0;
34*7c3d14c8STreehugger Robot     const unsigned N = sizeof(ti_int) * CHAR_BIT;
35*7c3d14c8STreehugger Robot     const ti_int s = a >> (N-1);
36*7c3d14c8STreehugger Robot     a = (a ^ s) - s;
37*7c3d14c8STreehugger Robot     int sd = N - __clzti2(a);  /* number of significant digits */
38*7c3d14c8STreehugger Robot     int e = sd - 1;             /* exponent */
39*7c3d14c8STreehugger Robot     if (sd > LDBL_MANT_DIG)
40*7c3d14c8STreehugger Robot     {
41*7c3d14c8STreehugger Robot         /*  start:  0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
42*7c3d14c8STreehugger Robot          *  finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
43*7c3d14c8STreehugger Robot          *                                                12345678901234567890123456
44*7c3d14c8STreehugger Robot          *  1 = msb 1 bit
45*7c3d14c8STreehugger Robot          *  P = bit LDBL_MANT_DIG-1 bits to the right of 1
46*7c3d14c8STreehugger Robot          *  Q = bit LDBL_MANT_DIG bits to the right of 1
47*7c3d14c8STreehugger Robot          *  R = "or" of all bits to the right of Q
48*7c3d14c8STreehugger Robot          */
49*7c3d14c8STreehugger Robot         switch (sd)
50*7c3d14c8STreehugger Robot         {
51*7c3d14c8STreehugger Robot         case LDBL_MANT_DIG + 1:
52*7c3d14c8STreehugger Robot             a <<= 1;
53*7c3d14c8STreehugger Robot             break;
54*7c3d14c8STreehugger Robot         case LDBL_MANT_DIG + 2:
55*7c3d14c8STreehugger Robot             break;
56*7c3d14c8STreehugger Robot         default:
57*7c3d14c8STreehugger Robot             a = ((tu_int)a >> (sd - (LDBL_MANT_DIG+2))) |
58*7c3d14c8STreehugger Robot                 ((a & ((tu_int)(-1) >> ((N + LDBL_MANT_DIG+2) - sd))) != 0);
59*7c3d14c8STreehugger Robot         };
60*7c3d14c8STreehugger Robot         /* finish: */
61*7c3d14c8STreehugger Robot         a |= (a & 4) != 0;  /* Or P into R */
62*7c3d14c8STreehugger Robot         ++a;  /* round - this step may add a significant bit */
63*7c3d14c8STreehugger Robot         a >>= 2;  /* dump Q and R */
64*7c3d14c8STreehugger Robot         /* a is now rounded to LDBL_MANT_DIG or LDBL_MANT_DIG+1 bits */
65*7c3d14c8STreehugger Robot         if (a & ((tu_int)1 << LDBL_MANT_DIG))
66*7c3d14c8STreehugger Robot         {
67*7c3d14c8STreehugger Robot             a >>= 1;
68*7c3d14c8STreehugger Robot             ++e;
69*7c3d14c8STreehugger Robot         }
70*7c3d14c8STreehugger Robot         /* a is now rounded to LDBL_MANT_DIG bits */
71*7c3d14c8STreehugger Robot     }
72*7c3d14c8STreehugger Robot     else
73*7c3d14c8STreehugger Robot     {
74*7c3d14c8STreehugger Robot         a <<= (LDBL_MANT_DIG - sd);
75*7c3d14c8STreehugger Robot         /* a is now rounded to LDBL_MANT_DIG bits */
76*7c3d14c8STreehugger Robot     }
77*7c3d14c8STreehugger Robot     long_double_bits fb;
78*7c3d14c8STreehugger Robot     fb.u.high.s.low = ((su_int)s & 0x8000) |        /* sign */
79*7c3d14c8STreehugger Robot                     (e + 16383);                  /* exponent */
80*7c3d14c8STreehugger Robot     fb.u.low.all = (du_int)a;                     /* mantissa */
81*7c3d14c8STreehugger Robot     return fb.f;
82*7c3d14c8STreehugger Robot }
83*7c3d14c8STreehugger Robot 
84*7c3d14c8STreehugger Robot #endif /* CRT_HAS_128BIT */
85