xref: /minix3/sys/external/bsd/compiler_rt/dist/lib/builtins/floatundisf.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*===-- floatundisf.c - Implement __floatundisf ---------------------------===
2*0a6a1f1dSLionel Sambuc  *
3*0a6a1f1dSLionel Sambuc  *                     The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc  *
5*0a6a1f1dSLionel Sambuc  * This file is dual licensed under the MIT and the University of Illinois Open
6*0a6a1f1dSLionel Sambuc  * Source Licenses. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc  *
8*0a6a1f1dSLionel Sambuc  * ===----------------------------------------------------------------------===
9*0a6a1f1dSLionel Sambuc  *
10*0a6a1f1dSLionel Sambuc  * This file implements __floatundisf for the compiler_rt library.
11*0a6a1f1dSLionel Sambuc  *
12*0a6a1f1dSLionel Sambuc  *===----------------------------------------------------------------------===
13*0a6a1f1dSLionel Sambuc  */
14*0a6a1f1dSLionel Sambuc 
15*0a6a1f1dSLionel Sambuc /* Returns: convert a to a float, rounding toward even. */
16*0a6a1f1dSLionel Sambuc 
17*0a6a1f1dSLionel Sambuc /* Assumption: float is a IEEE 32 bit floating point type
18*0a6a1f1dSLionel Sambuc  *            du_int is a 64 bit integral type
19*0a6a1f1dSLionel Sambuc  */
20*0a6a1f1dSLionel Sambuc 
21*0a6a1f1dSLionel Sambuc /* seee eeee emmm mmmm mmmm mmmm mmmm mmmm */
22*0a6a1f1dSLionel Sambuc 
23*0a6a1f1dSLionel Sambuc #include "int_lib.h"
24*0a6a1f1dSLionel Sambuc 
ARM_EABI_FNALIAS(ul2f,floatundisf)25*0a6a1f1dSLionel Sambuc ARM_EABI_FNALIAS(ul2f, floatundisf)
26*0a6a1f1dSLionel Sambuc 
27*0a6a1f1dSLionel Sambuc COMPILER_RT_ABI float
28*0a6a1f1dSLionel Sambuc __floatundisf(du_int a)
29*0a6a1f1dSLionel Sambuc {
30*0a6a1f1dSLionel Sambuc     if (a == 0)
31*0a6a1f1dSLionel Sambuc         return 0.0F;
32*0a6a1f1dSLionel Sambuc     const unsigned N = sizeof(du_int) * CHAR_BIT;
33*0a6a1f1dSLionel Sambuc     int sd = N - __builtin_clzll(a);  /* number of significant digits */
34*0a6a1f1dSLionel Sambuc     int e = sd - 1;             /* 8 exponent */
35*0a6a1f1dSLionel Sambuc     if (sd > FLT_MANT_DIG)
36*0a6a1f1dSLionel Sambuc     {
37*0a6a1f1dSLionel Sambuc         /*  start:  0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
38*0a6a1f1dSLionel Sambuc          *  finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
39*0a6a1f1dSLionel Sambuc          *                                                12345678901234567890123456
40*0a6a1f1dSLionel Sambuc          *  1 = msb 1 bit
41*0a6a1f1dSLionel Sambuc          *  P = bit FLT_MANT_DIG-1 bits to the right of 1
42*0a6a1f1dSLionel Sambuc          *  Q = bit FLT_MANT_DIG bits to the right of 1
43*0a6a1f1dSLionel Sambuc          *  R = "or" of all bits to the right of Q
44*0a6a1f1dSLionel Sambuc          */
45*0a6a1f1dSLionel Sambuc         switch (sd)
46*0a6a1f1dSLionel Sambuc         {
47*0a6a1f1dSLionel Sambuc         case FLT_MANT_DIG + 1:
48*0a6a1f1dSLionel Sambuc             a <<= 1;
49*0a6a1f1dSLionel Sambuc             break;
50*0a6a1f1dSLionel Sambuc         case FLT_MANT_DIG + 2:
51*0a6a1f1dSLionel Sambuc             break;
52*0a6a1f1dSLionel Sambuc         default:
53*0a6a1f1dSLionel Sambuc             a = (a >> (sd - (FLT_MANT_DIG+2))) |
54*0a6a1f1dSLionel Sambuc                 ((a & ((du_int)(-1) >> ((N + FLT_MANT_DIG+2) - sd))) != 0);
55*0a6a1f1dSLionel Sambuc         };
56*0a6a1f1dSLionel Sambuc         /* finish: */
57*0a6a1f1dSLionel Sambuc         a |= (a & 4) != 0;  /* Or P into R */
58*0a6a1f1dSLionel Sambuc         ++a;  /* round - this step may add a significant bit */
59*0a6a1f1dSLionel Sambuc         a >>= 2;  /* dump Q and R */
60*0a6a1f1dSLionel Sambuc         /* a is now rounded to FLT_MANT_DIG or FLT_MANT_DIG+1 bits */
61*0a6a1f1dSLionel Sambuc         if (a & ((du_int)1 << FLT_MANT_DIG))
62*0a6a1f1dSLionel Sambuc         {
63*0a6a1f1dSLionel Sambuc             a >>= 1;
64*0a6a1f1dSLionel Sambuc             ++e;
65*0a6a1f1dSLionel Sambuc         }
66*0a6a1f1dSLionel Sambuc         /* a is now rounded to FLT_MANT_DIG bits */
67*0a6a1f1dSLionel Sambuc     }
68*0a6a1f1dSLionel Sambuc     else
69*0a6a1f1dSLionel Sambuc     {
70*0a6a1f1dSLionel Sambuc         a <<= (FLT_MANT_DIG - sd);
71*0a6a1f1dSLionel Sambuc         /* a is now rounded to FLT_MANT_DIG bits */
72*0a6a1f1dSLionel Sambuc     }
73*0a6a1f1dSLionel Sambuc     float_bits fb;
74*0a6a1f1dSLionel Sambuc     fb.u = ((e + 127) << 23)       |  /* exponent */
75*0a6a1f1dSLionel Sambuc            ((su_int)a & 0x007FFFFF);  /* mantissa */
76*0a6a1f1dSLionel Sambuc     return fb.f;
77*0a6a1f1dSLionel Sambuc }
78