xref: /aosp_15_r20/external/compiler-rt/lib/builtins/floatdisf.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot /*===-- floatdisf.c - Implement __floatdisf -------------------------------===
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 __floatdisf 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 float, rounding toward even.*/
16*7c3d14c8STreehugger Robot 
17*7c3d14c8STreehugger Robot /* Assumption: float is a IEEE 32 bit floating point type
18*7c3d14c8STreehugger Robot  *             di_int is a 64 bit integral type
19*7c3d14c8STreehugger Robot  */
20*7c3d14c8STreehugger Robot 
21*7c3d14c8STreehugger Robot /* seee eeee emmm mmmm mmmm mmmm mmmm mmmm */
22*7c3d14c8STreehugger Robot 
23*7c3d14c8STreehugger Robot #include "int_lib.h"
24*7c3d14c8STreehugger Robot 
ARM_EABI_FNALIAS(l2f,floatdisf)25*7c3d14c8STreehugger Robot ARM_EABI_FNALIAS(l2f, floatdisf)
26*7c3d14c8STreehugger Robot 
27*7c3d14c8STreehugger Robot COMPILER_RT_ABI float
28*7c3d14c8STreehugger Robot __floatdisf(di_int a)
29*7c3d14c8STreehugger Robot {
30*7c3d14c8STreehugger Robot     if (a == 0)
31*7c3d14c8STreehugger Robot         return 0.0F;
32*7c3d14c8STreehugger Robot     const unsigned N = sizeof(di_int) * CHAR_BIT;
33*7c3d14c8STreehugger Robot     const di_int s = a >> (N-1);
34*7c3d14c8STreehugger Robot     a = (a ^ s) - s;
35*7c3d14c8STreehugger Robot     int sd = N - __builtin_clzll(a);  /* number of significant digits */
36*7c3d14c8STreehugger Robot     int e = sd - 1;             /* exponent */
37*7c3d14c8STreehugger Robot     if (sd > FLT_MANT_DIG)
38*7c3d14c8STreehugger Robot     {
39*7c3d14c8STreehugger Robot         /*  start:  0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
40*7c3d14c8STreehugger Robot          *  finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
41*7c3d14c8STreehugger Robot          *                                                12345678901234567890123456
42*7c3d14c8STreehugger Robot          *  1 = msb 1 bit
43*7c3d14c8STreehugger Robot          *  P = bit FLT_MANT_DIG-1 bits to the right of 1
44*7c3d14c8STreehugger Robot          *  Q = bit FLT_MANT_DIG bits to the right of 1
45*7c3d14c8STreehugger Robot          *  R = "or" of all bits to the right of Q
46*7c3d14c8STreehugger Robot          */
47*7c3d14c8STreehugger Robot         switch (sd)
48*7c3d14c8STreehugger Robot         {
49*7c3d14c8STreehugger Robot         case FLT_MANT_DIG + 1:
50*7c3d14c8STreehugger Robot             a <<= 1;
51*7c3d14c8STreehugger Robot             break;
52*7c3d14c8STreehugger Robot         case FLT_MANT_DIG + 2:
53*7c3d14c8STreehugger Robot             break;
54*7c3d14c8STreehugger Robot         default:
55*7c3d14c8STreehugger Robot             a = ((du_int)a >> (sd - (FLT_MANT_DIG+2))) |
56*7c3d14c8STreehugger Robot                 ((a & ((du_int)(-1) >> ((N + FLT_MANT_DIG+2) - sd))) != 0);
57*7c3d14c8STreehugger Robot         };
58*7c3d14c8STreehugger Robot         /* finish: */
59*7c3d14c8STreehugger Robot         a |= (a & 4) != 0;  /* Or P into R */
60*7c3d14c8STreehugger Robot         ++a;  /* round - this step may add a significant bit */
61*7c3d14c8STreehugger Robot         a >>= 2;  /* dump Q and R */
62*7c3d14c8STreehugger Robot         /* a is now rounded to FLT_MANT_DIG or FLT_MANT_DIG+1 bits */
63*7c3d14c8STreehugger Robot         if (a & ((du_int)1 << FLT_MANT_DIG))
64*7c3d14c8STreehugger Robot         {
65*7c3d14c8STreehugger Robot             a >>= 1;
66*7c3d14c8STreehugger Robot             ++e;
67*7c3d14c8STreehugger Robot         }
68*7c3d14c8STreehugger Robot         /* a is now rounded to FLT_MANT_DIG bits */
69*7c3d14c8STreehugger Robot     }
70*7c3d14c8STreehugger Robot     else
71*7c3d14c8STreehugger Robot     {
72*7c3d14c8STreehugger Robot         a <<= (FLT_MANT_DIG - sd);
73*7c3d14c8STreehugger Robot         /* a is now rounded to FLT_MANT_DIG bits */
74*7c3d14c8STreehugger Robot     }
75*7c3d14c8STreehugger Robot     float_bits fb;
76*7c3d14c8STreehugger Robot     fb.u = ((su_int)s & 0x80000000) |  /* sign */
77*7c3d14c8STreehugger Robot            ((e + 127) << 23)       |  /* exponent */
78*7c3d14c8STreehugger Robot            ((su_int)a & 0x007FFFFF);   /* mantissa */
79*7c3d14c8STreehugger Robot     return fb.f;
80*7c3d14c8STreehugger Robot }
81