xref: /aosp_15_r20/external/compiler-rt/lib/builtins/floatundidf.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot /* ===-- floatundidf.c - Implement __floatundidf ---------------------------===
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 __floatundidf for the compiler_rt library.
11*7c3d14c8STreehugger Robot  *
12*7c3d14c8STreehugger Robot  * ===----------------------------------------------------------------------===
13*7c3d14c8STreehugger Robot  */
14*7c3d14c8STreehugger Robot 
15*7c3d14c8STreehugger Robot /* Returns: convert a to a double, rounding toward even. */
16*7c3d14c8STreehugger Robot 
17*7c3d14c8STreehugger Robot /* Assumption: double is a IEEE 64 bit floating point type
18*7c3d14c8STreehugger Robot  *             du_int is a 64 bit integral type
19*7c3d14c8STreehugger Robot  */
20*7c3d14c8STreehugger Robot 
21*7c3d14c8STreehugger Robot /* seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm */
22*7c3d14c8STreehugger Robot 
23*7c3d14c8STreehugger Robot #include "int_lib.h"
24*7c3d14c8STreehugger Robot 
ARM_EABI_FNALIAS(ul2d,floatundidf)25*7c3d14c8STreehugger Robot ARM_EABI_FNALIAS(ul2d, floatundidf)
26*7c3d14c8STreehugger Robot 
27*7c3d14c8STreehugger Robot #ifndef __SOFT_FP__
28*7c3d14c8STreehugger Robot /* Support for systems that have hardware floating-point; we'll set the inexact flag
29*7c3d14c8STreehugger Robot  * as a side-effect of this computation.
30*7c3d14c8STreehugger Robot  */
31*7c3d14c8STreehugger Robot 
32*7c3d14c8STreehugger Robot COMPILER_RT_ABI double
33*7c3d14c8STreehugger Robot __floatundidf(du_int a)
34*7c3d14c8STreehugger Robot {
35*7c3d14c8STreehugger Robot     static const double twop52 = 4503599627370496.0; // 0x1.0p52
36*7c3d14c8STreehugger Robot     static const double twop84 = 19342813113834066795298816.0; // 0x1.0p84
37*7c3d14c8STreehugger Robot     static const double twop84_plus_twop52 = 19342813118337666422669312.0; // 0x1.00000001p84
38*7c3d14c8STreehugger Robot 
39*7c3d14c8STreehugger Robot     union { uint64_t x; double d; } high = { .d = twop84 };
40*7c3d14c8STreehugger Robot     union { uint64_t x; double d; } low = { .d = twop52 };
41*7c3d14c8STreehugger Robot 
42*7c3d14c8STreehugger Robot     high.x |= a >> 32;
43*7c3d14c8STreehugger Robot     low.x |= a & UINT64_C(0x00000000ffffffff);
44*7c3d14c8STreehugger Robot 
45*7c3d14c8STreehugger Robot     const double result = (high.d - twop84_plus_twop52) + low.d;
46*7c3d14c8STreehugger Robot     return result;
47*7c3d14c8STreehugger Robot }
48*7c3d14c8STreehugger Robot 
49*7c3d14c8STreehugger Robot #else
50*7c3d14c8STreehugger Robot /* Support for systems that don't have hardware floating-point; there are no flags to
51*7c3d14c8STreehugger Robot  * set, and we don't want to code-gen to an unknown soft-float implementation.
52*7c3d14c8STreehugger Robot  */
53*7c3d14c8STreehugger Robot 
54*7c3d14c8STreehugger Robot COMPILER_RT_ABI double
55*7c3d14c8STreehugger Robot __floatundidf(du_int a)
56*7c3d14c8STreehugger Robot {
57*7c3d14c8STreehugger Robot     if (a == 0)
58*7c3d14c8STreehugger Robot         return 0.0;
59*7c3d14c8STreehugger Robot     const unsigned N = sizeof(du_int) * CHAR_BIT;
60*7c3d14c8STreehugger Robot     int sd = N - __builtin_clzll(a);  /* number of significant digits */
61*7c3d14c8STreehugger Robot     int e = sd - 1;             /* exponent */
62*7c3d14c8STreehugger Robot     if (sd > DBL_MANT_DIG)
63*7c3d14c8STreehugger Robot     {
64*7c3d14c8STreehugger Robot         /*  start:  0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
65*7c3d14c8STreehugger Robot          *  finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
66*7c3d14c8STreehugger Robot          *                                                12345678901234567890123456
67*7c3d14c8STreehugger Robot          *  1 = msb 1 bit
68*7c3d14c8STreehugger Robot          *  P = bit DBL_MANT_DIG-1 bits to the right of 1
69*7c3d14c8STreehugger Robot          *  Q = bit DBL_MANT_DIG bits to the right of 1
70*7c3d14c8STreehugger Robot          *  R = "or" of all bits to the right of Q
71*7c3d14c8STreehugger Robot          */
72*7c3d14c8STreehugger Robot         switch (sd)
73*7c3d14c8STreehugger Robot         {
74*7c3d14c8STreehugger Robot         case DBL_MANT_DIG + 1:
75*7c3d14c8STreehugger Robot             a <<= 1;
76*7c3d14c8STreehugger Robot             break;
77*7c3d14c8STreehugger Robot         case DBL_MANT_DIG + 2:
78*7c3d14c8STreehugger Robot             break;
79*7c3d14c8STreehugger Robot         default:
80*7c3d14c8STreehugger Robot             a = (a >> (sd - (DBL_MANT_DIG+2))) |
81*7c3d14c8STreehugger Robot                 ((a & ((du_int)(-1) >> ((N + DBL_MANT_DIG+2) - sd))) != 0);
82*7c3d14c8STreehugger Robot         };
83*7c3d14c8STreehugger Robot         /* finish: */
84*7c3d14c8STreehugger Robot         a |= (a & 4) != 0;  /* Or P into R */
85*7c3d14c8STreehugger Robot         ++a;  /* round - this step may add a significant bit */
86*7c3d14c8STreehugger Robot         a >>= 2;  /* dump Q and R */
87*7c3d14c8STreehugger Robot         /* a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits */
88*7c3d14c8STreehugger Robot         if (a & ((du_int)1 << DBL_MANT_DIG))
89*7c3d14c8STreehugger Robot         {
90*7c3d14c8STreehugger Robot             a >>= 1;
91*7c3d14c8STreehugger Robot             ++e;
92*7c3d14c8STreehugger Robot         }
93*7c3d14c8STreehugger Robot         /* a is now rounded to DBL_MANT_DIG bits */
94*7c3d14c8STreehugger Robot     }
95*7c3d14c8STreehugger Robot     else
96*7c3d14c8STreehugger Robot     {
97*7c3d14c8STreehugger Robot         a <<= (DBL_MANT_DIG - sd);
98*7c3d14c8STreehugger Robot         /* a is now rounded to DBL_MANT_DIG bits */
99*7c3d14c8STreehugger Robot     }
100*7c3d14c8STreehugger Robot     double_bits fb;
101*7c3d14c8STreehugger Robot     fb.u.s.high = ((e + 1023) << 20)       |        /* exponent */
102*7c3d14c8STreehugger Robot                   ((su_int)(a >> 32) & 0x000FFFFF); /* mantissa-high */
103*7c3d14c8STreehugger Robot     fb.u.s.low = (su_int)a;                         /* mantissa-low  */
104*7c3d14c8STreehugger Robot     return fb.f;
105*7c3d14c8STreehugger Robot }
106*7c3d14c8STreehugger Robot #endif
107