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